Exemplo n.º 1
0
 def testKnownTrees(self):
     print "\n\n----- calculating fitness of known trees -----"
     # create trees - MI, OMES, whatever - that have a special form and
     #    check their fitness
     treenames = ['MI','OMES']
     for t in treenames:
         tree = CGAGenerator.generate_special_tree(t)
         print '%s : %s' % (t,tree.getString())
         print 'Weighted accuracy fitness : ',self.mySimulation.evaluate_fitness_weighted_accuracy(tree)
         print 'Top N weighted accuracy fitness : ',self.mySimulation.evaluate_fitness_topN_weighted_accuracy(tree)
         print 'Distance matrix fitness : ',self.mySimulation.evaluate_fitness_distance_matrix(tree)
Exemplo n.º 2
0
 def initialize_tree(self):
     """Creates a single random tree using the parameters in self.treeGenDict.  This functionality
     has been separated from self.populate() in order to make headless chicken crossover (in which we
     crossover with a randomly generated tree) easier."""
     r = self.treeGenDict['r']
     if self.treeGenDict['treetype'] == 'exponential':
         if self.treeGenDict['p'] < 1.0:
             p = self.treeGenDict['p']
         else:
             # ensures consistent parameter values
             p = 1/self.treeGenDict['p']
         return CGAGenerator.expgenerate(p,r)
     elif self.treeGenDict['treetype'] == 'fixed':
         if self.treeGenDict['p'] > 1:
             treeSize = self.treeGenDict['p']
         else:
             treeSize = MATH.int(1.0/self.treeGenDict['p'])
         return CGAGenerator.generate(treeSize,r)
     else:
         raise TypeError, 'Unknown tree type %s' % self.treeGenDict['treetype']
Exemplo n.º 3
0
 def mate(self, parentOne, parentTwo):
     """Accepts two parents and returns ONE offspring; if the offspring is unchanged from one of
     the parents, the fitness values are not re-evaluated, just copied.  Only one category of 
     mutation is performed on a single offspring."""
     # one parent will form the basis for the new offspring; the other is just there for 
     #    potential crossover (by default the mother will become the offspring)
     mother, father  = parentOne.copy(), parentTwo.copy()
     # fitEval will be set to True if any mutations occur that make the offspring different from
     #    its copied parent; this way we avoid unnecessary fitness evaluations
     fitEval = False
     # only one category of mutation is allowed per mating event
     r = urand()
     if r < self.pC: # parental crossover
         fitEval = True
         mNode = rchoice(mother.tree.getNodes())
         fNode = rchoice(father.tree.getNodes())
         CGAGenerator.single_crossover(mNode,fNode)
     elif r < self.pC + self.pHC: # headless chicken crossover
         fitEval = True
         mNode = rchoice(mother.tree.getNodes())
         rNode = rchoice(self.initialize_tree().getNodes())
         CGAGenerator.single_crossover(mNode,rNode)
     elif r < self.pC + self.pHC + self.pM: # point mutation (uses pM/node for mutation prob.)
         fitEval = True
         for n in mother.tree.getNodes():
             if urand() < self.pM:
                 CGAGenerator.point_mutate(mother.tree, n)
                 fitEval = True
     elif r < self.pC + self.pHC + self.pM + self.pP: # pruning - guaranteed to do one pruning operation
         fitEval = True
         mNode = rchoice(mother.tree.getNodes())
         CGAGenerator.prune(mother.tree,mNode)
     elif r < self.pC + self.pHC + self.pM + self.pP + self.pG: # growth - guaranteed to do one growth op.
         fitEval = True
         mTerm = rchoice(mother.tree.getTermini())
         CGAGenerator.grow(mother.tree,mTerm)
     else: # offspring will just be a copy
         pass
     if fitEval:
         mother.fitness,mother.parsimony,mother.finitewts = self.evaluate_fitness(mother.tree)
     return mother