示例#1
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)
示例#2
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)
示例#3
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]))
示例#4
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]))
示例#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)
示例#9
0
 def test_alleles_correctly_stored_after_initialization(self):
     alleles = utils.DummyAlleles()
     spawning_pool = ListIndividualSpawningPool(10, alleles)
     self.assertEquals(alleles, spawning_pool.alleles)
     self.assertIs(alleles, spawning_pool.alleles)
示例#10
0
 def test_size_is_correctly_stored_after_initialization(self):
     for size in (10, 100, 1000, 10000, 100000, 1000000):
         sp = ListIndividualSpawningPool(size, utils.DummyAlleles())
         self.assertEquals(size, sp.size)
示例#11
0
 def test_class_is_pickeable(self):
     """ Checks if it's pickeable by writing it into a temporary file. """
     with TemporaryFile() as f:
         pickle.dump(ListIndividualSpawningPool(10, utils.DummyAlleles()),
                     f)