@@ -69,7 +69,11 @@ def fitness_func_no_batch_multi(ga, solution, idx):
69
69
70
70
#### Single-Objective
71
71
def test_initial_population_int_by_replacement ():
72
- gene_constraint = [lambda x : x [0 ]>= 8 ,lambda x : x [1 ]>= 8 ,lambda x : 5 >= x [2 ]>= 1 ,lambda x : 5 > x [3 ]> 3 ,lambda x : x [4 ]< 2 ]
72
+ gene_constraint = [lambda x ,v : [val for val in v if val >= 8 ],
73
+ lambda x ,v : [val for val in v if val >= 8 ],
74
+ lambda x ,v : [val for val in v if 5 >= val >= 1 ],
75
+ lambda x ,v : [val for val in v if 5 > val > 3 ],
76
+ lambda x ,v : [val for val in v if val < 2 ]]
73
77
ga_instance = population_gene_constraint (gene_constraint = gene_constraint ,
74
78
init_range_low = 0 ,
75
79
init_range_high = 10 ,
@@ -79,17 +83,21 @@ def test_initial_population_int_by_replacement():
79
83
gene_type = int ,
80
84
mutation_by_replacement = True )
81
85
initial_population = ga_instance .initial_population
82
- # print(initial_population)
83
86
84
87
assert numpy .all (initial_population [:, 0 ] >= 8 ), "Not all values in column 0 are >= 8"
85
88
assert numpy .all (initial_population [:, 1 ] >= 8 ), "Not all values in column 1 are >= 8"
86
89
assert numpy .all (initial_population [:, 2 ] >= 1 ), "Not all values in column 2 are >= 1"
87
90
assert numpy .all ((initial_population [:, 2 ] >= 1 ) & (initial_population [:, 2 ] <= 5 )), "Not all values in column 2 between 1 and 5 (inclusive)"
88
91
assert numpy .all (initial_population [:, 3 ] == 4 ), "Not all values in column 3 between 3 and 5 (exclusive)"
89
92
assert numpy .all (initial_population [:, 4 ] < 2 ), "Not all values in column 4 < 2"
93
+ print ("All constraints are met" )
90
94
91
95
def test_initial_population_int_by_replacement_no_duplicates ():
92
- gene_constraint = [lambda x : x [0 ]>= 5 ,lambda x : x [1 ]>= 5 ,lambda x : x [2 ]>= 5 ,lambda x : x [3 ]>= 5 ,lambda x : x [4 ]>= 5 ]
96
+ gene_constraint = [lambda x ,v : [val for val in v if val >= 5 ],
97
+ lambda x ,v : [val for val in v if val >= 5 ],
98
+ lambda x ,v : [val for val in v if val >= 5 ],
99
+ lambda x ,v : [val for val in v if val >= 5 ],
100
+ lambda x ,v : [val for val in v if val >= 5 ]]
93
101
ga_instance = population_gene_constraint (gene_constraint = gene_constraint ,
94
102
init_range_low = 1 ,
95
103
init_range_high = 10 ,
@@ -117,9 +125,15 @@ def test_initial_population_int_by_replacement_no_duplicates():
117
125
assert numpy .all (initial_population [:, 2 ] >= 5 ), "Not all values in column 2 >= 5"
118
126
assert numpy .all (initial_population [:, 3 ] >= 5 ), "Not all values in column 3 >= 5"
119
127
assert numpy .all (initial_population [:, 4 ] >= 5 ), "Not all values in column 4 >= 5"
128
+ print ("All constraints are met" )
120
129
121
130
def test_initial_population_int_by_replacement_no_duplicates2 ():
122
- gene_constraint = [lambda x : x [0 ]>= 98 ,lambda x : x [1 ]>= 98 ,lambda x : 20 < x [2 ]< 40 ,lambda x : x [3 ]< 40 ,lambda x : x [4 ]< 50 ,lambda x : x [5 ]< 100 ]
131
+ gene_constraint = [lambda x ,v : [val for val in v if val >= 98 ],
132
+ lambda x ,v : [val for val in v if val >= 98 ],
133
+ lambda x ,v : [val for val in v if 20 < val < 40 ],
134
+ lambda x ,v : [val for val in v if val < 40 ],
135
+ lambda x ,v : [val for val in v if val < 50 ],
136
+ lambda x ,v : [val for val in v if val < 100 ]]
123
137
ga_instance = population_gene_constraint (gene_constraint = gene_constraint ,
124
138
random_mutation_min_val = 1 ,
125
139
random_mutation_max_val = 100 ,
@@ -149,9 +163,14 @@ def test_initial_population_int_by_replacement_no_duplicates2():
149
163
assert numpy .all (initial_population [:, 3 ] < 40 ), "Not all values in column 3 < 40"
150
164
assert numpy .all (initial_population [:, 4 ] < 50 ), "Not all values in column 4 < 50"
151
165
assert numpy .all (initial_population [:, 5 ] < 100 ), "Not all values in column 4 < 100"
166
+ print ("All constraints are met" )
152
167
153
168
def test_initial_population_float_by_replacement_no_duplicates ():
154
- gene_constraint = [lambda x : x [0 ]>= 5 ,lambda x : x [1 ]>= 5 ,lambda x : x [2 ]>= 5 ,lambda x : x [3 ]>= 5 ,lambda x : x [4 ]>= 5 ]
169
+ gene_constraint = [lambda x ,v : [val for val in v if val >= 5 ],
170
+ lambda x ,v : [val for val in v if val >= 5 ],
171
+ lambda x ,v : [val for val in v if val >= 5 ],
172
+ lambda x ,v : [val for val in v if val >= 5 ],
173
+ lambda x ,v : [val for val in v if val >= 5 ]]
155
174
ga_instance = population_gene_constraint (gene_constraint = gene_constraint ,
156
175
init_range_low = 1 ,
157
176
init_range_high = 10 ,
@@ -178,9 +197,48 @@ def test_initial_population_float_by_replacement_no_duplicates():
178
197
assert numpy .all (initial_population [:, 2 ] >= 5 ), "Not all values in column 2 >= 5"
179
198
assert numpy .all (initial_population [:, 3 ] >= 5 ), "Not all values in column 3 >= 5"
180
199
assert numpy .all (initial_population [:, 4 ] >= 5 ), "Not all values in column 4 >= 5"
200
+ print ("All constraints are met" )
181
201
182
202
def test_initial_population_float_by_replacement_no_duplicates2 ():
183
- gene_constraint = [lambda x : x [0 ]>= 1 ,lambda x : x [1 ]>= 1 ,lambda x : x [2 ]>= 1 ,lambda x : x [3 ]>= 1 ,lambda x : x [4 ]>= 1 ]
203
+ gene_constraint = [lambda x ,v : [val for val in v if val >= 1 ],
204
+ lambda x ,v : [val for val in v if val >= 1 ],
205
+ lambda x ,v : [val for val in v if val >= 1 ],
206
+ lambda x ,v : [val for val in v if val >= 1 ],
207
+ lambda x ,v : [val for val in v if val >= 1 ]]
208
+ ga_instance = population_gene_constraint (gene_constraint = gene_constraint ,
209
+ init_range_low = 1 ,
210
+ init_range_high = 2 ,
211
+ gene_type = [float , 1 ],
212
+ num_genes = 5 ,
213
+ crossover_type = None ,
214
+ mutation_by_replacement = False ,
215
+ allow_duplicate_genes = False )
216
+
217
+ num_duplicates = 0
218
+ for idx , solution in enumerate (ga_instance .solutions ):
219
+ num = len (solution ) - len (set (solution ))
220
+ if num != 0 :
221
+ print (solution , idx )
222
+ num_duplicates += num
223
+
224
+ assert num_duplicates == 0
225
+
226
+ initial_population = ga_instance .initial_population
227
+ # print(initial_population)
228
+
229
+ assert numpy .all (initial_population [:, 0 ] >= 1 ), "Not all values in column 0 >= 1"
230
+ assert numpy .all (initial_population [:, 1 ] >= 1 ), "Not all values in column 1 >= 1"
231
+ assert numpy .all (initial_population [:, 2 ] >= 1 ), "Not all values in column 2 >= 1"
232
+ assert numpy .all (initial_population [:, 3 ] >= 1 ), "Not all values in column 3 >= 1"
233
+ assert numpy .all (initial_population [:, 4 ] >= 1 ), "Not all values in column 4 >= 1"
234
+ print ("All constraints are met" )
235
+
236
+ def test_initial_population_float_by_replacement_no_duplicates_None_constraints ():
237
+ gene_constraint = [lambda x ,v : [val for val in v if val >= 1 ],
238
+ None ,
239
+ lambda x ,v : [val for val in v if val >= 1 ],
240
+ None ,
241
+ lambda x ,v : [val for val in v if val >= 1 ]]
184
242
ga_instance = population_gene_constraint (gene_constraint = gene_constraint ,
185
243
init_range_low = 1 ,
186
244
init_range_high = 2 ,
@@ -207,6 +265,7 @@ def test_initial_population_float_by_replacement_no_duplicates2():
207
265
assert numpy .all (initial_population [:, 2 ] >= 1 ), "Not all values in column 2 >= 1"
208
266
assert numpy .all (initial_population [:, 3 ] >= 1 ), "Not all values in column 3 >= 1"
209
267
assert numpy .all (initial_population [:, 4 ] >= 1 ), "Not all values in column 4 >= 1"
268
+ print ("All constraints are met" )
210
269
211
270
if __name__ == "__main__" :
212
271
#### Single-objective
@@ -221,3 +280,5 @@ def test_initial_population_float_by_replacement_no_duplicates2():
221
280
print ()
222
281
test_initial_population_float_by_replacement_no_duplicates2 ()
223
282
print ()
283
+ test_initial_population_float_by_replacement_no_duplicates_None_constraints ()
284
+ print ()
0 commit comments