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)
def test_inc_generation_callbacks(): """ Test inc_generation() callback support """ def callback(new_generation): assert new_generation == 1 my_inc_generation = inc_generation(context, callbacks=[callback]) # Incremented the generation should call our test callback my_inc_generation()
def test_inc_generation(): """ Test the inc_generation() function. """ my_inc_generation = inc_generation(context) # The core context is set to the current generation, which will start as zero assert context['leap']['generation'] == 0 # We can also directly query the internal state of the generation incrementer assert my_inc_generation.generation() == 0 # Now do the increment curr_generation = my_inc_generation() # The generation incrementer always returns the new generation assert curr_generation == 1 # The core context state should have updated assert context['leap']['generation'] == 1 # And, of course, the internal state will reflect that reality, too assert my_inc_generation.generation() == 1
# Evaluate initial population parents = Individual.evaluate_population(parents) # print initial, random population util.print_population(parents, generation=0) # When running the test harness, just run for two generations # (we use this to quickly ensure our examples don't get bitrot) if os.environ.get(test_env_var, False) == 'True': max_generation = 2 else: max_generation = 6 # Set up a generation counter using the default global context variable generation_counter = util.inc_generation() while generation_counter.generation() < max_generation: offspring = pipe( parents, ops.tournament_selection, ops.clone, # these are optional probes to demonstrate their use probe.print_individual(prefix='before mutation: '), mutate_bitflip(expected_num_mutations=1), probe.print_individual(prefix='after mutation: '), ops.uniform_crossover, ops.evaluate, ops.pool(size=len(parents)), # accumulate offspring ops.elitist_survival(parents=parents)) # keep best # parent to