def given_another_genome(): node1 = NodeGene(NodeGeneType.INPUT, 1, 1) node2 = NodeGene(NodeGeneType.INPUT, 2, 2) node3 = NodeGene(NodeGeneType.INPUT, 3, 3) node4 = NodeGene(NodeGeneType.OUTPUT, 4, 4) node5 = NodeGene(NodeGeneType.HIDDEN, 5, 5) node6 = NodeGene(NodeGeneType.HIDDEN, 6, 6) nodes = { node1.get_id(): node1, node2.get_id(): node2, node3.get_id(): node3, node4.get_id(): node4, node5.get_id(): node5, node6.get_id(): node6 } conn1 = ConnectionGene(node1.get_id(), node4.get_id(), 0.8, innovation_number=1) conn2 = ConnectionGene(node2.get_id(), node4.get_id(), -0.4, False, 2) conn3 = ConnectionGene(node3.get_id(), node4.get_id(), 0.5, innovation_number=3) conn4 = ConnectionGene(node2.get_id(), node5.get_id(), 0.2, innovation_number=4) conn5 = ConnectionGene(node5.get_id(), node6.get_id(), 0.4, innovation_number=5) conn6 = ConnectionGene(node3.get_id(), node5.get_id(), 0.6, innovation_number=6) conn7 = ConnectionGene(node1.get_id(), node6.get_id(), 0.6, innovation_number=7) conn8 = ConnectionGene(node6.get_id(), node4.get_id(), 0.6, innovation_number=8) connections = { conn1.get_innovation_number(): conn1, conn2.get_innovation_number(): conn2, conn3.get_innovation_number(): conn3, conn4.get_innovation_number(): conn4, conn5.get_innovation_number(): conn5, conn6.get_innovation_number(): conn6, conn7.get_innovation_number(): conn7, conn8.get_innovation_number(): conn8 } genome = Genome(connections, nodes, InnovationGenerator()) return genome
def test_distance(): genome_less_fitted = given_a_genome() genome_more_ftted = given_another_genome() distance = Genome.compute_distance(genome_less_fitted, genome_more_ftted, 1, 1, 1) print(distance)
def given_a_genome(): node1 = NodeGene(NodeGeneType.INPUT, 0, 0) node2 = NodeGene(NodeGeneType.INPUT, 1, 1) node3 = NodeGene(NodeGeneType.INPUT, 2, 2) node4 = NodeGene(NodeGeneType.OUTPUT, 3, 3) node5 = NodeGene(NodeGeneType.HIDDEN, 4, 4) nodes = { node1.get_id(): node1, node2.get_id(): node2, node3.get_id(): node3, node4.get_id(): node4, node5.get_id(): node5 } conn1 = ConnectionGene(node1.get_id(), node4.get_id(), 0.7, innovation_number=1) conn2 = ConnectionGene(node2.get_id(), node4.get_id(), -0.5, False, 2) conn3 = ConnectionGene(node3.get_id(), node4.get_id(), 0.5, innovation_number=3) conn4 = ConnectionGene(node2.get_id(), node5.get_id(), 0.2, innovation_number=4) conn5 = ConnectionGene(node5.get_id(), node4.get_id(), 0.4, innovation_number=5) conn6 = ConnectionGene(node1.get_id(), node5.get_id(), 0.6, innovation_number=6) conn7 = ConnectionGene(node4.get_id(), node5.get_id(), 0.6, False, innovation_number=11) connections = { conn1.get_innovation_number(): conn1, conn2.get_innovation_number(): conn2, conn3.get_innovation_number(): conn3, conn4.get_innovation_number(): conn4, conn5.get_innovation_number(): conn5, conn6.get_innovation_number(): conn6, conn7.get_innovation_number(): conn7 } inno = InnovationGenerator() inno.counter = 11 genome = Genome(connections, nodes, inno) return genome
def is_same_species(parent_1_genome, species): from NEAT.Genome import Genome """ Returns True if the genomes is in same species else False Threshold value = 3.0 Refer paper section 4.1 :param parent_1_genome: The genome of anyone to be added in this species :param species: Species object :return: Boolean representing whether genome belong in the provided species """ threshold = 3.0 return True if Genome.get_compatibility_distance( parent_1_genome, species.genomes[0]) < threshold else False
def initialize_population(self): """ Initializes population by creating random genomes and adding them to appropriate species """ from NEAT.Genome import Genome import numpy as np birds = list() for i in range(self.population_size): random_genome_connections = Genome.get_random_connection_genes( input_nodes=2, output_nodes=1, init=True) genome = Genome(random_genome_connections, input_nodes=2, output_nodes=1) self.add_to_species(genome) birds.append( Bird(100, np.random.randint(20, 500), genome=genome, show_bird=True)) return birds
def crossover(parent_1_genome, parent_2_genome): from NEAT.Genome import Genome """ Abstract for crossover between two genomes. Actual crossover at get_child_connections(parent_1_genome, parent_2_genome) Mutation rates: Refer paper section 4.1 Node Mutation = 0.03 Connection Mutation = 0.05 Weight Mutation = 0.8 Weight Perturb Rate = 0.9 Weight Reassign Rate = 0.1 Connection Enable/Disable Rate = 0.75 Inter-species Crossover = 0.001 :param parent_1_genome: The genome for 1st parent :param parent_2_genome: The genome for 2nd parent :return: Child genome with mutation applied """ inter_species_crossover_rate = 0.001 node_mutation_rate = 0.03 connection_mutation_rate = 0.05 connection_enable_disable_rate = 0.75 weight_mutation = 0.8 weight_perturb_rate = 0.9 weight_reassign_rate = 0.1 if Species.is_same_species(parent_1_genome, parent_2_genome): perform_crossover = True else: perform_crossover = True if np.random.uniform( ) < inter_species_crossover_rate else False if perform_crossover: child_connections = Species.get_child_connections( parent_1_genome, parent_2_genome) child_genome = Genome(child_connections) if np.random.uniform() < node_mutation_rate: child_genome.add_node_mutation() if np.random.uniform() < connection_mutation_rate: child_genome.add_connection_mutation() if np.random.uniform() < weight_mutation: child_genome.perturb_weights(weight_perturb_rate) child_genome.reassign_weights(weight_reassign_rate) if np.random.uniform() < connection_enable_disable_rate: child_genome.enable_disable_gene( connection_enable_disable_rate) return child_genome else: return max([parent_1_genome, parent_2_genome], key=lambda genome: genome.fitness)
def test_crossover(): genome1 = given_a_genome() genome2 = given_another_genome() new_genome = Genome.crossover(genome2, genome1) print(new_genome)