def mutate(self, mother): child = Program.Program('Mutant', self.config) child.assignments['preloop'] = [] for assignment in mother.assignments['preloop']: new_assignment = Program.Assignment( self.config, assignment.variable, False, np.random.normal( assignment.lhs, self.config.CONST_MATING_DRIFT * abs(assignment.lhs))) child.assignments['preloop'].append(new_assignment) if ('const' in assignment.variable): child.consts['loop'].append(assignment.variable) child.consts['global'].append(assignment.variable) tree_mother = copy.deepcopy(mother.loop_tree) crossover_mother = rec_select_random_tree_node(tree_mother) crossover_direction = random.choice(['LEFT', 'RIGHT']) if (crossover_direction == 'LEFT'): crossover_mother.left_child = rec_create_random_tree( self.config, child.consts['loop']) else: crossover_mother.right_child = rec_create_random_tree( self.config, child.consts['loop']) child.loop_tree = tree_mother child.rec_reduce() return child
def create_random_program(config): program = Program.Program('Alien', config) n_consts = abs( int(np.random.normal(config.N_CONSTS_EV, config.N_CONSTS_SD))) for i in range(n_consts): value = create_random_const(config) const = 'const_' + str(random.getrandbits(100)) program.consts['loop'].append(const) program.consts['global'].append(const) assignment = Program.Assignment(config, const, False, value) program.assignments['preloop'].append(assignment) program.loop_tree = rec_create_random_tree(config, program.consts['loop']) program.rec_reduce() return program