def test_init_1(self): print("\n\ntest_init_1.1") generation = Generation() genome = Genome(generation=generation, input_nodes=2, output_nodes=2) for con in genome.connections(): print(con) for node in genome.nodes(): print(node) print('-' * 40) for node in generation.nodes(): print(node) print('-' * 60) viz.construct(genome, 'first') print("\n\ntest_init_1.2") genome_1 = Genome(generation, 2, 2) for con in genome_1.connections(): print(con) for node in genome_1.nodes(): print(node) print('-' * 40) for node in generation.nodes(): print(node) viz.construct(genome_1, 'second') print('-' * 20 + 'Mutations') for mutation in generation.mutations(): print(mutation)
def test_evaluate(self): generation = Generation() generation.step(solve_task, tmp_reward, first=True, input_neurons=2, output_neurons=1, input=[0, 1]) for i, org in enumerate(generation.organisms()): viz.construct(org.genome(), f'Organism {i}') print(org.score())
def test_add_connection(self): print('\n\ntest_add_connection\nBEFORE 0') generation = Generation() genome = Genome(generation, 2, 1) for con in genome.connections(): print(con) viz.construct(genome, 'before_0') genome._add_node() genome._add_connection() genome._add_connection() genome._add_connection() print('\nAFTER 0\n') for con in genome.connections(): print(con) viz.construct(genome, 'after_0')
def test_init_2(self): generation = Generation() genome = Genome(generation=generation, input_nodes=1, output_nodes=1) genome._add_node() new_generation = Generation(_copy=True, prev_generation=generation) genome_copied = Genome(generation=new_generation, _copy=True, genome_to_copy=genome) for node in generation.nodes(): print(node) print('-' * 20) viz.construct(genome, 'first_1') genome_copied._add_node() for node in new_generation.nodes(): print(node) viz.construct(genome_copied, 'first_copied_1')
def test_add_node(self): print('-' * 20 + 'test_add_node\nBEFORE 0') generation = Generation() genome = Genome(generation, 2, 1) for con in genome.connections(): print(con) for node in genome.nodes(): print(node) viz.construct(genome, 'before_0') genome._add_node() genome._add_node() genome._add_node() for node in generation.nodes(): print(node) print('\nAFTER 0 \n') for con in genome.connections(): print(con) print('-' * 40) for node in genome.nodes(): print(node) viz.construct(genome, 'after_0') print('\n\ntest_add_node\nBEFORE 1') genome_1 = Genome(generation, 1, 1) viz.construct(genome_1, 'before_1') for con in genome_1.connections(): print(con) for node in genome_1.nodes(): print(node) genome_1._add_node() genome_1._add_node() genome_1._add_node() genome_1._add_node() print('\nAFTER 0 \n') for con in genome_1.connections(): print(con) for node in genome_1.nodes(): print(node) viz.construct(genome_1, 'after_1')
def start_simulation(self, X_train, y_train, score): sys.setrecursionlimit(2500) new_gen = Generation() print('Simulation started...') i = 0 while i < number_of_generations: prev_gen = new_gen avg_fitness = prev_gen.step(reward_function=self._reward, solve_task=self._solve_task, X_train=X_train, y_train=y_train) org = prev_gen.ready(score) if org is not None: print('Done!') print(org) construct(org.genome(), 'The Best', view=True) break print(prev_gen.info()) prev_gen.visualize_species() new_gen = prev_gen.reproduce(avg_fitness) i += 1
def test_predict(self): generation = Generation() genome = Genome(generation=generation, input_nodes=2, output_nodes=2) nn = NeuralNetwork(genome=genome) construct(nn.genome(), 'Before') nn.add_node() construct(nn.genome(), '1 Node Added') nn.add_node() construct(nn.genome(), '2 Nodes Added') nn.add_connection() construct(nn.genome(), 'Connection Added') print("CONNECTIONS") for con in nn.genome().connections(): print(con) print("CONNECTIONS") for pred in nn.predict([2, 3]): print(pred)
def test_excesses_and_disjoints(self): generation = Generation() genome_1 = Genome(generation, 1, 2) genome_2 = genome_1.copy_genome() viz.construct(genome_1, 'First Before') genome_1._add_node() genome_1._add_connection() for con in genome_1.connections(): print(con) viz.construct(genome_1, 'First') genome_2._add_node() viz.construct(genome_2, 'Second') E, D = _excesses_disjoints(genome_1, genome_2) print('-' * 20) for con in genome_2.connections(): print(con) print(f'Excesses: {E}') print(f'Disjoints: {D}')
def test_offspring(self): generation = Generation() genome_1 = Genome(generation, 2, 2) genome_2 = Genome(generation, 2, 2) # for i in range(2): # genome_1._add_connection() # genome_2._add_connection() # for i in range(1): # genome_1._add_node() # genome_2._add_node() viz.construct(genome_1, 'Genome 1') viz.construct(genome_2, 'Genome 2') for con in genome_1.connections(): print(con) print('-' * 40) for con in genome_2.connections(): print(con) genome = produce_offspring(generation, genome_1, genome_2) print('-' * 40) for node in genome.nodes(): print(node) print('\n' + '-' * 40) for con in genome.connections(): print(con) viz.construct(genome, 'offspring') E, D = _excesses_disjoints(genome_1, genome_2) print('-' * 20) print(f'Excesses: {E}') print(f'Disjoints: {D}')
def visualize_species(self): for s in self._species: if not s.empty(): rep = random.choice(s.representatives()) construct(rep.genome(), str(s), view=False)