def test(self):
     zeros = BinaryChromosome(size=5, initial_state='ones', evaluate=arb_eval)
     ones = BinaryChromosome(size=5, initial_state='ones', evaluate=arb_eval)
     crossover = MidpointCrossoverProcreator()
     children = crossover.procreate([zeros, ones])
     actuals = [1, 1, 1, 1, 1]
     for gene, actual in zip(children[0].genes, actuals):
         self.assertEqual(gene, actual)
예제 #2
0
 def test(self):
     crossover = Procreator()
     parents = [
         BinaryChromosome(size=5, evaluate=arb_eval, initial_state='zeros'),
         BinaryChromosome(size=5, evaluate=arb_eval, initial_state='zeros')
     ]
     children = crossover.procreate(parents)
     self.assertEqual(parents, children)
 def runTest(self):
     mutator = BinaryMutationProcreator(mutation_rate=1)
     choms = [
         BinaryChromosome(size=5, evaluate=arb_eval, initial_state='zeros'),
         BinaryChromosome(size=5, evaluate=arb_eval, initial_state='zeros')
     ]
     mutated = mutator.mutate(choms)
     self.assertEqual([1,1,1,1,1], list(mutated[0].genes))
     self.assertEqual([1,1,1,1,1], list(mutated[1].genes))
 def runTest(self):
     seed(2222)
     mutator = BinaryMutationProcreator(mutation_rate=0.5)
     chroms = [
         BinaryChromosome(size=5, evaluate=arb_eval, initial_state='zeros'),
         BinaryChromosome(size=5, evaluate=arb_eval, initial_state='ones'),
         BinaryChromosome(size=5, evaluate=arb_eval, initial_state='random')
     ]
     mutated = mutator.mutate(chroms)
     self.assertEqual([0,0,0,0,1], list(mutated[0].genes))
     self.assertEqual([1,1,0,0,0], list(mutated[1].genes))
     self.assertEqual([0,1,1,0,1], list(mutated[2].genes))
 def test(self):
     seed(1)
     zeros = BinaryChromosome(size=5,
                              initial_state='zeros',
                              evaluate=arb_eval)
     ones = BinaryChromosome(size=5,
                             initial_state='ones',
                             evaluate=arb_eval)
     crossover = UniformCrossoverProcreator(0)
     children = crossover.procreate([zeros, ones])
     actuals = [0, 0, 0, 0, 0]
     for gene, actual in zip(children[0].genes, actuals):
         self.assertEqual(gene, actual)
 def runTest(self):
     mutator = Mutator(mutation_rate=0.01)
     chrom = BinaryChromosome(size=5,
                              evaluate=arb_eval,
                              initial_state='zeros')
     with self.assertRaises(TypeError):
         mutator.mutate(chrom, 'asdf')
 def test(self):
     seed(1)
     zeros = BinaryChromosome(size=5,
                              initial_state='zeros',
                              evaluate=arb_eval)
     ones = BinaryChromosome(size=5,
                             initial_state='ones',
                             evaluate=arb_eval)
     crossover = FlatCrossoverProcreator()
     children = crossover.procreate([zeros, ones])
     actuals = [
         0.417022004702574, 0.7203244934421581, 0.00011437481734488664,
         0.30233257263183977, 0.14675589081711304
     ]
     for gene, actual in zip(children[0].genes, actuals):
         self.assertEqual(gene, actual)
예제 #8
0
        self.assertEqual('NPointCrossoverProcreator(crossovers=1)',
                         repr(NPointCrossoverProcreator()))
        self.assertEqual('NPointCrossoverProcreator(crossovers=2)',
                         repr(NPointCrossoverProcreator(2)))


#
# MARK: procreate
#


def arb_eval(genes):
    return 0


zeros = BinaryChromosome(size=5, initial_state='zeros', evaluate=arb_eval)
ones = BinaryChromosome(size=5, initial_state='ones', evaluate=arb_eval)


class ShouldRaiseErrorOnNoParents(TestCase):
    def test(self):
        crossover = NPointCrossoverProcreator()
        with self.assertRaises(ValueError):
            crossover.procreate([])


class ShouldRaiseErrorOnTooFewParents(TestCase):
    def test(self):
        crossover = NPointCrossoverProcreator()
        with self.assertRaises(ValueError):
            crossover.procreate([ones])
예제 #9
0
 def test(self):
     crossover = Procreator()
     with self.assertRaises(ValueError):
         crossover.procreate([BinaryChromosome(size=5, evaluate=arb_eval)])
 def runTest(self):
     mutator = BinaryMutationProcreator(mutation_rate=0)
     mutated = mutator.mutate(BinaryChromosome(size=5,
                                               evaluate=arb_eval,
                                               initial_state='zeros'))
     self.assertEqual([0,0,0,0,0], list(mutated.genes))
 def runTest(self):
     mutator = Mutator(mutation_rate=0.01)
     chrom = BinaryChromosome(size=5,
                              evaluate=arb_eval,
                              initial_state='zeros')
     mutator.mutate(chrom, True)