Example #1
0
 def mutator(self):        
     def mut(random, cand, args):
         maybe_mutate = lambda v, func: func(v) if random.random() < args['mutation_rate'] else v
         for gene in cand:
             gene.function = maybe_mutate(gene.function, self.random_func)
             gene.inputs = [maybe_mutate(c, self.random_edge) for c in gene.inputs]
             gene.vars = [maybe_mutate(c, self.random_var) for c in gene.vars]
             #gene.vars = [maybe_noise(c, randomize_var) for c in gene.vars] #TODO:get right noise
         return cand
     
     return mutator(mut)       
Example #2
0
File: cgp.py Project: thanos/ecspy
 def mutator(self):        
     def mutate_edge(v):
         return (v + random.randint(1, self.levelsback)) % self.levelsback
     
     def mutate_func(v):
         l = self._function_count
         return (v + random.randint(1, l)) % l 
     
     def mut(random, cand, args):
         maybe_mutate = lambda v, func: func(v) if random.random() < args['mutation_rate'] else v
         matrix = []
         for col in cand:
             node = [maybe_mutate(col[0], mutate_func)] 
             node += [maybe_mutate(c, mutate_edge) for c in col[1:1+self.arity]]
             matrix.append(node)
             
         return matrix
     
     return mutator(mut)