def fitness(self, net, proc, id_for_printing=-1): total_weights = net.num_edges() total_delays = net.num_edges() total_thresholds = net.num_nodes() dec = [8] * total_weights + [4] * total_delays + [7] * total_thresholds leap_decoder = BinaryToIntDecoder(*dec) problem = NetworkProblem(net, proc, self) genome_len = 8 * total_weights + 4 * total_delays + 7 * total_thresholds parents = Individual.create_population( 10, initialize=create_binary_sequence(genome_len), decoder=leap_decoder, problem=problem) parents = Individual.evaluate_population(parents) max_generation = 10 #stdout_probe = probe.FitnessStatsCSVProbe(context, stream=sys.stdout) generation_counter = util.inc_generation(context=context) while generation_counter.generation() < max_generation: offspring = pipe( parents, ops.tournament_selection, ops.clone, mutate_bitflip, ops.uniform_crossover, ops.evaluate, ops.pool(size=len(parents))) #, # accumulate offspring #stdout_probe) parents = offspring generation_counter() # increment to the next generation best = probe.best_of_gen(parents).decode() set_weights(best, net) return self.get_fitness_score(net, proc, id_for_printing)
""" from toolz import pipe from leap_ec.individual import Individual from leap_ec.decoder import IdentityDecoder from leap_ec.global_vars import context import leap_ec.ops as ops from leap_ec.binary_rep.problems import MaxOnes from leap_ec.binary_rep.initializers import create_binary_sequence from leap_ec.binary_rep.ops import mutate_bitflip from leap_ec import util # create initial rand population of 5 individuals parents = Individual.create_population(5, initialize=create_binary_sequence(4), decoder=IdentityDecoder(), problem=MaxOnes()) # Evaluate initial population parents = Individual.evaluate_population(parents) # print initial, random population util.print_population(parents, generation=0) # generation_counter is an optional convenience for generation tracking generation_counter = util.inc_generation(context=context) while generation_counter.generation() < 6: offspring = pipe(parents, ops.tournament_selection, ops.clone, mutate_bitflip(expected_num_mutations=1), ops.uniform_crossover(p_swap=0.2), ops.evaluate, ops.pool(size=len(parents))) # accumulate offspring
# this was in the context of the 1/5 success rule, which we've not # implemented here. # Handbook of EC, B1.3:2 context['leap']['std'] *= .85 if __name__ == '__main__': # Define the real value bounds for initializing the population. In this case, # we define a genome of four bounds. # the (-5.12,5.12) was what was originally used for this problem in # Ken De Jong's 1975 dissertation, so was used for historical reasons. bounds = [(-5.12, 5.12), (-5.12, 5.12), (-5.12, 5.12), (-5.12, 5.12)] parents = Individual.create_population( 5, initialize=create_real_vector(bounds), decoder=IdentityDecoder(), problem=SpheroidProblem(maximize=False)) # Evaluate initial population parents = Individual.evaluate_population(parents) context['leap']['std'] = 2 # We use the provided context, but we could roll our own if we # wanted to keep separate contexts. E.g., island models may want to have # their own contexts. generation_counter = util.inc_generation(context=context, callbacks=(anneal_std, )) # print initial, random population
# print(f"{i}, {best}") def init(length, seq_initializer): def create(): return initializers.create_segmented_sequence( gene_size, create_int_vector(bounds)) return create #create initial rand population of 5 individuals parents = Individual.create_population(n=pop_size, initialize=init( gene_size, create_int_vector(bounds)), decoder=decoder.IdentityDecoder(), problem=FunctionProblem(p.f, True)) # Evaluate initial population parents = Individual.evaluate_population(parents) # print initial, random population util.print_population(parents, generation=0) # generation_counter is an optional convenience for generation tracking generation_counter = util.inc_generation(context=context) #results = [] while generation_counter.generation() < 20: p.setStat()