@@ -184,13 +184,9 @@ def unique_int_gene_from_range(self,
184
184
all_gene_values = numpy.asarray(all_gene_values,
185
185
dtype[0])
186
186
187
- values_to_select_from = list(set(list(all_gene_values)) - set(solution))
188
-
189
- if len(values_to_select_from) == 0:
190
- # If there are no values, then keep the current gene value.
191
- selected_value = solution[gene_index]
192
- else:
193
- selected_value = random.choice(values_to_select_from)
187
+ selected_value = self.select_unique_value(gene_values=all_gene_values,
188
+ solution=solution,
189
+ gene_index=gene_index)
194
190
195
191
selected_value = dtype[0](selected_value)
196
192
@@ -224,39 +220,57 @@ def unique_float_gene_from_range(self,
224
220
# The gene_type is of the form [type, precision]
225
221
dtype = gene_type
226
222
227
- for trial_index in range(num_trials):
228
- temp_val = numpy.random.uniform(low=min_val,
229
- high=max_val,
230
- size=1)[0]
223
+ # We cannot have a list of all values out of a continous range.
224
+ # Solution is to create a subset (e.g. 100) of all the values.
225
+ some_gene_values = numpy.random.uniform(low=min_val,
226
+ high=max_val,
227
+ size=100)
231
228
232
- # If mutation is by replacement, do not add the current gene value into the list.
233
- # This is to avoid replacing the value by itself again. We are doing nothing in this case.
234
- if mutation_by_replacement:
235
- pass
236
- else:
237
- temp_val = temp_val + solution[gene_index]
229
+ # If mutation is by replacement, do not add the current gene value into the list.
230
+ # This is to avoid replacing the value by itself again. We are doing nothing in this case.
231
+ if mutation_by_replacement:
232
+ pass
233
+ else:
234
+ some_gene_values = some_gene_values + solution[gene_index]
235
+
236
+ if not dtype[1] is None:
237
+ # Precision is available and we have to round the number.
238
+ # Convert the data type and round the number.
239
+ some_gene_values = numpy.round(numpy.asarray(some_gene_values,
240
+ dtype[0]),
241
+ dtype[1])
242
+ else:
243
+ # There is no precision and rounding the number is not needed. The type is [type, None]
244
+ # Just convert the data type.
245
+ some_gene_values = numpy.asarray(some_gene_values,
246
+ dtype[0])
247
+
248
+ selected_value = self.select_unique_value(gene_values=some_gene_values,
249
+ solution=solution,
250
+ gene_index=gene_index)
251
+ return selected_value
238
252
239
- if not dtype[1] is None :
240
- # Precision is available and we have to round the number.
241
- # Convert the data type and round the number.
242
- temp_val = numpy.round(dtype[0](temp_val),
243
- dtype[1])
244
- else :
245
- # There is no precision and rounding the number is not needed. The type is [type, None]
246
- # Just convert the data type .
247
- temp_val = dtype[0](temp_val)
248
-
249
- if temp_val in solution and trial_index == (num_trials - 1):
250
- # If there are no values, then keep the current gene value.
251
- if not self.suppress_warnings: warnings.warn("You set 'allow_duplicate_genes=False' but cannot find a value to prevent duplicates.")
252
- selected_value = solution[gene_index]
253
- elif temp_val in solution:
254
- # Keep trying in the other remaining trials.
255
- continue
256
- else:
257
- # Unique gene value found.
258
- selected_value = temp_val
259
- break
253
+ def select_unique_value(self, gene_values, solution, gene_index) :
254
+
255
+ """
256
+ Select a unique value (if possible) from a list of gene values.
257
+
258
+ Args :
259
+ gene_values (NumPy Array): An array of values from which a unique value should be selected.
260
+ solution (list): A solution containing genes, potentially with duplicate values .
261
+
262
+ Returns:
263
+ selected_gene: The new (hopefully unique) value of the gene. If no unique value can be found, the original gene value is returned.
264
+ """
265
+
266
+ values_to_select_from = list(set(list(gene_values)) - set( solution))
267
+
268
+ if len(values_to_select_from) == 0:
269
+ # If there are no values, then keep the current gene value.
270
+ if not self.suppress_warnings: warnings.warn("You set 'allow_duplicate_genes=False' but cannot find a value to prevent duplicates.")
271
+ selected_value = solution[gene_index]
272
+ else:
273
+ selected_value = random.choice(values_to_select_from)
260
274
261
275
return selected_value
262
276
0 commit comments