forked from ahmedfgad/GeneticAlgorithmPython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_gene_space_allow_duplicate_genes.py
449 lines (368 loc) · 22.7 KB
/
test_gene_space_allow_duplicate_genes.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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
"""
This script is identical to the test_gene_space.py script except for:
Setting allow_duplicate_genes=False instead of True.
"""
import pygad
import random
import numpy
num_generations = 100
initial_population = [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]
# Test single gene space with nested gene type.
def number_respect_gene_space(gene_space=None,
gene_type=float,
num_genes=10,
mutation_by_replacement=False,
random_mutation_min_val=-1,
random_mutation_max_val=1,
init_range_low=-4,
init_range_high=4,
initial_population=None):
def fitness_func(ga, solution, idx):
return random.random()
ga_instance = pygad.GA(num_generations=num_generations,
num_parents_mating=5,
fitness_func=fitness_func,
sol_per_pop=10,
num_genes=num_genes,
gene_space=gene_space,
gene_type=gene_type,
initial_population=initial_population,
init_range_low=init_range_low,
init_range_high=init_range_high,
random_mutation_min_val=random_mutation_min_val,
random_mutation_max_val=random_mutation_max_val,
allow_duplicate_genes=False,
mutation_by_replacement=mutation_by_replacement,
save_solutions=True,
suppress_warnings=True,
random_seed=2)
ga_instance.run()
ga_instance.solutions = numpy.array(ga_instance.solutions,
dtype=object)
# gene_space_unpacked = ga_instance.unpack_gene_space(num_values_from_inf_range=100)
num_outside = 0
if ga_instance.gene_space_nested == True:
for gene_idx in range(ga_instance.num_genes):
all_gene_values = ga_instance.solutions[:, gene_idx]
if type(ga_instance.gene_space[gene_idx]) in [list, tuple, range, numpy.ndarray]:
current_gene_space = list(ga_instance.gene_space[gene_idx])
for val in all_gene_values:
if val in current_gene_space:
# print(val, current_gene_space)
pass
else:
# print(gene_idx, val, current_gene_space)
num_outside += 1
elif type(ga_instance.gene_space[gene_idx]) is dict:
if not "step" in ga_instance.gene_space[gene_idx].keys():
for val in all_gene_values:
if val >= ga_instance.gene_space[gene_idx]["low"] and val < ga_instance.gene_space[gene_idx]["high"]:
pass
else:
num_outside += 1
else:
gene_space_values = numpy.arange(ga_instance.gene_space[gene_idx]["low"],
ga_instance.gene_space[gene_idx]["high"],
ga_instance.gene_space[gene_idx]["step"])
for val in all_gene_values:
if val in gene_space_values:
pass
else:
num_outside += 1
elif type(ga_instance.gene_space[gene_idx]) in ga_instance.supported_int_float_types:
for val in all_gene_values:
if val == ga_instance.gene_space[gene_idx]:
pass
else:
num_outside += 1
else:
for gene_idx in range(ga_instance.num_genes):
all_gene_values = ga_instance.solutions[:, gene_idx]
# print("all_gene_values", gene_idx, all_gene_values)
if type(ga_instance.gene_space) in [list, tuple, range, numpy.ndarray]:
current_gene_space = list(ga_instance.gene_space)
for val in all_gene_values:
if val in current_gene_space:
pass
else:
num_outside += 1
elif type(ga_instance.gene_space) is dict:
if not "step" in ga_instance.gene_space.keys():
for val in all_gene_values:
if val >= ga_instance.gene_space["low"] and val < ga_instance.gene_space["high"]:
pass
else:
num_outside += 1
else:
gene_space_values = numpy.arange(ga_instance.gene_space["low"],
ga_instance.gene_space["high"],
ga_instance.gene_space["step"])
for val in all_gene_values:
if val in gene_space_values:
pass
else:
num_outside += 1
print("Number of outside range is {num_outside}.".format(num_outside=num_outside))
return num_outside, ga_instance
def test_gene_space_range():
num_outside, _ = number_respect_gene_space(gene_space=range(10))
assert num_outside == 0
def test_gene_space_numpy_arange():
num_outside, _ = number_respect_gene_space(gene_space=numpy.arange(10))
assert num_outside == 0
def test_gene_space_list():
num_outside, _ = number_respect_gene_space(gene_space=list(range(10)))
assert num_outside == 0
def test_gene_space_numpy():
num_outside, _ = number_respect_gene_space(gene_space=numpy.array(list(range(10))))
assert num_outside == 0
def test_gene_space_dict_without_step():
num_outside, ga_instance = number_respect_gene_space(gene_space={"low": 0, "high": 10})
# print(ga_instance.population)
assert num_outside == 0
def test_gene_space_dict_with_step():
num_outside, ga_instance = number_respect_gene_space(gene_space={"low": 0, "high": 10, "step": 2})
assert num_outside == 0
def test_gene_space_list_single_value():
num_outside, ga_instance = number_respect_gene_space(gene_space=[5])
# print(ga_instance.population)
assert num_outside == 0
def test_gene_space_range_nested_gene_type():
num_outside, _ = number_respect_gene_space(gene_space=range(10),
gene_type=[int, float, numpy.float64, [float, 3], [float, 4], numpy.int16, [numpy.float32, 1], int, float, [float, 3]])
assert num_outside == 0
def test_gene_space_numpy_arange_nested_gene_type():
num_outside, _ = number_respect_gene_space(gene_space=numpy.arange(10),
gene_type=[int, float, numpy.float64, [float, 3], [float, 4], numpy.int16, [numpy.float32, 1], int, float, [float, 3]])
assert num_outside == 0
def test_gene_space_list_nested_gene_type():
num_outside, _ = number_respect_gene_space(gene_space=list(range(10)),
gene_type=[int, float, numpy.float64, [float, 3], [float, 4], numpy.int16, [numpy.float32, 1], int, float, [float, 3]])
assert num_outside == 0
def test_gene_space_numpy_nested_gene_type():
num_outside, _ = number_respect_gene_space(gene_space=numpy.array(list(range(10))),
gene_type=[int, float, numpy.float64, [float, 3], [float, 4], numpy.int16, [numpy.float32, 1], int, float, [float, 3]])
assert num_outside == 0
def test_gene_space_dict_without_step_nested_gene_type():
num_outside, ga_instance = number_respect_gene_space(gene_space={"low": 0, "high": 10},
gene_type=[int, float, numpy.float64, [float, 3], [float, 4], numpy.int16, [numpy.float32, 1], int, float, [float, 3]])
# print(ga_instance.population)
assert num_outside == 0
def test_gene_space_dict_with_step_nested_gene_type():
num_outside, ga_instance = number_respect_gene_space(gene_space={"low": 0, "high": 10, "step": 2},
gene_type=[int, float, numpy.float64, [float, 3], [float, 4], numpy.int16, [numpy.float32, 1], int, float, [float, 3]])
assert num_outside == 0
def test_gene_space_list_single_value_nested_gene_type():
num_outside, ga_instance = number_respect_gene_space(gene_space=[5],
gene_type=[int, float, numpy.float64, [float, 3], [float, 4], numpy.int16, [numpy.float32, 1], int, float, [float, 3]])
# print(ga_instance.population)
assert num_outside == 0
def test_nested_gene_space_range():
num_outside, ga_instance = number_respect_gene_space(gene_space=[range(0, 10),
range(10, 20),
range(20, 30),
range(30, 40),
range(40, 50),
range(50, 60),
range(60, 70),
range(70, 80),
range(80, 90),
range(90, 100)])
# print(ga_instance.population)
assert num_outside == 0
def test_nested_gene_space_dict_without_step():
num_outside, ga_instance = number_respect_gene_space(gene_space=[{"low": 0, "high": 10},
{"low": 10, "high": 20},
{"low": 20, "high": 30},
{"low": 30, "high": 40},
{"low": 40, "high": 50},
{"low": 50, "high": 60},
{"low": 60, "high": 70},
{"low": 70, "high": 80},
{"low": 80, "high": 90},
{"low": 90, "high": 100}])
# print(ga_instance.population)
assert num_outside == 0
def test_nested_gene_space_dict_without_step_float_gene_type():
num_outside, ga_instance = number_respect_gene_space(gene_space=[{"low": 0, "high": 10},
{"low": 10, "high": 20},
{"low": 20, "high": 30},
{"low": 30, "high": 40},
{"low": 40, "high": 50},
{"low": 50, "high": 60},
{"low": 60, "high": 70},
{"low": 70, "high": 80},
{"low": 80, "high": 90},
{"low": 90, "high": 100}],
gene_type=[float, 3])
# print(ga_instance.population)
assert num_outside == 0
def test_nested_gene_space_dict_with_step():
num_outside, ga_instance = number_respect_gene_space(gene_space=[{"low": 0, "high": 10, "step": 1},
{"low": 10, "high": 20, "step": 1.5},
{"low": 20, "high": 30, "step": 2},
{"low": 30, "high": 40, "step": 2.5},
{"low": 40, "high": 50, "step": 3},
{"low": 50, "high": 60, "step": 3.5},
{"low": 60, "high": 70, "step": 4},
{"low": 70, "high": 80, "step": 4.5},
{"low": 80, "high": 90, "step": 5},
{"low": 90, "high": 100, "step": 5.5}])
# print(ga_instance.population)
assert num_outside == 0
def test_nested_gene_space_numpy_arange():
num_outside, ga_instance = number_respect_gene_space(gene_space=[numpy.arange(0, 10),
numpy.arange(10, 20),
numpy.arange(20, 30),
numpy.arange(30, 40),
numpy.arange(40, 50),
numpy.arange(50, 60),
numpy.arange(60, 70),
numpy.arange(70, 80),
numpy.arange(80, 90),
numpy.arange(90, 100)])
# print(ga_instance.population)
assert num_outside == 0
def test_nested_gene_space_list():
num_outside, ga_instance = number_respect_gene_space(gene_space=[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[-10, 10, 20, 30, 40, 50, 60, 70, 80, 90],
[-11, 11, 22, 33, 44, 55, 66, 77, 88, 99],
[-100, 100, 200, 300, 400, 500, 600, 700, 800, 900],
[-4.1, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9],
[-5.1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9],
[-10.5, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9],
[-15, 15, 25, 35, 45, 55, 65, 75, 85, 95],
[30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49]])
# print(ga_instance.population)
assert num_outside == 0
def test_nested_gene_space_list2():
num_outside, ga_instance = number_respect_gene_space(gene_space=[[0, 1],
[1, 2],
[2, 3],
[3, 4],
[4, 5],
[5, 6],
[6, 7],
[7, 8],
[8, 9],
[9, 10]])
assert num_outside == 0
def test_nested_gene_space_mix():
num_outside, ga_instance = number_respect_gene_space(gene_space=[[0, 1, 2, 3, 4],
numpy.arange(5, 10),
range(10, 15),
{"low": 15, "high": 20},
{"low": 20, "high": 30, "step": 2},
None,
numpy.arange(30, 35),
numpy.arange(35, 40),
numpy.arange(40, 45),
[45, 46, 47, 48, 49]],
gene_type=int)
assert num_outside == 0
def test_nested_gene_space_mix_nested_gene_type():
num_outside, ga_instance = number_respect_gene_space(gene_space=[[0, 1, 2, 3, 4],
numpy.arange(5, 10),
range(10, 15),
{"low": 15, "high": 20},
{"low": 20, "high": 30, "step": 2},
None,
numpy.arange(30, 35),
numpy.arange(35, 40),
numpy.arange(40, 45),
[45, 46, 47, 48, 49]],
gene_type=[int, float, numpy.float64, [float, 3], [float, 4], numpy.int16, [numpy.float32, 1], int, float, [float, 3]])
# print(ga_instance.population)
assert num_outside == 0
def test_nested_gene_space_mix_initial_population():
num_outside, ga_instance = number_respect_gene_space(gene_space=[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
numpy.arange(0, 10),
range(0, 10),
{"low": 0, "high": 10},
{"low": 00, "high": 10, "step": 1},
range(0, 10),
numpy.arange(0, 10),
numpy.arange(0, 10),
{"low": 0, "high": 10},
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]],
gene_type=[int, float, numpy.float64, [float, 3], [float, 4], numpy.int16, [numpy.float32, 1], int, float, [float, 3]],
initial_population=initial_population)
# print(ga_instance.population)
assert num_outside == 0
def test_nested_gene_space_mix_initial_population_single_gene_type():
num_outside, ga_instance = number_respect_gene_space(gene_space=[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
numpy.arange(0, 10),
range(0, 10),
{"low": 0, "high": 10},
{"low": 0, "high": 10},
range(0, 10),
numpy.arange(0, 10),
numpy.arange(0, 10),
{"low": 0, "high": 10},
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]],
gene_type=[float, 2],
initial_population=initial_population)
# print(ga_instance.population)
assert num_outside == 0
if __name__ == "__main__":
# print()
# test_gene_space_range()
# print()
# test_gene_space_range_nested_gene_type()
# print()
# test_gene_space_numpy_arange()
# print()
# test_gene_space_numpy_arange_nested_gene_type()
# print()
# test_gene_space_list()
# print()
# test_gene_space_list_nested_gene_type()
# print()
# test_gene_space_list_single_value()
# print()
# test_gene_space_list_single_value_nested_gene_type()
# print()
# test_gene_space_numpy()
# print()
# test_gene_space_numpy_nested_gene_type()
# print()
# test_gene_space_dict_without_step()
# print()
# test_gene_space_dict_without_step_nested_gene_type()
# print()
# test_gene_space_dict_with_step()
# print()
test_gene_space_dict_with_step_nested_gene_type()
print()
test_nested_gene_space_range()
print()
test_nested_gene_space_dict_without_step()
print()
test_nested_gene_space_dict_without_step_float_gene_type()
print()
test_nested_gene_space_dict_with_step()
print()
test_nested_gene_space_numpy_arange()
print()
test_nested_gene_space_list()
print()
test_nested_gene_space_list2()
print()
test_nested_gene_space_mix()
print()
test_nested_gene_space_mix_nested_gene_type()
print()
test_nested_gene_space_mix_initial_population()
print()
test_nested_gene_space_mix_initial_population_single_gene_type()
print()