def mutant_hero(population, crossover_fnc, cmp_fnc): hero = population[0] hero_o = hero['org'][0] hero_a = hero['amplitude'] child_o = [ele for ele in hero_o] child_o = [child_o, [0,0,0]] child = {'org' : child_o, 'amplitude' : hero_a} child = mut.mutation(child, True) population.append(child) population.sort(lambda x,y : mtr.comparator(x,y,cmp_fnc)) population = population[:-1] return population
def greedy_selection(population, crossover_fnc, cmp_fnc): indices = four_indices(len(population)) parents = [population[indices[i]] for i in range(4)] parents.sort(lambda x,y : mtr.comparator(x,y,cmp_fnc)) winner1, winner2 = parents[0], parents[1] # Fitness is measured before crossover is complete and automatically # cached in a tuple. child1 and child2 should be tuples now. child1, child2 = cvr.crossover(winner1, winner2, crossover_fnc) # Mutate (if the check passes) child1 = mut.mutation(child1) child2 = mut.mutation(child2) # Add the children, resort, remove the worst 2 population.append(child1) population.append(child2) population.sort(lambda x,y : mtr.comparator(x,y,cmp_fnc)) population = population[:-2] return population
def do_experiment(cmp_fnc, c_over_op, select_op, trial, force=False): ID = str(cmp_fnc)+"."+str(c_over_op)+"."+str(select_op)+"."+str(trial) cmp_fnc = mtr.fn_list[cmp_fnc] c_over_op = cvr.fn_list[c_over_op] select_op = sel.fn_list[select_op] print "ID:", ID, print str(cmp_fnc)[10:-15],str(c_over_op)[10:-15],str(select_op)[10:-15] pop = None generation = 0 while ( generation < max_gens ): # First check to see if this experiment is already done... d = shelve.open("dat/"+ID+"." + str(generation) + ".pop") if 'pop' in d: prog = 100.0 * float(generation)/(max_gens-1) print "\rAlready computed. Skipping ahead. %",prog," f:",first, sys.stdout.flush() # Update our percentage ticker. generation = generation + 1 # Advance the generation counter. pop = d['pop'] # Load that population into memory. d.close() continue d.close() # Initialize our population if we haven't already if not pop: pop = initialize_pop() # Otherwise we need to compute! pop.sort(lambda x,y : mtr.comparator(x,y, cmp_fnc) ) # Eval and sort IO_update(ID, generation, pop, max_gens) # Spit out status pop = select_op(pop, c_over_op, cmp_fnc) # Breed generation = generation + 1 # Tick # Forcibly revaluate the fitness of the hero. try: del pop[0]['fitness'] except KeyError: pass print " Done."
def non_greedy_selection(population, crossover_fnc, cmp_fnc): indices = four_indices(len(population)) parents = [population[indices[i]] for i in range(4)] parents.sort(lambda x,y : mtr.comparator(x,y,cmp_fnc)) winner1, winner2 = parents[0], parents[1] loser1, loser2 = parents[2], parents[3] # Fitness is measured before crossover is complete and automatically # cached in a tuple. child1 and child2 should be tuples now. child1, child2 = cvr.crossover(winner1, winner2, crossover_fnc) # Mutate (if the check passes) child1 = mut.mutation(child1) child2 = mut.mutation(child2) # Replace the losers from selection population[population.index(loser1)] = child1 population[population.index(loser2)] = child2 return population
def do_experiment(cmp_fnc, c_over_op, select_op, trial, force=False): ID = combo_to_ID(cmp_fnc, c_over_op, select_op, trial) cmp_fnc = mtr.fn_list[cmp_fnc] c_over_op = cvr.fn_list[c_over_op] select_op = sel.fn_list[select_op] print "ID:", ID, print str(cmp_fnc)[10:-15],str(c_over_op)[10:-15],str(select_op)[10:-15] pop = already_computed(ID, max_gens-1) if pop: return pop = None generation = 0 while ( generation < max_gens ): tmp = already_computed(ID, generation) if tmp: # Iteratively load saved generations up to where we stopped before pop, generation = tmp, generation + 1 continue # Initialize our population if we haven't already if not pop: pop = initialize_pop() # Otherwise we need to compute! pop.sort(lambda x,y : mtr.comparator(x,y, cmp_fnc) ) # Eval and sort IO_update(ID, generation, pop, max_gens) # Spit out status pop = select_op(pop, c_over_op, cmp_fnc) # Breed generation = generation + 1 # Tick # Forcibly revaluate the fitness of the hero. try: del pop[0]['fitness'] except KeyError: pass