Example #1
0
 def test_instantiate_prune(self):
     """ Ensures a random number of sub matrices are generated. """
     kgnm = KitanoGenome(genome_size=10)
     length = kgnm.get_genome_length()
     grammar = ""
     assert length == 10
     grammar = ""
     kgnm = KitanoGenome(genome_size=15)  # Try an odd size
     length = kgnm.get_genome_length()
     assert length == 16
     kgnm = KitanoGenome(genome_size=15)  # Try a different size
     kgnm._expected_genome_size = 6
     kgnm.prune_genome()
     length = kgnm.get_genome_length()
     assert length == 6
     kgnm = KitanoGenome(genome_size=10)  # Try a different size
     kgnm._expected_genome_size = 6
     kgnm.prune_genome()
     length = kgnm.get_genome_length()
     assert length == 6
     kgnm = KitanoGenome(9)
     kgnm.clear_genes()
     x1 = SubMatrixGene("aadc")
     x2 = SubMatrixGene("aadc")
     x3 = SubMatrixGene("c")
     x4 = SubMatrixGene("cb")
     x5 = SubMatrixGene("b")
     x6 = SubMatrixGene("da")
     kgnm._genes = [x1, x2, x3, x4, x5, x6]
     kgnm.prune_genome()
     length = kgnm.get_genome_length()
     assert length == 10
Example #2
0
 def _reproduction(self, parents):
     """ Reproduces the organism from the parent. """
     genome_size = parents[0].get_expected_genome_size()
     population = []  # New population
     parent_a_genes = parents[0].get_genes(
     )  # Get the parents genes for crossover
     parent_b_genes = parents[1].get_genes()
     all_genes = []
     all_genes.extend(parent_a_genes)  # Generate a list of all genes
     all_genes.extend(parent_b_genes)
     while len(population) < 10:
         # Make child by randomly selecting genes then pruning
         child = KitanoGenome(genome_size)
         child.clear_genes()
         # Create a random permutation of the genes to be added
         shuffle(all_genes)
         # Add all genes to child then prune
         for gene in all_genes:
             child.add_gene(SubMatrixGene(gene.get_data()))
         child.prune_genome()
         population.append(child)
     return population