forked from anyoptimization/pymoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_archive.py
117 lines (80 loc) · 3.23 KB
/
test_archive.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import numpy as np
from pymoo.algorithms.moo.nsga2 import RankAndCrowdingSurvival
from pymoo.core.individual import Individual
from pymoo.core.population import Population
from pymoo.util.archive import SingleObjectiveArchive, MultiObjectiveArchive, SurvivalTruncation
from pymoo.util.nds.non_dominated_sorting import NonDominatedSorting
def test_unconstr_add_to_archive():
archive = SingleObjectiveArchive()
assert len(archive) == 0
a = Individual(X=np.array([5.0]), F=np.array([5.0]))
pop = Population.create(a)
archive = archive.add(pop)
assert len(archive) == 1
b = Individual(X=np.array([0.0]), F=np.array([0.0]))
pop = Population.create(b)
archive = archive.add(pop)
assert len(archive) == 1
assert archive[0].f == 0.0
archive = archive.add(pop)
assert len(archive) == 1
c = Individual(X=np.array([-1.0]), F=np.array([0.0]))
pop = Population.create(c)
archive = archive.add(pop)
assert len(archive) == 2
def test_constr_add_to_archive():
archive = SingleObjectiveArchive()
assert len(archive) == 0
a = Individual(X=np.array([0.0]), F=np.array([0.0]), CV=np.array([1.0]))
pop = Population.create(a)
archive = archive.add(pop)
assert len(archive) == 1
b = Individual(X=np.array([5.0]), F=np.array([5.0]), CV=np.array([0.5]))
pop = Population.create(b)
archive = archive.add(pop)
assert len(archive) == 1
assert archive[0].f == 5.0
assert archive[0].cv == 0.5
c = Individual(X=np.array([10.0]), F=np.array([10.0]), CV=np.array([0.0]))
pop = Population.create(c)
archive = archive.add(pop)
assert len(archive) == 1
assert archive[0].f == 10.0
assert archive[0].cv == 0.0
d = Individual(X=np.array([7.0]), F=np.array([7.0]), CV=np.array([0.0]))
pop = Population.create(d)
archive = archive.add(pop)
assert len(archive) == 1
assert archive[0].f == 7.0
assert archive[0].cv == 0.0
def test_max_size():
archive = SingleObjectiveArchive(max_size=1)
assert len(archive) == 0
a = Individual(X=np.array([5.0]), F=np.array([0.0]))
pop = Population.create(a)
archive = archive.add(pop)
assert len(archive) == 1
c = Individual(X=np.array([-1.0]), F=np.array([0.0]))
pop = Population.create(c)
archive = archive.add(pop)
assert len(archive) == 1
def test_multi_objective_archive():
a = Individual(X=np.array([5.0]), F=np.array([1.0, 5.0]))
pop = Population.create(a)
archive = MultiObjectiveArchive().add(pop)
assert len(archive) == 1
b = Individual(X=np.array([10.0]), F=np.array([2.0, 2.0]))
pop = Population.create(b)
archive = archive.add(pop)
assert len(archive) == 2
def test_multi_objective_archive_multi():
np.random.seed(1)
X, F = np.random.random(size=(100, 10)), np.random.random(size=(100, 3))
pop = Population.new(X=X, F=F)
archive = MultiObjectiveArchive().add(pop)
actual = pop[NonDominatedSorting().do(F, only_non_dominated_front=True)]
assert np.all(actual == archive)
archive = MultiObjectiveArchive(max_size=5).add(pop)
assert len(archive) == 5
archive = MultiObjectiveArchive(max_size=5, truncation=SurvivalTruncation(RankAndCrowdingSurvival())).add(pop)
assert len(archive) == 5