Module settings
[hide private]
[frames] | no frames]

Source Code for Module settings

  1  """ 
  2  settings 
  3  ======== 
  4  Contains all parameters for the evolutionary run, grammar rules, constraints,  
  5  and specifics about the terminal and function set of the trees. 
  6   
  7  First we have to create a Terminal set and a Function set for the tree. 
  8  A Node object is a tuple composed of three elements e.g. (0,2,'root') 
  9      - The first element represent the type of node: 
 10          - 0 Root Branch 
 11          - 1 Function Branch 
 12          - 2 ADF Defining Branch 
 13          - 3 Variable Leaf 
 14          - 4 Constant Leaf 
 15          - 5 ADF Leaf 
 16      - The second element is the arity of the Node (number of children). 
 17      If the number of children >0 then it is a branch node, if arity = 0  
 18      then it is a leaf node... e.g. (0,2,'root') means the root node has two children. 
 19   
 20      - The third element is the name or unique identifier of the Node. 
 21      e.g (1,2,'+') is a function branch node with two children and unique identifier '+' 
 22      while (1,2,'*') is a function branch node with two children and unique identifier '*' 
 23   
 24   
 25  Then we define the terminal nodes, and map them with corresponding methods. 
 26  e.g. 
 27   
 28  >>>    terminals = { 
 29  'x':all_x, 
 30  'y':all_y, 
 31  '1-10':random.random()*10 
 32  } 
 33   
 34  Then we have to map values to the leaf nodes. 
 35  We define the number of examples we learn from  
 36   
 37  >>>    nb_ex=100 
 38   
 39  We define all variable x in a list of corresponding size, and same for y. 
 40  e.g. 
 41   
 42  >>>    all_x=[] 
 43  all_y=[] 
 44  for i in xrange(nb_ex): 
 45  >>>    all_x.append(random.random()*10) 
 46  >>>    all_y.append(random.random()*10) 
 47   
 48   
 49   
 50  Then we have to define and map functions to the branch nodes to perform operations   
 51  such as for example: arithmetic addition, subtraction, multiplication... Whatever operation is 
 52  needed in the function set of a tree should be created here. 
 53  e.g. 
 54   
 55  >>>    functions = {'+':add, 
 56  '*':multiply, 
 57  'adf2_+':add, 
 58  'adf2_*':multiply, 
 59  'adf1':adfBranch, 
 60  'adf2':adfBranch, 
 61  'root':rootBranch 
 62  } 
 63   
 64  Where functions like add are defined like this: 
 65   
 66  >>>    def add(listElem): 
 67  try: 
 68  >>>    return listElem[0]+listElem[1] 
 69  except: 
 70  >>>    raise WrongValues, "Wrong values sent to function node.\nCan't get result" 
 71  exit 
 72   
 73  We finally need to define some kind of rules or constraints that will dictate  
 74  the shapes of the generated trees.  
 75   
 76  We define how strongly typed we want the evolution to be.  
 77    
 78  >>>    Strongly_Typed_Crossover_degree=1  
 79   
 80  It means the trees generated have to be compliant with the constraints, and that 
 81  the crossover and mutation operations will spend some computational time  
 82  trying to make sure offspring are compliant.  
 83  We also specify what happens after the system has tried 100 times to produced rules-compliant offsprings using crossover but has failed. 
 84  Either we accept the unfit offsprings with Substitute_Mutation=0 or we substitute them with a mutated tree by setting Substitute_Mutation=1. 
 85  In our case, the rules are very simple, so we can set :  
 86   
 87  >>>    Substitute_Mutation=0 
 88   
 89  Next, we need to specify a fitness function. e.g. 
 90   
 91  >>>    def FitnessFunction(my_tree): 
 92  >>>        return evalfitness.FinalFitness(evalfitness.EvalTreeForAllInputSets(my_tree,xrange(nb_eval))) 
 93   
 94  This fitness function reuses two functions : 
 95  EvalTreeForAllInputSets: parse the tree by plugging input values and computing the result obtained at the top of the tree.  
 96  FinalFitness: returns the overall fitness of a tree over a set of datapoints using the result of the previous function as an input. It contains problem specific aspects of the fitness calculation.  
 97   
 98  Then we set the rules treeRules for producing a tree. e.g. 
 99   
100  >>>    # the crossover mapping spec is empty in this case 
101  crossover_mapping=[] 
102  # default function set applicable by for branches: 
103  defaultFunctionSet= [(1,2,'+'),(1,2,'*'),(1,2,'-'),(1,1,'neg'),(1,1,'^2')] 
104  # default terminal set applicable by for branches: 
105  defaultTerminalSet= [(3,0,'x'),(3,0,'y')] 
106  defaultTerminalSet.extend(set_ERC) 
107  treeRules = {'root':[(defaultFunctionSet,defaultTerminalSet)], 
108  >>>      '+':[(defaultFunctionSet,defaultTerminalSet),(defaultFunctionSet,defaultTerminalSet)], 
109  >>>      '*':[(defaultFunctionSet,defaultTerminalSet),(defaultFunctionSet,defaultTerminalSet)], 
110  >>>      '^2':[(defaultFunctionSet,defaultTerminalSet)], 
111  >>>      '-':[(defaultFunctionSet,defaultTerminalSet),(defaultFunctionSet,defaultTerminalSet)], 
112  >>>      'neg':[([(1,2,'+'),(1,2,'*'),(1,2,'-'),(1,1,'^2')],defaultTerminalSet)] 
113  >>>      } 
114   
115  We also define what function branches can be changed during crossover, to make an 
116  a fragment compliant with a new parent tree. 
117  e.g.  
118   
119  >>>    crossover_mapping=[('+','adf2_+'),('adf2_+','+'),('*','adf2_*'),('adf2_*','*')] 
120   
121  We finally precise that order matters in the appearance of the ADF defining branches 
122   
123  >>>    adfOrdered = True  
124   
125  means that the ADF1 defining branch will appear before the ADF2 defining branch.  
126  This is especially useful if you need to define a model a forest of ordered  
127  sub trees... 
128   
129  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
130  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
131  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
132  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
133  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
134  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
135  THE SOFTWARE. 
136   
137  @author: by Mehdi Khoury 
138  @version: 1.20 
139  @copyright: (c) 2009 Mehdi Khoury under the mit license 
140  http://www.opensource.org/licenses/mit-license.html 
141  @contact: mehdi.khoury at gmail.com 
142  """ 
143   
144  import psyco 
145  import evalfitness 
146  import tutorial1 
147  import tutorial2 
148  import tutorial3 
149  import tutorial4 
150  import tutorial5 
151  #import tutorial6 
152  #import tutorial7 
153  #import tutorial8 
154  #import tutorial9 
155  psyco.profile() 
156   
157  # setup for running tutorial 1 
158  functions = tutorial1.functions 
159  terminals =tutorial1.terminals 
160  crossover_mapping=tutorial1.crossover_mapping 
161  nb_eval=tutorial1.nb_eval 
162  ideal_results=tutorial1.GetIdealResultsData() 
163  Strongly_Typed_Crossover_degree=tutorial1.Strongly_Typed_Crossover_degree 
164  Substitute_Mutation=tutorial1.Substitute_Mutation 
165  treeRules = tutorial1.treeRules 
166  adfOrdered = tutorial1.adfOrdered 
167  FitnessFunction = tutorial1.FitnessFunction 
168   
169  ## setup for running tutorial 2 
170  #functions = tutorial2.functions 
171  #crossover_mapping=tutorial2.crossover_mapping 
172  #nb_eval=tutorial2.nb_eval 
173  #ideal_results=tutorial2.GetIdealResultsData() 
174  #terminals =tutorial2.terminals 
175  #Strongly_Typed_Crossover_degree=tutorial2.Strongly_Typed_Crossover_degree 
176  #Substitute_Mutation=tutorial2.Substitute_Mutation 
177  #treeRules = tutorial2.treeRules 
178  #adfOrdered = tutorial2.adfOrdered 
179  #FitnessFunction = tutorial2.FitnessFunction 
180   
181  ## setup for running tutorial 3 
182  #functions = tutorial3.functions 
183  #crossover_mapping=tutorial3.crossover_mapping 
184  #nb_eval=tutorial3.nb_eval 
185  #ideal_results=tutorial3.GetIdealResultsData() 
186  #terminals =tutorial3.terminals 
187  #Strongly_Typed_Crossover_degree=tutorial3.Strongly_Typed_Crossover_degree 
188  #Substitute_Mutation=tutorial3.Substitute_Mutation 
189  #treeRules = tutorial3.treeRules 
190  #adfOrdered = tutorial3.adfOrdered 
191  #FitnessFunction = tutorial3.FitnessFunction 
192   
193  ## setup for running tutorial 4 
194  #functions = tutorial4.functions 
195  #crossover_mapping=tutorial4.crossover_mapping 
196  #nb_eval=tutorial4.nb_eval 
197  #nb_ex=tutorial4.nb_ex 
198  #ideal_results=tutorial4.GetIdealResultsData() 
199  #terminals =tutorial4.terminals 
200  #Strongly_Typed_Crossover_degree=tutorial4.Strongly_Typed_Crossover_degree 
201  #Substitute_Mutation=tutorial4.Substitute_Mutation 
202  #treeRules = tutorial4.treeRules 
203  #adfOrdered = tutorial4.adfOrdered 
204  #FitnessFunction = tutorial4.FitnessFunction 
205   
206  ## setup for running tutorial 5 
207  #functions = tutorial5.functions 
208  #crossover_mapping=tutorial5.crossover_mapping 
209  #nb_eval=tutorial5.nb_eval 
210  #ideal_results=tutorial5.GetIdealResultsData() 
211  #terminals =tutorial5.terminals 
212  #Strongly_Typed_Crossover_degree=tutorial5.Strongly_Typed_Crossover_degree 
213  #Substitute_Mutation=tutorial5.Substitute_Mutation 
214  #treeRules = tutorial5.treeRules 
215  #adfOrdered = tutorial5.adfOrdered 
216  #FitnessFunction = tutorial5.FitnessFunction 
217