-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_unit.py
77 lines (55 loc) · 2.61 KB
/
test_unit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# To change this template, choose Tools | Templates
# and open the template in the editor.
import unittest
import random
import eap.base as base
import eap.operators as operators
class TestCrossovers(unittest.TestCase):
def setUp(self):
random.seed(0)
self.Ind1 = base.ListIndividual(size=5, generator=base.indiceGenerator(5),
fitness=base.Fitness)
self.Ind2 = base.ListIndividual(size=5, generator=base.indiceGenerator(5),
fitness=base.Fitness)
def testTwoPointsCrossover(self):
expectedChild1 = [2, 0, 1, 1, 4]
expectedChild2 = [3, 0, 4, 3, 2]
child1, child2 = operators.twoPointsCx(self.Ind1, self.Ind2)
self.assertEquals(child1, expectedChild1)
self.assertEquals(child2, expectedChild2)
def testOnePointCrossover(self):
expectedChild1 = [2, 0, 4, 1, 2]
expectedChild2 = [3, 0, 1, 3, 4]
child1, child2 = operators.onePointCx(self.Ind1, self.Ind2)
self.assertEquals(child1, expectedChild1)
self.assertEquals(child2, expectedChild2)
def testPMXCrossover(self):
expectedChild1 = [2, 0, 4, 3, 1]
expectedChild2 = [3, 0, 1, 4, 2]
child1, child2 = operators.pmxCx(self.Ind1, self.Ind2)
self.assertEquals(child1, expectedChild1)
self.assertEquals(child2, expectedChild2)
class TestMutations(unittest.TestCase):
def setUp(self):
random.seed(0)
self.RealInd = base.ListIndividual(size=5, generator=base.realGenerator(),
fitness=base.Fitness)
self.BoolInd = base.ListIndividual(size=5, generator=base.booleanGenerator(),
fitness=base.Fitness)
def testFilpBitMutation(self):
expectedMutant = [False, True, True, False, True]
mutant = operators.flipBitMut(self.BoolInd, flipIndxPb=0.5)
self.assertEquals(mutant, expectedMutant)
def testShuffleIndexesMutation(self):
expectedMutant = [False, True, True, False, False]
mutant = operators.shuffleIndxMut(self.BoolInd, shuffleIndxPb=0.5)
self.assertEquals(mutant, expectedMutant)
def testGaussianMutation(self):
expectedMutant = [0.84442185152504812, 0.75795440294030247,
1.445875763057654, 0.56536772757640075, 0.51127472136860852]
mutant = operators.gaussMut(self.RealInd, mu=1, sigma=0.5, mutIndxPb=0.5)
self.assertEquals(mutant, expectedMutant)
class TestAlgorithms(unittest.TestCase):
pass
if __name__ == '__main__':
unittest.main()