Пример #1
0
    def test_crossover_is_performed(self):
        """ Checks that one point crossover works as expected. """
        # TODO Not in the mood. Please future me, rewrite it later
        size = 10
        symbols = (0, 1)
        alleles = FiniteSetAlleles(symbols)
        sp = ListIndividualSpawningPool(size, alleles)
        parents = sp.create(), sp.create()
        progeny = RandomMaskRecombination()(*parents)

        self.assertEquals(len(progeny), len(parents))
        self.assertEquals(len(parents[0]), len(progeny[0]))
        self.assertEquals(len(parents[1]), len(progeny[1]))
Пример #2
0
    def test_crossover_is_performed(self):
        """ Checks that one point crossover works as expected. """
        # TODO Not in the mood. Please future me, rewrite it later
        size = 10
        symbols = (0, 1)
        alleles = FiniteSetAlleles(symbols)
        sp = ListIndividualSpawningPool(size, alleles)
        parents = sp.create(), sp.create()
        progeny = RandomMaskRecombination()(*parents)

        self.assertEquals(len(progeny), len(parents))
        self.assertEquals(len(parents[0]), len(progeny[0]))
        self.assertEquals(len(parents[1]), len(progeny[1]))
Пример #3
0
 def test_created_individuals_are_list_individuals_with_correct_size(self):
     symbols = (0, 1)
     alleles = FiniteSetAlleles(symbols)
     for size in (10, 100, 1000):
         spawning_pool = ListIndividualSpawningPool(size, alleles)
         for _ in range(10):
             individual = spawning_pool.create()
             self.assertEquals(size, len(individual))
             for g in individual:
                 self.assertIn(g, symbols)
Пример #4
0
 def test_created_individuals_are_list_individuals_with_correct_size(self):
     symbols = (0, 1)
     alleles = FiniteSetAlleles(symbols)
     for size in (10, 100, 1000):
         spawning_pool = ListIndividualSpawningPool(size, alleles)
         for _ in range(10):
             individual = spawning_pool.create()
             self.assertEquals(size, len(individual))
             for g in individual:
                 self.assertIn(g, symbols)
Пример #5
0
    def test_check_genes_are_modified(self):
        """ Checks that the genes of the individual are modified.

        For this purpose, two checks are performed. First, the method checks if
        all except one gene are the same. Second, it checks that the different
        gene belongs to the list of allowed values.
        """
        symbols = (0, 1)
        alleles = FiniteSetAlleles(symbols)
        sp = ListIndividualSpawningPool(10, alleles)
        for _ in range(10):
            individual = sp.create()
            mutated = SingleGeneRandomValue(alleles=alleles)(individual, p=1)
            # Have all the genes values allowed by the alleles?
            self.assertEquals([],
                              [i for i in mutated if i not in alleles.symbols])
            # Are all the genes (except one) in the same position?
            different_genes = [
                i for i, j in zip(mutated, individual) if i != j
            ]
            self.assertEquals(len(different_genes), 1)
Пример #6
0
    def test_check_genes_are_modified(self):
        """ Checks that the genes of the individual are modified.

        For this purpose, two checks are performed. First, the method checks if
        all except one gene are the same. Second, it checks that the different
        gene belongs to the list of allowed values.
        """
        symbols = (0, 1)
        alleles = FiniteSetAlleles(symbols)
        sp = ListIndividualSpawningPool(10, alleles)
        for _ in range(10):
            individual = sp.create()
            mutated = SingleGeneRandomValue(alleles=alleles)(individual, p=1)
            # Have all the genes values allowed by the alleles?
            self.assertEquals(
                [],
                [i for i in mutated if i not in alleles.symbols]
            )
            # Are all the genes (except one) in the same position?
            different_genes = [i for i, j in zip(mutated, individual) if i != j]
            self.assertEquals(len(different_genes), 1)
Пример #7
0
 def test_correct_recombination_of_individuals_with_same_length(self):
     symbols = (0, 1)
     alleles = FiniteSetAlleles(symbols)
     sp = ListIndividualSpawningPool(10, alleles)
     parents_list = (
         (sp.create(), sp.create()),
         (sp.create(), sp.create(), sp.create()),
         (sp.create(), sp.create(), sp.create(), sp.create()),
     )
     recombination = self.ListRecombinationNoAbstract()
     for parents in parents_list:
         progeny = recombination(*parents)
         self.assertEquals(len(progeny), len(parents))
         for parent, child in zip(parents, progeny):
             self.assertEquals(parent, child)
             self.assertIsNot(parent, child)
Пример #8
0
 def test_correct_recombination_of_individuals_with_same_length(self):
     symbols = (0, 1)
     alleles = FiniteSetAlleles(symbols)
     sp = ListIndividualSpawningPool(10, alleles)
     parents_list = (
         (sp.create(), sp.create()),
         (sp.create(), sp.create(), sp.create()),
         (sp.create(), sp.create(), sp.create(), sp.create()),
     )
     recombination = self.ListRecombinationNoAbstract()
     for parents in parents_list:
         progeny = recombination(*parents)
         self.assertEquals(len(progeny), len(parents))
         for parent, child in zip(parents, progeny):
             self.assertEquals(parent, child)
             self.assertIsNot(parent, child)