-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_crossovers.py
78 lines (50 loc) · 2.22 KB
/
test_crossovers.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
77
78
import pytest
from pyga.individual import Individual
from pyga.utils.crossovers import *
@pytest.fixture
def parent_a():
return Individual([0.0, 0.0, 0.0, 0.0], [50.0, 50.0, 50.0, 50.0])
@pytest.fixture
def parent_b():
return Individual([0.0, 0.0, 0.0, 0.0], [50.0, 50.0, 50.0, 50.0])
class TestOnePointCrossover:
def test_cross(self, parent_a, parent_b):
set_a, set_b = set(parent_a.position), set(parent_b.position)
total_parent_set = set_a | set_b
crossover = OnePointCrossover()
ret_a, ret_b = crossover.cross(parent_a, parent_b)
set_ret_a, set_ret_b = set(ret_a.position), set(ret_b.position)
total_ret_set = set_ret_a | set_ret_b
assert total_parent_set == total_ret_set
assert isinstance(ret_a, Individual)
assert isinstance(ret_b, Individual)
class TestTwoPointCrossover:
def test_cross(self, parent_a, parent_b):
set_a, set_b = set(parent_a.position), set(parent_b.position)
total_parent_set = set_a | set_b
crossover = TwoPointCrossover()
ret_a, ret_b = crossover.cross(parent_a, parent_b)
set_ret_a, set_ret_b = set(ret_a.position), set(ret_b.position)
total_ret_set = set_ret_a | set_ret_b
assert total_parent_set == total_ret_set
assert isinstance(ret_a, Individual)
assert isinstance(ret_b, Individual)
class TestUniformCrossover:
@pytest.mark.parametrize('p_swap', [0.1, None, 0.7])
def test_cross(self, parent_a, parent_b, p_swap):
if p_swap == 0.7:
with pytest.raises(ValueError):
crossover = UniformCrossover(p_swap=p_swap)
else:
crossover = UniformCrossover(p_swap=p_swap)
ret_a, ret_b = crossover.cross(parent_a, parent_b)
set_a, set_b = set(parent_a.position), set(parent_b.position)
total_parent_set = set_a | set_b
set_ret_a, set_ret_b = set(ret_a.position), set(ret_b.position)
total_ret_set = set_ret_a | set_ret_b
assert total_parent_set == total_ret_set
assert isinstance(ret_a, Individual)
assert isinstance(ret_b, Individual)
class TestKVectorUniformCrossover:
def test_cross(self):
pass