@@ -15,7 +15,7 @@ def solve_duplicate_genes_randomly(self,
15
15
max_val ,
16
16
mutation_by_replacement ,
17
17
gene_type ,
18
- num_values = 100 ):
18
+ sample_size = 100 ):
19
19
"""
20
20
Resolves duplicates in a solution by randomly selecting new values for the duplicate genes.
21
21
@@ -25,7 +25,7 @@ def solve_duplicate_genes_randomly(self,
25
25
max_val (int): The maximum value of the range to sample a number randomly.
26
26
mutation_by_replacement (bool): Indicates if mutation is performed by replacement.
27
27
gene_type (type): The data type of the gene (e.g., int, float).
28
- num_values (int): The maximum number of random values to generate to find a unique value.
28
+ sample_size (int): The maximum number of random values to generate to find a unique value.
29
29
30
30
Returns:
31
31
tuple:
@@ -58,7 +58,7 @@ def solve_duplicate_genes_randomly(self,
58
58
max_val = max_val ,
59
59
mutation_by_replacement = mutation_by_replacement ,
60
60
gene_type = gene_type ,
61
- num_values = num_values )
61
+ sample_size = sample_size )
62
62
63
63
if temp_val in new_solution :
64
64
num_unsolved_duplicates = num_unsolved_duplicates + 1
@@ -162,14 +162,14 @@ def unique_int_gene_from_range(self,
162
162
163
163
if self .gene_constraint and self .gene_constraint [gene_index ]:
164
164
# A unique value is created out of the values that satisfy the constraint.
165
- # num_values =None to return all the values.
165
+ # sample_size =None to return all the values.
166
166
random_values = self .get_valid_gene_constraint_values (range_min = min_val ,
167
167
range_max = max_val ,
168
168
gene_value = solution [gene_index ],
169
169
gene_idx = gene_index ,
170
170
mutation_by_replacement = mutation_by_replacement ,
171
171
solution = solution ,
172
- num_values = None ,
172
+ sample_size = None ,
173
173
step = step )
174
174
# If there is no value satisfying the constraint, then return the current gene value.
175
175
if random_values is None :
@@ -178,14 +178,14 @@ def unique_int_gene_from_range(self,
178
178
pass
179
179
else :
180
180
# There is no constraint for the current gene. Return the same range.
181
- # num_values =None to return all the values.
182
- random_values = self .generate_gene_random_value (range_min = min_val ,
183
- range_max = max_val ,
184
- gene_value = solution [gene_index ],
185
- gene_idx = gene_index ,
186
- mutation_by_replacement = mutation_by_replacement ,
187
- num_values = None ,
188
- step = step )
181
+ # sample_size =None to return all the values.
182
+ random_values = self .generate_gene_value (range_min = min_val ,
183
+ range_max = max_val ,
184
+ gene_value = solution [gene_index ],
185
+ gene_idx = gene_index ,
186
+ mutation_by_replacement = mutation_by_replacement ,
187
+ sample_size = None ,
188
+ step = step )
189
189
190
190
"""
191
191
# For non-integer steps, the numpy.arange() function returns zeros if the dtype parameter is set to an integer data type. So, this returns zeros if step is non-integer and dtype is set to an int data type: numpy.arange(min_val, max_val, step, dtype=gene_type[0])
@@ -223,7 +223,7 @@ def unique_float_gene_from_range(self,
223
223
max_val ,
224
224
mutation_by_replacement ,
225
225
gene_type ,
226
- num_values = 100 ):
226
+ sample_size = 100 ):
227
227
228
228
"""
229
229
Finds a unique floating-point value for a specific gene in a solution.
@@ -235,66 +235,36 @@ def unique_float_gene_from_range(self,
235
235
max_val (int): The maximum value of the range to sample a floating-point number randomly.
236
236
mutation_by_replacement (bool): Indicates if mutation is performed by replacement.
237
237
gene_type (type): The data type of the gene (e.g., float, float16, float32, etc).
238
- num_values (int): The maximum number of random values to generate to find a unique value.
238
+ sample_size (int): The maximum number of random values to generate to find a unique value.
239
239
240
240
Returns:
241
241
int: The new floating-point value of the gene. If no unique value can be found, the original gene value is returned.
242
242
"""
243
243
244
244
if self .gene_constraint and self .gene_constraint [gene_index ]:
245
245
# A unique value is created out of the values that satisfy the constraint.
246
- random_values = self .get_valid_gene_constraint_values (range_min = min_val ,
247
- range_max = max_val ,
248
- gene_value = solution [gene_index ],
249
- gene_idx = gene_index ,
250
- mutation_by_replacement = mutation_by_replacement ,
251
- solution = solution ,
252
- num_values = num_values )
246
+ values = self .get_valid_gene_constraint_values (range_min = min_val ,
247
+ range_max = max_val ,
248
+ gene_value = solution [gene_index ],
249
+ gene_idx = gene_index ,
250
+ mutation_by_replacement = mutation_by_replacement ,
251
+ solution = solution ,
252
+ sample_size = sample_size )
253
253
# If there is no value satisfying the constraint, then return the current gene value.
254
- if random_values is None :
254
+ if values is None :
255
255
return solution [gene_index ]
256
256
else :
257
257
pass
258
258
else :
259
259
# There is no constraint for the current gene. Return the same range.
260
- random_values = self .generate_gene_random_value (range_min = min_val ,
261
- range_max = max_val ,
262
- gene_value = solution [gene_index ],
263
- gene_idx = gene_index ,
264
- mutation_by_replacement = mutation_by_replacement ,
265
- num_values = num_values )
266
-
267
- """
268
- # The gene_type is of the form [type, precision]
269
- dtype = gene_type
270
-
271
- # We cannot have a list of all values out of a continous range.
272
- # Solution is to create a subset (e.g. 100) of some random values out of the range.
273
- some_gene_values = numpy.random.uniform(low=min_val,
274
- high=max_val,
275
- size=num_values)
276
-
277
- # If mutation is by replacement, do not add the current gene value into the list.
278
- # This is to avoid replacing the value by itself again. We are doing nothing in this case.
279
- if mutation_by_replacement:
280
- pass
281
- else:
282
- some_gene_values = some_gene_values + solution[gene_index]
283
-
284
- if not dtype[1] is None:
285
- # Precision is available and we have to round the number.
286
- # Convert the data type and round the number.
287
- some_gene_values = numpy.round(numpy.asarray(some_gene_values,
288
- dtype[0]),
289
- dtype[1])
290
- else:
291
- # There is no precision and rounding the number is not needed. The type is [type, None]
292
- # Just convert the data type.
293
- some_gene_values = numpy.asarray(some_gene_values,
294
- dtype[0])
295
- """
296
-
297
- selected_value = self .select_unique_value (gene_values = random_values ,
260
+ values = self .generate_gene_value (range_min = min_val ,
261
+ range_max = max_val ,
262
+ gene_value = solution [gene_index ],
263
+ gene_idx = gene_index ,
264
+ mutation_by_replacement = mutation_by_replacement ,
265
+ sample_size = sample_size )
266
+
267
+ selected_value = self .select_unique_value (gene_values = values ,
298
268
solution = solution ,
299
269
gene_index = gene_index )
300
270
return selected_value
@@ -439,7 +409,7 @@ def unique_gene_by_space(self,
439
409
max_val = high ,
440
410
mutation_by_replacement = True ,
441
411
gene_type = dtype ,
442
- num_values = num_trials )
412
+ sample_size = num_trials )
443
413
444
414
445
415
elif type (curr_gene_space ) is dict :
@@ -532,8 +502,10 @@ def unique_gene_by_space(self,
532
502
# If the space type is not of type dict, then a value is randomly selected from the gene_space attribute.
533
503
# Remove all the genes in the current solution from the gene_space.
534
504
# This only leaves the unique values that could be selected for the gene.
535
- values_to_select_from = list (set (self .gene_space ) - set (solution ))
536
-
505
+
506
+ # Before using the gene_space, use gene_space_unpacked instead of gene_space to make sure the numbers has the right data type and its values are rounded.
507
+ values_to_select_from = list (set (self .gene_space_unpacked ) - set (solution ))
508
+
537
509
if len (values_to_select_from ) == 0 :
538
510
if not self .suppress_warnings : warnings .warn ("You set 'allow_duplicate_genes=False' but the gene space does not have enough values to prevent duplicates." )
539
511
value_from_space = solution [gene_idx ]
@@ -591,14 +563,14 @@ def find_two_duplicates(self,
591
563
def unpack_gene_space (self ,
592
564
range_min ,
593
565
range_max ,
594
- num_values_from_inf_range = 100 ):
566
+ sample_size_from_inf_range = 100 ):
595
567
"""
596
568
Unpacks the gene space for selecting a value to resolve duplicates by converting ranges into lists of values.
597
569
598
570
Args:
599
571
range_min (float or int): The minimum value of the range.
600
572
range_max (float or int): The maximum value of the range.
601
- num_values_from_inf_range (int): The number of values to generate for an infinite range of float values using `numpy.linspace()`.
573
+ sample_size_from_inf_range (int): The number of values to generate for an infinite range of float values using `numpy.linspace()`.
602
574
603
575
Returns:
604
576
list: A list representing the unpacked gene space.
@@ -621,7 +593,7 @@ def unpack_gene_space(self,
621
593
else :
622
594
gene_space_unpacked = numpy .linspace (start = self .gene_space ['low' ],
623
595
stop = self .gene_space ['high' ],
624
- num = num_values_from_inf_range ,
596
+ num = sample_size_from_inf_range ,
625
597
endpoint = False )
626
598
627
599
if self .gene_type_single == True :
@@ -662,7 +634,7 @@ def unpack_gene_space(self,
662
634
elif type (space ) is dict :
663
635
# Create a list of values using the dict range.
664
636
# Use numpy.linspace()
665
- dtype = self .get_gene_dtype (gene_index = gene_idx )
637
+ dtype = self .get_gene_dtype (gene_index = space_idx )
666
638
667
639
if dtype [0 ] in pygad .GA .supported_int_types :
668
640
if 'step' in space .keys ():
@@ -681,7 +653,7 @@ def unpack_gene_space(self,
681
653
else :
682
654
gene_space_unpacked [space_idx ] = numpy .linspace (start = space ['low' ],
683
655
stop = space ['high' ],
684
- num = num_values_from_inf_range ,
656
+ num = sample_size_from_inf_range ,
685
657
endpoint = False )
686
658
elif type (space ) in [numpy .ndarray , list , tuple ]:
687
659
# list/tuple/numpy.ndarray
0 commit comments