forked from ahmedfgad/GeneticAlgorithmPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_nsga3_pipeline.py
More file actions
325 lines (280 loc) · 13.7 KB
/
Copy pathtest_nsga3_pipeline.py
File metadata and controls
325 lines (280 loc) · 13.7 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
"""
Pipeline-level tests for NSGA-III using hand-built ground-truth values.
Every test in this file pins the output of one NSGA-III step (reference
point generation, ideal point, extreme points, intercepts, normalized
fitness, association, niching, or the full pipeline) to a hardcoded
value derived from a small dataset whose answer can be checked on paper.
The main dataset is THREE_OBJECTIVE_FITNESS: seven solutions in M=3
space whose hand-derived ideal point, extreme points, intercepts, and
normalized positions are all simple round numbers. This makes the
expected outputs easy to verify without re-running the algorithm.
"""
import numpy
import pytest
from pygad.utils.nsga3 import NSGA3
# Three-objective dataset used by most tests below. Values are expressed
# in PyGAD's maximization convention, so all fitness values are <= 0 and
# the ideal point sits at the origin.
#
# s0..s2 : axis extremes (best on one objective, ideal on the others).
# s3..s5 : midpoints of each edge of the simplex.
# s6 : center of the unit simplex.
THREE_OBJECTIVE_FITNESS = numpy.array([
[-1.0, 0.0, 0.0], # s0 - extreme for f0
[ 0.0, -1.0, 0.0], # s1 - extreme for f1
[ 0.0, 0.0, -1.0], # s2 - extreme for f2
[-0.5, -0.5, 0.0], # s3
[-0.5, 0.0, -0.5], # s4
[ 0.0, -0.5, -0.5], # s5
[-1 / 3, -1 / 3, -1 / 3], # s6 - simplex center
])
@pytest.fixture
def nsga3():
return NSGA3()
@pytest.mark.parametrize("num_objectives,num_divisions,expected_count", [
(2, 3, 4),
(3, 4, 15),
(3, 12, 91),
(5, 4, 70),
(8, 3, 120),
])
def test_reference_point_count_matches_binomial(nsga3, num_objectives,
num_divisions, expected_count):
points = nsga3.nsga3_generate_reference_points(num_objectives, num_divisions)
assert points.shape == (expected_count, num_objectives)
numpy.testing.assert_allclose(points.sum(axis=1), 1.0, atol=1e-12)
def test_reference_points_M3_p2_match_expected_set(nsga3):
points = nsga3.nsga3_generate_reference_points(3, 2)
expected = numpy.array([
[1.0, 0.0, 0.0],
[0.5, 0.5, 0.0],
[0.5, 0.0, 0.5],
[0.0, 1.0, 0.0],
[0.0, 0.5, 0.5],
[0.0, 0.0, 1.0],
])
sorted_actual = numpy.array(sorted(points.tolist(), reverse=True))
sorted_expected = numpy.array(sorted(expected.tolist(), reverse=True))
numpy.testing.assert_allclose(sorted_actual, sorted_expected, atol=1e-12)
def test_ideal_point_for_three_objective_set(nsga3):
ideal = nsga3.nsga3_compute_ideal_point(THREE_OBJECTIVE_FITNESS)
numpy.testing.assert_allclose(ideal, [0.0, 0.0, 0.0])
def test_extreme_points_for_three_objective_set(nsga3):
ideal = nsga3.nsga3_compute_ideal_point(THREE_OBJECTIVE_FITNESS)
extremes = nsga3.nsga3_find_extreme_points(THREE_OBJECTIVE_FITNESS, ideal)
expected = numpy.array([
[-1.0, 0.0, 0.0],
[ 0.0, -1.0, 0.0],
[ 0.0, 0.0, -1.0],
])
numpy.testing.assert_allclose(extremes, expected, atol=1e-12)
def test_intercepts_for_three_objective_set(nsga3):
ideal = nsga3.nsga3_compute_ideal_point(THREE_OBJECTIVE_FITNESS)
extremes = nsga3.nsga3_find_extreme_points(THREE_OBJECTIVE_FITNESS, ideal)
intercepts = nsga3.nsga3_compute_intercepts(extremes, ideal, THREE_OBJECTIVE_FITNESS)
numpy.testing.assert_allclose(intercepts, [-1.0, -1.0, -1.0], atol=1e-12)
def test_intercepts_cap_at_worst_observed_per_objective(nsga3):
# Make the linear solve extrapolate well beyond the actual data:
# the extreme points are packed close to the ideal so 1/b is large,
# but the pool's worst values per axis sit much closer to the ideal.
# The cap should pull each intercept back to the worst observed
# value.
ideal = numpy.array([0.0, 0.0])
extremes = numpy.array([
[-0.001, -0.5],
[-0.5, -0.001],
])
pool = numpy.array([
[-0.001, -0.5],
[-0.5, -0.001],
[-0.1, -0.1],
])
intercepts = nsga3.nsga3_compute_intercepts(extremes, ideal, pool)
numpy.testing.assert_allclose(intercepts, pool.min(axis=0), atol=1e-12)
def test_intercepts_fall_back_when_extremes_singular(nsga3):
# Both extreme points are the same row, so the linear system is
# singular. The function must fall back to the worst per objective.
ideal = numpy.array([0.0, 0.0])
extremes = numpy.array([
[-1.0, -1.0],
[-1.0, -1.0],
])
pool = numpy.array([
[-1.0, -1.0],
[-2.0, -0.5],
])
intercepts = nsga3.nsga3_compute_intercepts(extremes, ideal, pool)
numpy.testing.assert_allclose(intercepts, pool.min(axis=0))
def test_normalized_fitness_for_three_objective_set(nsga3):
ideal = nsga3.nsga3_compute_ideal_point(THREE_OBJECTIVE_FITNESS)
extremes = nsga3.nsga3_find_extreme_points(THREE_OBJECTIVE_FITNESS, ideal)
intercepts = nsga3.nsga3_compute_intercepts(extremes, ideal, THREE_OBJECTIVE_FITNESS)
normalized = nsga3.nsga3_normalize_fitness(THREE_OBJECTIVE_FITNESS, ideal, intercepts)
expected = numpy.array([
[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0],
[0.5, 0.5, 0.0],
[0.5, 0.0, 0.5],
[0.0, 0.5, 0.5],
[1 / 3, 1 / 3, 1 / 3],
])
numpy.testing.assert_allclose(normalized, expected, atol=1e-12)
def test_associations_for_three_objective_set(nsga3):
# Reference points produced by nsga3_generate_reference_points(3, 2),
# in the order our enumeration emits them:
# ref[0] = (0, 0, 1 )
# ref[1] = (0, 0.5, 0.5)
# ref[2] = (0, 1, 0 )
# ref[3] = (0.5, 0, 0.5)
# ref[4] = (0.5, 0.5, 0 )
# ref[5] = (1, 0, 0 )
# Each on-simplex solution sits on one reference line and has zero
# distance. The center solution is the same distance from ref[1], ref[3]
# and ref[4]; the lower-index tie break picks ref[1].
nsga3_ref_points = nsga3.nsga3_generate_reference_points(3, 2)
ideal = nsga3.nsga3_compute_ideal_point(THREE_OBJECTIVE_FITNESS)
extremes = nsga3.nsga3_find_extreme_points(THREE_OBJECTIVE_FITNESS, ideal)
intercepts = nsga3.nsga3_compute_intercepts(extremes, ideal, THREE_OBJECTIVE_FITNESS)
normalized = nsga3.nsga3_normalize_fitness(THREE_OBJECTIVE_FITNESS, ideal, intercepts)
nearest, distance = nsga3.nsga3_associate_to_reference_points(normalized, nsga3_ref_points)
expected_nearest = numpy.array([5, 2, 0, 4, 3, 1, 1])
expected_distance = numpy.array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1 / 3])
numpy.testing.assert_array_equal(nearest, expected_nearest)
numpy.testing.assert_allclose(distance, expected_distance, atol=1e-9)
def test_niching_picks_one_candidate_per_empty_niche(nsga3):
# Three critical-front candidates, each attached to a different
# empty niche. We need three survivors. Every empty niche must take
# its only candidate.
critical_front_indices = [10, 11, 12]
critical_front_associations = numpy.array([0, 1, 2])
critical_front_distances = numpy.array([0.01, 0.02, 0.03])
accepted_associations = numpy.array([3, 3, 3])
picked = nsga3.nsga3_niching_select(
critical_front_indices=critical_front_indices,
critical_front_associations=critical_front_associations,
critical_front_distances=critical_front_distances,
accepted_associations=accepted_associations,
num_reference_points=4,
num_to_select=3)
assert set(picked) == {10, 11, 12}
def test_niching_picks_closest_candidate_when_niche_count_is_zero(nsga3):
# Two candidates at the same empty niche. The closer one wins
# deterministically.
critical_front_indices = [20, 21]
critical_front_associations = numpy.array([0, 0])
critical_front_distances = numpy.array([0.5, 0.1])
accepted_associations = numpy.array([1, 2, 3])
picked = nsga3.nsga3_niching_select(
critical_front_indices=critical_front_indices,
critical_front_associations=critical_front_associations,
critical_front_distances=critical_front_distances,
accepted_associations=accepted_associations,
num_reference_points=4,
num_to_select=1)
assert picked == [21]
def test_niching_picks_candidate_in_lower_niche_count_with_unique_owner(nsga3):
# ref 0 has niche count 2, ref 1 has niche count 3. We want the
# candidate at ref 0 because its niche count is smaller.
critical_front_indices = [30, 31]
critical_front_associations = numpy.array([0, 1])
critical_front_distances = numpy.array([0.4, 0.2])
accepted_associations = numpy.array([0, 0, 1, 1, 1])
picked = nsga3.nsga3_niching_select(
critical_front_indices=critical_front_indices,
critical_front_associations=critical_front_associations,
critical_front_distances=critical_front_distances,
accepted_associations=accepted_associations,
num_reference_points=4,
num_to_select=1)
assert picked == [30]
def test_full_pipeline_recovers_simplex_corners(nsga3):
# Run the full pipeline on the THREE_OBJECTIVE_FITNESS dataset and
# verify that the six on-simplex solutions cover six different
# reference points with zero perpendicular distance.
nsga3_ref_points = nsga3.nsga3_generate_reference_points(3, 2)
fitness = THREE_OBJECTIVE_FITNESS
ideal = nsga3.nsga3_compute_ideal_point(fitness)
extremes = nsga3.nsga3_find_extreme_points(fitness, ideal)
intercepts = nsga3.nsga3_compute_intercepts(extremes, ideal, fitness)
normalized = nsga3.nsga3_normalize_fitness(fitness, ideal, intercepts)
nearest, distance = nsga3.nsga3_associate_to_reference_points(normalized, nsga3_ref_points)
covered = numpy.unique(nearest[:6])
assert set(covered.tolist()) == {0, 1, 2, 3, 4, 5}
assert numpy.all(distance[:6] < 1e-9)
# Three solutions used by the wide-range and narrow-range normalization
# tests below. The first row is the f0 extreme, the second is the f1
# extreme, and the third is the middle point of the front. Both versions
# of the dataset must produce the same normalized positions because the
# NSGA-III normalization is invariant to positive affine transforms of
# the fitness.
WIDE_RANGE_FITNESS = numpy.array([
[15.0, -10.0], # s0 - best f0, worst f1
[-10.0, 15.0], # s1 - worst f0, best f1
[ 0.0, 0.0], # s2 - middle
])
NARROW_RANGE_FITNESS = numpy.array([
[0.7, 0.3], # s0
[0.3, 0.7], # s1
[0.5, 0.5], # s2
])
def test_normalize_fitness_for_wide_range_input(nsga3):
# Fitness values cross zero and span 25 units per axis. The
# algorithm must still map the two extremes onto the simplex corners
# and the middle point to (0.6, 0.6).
ideal = nsga3.nsga3_compute_ideal_point(WIDE_RANGE_FITNESS)
extremes = nsga3.nsga3_find_extreme_points(WIDE_RANGE_FITNESS, ideal)
intercepts = nsga3.nsga3_compute_intercepts(extremes, ideal, WIDE_RANGE_FITNESS)
normalized = nsga3.nsga3_normalize_fitness(WIDE_RANGE_FITNESS, ideal, intercepts)
numpy.testing.assert_allclose(ideal, [15.0, 15.0])
numpy.testing.assert_allclose(intercepts, [-10.0, -10.0])
expected_normalized = numpy.array([
[0.0, 1.0],
[1.0, 0.0],
[0.6, 0.6],
])
numpy.testing.assert_allclose(normalized, expected_normalized, atol=1e-12)
def test_normalize_fitness_for_narrow_range_input(nsga3):
# Fitness values are all inside [0.3, 0.7] (a 0.4-wide window).
# Normalization must still pin the two extremes to the simplex
# corners and place the middle point at (0.5, 0.5).
ideal = nsga3.nsga3_compute_ideal_point(NARROW_RANGE_FITNESS)
extremes = nsga3.nsga3_find_extreme_points(NARROW_RANGE_FITNESS, ideal)
intercepts = nsga3.nsga3_compute_intercepts(extremes, ideal, NARROW_RANGE_FITNESS)
normalized = nsga3.nsga3_normalize_fitness(NARROW_RANGE_FITNESS, ideal, intercepts)
numpy.testing.assert_allclose(ideal, [0.7, 0.7])
numpy.testing.assert_allclose(intercepts, [0.3, 0.3])
expected_normalized = numpy.array([
[0.0, 1.0],
[1.0, 0.0],
[0.5, 0.5],
])
numpy.testing.assert_allclose(normalized, expected_normalized, atol=1e-12)
@pytest.mark.parametrize("scale,shift", [
(1.0, 0.0), # identity
(37.0, 0.0), # pure positive scale
(0.01, 0.0), # pure positive scale (shrink)
(1.0, 100.0), # pure shift up
(1.0, -100.0), # pure shift down
(5.0, -12.5), # mixed
])
def test_normalize_fitness_is_invariant_under_positive_affine_transforms(nsga3, scale, shift):
# NSGA-III normalization should not care about the absolute scale or
# offset of fitness as long as the transform is a positive affine
# one. Verify by transforming the base dataset and checking that the
# normalized positions match the untransformed reference.
base = NARROW_RANGE_FITNESS
transformed = scale * base + shift
base_ideal = nsga3.nsga3_compute_ideal_point(base)
base_extremes = nsga3.nsga3_find_extreme_points(base, base_ideal)
base_intercepts = nsga3.nsga3_compute_intercepts(base_extremes, base_ideal, base)
base_normalized = nsga3.nsga3_normalize_fitness(base, base_ideal, base_intercepts)
transformed_ideal = nsga3.nsga3_compute_ideal_point(transformed)
transformed_extremes = nsga3.nsga3_find_extreme_points(transformed, transformed_ideal)
transformed_intercepts = nsga3.nsga3_compute_intercepts(transformed_extremes,
transformed_ideal,
transformed)
transformed_normalized = nsga3.nsga3_normalize_fitness(transformed,
transformed_ideal,
transformed_intercepts)
numpy.testing.assert_allclose(transformed_normalized, base_normalized, atol=1e-9)