Example #1
0
 def test_evaluate(self, inst_mock):
     """ Ensures the evaluation method works correctly. """
     # Setup fake components
     gene_one = Mock()
     gene_one.get_information = [1, 1, 1]
     gene_two = Mock()
     gene_two.get_information = [0, 0, 0]
     fake_equation = Mock()  # Fake equation to evaluate it against
     fake_equation.get_clause_evaluation = Mock(
         return_value=[False, False, False])  # 0%
     kgnm = KitanoGenome(genome_size=2)
     kgnm._genes = []
     assert kgnm.evaluate(fake_equation) == 0
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
Example #3
0
 def _initialise(self, no_vars):
     """ Initialises the population of directly encoded genomes. """
     self._repopulate([])  # Empty pop
     while len(self.get_population()) < 10:
         new_organism = KitanoGenome(no_vars)
         self._add_organism(new_organism)
Example #4
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 #5
0
 def test_instantiate_1(self, sm_gene, prune_mock):
     """ Ensures a random number of sub matrices are generated. """
     kgnm = KitanoGenome(genome_size=10)
     assert sm_gene.call_count == 10  # Generated 10 Genes?
     assert len(kgnm._genes) == 10
     assert prune_mock.call_count == 1
Example #6
0
 def test_init(self, inst_mock):
     """ Ensures the init initialises the number of genes to be expected.  """
     bgnm = KitanoGenome(genome_size=500)
     assert inst_mock.called