def auto_simplify(ind, error_function, steps, printBool, progress_interval, maintain_ancestors = False): ''' Auto-simplifies the provided individual. ''' if printBool: print '\nAuto-simplifying with starting size: ' + str(util.count_points(ind['program'])) looping = True step = 0 program = ind['program'] errors = ind['errors'] total_errors = ind['total-error'] while looping: if printBool and (step >= steps or step % progress_interval == 0): print '\nstep: ' + step + '\nprogram: ' + str(program) + '\nerrors: ' + str(errors) + '\ntotal: ' + str(total_errors) + '\nsize: ' + str(util.count_points(program)) if step >= steps: if maintain_ancestors: ancestors = [ind['program']] + ind['ancestors'] else: ancestors = ind['ancestors'] return individual.make_induvidual(program = program, errors = errors, total_errors = total_errors, history = ind['history'], ancestors = ancestors) else: if random_push.lrand_int(5)< 4: p = program how_many = random_push.lrand_int(2)+1 looping2 = True while looping2: if how_many == 0: new_program = p looping2 = False else: p = util.remove_code_at_point(p, random_push.lrand_int(util.count_points(p))) how_many -= 1 point_index = random_push.lrand_int(util.count_points(program)) point = util.code_at_point(program, point_index) if type(point) == list: new_program = util.insert_code_at_point(program, point_index, flatten_seqs(point)) else: new_program = program new_errors = error_function(new_program) new_total_errors = evaluate.compute_total_error(new_errors) if new_errors == errors: step += 1 program = new_program errors = new_errors total_errors = new_total_errors else: step += 1
def select_node_index(tree, keys): ''' Returns an index into tree using the node selection method indicated by node-selection-method. ''' if keys['node-selection-method'] == 'unbiased': return random_push.lrand_int(util.count_points(tree)) elif keys['node-selection-method'] == 'leaf-probability': return choose_node_index_with_leaf_probability(tree, keys['node-selection-leaf-probability']) elif keys['node-selection-method'] == 'size-tournament': return choose_node_index_by_tournament(tree, keys['node-selection-tournament-size']) else: print 'ERROR: :node-selection-method set to unrecognized value '
def choose_node_index_by_tournament(tree, node_selection_tournament_size): ''' Returns an index into tree, choosing the largest subtree found in a tournament of size node-selection-tournament-size. ''' c = util.count_points(tree) tournament_set = [] for i in range(node_selection_tournament_size): point_index = random_push.lrand_int(c) subtree_size = util.count_points(util.code_at_point(tree, point_index)) tournament_set.append({'i' : point_index, 'size' : subtree_size}) biggest = tournament_set[0] for s in tournament_set: if biggest['size'] < s['size']: biggest = s return biggest['i']
print('FINAL:') print('======') pushstate.state_pretty_print(final_state) ''' #''' # Testing Code Generation # ########################### import random_push import interpreter import Pysh.pushstate #print random_push.decompose(100, 100) atom_generators = Pysh.pushstate.registered_instructions atom_generators.append([random_push.lrand_int(100), random_push.lrand()]) #print atom_generators random_code = random_push.random_code(50, atom_generators) print random_code print starting_state = Pysh.pushstate.make_push_state() final_state = interpreter.run_push(random_code, starting_state, True, True, True, False) #''' ''' starting_state = pushstate.make_push_state() starting_code = '("Hello World" string_contained)' final_state = interpreter.run_push(starting_code, starting_state, True, True, True, True) '''