Example #1
0
    def test_clone(self):
        ''' Make sure individual can be cloned correctly.
        '''
        indv = GAIndividual(ranges=[(0, 1)],
                            encoding='binary',
                            eps=0.001).init(variants=[0.398])
        indv_clone = indv.clone()

        self.assertListEqual(indv.chromsome, indv_clone.chromsome)
        self.assertAlmostEqual(indv.variants[0], indv_clone.variants[0], places=2)
        self.assertEqual(indv.ranges, indv_clone.ranges)
        self.assertEqual(indv.eps, indv_clone.eps)
        self.assertEqual(indv.encoding, indv_clone.encoding)
Example #2
0
 def test_multi_precisions(self):
     ''' Make sure we can construct individual using different decrete precisions.
     '''
     indv = GAIndividual(ranges=[(0, 1), (0, 10)],
                         encoding='binary',
                         eps=[0.01, 1.0]).init(variants=[0.3, 0.5])
     self.assertNotEqual(indv.precisions[0], indv.precisions[1])
Example #3
0
    def setUp(self):
        self.maxDiff = True
        self.indv_template = GAIndividual(ranges=[(0, 1)])

        def fitness(indv):
            x, = indv.variants
            return x**3 - 60 * x**2 + 900 * x + 100

        self.fitness = fitness
    def test_selection(self):
        indv = GAIndividual(ranges=[(0, 30)])
        p = GAPopulation(indv)
        p.init()

        selection = TournamentSelection()
        father, mother = selection.select(p, fitness=self.fitness)

        self.assertTrue(isinstance(father, GAIndividual))
        self.assertTrue(isinstance(mother, GAIndividual))
Example #5
0
 def test_mutate(self):
     ''' Make sure the individual can be mutated correctly.
     '''
     indv = GAIndividual(ranges=[(0, 1)]).init(variants=[0.398])
     mutation = FlipBitMutation(pm=1.0)
     chromsome_before = [0, 1, 1, 0, 0, 1, 0, 1, 1]
     chromsome_after = [1, 0, 0, 1, 1, 0, 1, 0, 0]
     self.assertListEqual(indv.chromsome, chromsome_before)
     mutation.mutate(indv)
     self.assertListEqual(indv.chromsome, chromsome_after)
Example #6
0
    def test_selection(self):
        indv = GAIndividual(ranges=[(0, 30)])
        p = GAPopulation(indv)
        p.init()

        selection = LinearRankingSelection()
        father, mother = selection.select(p, fitness=self.fitness)

        self.assertTrue(isinstance(father, GAIndividual))
        self.assertTrue(isinstance(mother, GAIndividual))
        self.assertNotEqual(father.chromsome, mother.chromsome)
Example #7
0
    def test_binary_encoding(self):
        ''' Make sure individual can decode and encode binary gene correctly.
        '''
        indv = GAIndividual(ranges=[(0, 1)], encoding='binary', eps=0.001)
        indv.init(variants=[0.398])

        # Test binary chromsome.
        ref_chromsome = [0, 1, 1, 0, 0, 1, 0, 1, 1]
        self.assertListEqual(indv.chromsome, ref_chromsome)

        # Test decode.
        self.assertListEqual(indv.decode(), [0.3972602739726027])

        indv = GAIndividual(ranges=[(0, 1), (-1, 1)], encoding='binary', eps=0.001)
        indv.init(variants=[0.398, 0.66])

        # Test binary chromsome.
        ref_chromsome = [0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1]
        self.assertListEqual(indv.chromsome, ref_chromsome)

        # Test decode.
        self.assertListEqual(indv.decode(), [0.3972602739726027, 0.6598240469208212])
Example #8
0
    def test_decimal_construction(self):
        ''' Make sure individual can decode and encode decimal gene correctly.
        '''
        indv = GAIndividual(ranges=[(0, 1)], encoding='decimal', eps=0.001)

        indv.init(variants=[0.398])
        self.assertListEqual(indv.encode(), [0.398])
        self.assertListEqual(indv.decode(), [0.398])
Example #9
0
    def test_init(self):
        ''' Make sure the individual can be initialized correctly.
        '''
        indv = GAIndividual(ranges=[(0, 1)], encoding='binary', eps=0.001)

        # Check chromsome initialization.
        indv.init(chromsome=[0, 1, 1, 0, 0, 0, 1, 1, 1, 0])
        
        self.assertListEqual([0, 1, 1, 0, 0, 0, 1, 1, 1, 0], indv.chromsome)
        self.assertListEqual(indv.variants, [0.38943248532289626])

        # Check variants initialization.
        indv.init(variants=[0.398])
        
        self.assertListEqual(indv.variants, [0.398])
        self.assertListEqual(indv.chromsome, [0, 1, 1, 0, 0, 1, 0, 1, 1])
Example #10
0
 def __init__(self, sData, pData, ranges, encoding='binary', eps=0.001):
     self.sData = sData
     self.pData = pData
     GAIndividual.__init__(self, ranges, encoding, eps)