Repository navigation
Expand file tree
/
Copy pathmisc.py
More file actions
1493 lines (1369 loc) · 74 KB
/
Copy pathmisc.py
File metadata and controls
1493 lines (1369 loc) · 74 KB
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
The pygad.helper.misc module has some generic helper methods.
"""
import numpy
import math
import warnings
import pygad
class Helper:
def summary(self,
line_length=70,
fill_character=" ",
line_character="-",
line_character2="=",
columns_equal_len=False,
print_step_parameters=True,
print_parameters_summary=True):
"""
Print a Keras-style summary of the PyGAD lifecycle. Each
configured step (fitness, parent selection, crossover,
mutation, etc.) is shown on its own row together with the
handler name and an output-shape hint. The string written to
the logger is also returned.
Parameters
----------
line_length : int
Total width of a printed line in characters.
fill_character : str
Character used to pad cells to the column width.
line_character : str
Character used to draw the lighter horizontal separator
between rows.
line_character2 : str
Character used to draw the heavier separator between the
header and the body.
columns_equal_len : bool
If True, the three columns are split into equal widths.
Otherwise the widths follow the longest content in each
column.
print_step_parameters : bool
If True, the extra parameters of each step are printed
inside the step's row.
print_parameters_summary : bool
If True, a summary block of global parameters is printed
below the table. When ``print_step_parameters`` is False,
the per-step extras are folded into this summary block.
Returns
-------
summary_output : str
The full summary as a single string (the same text that
was written to the logger).
"""
summary_output = ""
def fill_message(msg, line_length=line_length, fill_character=fill_character):
num_spaces = int((line_length - len(msg))/2)
num_spaces = int(num_spaces / len(fill_character))
msg = "{spaces}{msg}{spaces}".format(
msg=msg, spaces=fill_character * num_spaces)
return msg
def line_separator(line_length=line_length, line_character=line_character):
num_characters = int(line_length / len(line_character))
return line_character * num_characters
def create_row(columns, line_length=line_length, fill_character=fill_character, split_percentages=None):
filled_columns = []
if split_percentages is None:
split_percentages = [int(100/len(columns))] * 3
columns_lengths = [int((split_percentages[idx] * line_length) / 100)
for idx in range(len(split_percentages))]
for column_idx, column in enumerate(columns):
current_column_length = len(column)
extra_characters = columns_lengths[column_idx] - \
current_column_length
filled_column = column + fill_character * extra_characters
filled_columns.append(filled_column)
return "".join(filled_columns)
def print_parent_selection_params():
nonlocal summary_output
m = f"Number of Parents: {self.num_parents_mating}"
self.logger.info(m)
summary_output = summary_output + m + "\n"
if self.parent_selection_type == "tournament":
m = f"K Tournament: {self.K_tournament}"
self.logger.info(m)
summary_output = summary_output + m + "\n"
def print_fitness_params():
nonlocal summary_output
if not self.fitness_batch_size is None:
m = f"Fitness batch size: {self.fitness_batch_size}"
self.logger.info(m)
summary_output = summary_output + m + "\n"
def print_crossover_params():
nonlocal summary_output
if not self.crossover_probability is None:
m = f"Crossover probability: {self.crossover_probability}"
self.logger.info(m)
summary_output = summary_output + m + "\n"
def print_mutation_params():
nonlocal summary_output
if not self.mutation_probability is None:
m = f"Mutation Probability: {self.mutation_probability}"
self.logger.info(m)
summary_output = summary_output + m + "\n"
if self.mutation_percent_genes == "default":
m = f"Mutation Percentage: {self.mutation_percent_genes}"
self.logger.info(m)
summary_output = summary_output + m + "\n"
# Number of mutation genes is already shown above.
m = f"Mutation Genes: {self.mutation_num_genes}"
self.logger.info(m)
summary_output = summary_output + m + "\n"
m = f"Random Mutation Range: ({self.random_mutation_min_val}, {self.random_mutation_max_val})"
self.logger.info(m)
summary_output = summary_output + m + "\n"
if not self.gene_space is None:
m = f"Gene Space: {self.gene_space}"
self.logger.info(m)
summary_output = summary_output + m + "\n"
m = f"Mutation by Replacement: {self.mutation_by_replacement}"
self.logger.info(m)
summary_output = summary_output + m + "\n"
m = f"Allow Duplicated Genes: {self.allow_duplicate_genes}"
self.logger.info(m)
summary_output = summary_output + m + "\n"
def print_on_generation_params():
nonlocal summary_output
if not self.stop_criteria is None:
m = f"Stop Criteria: {self.stop_criteria}"
self.logger.info(m)
summary_output = summary_output + m + "\n"
def print_params_summary():
nonlocal summary_output
m = f"Population Size: ({self.sol_per_pop}, {self.num_genes})"
self.logger.info(m)
summary_output = summary_output + m + "\n"
m = f"Number of Generations: {self.num_generations}"
self.logger.info(m)
summary_output = summary_output + m + "\n"
m = f"Initial Population Range: ({self.init_range_low}, {self.init_range_high})"
self.logger.info(m)
summary_output = summary_output + m + "\n"
if not print_step_parameters:
print_fitness_params()
if not print_step_parameters:
print_parent_selection_params()
if self.keep_elitism != 0:
m = f"Keep Elitism: {self.keep_elitism}"
self.logger.info(m)
summary_output = summary_output + m + "\n"
else:
m = f"Keep Parents: {self.keep_parents}"
self.logger.info(m)
summary_output = summary_output + m + "\n"
m = f"Gene DType: {self.gene_type}"
self.logger.info(m)
summary_output = summary_output + m + "\n"
if not print_step_parameters:
print_crossover_params()
if not print_step_parameters:
print_mutation_params()
if not print_step_parameters:
print_on_generation_params()
if not self.parallel_processing is None:
m = f"Parallel Processing: {self.parallel_processing}"
self.logger.info(m)
summary_output = summary_output + m + "\n"
if not self.random_seed is None:
m = f"Random Seed: {self.random_seed}"
self.logger.info(m)
summary_output = summary_output + m + "\n"
m = f"Save Best Solutions: {self.save_best_solutions}"
self.logger.info(m)
summary_output = summary_output + m + "\n"
m = f"Save Solutions: {self.save_solutions}"
self.logger.info(m)
summary_output = summary_output + m + "\n"
m = line_separator(line_character=line_character)
self.logger.info(m)
summary_output = summary_output + m + "\n"
m = fill_message("PyGAD Lifecycle")
self.logger.info(m)
summary_output = summary_output + m + "\n"
m = line_separator(line_character=line_character2)
self.logger.info(m)
summary_output = summary_output + m + "\n"
lifecycle_steps = ["on_start()", "Fitness Function", "On Fitness", "Parent Selection", "On Parents",
"Crossover", "On Crossover", "Mutation", "On Mutation", "On Generation", "On Stop"]
lifecycle_functions = [self.on_start, self.fitness_func, self.on_fitness, self.select_parents, self.on_parents,
self.crossover, self.on_crossover, self.mutation, self.on_mutation, self.on_generation, self.on_stop]
lifecycle_functions = [getattr(
lifecycle_func, '__name__', "None") for lifecycle_func in lifecycle_functions]
lifecycle_functions = [lifecycle_func + "()" if lifecycle_func !=
"None" else "None" for lifecycle_func in lifecycle_functions]
lifecycle_output = ["None", "(1)", "None", f"({self.num_parents_mating}, {self.num_genes})", "None",
f"({self.num_parents_mating}, {self.num_genes})", "None", f"({self.num_parents_mating}, {self.num_genes})", "None", "None", "None"]
lifecycle_step_parameters = [None, print_fitness_params, None, print_parent_selection_params, None,
print_crossover_params, None, print_mutation_params, None, print_on_generation_params, None]
if not columns_equal_len:
max_lengths = [max(list(map(len, lifecycle_steps))), max(
list(map(len, lifecycle_functions))), max(list(map(len, lifecycle_output)))]
split_percentages = [
int((column_len / sum(max_lengths)) * 100) for column_len in max_lengths]
else:
split_percentages = None
header_columns = ["Step", "Handler", "Output Shape"]
header_row = create_row(
header_columns, split_percentages=split_percentages)
m = header_row
self.logger.info(m)
summary_output = summary_output + m + "\n"
m = line_separator(line_character=line_character2)
self.logger.info(m)
summary_output = summary_output + m + "\n"
for lifecycle_idx in range(len(lifecycle_steps)):
lifecycle_column = [lifecycle_steps[lifecycle_idx],
lifecycle_functions[lifecycle_idx], lifecycle_output[lifecycle_idx]]
if lifecycle_column[1] == "None":
continue
lifecycle_row = create_row(
lifecycle_column, split_percentages=split_percentages)
m = lifecycle_row
self.logger.info(m)
summary_output = summary_output + m + "\n"
if print_step_parameters:
if not lifecycle_step_parameters[lifecycle_idx] is None:
lifecycle_step_parameters[lifecycle_idx]()
m = line_separator(line_character=line_character)
self.logger.info(m)
summary_output = summary_output + m + "\n"
m = line_separator(line_character=line_character2)
self.logger.info(m)
summary_output = summary_output + m + "\n"
if print_parameters_summary:
print_params_summary()
m = line_separator(line_character=line_character2)
self.logger.info(m)
summary_output = summary_output + m + "\n"
return summary_output
def initialize_parents_array(self, shape):
"""
Allocate an empty parents (or offspring) array with the right
dtype. Uses the gene's storage dtype when every gene shares
the same type, otherwise falls back to ``object``. Python
``int`` uses signed 64-bit array storage.
Parameters
----------
shape : tuple
The shape of the array, usually
``(num_parents, num_genes)``.
Returns
-------
array : numpy.ndarray
An uninitialised array of the requested shape and dtype.
"""
if self.gene_type_single:
return numpy.empty(shape, dtype=self._get_gene_storage_dtype(self.gene_type[0]))
else:
return numpy.empty(shape, dtype=object)
def _get_gene_storage_dtype(self, gene_dtype):
"""Return the array storage type for a declared gene type.
Python int uses explicit signed 64-bit storage so array conversion,
allocation, and range sampling agree across NumPy versions and
platforms. Explicit NumPy types keep their requested widths. This
does not change scalar conversion or the declared gene_type.
"""
return numpy.int64 if gene_dtype is int else gene_dtype
def change_population_dtype_and_round(self,
population):
"""
Cast a 2D population to the dtype encoded in
``self.gene_type`` and round non-integer genes to the
configured precision. When ``gene_type_single`` is True, the
same dtype and precision are applied to every gene; otherwise
each gene gets its own dtype and precision.
Parameters
----------
population : list or numpy.ndarray
A 2D iterable with shape ``(num_solutions, num_genes)``.
Returns
-------
population_new : numpy.ndarray
The same data cast (and rounded) to the right type.
"""
if self.gene_type_single:
return self._convert_gene_values(population, self.gene_type)
# An object array keeps each value exact until its own type is
# applied. Group matching types and precisions to convert whole
# blocks instead of converting every scalar separately.
population = numpy.asarray(population, dtype=object)
population_new = numpy.empty(population.shape, dtype=object)
gene_columns_by_type = {}
for gene_index, gene_type in enumerate(self.gene_type):
gene_columns_by_type.setdefault(tuple(gene_type), []).append(gene_index)
for gene_type, gene_indices in gene_columns_by_type.items():
values = self._convert_gene_values(population[:, gene_indices], gene_type)
dtype = gene_type[0]
if dtype in [int, float, object]:
population_new[:, gene_indices] = values.astype(object)
else:
# astype(object) alone turns NumPy scalars into Python
# numbers. Preserve explicitly requested NumPy types.
population_new[:, gene_indices] = numpy.frompyfunc(dtype, 1, 1)(values)
return population_new
def change_gene_dtype_and_round(self, gene_index, gene_value):
"""
Convert a scalar or an array of candidates using one gene's type
and precision. Return a scalar for a scalar input, or an array
with the input shape. The input is not modified.
Parameters
----------
gene_index : int
Index of the gene whose type and precision are applied.
gene_value : numeric or iterable
A single value or an array of candidate values for this gene.
Returns
-------
numeric or numpy.ndarray
The converted scalar or array of candidates.
"""
return self._convert_gene_values(gene_value, self.get_gene_dtype(gene_index))
def _convert_gene_values(self, values, gene_type):
"""
Apply the shared conversion rule: round floating-point values
before casting to the requested type. Integer casts truncate
towards zero. None precision leaves values unrounded. NumPy's
rounding rule selects the nearest even value at halfway points.
"""
dtype, precision = gene_type
if precision is None:
if numpy.isscalar(values):
return values if dtype is object else dtype(values)
converted_values = numpy.array(values, dtype=self._get_gene_storage_dtype(dtype), copy=True)
else:
rounded_values = self._round_gene_values(numpy.asarray(values, dtype=float), precision)
converted_values = numpy.asarray(rounded_values, dtype=dtype)
if converted_values.ndim == 0:
value = converted_values[()]
return value if dtype is object else dtype(value)
return converted_values
def _round_gene_values(self, values, precision):
"""
Use NumPy's array rounding, preserving finite inputs when its
decimal scaling overflows. Python's scalar round handles those
uncommon values without the intermediate scaling operation.
"""
try:
with numpy.errstate(over='ignore', invalid='ignore', divide='ignore'):
rounded_values = numpy.round(values, precision)
except OverflowError:
# NumPy limits decimals to a C integer; Python round accepts
# the full integer precision supplied by the user.
return numpy.asarray(numpy.frompyfunc(lambda value: round(float(value), precision), 1, 1)(values), dtype=float)
invalid_results = numpy.isfinite(values) & ~numpy.isfinite(rounded_values)
if numpy.any(invalid_results):
rounded_values = numpy.asarray(rounded_values).copy()
rounded_values[invalid_results] = [round(float(value), precision)
for value in numpy.atleast_1d(values[invalid_results])]
return rounded_values
def mutation_change_gene_dtype_and_round(self,
random_value,
gene_index,
gene_value,
mutation_by_replacement):
"""
Apply a random mutation value to a gene and cast / round the
result. If ``mutation_by_replacement`` is True, the random
value replaces the gene; otherwise it is added to the
existing value. NumPy integer results are clipped to the
destination type's limits before casting.
Parameters
----------
random_value : numeric or numpy.ndarray
The freshly drawn mutation value or array of candidates.
gene_index : int
Index of the gene being mutated.
gene_value : numeric
Gene value before mutation. Only used when
``mutation_by_replacement`` is False.
mutation_by_replacement : bool
If True, replace the gene; otherwise add the random
value to it.
Returns
-------
gene_value_new : numeric or numpy.ndarray
The mutated value or candidates after clipping NumPy
integer values, casting, and rounding.
"""
if mutation_by_replacement:
mutated_value = random_value
else:
# NumPy can add narrow scalars in their original dtype, losing
# precision or overflowing before the final cast. Python
# numeric values keep integer addition exact and float
# addition in the working precision used by the converter.
gene_value = gene_value.item() if isinstance(gene_value, numpy.generic) else gene_value
if numpy.ndim(random_value) == 0:
random_value = random_value.item() if isinstance(random_value, (numpy.generic, numpy.ndarray)) else random_value
mutated_value = gene_value + random_value
else:
mutated_value = gene_value + numpy.asarray(random_value, dtype=object)
gene_dtype = self.get_gene_dtype(gene_index)[0]
if gene_dtype is not int and numpy.issubdtype(numpy.dtype(gene_dtype), numpy.integer):
type_limits = numpy.iinfo(gene_dtype)
# Object storage and Python bounds keep 64-bit integers exact.
# Clip the final sum or replacement, so an unsigned gene can
# still use negative offsets without wrapping or overflowing.
mutated_value = numpy.clip(numpy.asarray(mutated_value, dtype=object),
int(type_limits.min), int(type_limits.max))
return self.change_gene_dtype_and_round(gene_index, mutated_value)
def validate_gene_constraint_callable_output(self,
selected_values,
values):
"""
Check that a gene constraint callable returned a list or
numpy array whose elements are all members of the original
candidate ``values``.
Parameters
----------
selected_values : list, numpy.ndarray, or other
The return value from the user-supplied constraint
callable.
values : iterable
The full set of candidate values that was passed to the
callable.
Returns
-------
valid : bool
True when ``selected_values`` is a list or numpy array
and is a subset of ``values``. False otherwise.
"""
if type(selected_values) in [list, numpy.ndarray]:
selected_values_set = set(selected_values)
if selected_values_set.issubset(values):
pass
else:
return False
else:
return False
return True
def filter_gene_values_by_constraint(self,
values,
solution,
gene_idx,
warn=True):
"""
Pass a list of candidate values through the user-supplied
gene constraint callable and return the subset that satisfies
the constraint.
Parameters
----------
values : list or numpy.ndarray
Candidate values to filter.
solution : numpy.ndarray
The solution that owns the gene. Passed to the constraint
callable so it can look at the other genes if needed.
gene_idx : int
Index of the gene inside ``solution``.
warn : bool
Warn if no candidate satisfies the constraint. Repair uses
False while exploring alternatives and warns about its final result.
Returns
-------
filtered_values : list, numpy.ndarray, or None
The values that satisfy the constraint, or None when no
value satisfies it (a warning is issued in that case).
Raises
------
Exception
If the gene has no constraint, or the constraint callable
returns a result that is not a subset of ``values``.
"""
if self.gene_constraint and self.gene_constraint[gene_idx] is not None:
pass
else:
raise Exception(f"Either the gene at index {gene_idx} is not assigned a callable/function or the gene_constraint itself is not used.")
# A temporary solution to avoid changing the original solution.
solution_tmp = solution.copy()
filtered_values = self.gene_constraint[gene_idx](solution_tmp, values.copy())
result = self.validate_gene_constraint_callable_output(selected_values=filtered_values,
values=values)
if result:
pass
else:
raise Exception("The output from the gene_constraint callable/function must be a list or NumPy array that is a subset of the passed values (second argument).")
# After going through all the values, check if any value satisfies the constraint.
if len(filtered_values) > 0:
# At least one value was found that meets the gene constraint.
pass
else:
# No value found for the current gene that satisfies the constraint.
if warn and not self.suppress_warnings: warnings.warn(f"Failed to find a value that satisfies its gene constraint for the gene at index {gene_idx} with value {solution[gene_idx]} at generation {getattr(self, 'generations_completed', 0)}.")
return None
return filtered_values
def get_gene_dtype(self, gene_index):
"""
Return the dtype (and optional precision) for the gene at the
given index. When ``gene_type_single`` is True the same dtype
is returned for every gene; otherwise the per-gene entry is
returned.
Parameters
----------
gene_index : int
Index of the gene whose dtype is wanted. Ignored when
``gene_type_single`` is True.
Returns
-------
dtype : type or list
Either a single Python / numpy type, or a
``[type, precision]`` pair.
"""
if self.gene_type_single == True:
dtype = self.gene_type
else:
dtype = self.gene_type[gene_index]
return dtype
def get_random_mutation_range(self, gene_index):
"""
Return the random-mutation range ``(min, max)`` for the gene
at the given index. When ``random_mutation_min_val`` is a
scalar, the same range is used for every gene; otherwise the
per-gene entry is returned.
Parameters
----------
gene_index : int
Index of the gene. Ignored when the range parameters are
scalars.
Returns
-------
range_min : numeric
Lower bound of the random delta.
range_max : numeric
Upper bound of the random delta.
"""
# We can use either random_mutation_min_val or random_mutation_max_val.
if type(self.random_mutation_min_val) in self.supported_int_float_types:
range_min = self.random_mutation_min_val
range_max = self.random_mutation_max_val
else:
range_min = self.random_mutation_min_val[gene_index]
range_max = self.random_mutation_max_val[gene_index]
return tuple(sorted([range_min, range_max]))
def get_initial_population_range(self, gene_index):
"""
Return the initial-population range ``(min, max)`` for the
gene at the given index. When ``init_range_low`` is a scalar,
the same range is used for every gene; otherwise the
per-gene entry is returned.
Parameters
----------
gene_index : int
Index of the gene. Ignored when the range parameters are
scalars.
Returns
-------
range_min : numeric
Lower bound for the random initial gene value.
range_max : numeric
Upper bound for the random initial gene value.
"""
# We can use either init_range_low or init_range_high.
if type(self.init_range_low) in self.supported_int_float_types:
range_min = self.init_range_low
range_max = self.init_range_high
else:
range_min = self.init_range_low[gene_index]
range_max = self.init_range_high[gene_index]
return tuple(sorted([range_min, range_max]))
def get_bounded_operator_gene_range(self, gene_index):
"""Return SBX and polynomial bounds from the gene space or initialization range."""
if self.gene_space is None:
return self.get_initial_population_range(gene_index)
space = self.gene_space[gene_index] if self.gene_space_nested else self.gene_space
if space is None:
return self.get_initial_population_range(gene_index)
if isinstance(space, dict) and 'step' not in space:
if numpy.issubdtype(numpy.dtype(self.get_gene_dtype(gene_index)[0]), numpy.integer):
return self._continuous_gene_space_integer_bounds(space)
return space['low'], space['high']
if isinstance(space, range) or isinstance(space, dict):
count = self._finite_gene_space_length(space)
values = [self._finite_gene_space_value(space, 0), self._finite_gene_space_value(space, count - 1)]
elif isinstance(space, (list, tuple, numpy.ndarray)):
values = [value for value in space if value is not None]
if any(value is None for value in space):
values.extend(self.get_initial_population_range(gene_index))
else:
values = [space]
values = self.change_gene_dtype_and_round(gene_index, values)
return min(values), max(values)
def convert_bounded_operator_gene_value(self, gene_index, value):
"""Convert a generated value within its bounds and select an allowed space value."""
lower, upper = self.get_bounded_operator_gene_range(gene_index)
lower = lower.item() if isinstance(lower, numpy.generic) else lower
upper = upper.item() if isinstance(upper, numpy.generic) else upper
dtype = self.get_gene_dtype(gene_index)[0]
# When the gene has a finite list of allowed values, move the
# generated decimal straight to the nearest allowed value. Rounding
# it to an integer first would round twice and favor lower values.
nearest_allowed_value = self._nearest_finite_gene_space_value(gene_index, value)
if nearest_allowed_value is not None:
value = nearest_allowed_value
if numpy.issubdtype(numpy.dtype(dtype), numpy.integer):
value = value.item() if isinstance(value, numpy.generic) else value
type_limits = numpy.iinfo(self._get_gene_storage_dtype(dtype))
first = max(math.ceil(lower), int(type_limits.min))
last = min(math.floor(upper), int(type_limits.max))
if first > last:
raise ValueError(f'The operator bounds have no value representable by gene_type for the gene at index {gene_index}.')
# Round to the nearest integer. Dropping the fraction instead
# would never reach the highest value and would pull every gene
# toward zero.
# Real-coded arithmetic may round an integer type's maximum
# up to the next power of two. Clip as Python integers before
# casting so a valid bounded proposal does not overflow.
value = min(max(math.floor(value + 0.5), first), last)
converted = self.change_gene_dtype_and_round(gene_index, value)
if not lower <= float(converted) <= upper:
if numpy.issubdtype(numpy.dtype(self.get_gene_dtype(gene_index)[0]), numpy.integer):
converted = min(max(converted, math.ceil(lower)), math.floor(upper))
else:
converted = self._convert_initial_population_range_values(gene_index, lower, upper, [value])[0]
if self.gene_space is not None:
space = self.gene_space[gene_index] if self.gene_space_nested else self.gene_space
if space is not None and not self.is_bounded_operator_gene_value_in_space(gene_index, converted):
if isinstance(space, range) or (isinstance(space, dict) and 'step' in space):
count = self._finite_gene_space_length(space)
low = space.start if isinstance(space, range) else space['low']
step = space.step if isinstance(space, range) else space['step']
position = math.floor((float(converted) - low) / step)
indices = {max(0, min(count - 1, position + offset)) for offset in range(-2, 4)}
candidates = [self._finite_gene_space_value(space, index) for index in sorted(indices)]
candidates = self.change_gene_dtype_and_round(gene_index, candidates)
elif isinstance(space, dict):
if numpy.issubdtype(numpy.dtype(self.get_gene_dtype(gene_index)[0]), numpy.integer):
# Integer conversion of a continuous space truncates
# its endpoints, including a fixed fractional value.
first, last = self._continuous_gene_space_integer_bounds(space)
converted = min(max(converted, first), last)
else:
converted = self._convert_initial_population_range_values(
gene_index, space['low'], space['high'], [value])[0]
candidates = [converted]
else:
candidates = self.get_initial_population_gene_candidates(gene_index, self.sample_size, all_integer_values=False)
converted = min(candidates, key=lambda candidate: abs(float(candidate) - float(converted)))
return converted if dtype is object else dtype(converted)
def _nearest_finite_gene_space_value(self, gene_index, value):
"""
Return the allowed value of a finite gene space that is nearest
to a value generated by SBX crossover or polynomial mutation.
A gene space is finite when it is a range, a dictionary with a
``step``, or a list of numbers. Lists that contain ``None`` are
not finite because ``None`` stands for any value in the
initialization range.
Parameters
----------
gene_index : int
Index of the gene whose space is used.
value : float
The decimal value generated by the operator.
Returns
-------
number or None
The nearest allowed value, or None when the gene has no
finite gene space.
"""
if self.gene_space is None:
return None
space = self.gene_space[gene_index] if self.gene_space_nested else self.gene_space
value = float(value)
if isinstance(space, range) or (isinstance(space, dict) and 'step' in space):
# The values are evenly spaced, so the nearest one is found
# from its position without listing all the values.
count = self._finite_gene_space_length(space)
low = space.start if isinstance(space, range) else space['low']
step = space.step if isinstance(space, range) else space['step']
position = math.floor((value - low) / step + 0.5)
position = min(max(position, 0), count - 1)
return self._finite_gene_space_value(space, position)
if isinstance(space, (list, tuple, numpy.ndarray)):
if any(item is None for item in space):
return None
return min(space, key=lambda allowed_value: abs(float(allowed_value) - value))
return None
def is_categorical_gene(self, gene_index):
"""
Check whether a gene is marked as categorical in ``gene_categorical``.
Parameters
----------
gene_index : int
Index of the gene.
Returns
-------
bool
True when the gene is categorical. A GA instance saved by an
older PyGAD version has no ``gene_categorical`` attribute, so
its genes are treated as ordered.
"""
gene_categorical = getattr(self, 'gene_categorical', None)
if gene_categorical is None:
return False
return bool(gene_categorical[gene_index])
def pick_other_categorical_value(self, gene_index, current_value):
"""
Pick one allowed value of a categorical gene, other than its
current value, with every other value equally likely.
Parameters
----------
gene_index : int
Index of the categorical gene. Its gene space is finite, which
``gene_categorical`` validation guarantees.
current_value : number
The value the gene has now.
Returns
-------
number
The new value. When the gene has only one allowed value, that
value is returned. When the current value is not one of the
allowed values, any allowed value can be returned.
"""
space = self.gene_space[gene_index] if self.gene_space_nested else self.gene_space
if isinstance(space, range) or isinstance(space, dict):
# Evenly spaced values: work with positions so a long range is
# never listed in full.
count = self._finite_gene_space_length(space)
low = space.start if isinstance(space, range) else space['low']
step = space.step if isinstance(space, range) else space['step']
current_position = int(round((float(current_value) - low) / step))
if not (0 <= current_position < count
and math.isclose(float(self._finite_gene_space_value(space, current_position)), float(current_value))):
current_position = None
allowed_value_at = lambda position: self._finite_gene_space_value(space, position)
else:
count = len(space)
current_position = None
for position, allowed_value in enumerate(space):
if float(allowed_value) == float(current_value):
current_position = position
break
allowed_value_at = lambda position: space[position]
if count == 1:
new_position = 0
elif current_position is None:
new_position = int(self.numpy_random_generator.randint(0, count))
else:
# Draw from the other count - 1 positions, then skip over the
# current position so it can never be chosen.
new_position = int(self.numpy_random_generator.randint(0, count - 1))
if new_position >= current_position:
new_position += 1
# The chosen value is already allowed. The conversion only applies
# the gene type, for example turning 2.0 into 2 for an int gene.
return self.convert_bounded_operator_gene_value(gene_index, allowed_value_at(new_position))
def is_bounded_operator_gene_value_in_space(self, gene_index, value):
"""Check a bounded operator against the original space, including None entries."""
if self.gene_space is None:
return True
space = self.gene_space[gene_index] if self.gene_space_nested else self.gene_space
if space is None:
return True
if isinstance(space, (list, tuple, numpy.ndarray)) and any(item is None for item in space):
lower, upper = self.get_initial_population_range(gene_index)
if lower <= float(value) <= upper:
return True
return self.is_gene_value_in_space(gene_index, value, value)
def prepare_changed_operator_solution(self, original_solution, proposed_solution, build_initial_pop=False):
"""Return a valid converted change, or None when it violates a destination rule.
Validate complete solutions after conversion and duplicate repair so
dependent constraints can see every changed gene. The caller keeps the
original solution when its candidate cannot be accepted.
"""
candidate = self.change_population_dtype_and_round([proposed_solution])[0]
if not self.allow_duplicate_genes:
candidate, _, remaining = self.solve_duplicate_genes(candidate, build_initial_pop=build_initial_pop, warn=False)
if remaining > len(self.get_duplicate_gene_indices(original_solution)):
return None
if self.gene_space is not None:
for index, value in enumerate(candidate):
valid = (self.is_bounded_operator_gene_value_in_space(index, value) if build_initial_pop else
self.is_gene_value_in_space(index, value, original_solution[index]))
if not valid:
return None
if not self.solution_satisfies_gene_constraints(candidate):
return None
return candidate
def prepare_bounded_operator_solution(self, original_solution, proposed_solution):
"""Apply constraints to a bounded proposal and retain a valid fallback if needed."""
candidate = self.change_population_dtype_and_round([proposed_solution])[0]
if self.gene_constraint:
candidate = self.apply_initial_population_gene_constraints([candidate], warn=False)[0]
prepared = self.prepare_changed_operator_solution(original_solution, candidate, build_initial_pop=True)
return original_solution.copy() if prepared is None else prepared
def _continuous_gene_space_integer_bounds(self, space):
"""Find the integer endpoints produced by truncating a continuous space.
Include integers reached from fractional endpoints, while excluding
a positive integral upper bound. Equal bounds describe one fixed value.
"""
lower, upper = space['low'], space['high']
lower = lower.item() if isinstance(lower, numpy.generic) else lower
upper = upper.item() if isinstance(upper, numpy.generic) else upper
first = math.trunc(lower)
if lower == upper:
last = first
elif upper > 0:
last = math.ceil(upper) - 1
else:
last = math.trunc(upper)
return first, last
def _finite_gene_space_length(self, space):
"""Count a range or stepped dictionary without allocating its values."""
if isinstance(space, range):
lower, upper, step = space.start, space.stop, space.step
else:
lower, upper, step = space['low'], space['high'], space['step']
# Integer arithmetic keeps large discrete bounds exact.
if all(isinstance(value, (int, numpy.integer)) for value in (lower, upper, step)):
lower, upper, step = int(lower), int(upper), int(step)
return max(0, (abs(upper - lower) + abs(step) - 1) // abs(step)) if (upper - lower) * step > 0 else 0
return max(0, math.ceil((upper - lower) / step))
def _finite_gene_space_value(self, space, index):
"""Return one indexed value from a lazy finite numeric space."""
if isinstance(space, range):
return space.start + index * space.step
lower, step = space['low'], space['step']
if index == 0:
return lower
if isinstance(lower, int) and isinstance(step, int) and isinstance(space['high'], int):
return lower + index * step
# Match numpy.arange's stored floating-point increment, which
# can differ slightly from the supplied step after adding low.
lower = float(lower)
stored_step = (lower + float(step)) - lower
return lower + index * stored_step
def _sample_finite_gene_space(self, space, sample_size, without_replacement=False):
"""Sample finite-space indices, enumerating only when explicitly requested.
None requests the full domain for exhaustive duplicate repair. Regular
generation samples indices directly, including domains larger than a
NumPy integer bound or Python's maximum sequence length.
"""
count = self._finite_gene_space_length(space)
if count == 0:
return numpy.empty(0, dtype=object)
if sample_size is None or (without_replacement and sample_size >= count):
indices = range(count)
elif without_replacement:
# random.sample requires len(range(count)) to fit a platform
# integer. Small samples from larger domains use rejection.
if count <= numpy.iinfo(numpy.intp).max:
indices = self.python_random_generator.sample(range(count), sample_size)
else:
indices, selected = [], set()
while len(indices) < sample_size:
index = self.python_random_generator.randrange(count)
if index not in selected:
selected.add(index)
indices.append(index)
else:
indices = [self.python_random_generator.randrange(count) for _ in range(sample_size)]
return numpy.asarray([self._finite_gene_space_value(space, index) for index in indices], dtype=object)
def sample_initial_population_gene_values(self, gene_index, num_values):
"""
Sample a column using its own space, range, type, and precision.
Finite spaces are converted once before selection. A None entry
draws fresh values from the initialization range. Integer ranges
are sampled directly without allocating every possible value.
"""
space = self.gene_space[gene_index] if self.gene_space_nested else self.gene_space
if space is None:
lower, upper = self.get_initial_population_range(gene_index)
return self._initial_population_range_values(gene_index, lower, upper, num_values)
if type(space) is dict and 'step' not in space:
return self._initial_population_range_values(
gene_index, space['low'], space['high'], num_values)
if type(space) in [list, tuple, numpy.ndarray] and any(value is None for value in space):
explicit_values = [value for value in space if value is not None]
explicit_values = numpy.unique(self.change_gene_dtype_and_round(gene_index, explicit_values))
# None is one choice in the space, rather than a fixed value
# sampled once and reused throughout the column.
selected_indices = self.numpy_random_generator.randint(0, len(explicit_values) + 1, size=num_values)
values = numpy.empty(num_values, dtype=object)
random_positions = selected_indices == len(explicit_values)
values[~random_positions] = explicit_values[selected_indices[~random_positions]]
if numpy.any(random_positions):
lower, upper = self.get_initial_population_range(gene_index)
values[random_positions] = self._initial_population_range_values(
gene_index, lower, upper, int(numpy.sum(random_positions)))
return values
if isinstance(space, range) or (isinstance(space, dict) and 'step' in space):
values = self._sample_finite_gene_space(space, num_values)
return self.change_gene_dtype_and_round(gene_index, values)
candidates = self.get_gene_space_values(gene_index)
if len(candidates) == 0:
raise ValueError(f"There are no values to select from the gene_space for the gene at index {gene_index}.")
return self.numpy_random_generator.choice(candidates, size=num_values, replace=True)
def get_initial_population_gene_candidates(self, gene_index, sample_size,
all_integer_values=True):
"""
Return replacement candidates for initialization constraints and
duplicates. Finite domains are considered in full; continuous
domains contribute sample_size values. When all_integer_values
is False, large ranges and stepped dictionaries are also sampled. Candidates
already have the configured gene type and precision.
"""
space = self.gene_space[gene_index] if self.gene_space_nested else self.gene_space
if space is None: