@@ -65,12 +65,37 @@ def fitness_func(ga, solution, idx):
6565 num_outside = 0
6666 if ga_instance .gene_space_nested == True :
6767 for gene_idx in range (ga_instance .num_genes ):
68+
69+ if type (ga_instance .init_range_low ) in ga_instance .supported_int_float_types :
70+ range_min_init = ga_instance .init_range_low
71+ range_max_init = ga_instance .init_range_high
72+ else :
73+ range_min_init = ga_instance .init_range_low [gene_idx ]
74+ range_max_init = ga_instance .init_range_high [gene_idx ]
75+ if type (ga_instance .random_mutation_min_val ) in ga_instance .supported_int_float_types :
76+ range_min_mutation = ga_instance .random_mutation_min_val
77+ range_max_mutation = ga_instance .random_mutation_max_val
78+ else :
79+ range_min_mutation = ga_instance .random_mutation_min_val [gene_idx ]
80+ range_max_mutation = ga_instance .random_mutation_max_val [gene_idx ]
81+
6882 all_gene_values = ga_instance .solutions [:, gene_idx ]
6983 if type (ga_instance .gene_space [gene_idx ]) in [list , tuple , range , numpy .ndarray ]:
7084 current_gene_space = list (ga_instance .gene_space [gene_idx ])
71- for val in all_gene_values :
72- if val in current_gene_space :
73- # print(val, current_gene_space)
85+ # print("current_gene_space", current_gene_space)
86+ for val_idx , val in enumerate (all_gene_values ):
87+ if None in current_gene_space :
88+ if (val in current_gene_space ) or (val >= range_min_init and val < range_max_init ) or (val >= range_min_mutation and val < range_max_mutation ):
89+ pass
90+ else :
91+ # print("###########")
92+ # print(gene_idx, val)
93+ # print(current_gene_space)
94+ # print(range_min_mutation, range_max_mutation)
95+ # print("\n\n")
96+ num_outside += 1
97+ elif val in current_gene_space :
98+ # print("val, current_gene_space", val, current_gene_space)
7499 pass
75100 else :
76101 # print(gene_idx, val, current_gene_space)
@@ -98,14 +123,51 @@ def fitness_func(ga, solution, idx):
98123 pass
99124 else :
100125 num_outside += 1
126+ elif ga_instance .gene_space [gene_idx ] is None :
127+ for val in all_gene_values :
128+ # print(val)
129+ if (val >= range_min_init and val < range_max_init ) or (val >= range_min_mutation and val < range_max_mutation ):
130+ pass
131+ else :
132+ # print("###########")
133+ # print(gene_idx, val)
134+ # print(ga_instance.gene_space[gene_idx])
135+ # print(range_min_init, range_max_init)
136+ # print(range_min_mutation, range_max_mutation)
137+ # print("\n\n")
138+ num_outside += 1
101139 else :
102140 for gene_idx in range (ga_instance .num_genes ):
141+
142+ if type (ga_instance .init_range_low ) in ga_instance .supported_int_float_types :
143+ range_min_init = ga_instance .init_range_low
144+ range_max_init = ga_instance .init_range_high
145+ else :
146+ range_min_init = ga_instance .init_range_low [gene_idx ]
147+ range_max_init = ga_instance .init_range_high [gene_idx ]
148+ if type (ga_instance .random_mutation_min_val ) in ga_instance .supported_int_float_types :
149+ range_min_mutation = ga_instance .random_mutation_min_val
150+ range_max_mutation = ga_instance .random_mutation_max_val
151+ else :
152+ range_min_mutation = ga_instance .random_mutation_min_val [gene_idx ]
153+ range_max_mutation = ga_instance .random_mutation_max_val [gene_idx ]
154+
103155 all_gene_values = ga_instance .solutions [:, gene_idx ]
104156 # print("all_gene_values", gene_idx, all_gene_values)
105157 if type (ga_instance .gene_space ) in [list , tuple , range , numpy .ndarray ]:
106158 current_gene_space = list (ga_instance .gene_space )
107159 for val in all_gene_values :
108- if val in current_gene_space :
160+ if None in current_gene_space :
161+ if (val in current_gene_space ) or (val >= range_min_init and val < range_max_init ) or (val >= range_min_mutation and val < range_max_mutation ):
162+ pass
163+ else :
164+ # print("###########")
165+ # print(gene_idx, val)
166+ # print(current_gene_space)
167+ # print(range_min_mutation, range_max_mutation)
168+ # print("\n\n")
169+ num_outside += 1
170+ elif val in current_gene_space :
109171 pass
110172 else :
111173 num_outside += 1
@@ -144,6 +206,11 @@ def test_gene_space_list():
144206
145207 assert num_outside == 0
146208
209+ def test_gene_space_list_None ():
210+ num_outside , _ = number_respect_gene_space (gene_space = [30 , None , 40 , 50 , None , 60 , 70 , None , None , None ])
211+
212+ assert num_outside == 0
213+
147214def test_gene_space_numpy ():
148215 num_outside , _ = number_respect_gene_space (gene_space = numpy .array (list (range (10 ))))
149216
@@ -318,6 +385,38 @@ def test_nested_gene_space_list2():
318385
319386 assert num_outside == 0
320387
388+ def test_nested_gene_space_list3_None ():
389+ num_outside , ga_instance = number_respect_gene_space (gene_space = [[0 , None ],
390+ [1 , 2 ],
391+ [2 , None ],
392+ [3 , 4 ],
393+ [None , 5 ],
394+ None ,
395+ [None , 7 ],
396+ [None , None ],
397+ [8 , 9 ],
398+ None ],
399+ mutation_by_replacement = True )
400+
401+ assert num_outside == 0
402+
403+ def test_nested_gene_space_list4_None_custom_mutation_range ():
404+ num_outside , ga_instance = number_respect_gene_space (gene_space = [[0 , None ],
405+ [1 , 2 ],
406+ [2 , None ],
407+ [3 , 4 ],
408+ [None , 5 ],
409+ None ,
410+ [None , 7 ],
411+ [None , None ],
412+ [8 , 9 ],
413+ None ],
414+ random_mutation_min_val = 20 ,
415+ random_mutation_max_val = 40 ,
416+ mutation_by_replacement = True )
417+
418+ assert num_outside == 0
419+
321420def test_nested_gene_space_mix ():
322421 num_outside , ga_instance = number_respect_gene_space (gene_space = [[0 , 1 , 2 , 3 , 4 ],
323422 numpy .arange (5 , 10 ),
@@ -329,7 +428,8 @@ def test_nested_gene_space_mix():
329428 numpy .arange (35 , 40 ),
330429 numpy .arange (40 , 45 ),
331430 [45 , 46 , 47 , 48 , 49 ]],
332- gene_type = int )
431+ gene_type = int ,
432+ mutation_by_replacement = True )
333433
334434 assert num_outside == 0
335435
@@ -344,7 +444,7 @@ def test_nested_gene_space_mix_nested_gene_type():
344444 numpy .arange (35 , 40 ),
345445 numpy .arange (40 , 45 ),
346446 [45 , 46 , 47 , 48 , 49 ]],
347- gene_type = [int , float , numpy .float64 , [float , 3 ], [ float , 4 ] , numpy .int16 , [numpy .float32 , 1 ], int , float , [float , 3 ]])
447+ gene_type = [int , float , numpy .float64 , [float , 3 ], int , numpy .int16 , [numpy .float32 , 1 ], int , float , [float , 3 ]])
348448 # print(ga_instance.population)
349449
350450 assert num_outside == 0
@@ -434,6 +534,8 @@ def test_nested_gene_space_nested_gene_type_adaptive_mutation():
434534
435535 test_gene_space_list ()
436536 print ()
537+ test_gene_space_list_None ()
538+ print ()
437539 test_gene_space_list_nested_gene_type ()
438540 print ()
439541
@@ -478,6 +580,12 @@ def test_nested_gene_space_nested_gene_type_adaptive_mutation():
478580 test_nested_gene_space_list2 ()
479581 print ()
480582
583+ test_nested_gene_space_list3_None ()
584+ print ()
585+
586+ test_nested_gene_space_list4_None_custom_mutation_range ()
587+ print ()
588+
481589 test_nested_gene_space_mix ()
482590 print ()
483591
0 commit comments