forked from ahmedfgad/GeneticAlgorithmPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmutation.py
More file actions
1127 lines (985 loc) · 71.6 KB
/
mutation.py
File metadata and controls
1127 lines (985 loc) · 71.6 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.utils.mutation module has all the built-in mutation operators.
"""
import numpy
import random
import pygad
import concurrent.futures
class Mutation:
def __init__():
pass
def random_mutation(self, offspring):
"""
Applies the random mutation which changes the values of a number of genes randomly.
The random value is selected either using the 'gene_space' parameter or the 2 parameters 'random_mutation_min_val' and 'random_mutation_max_val'.
It accepts a single parameter:
-offspring: The offspring to mutate.
It returns an array of the mutated offspring.
"""
# If the mutation values are selected from the mutation space, the attribute 'gene_space' is not None. Otherwise, it is None.
# When the 'mutation_probability' parameter exists (i.e. not None), then it is used in the mutation. Otherwise, the 'mutation_num_genes' parameter is used.
if self.mutation_probability is None:
# When the 'mutation_probability' parameter does not exist (i.e. None), then the parameter 'mutation_num_genes' is used in the mutation.
if not (self.gene_space is None):
# When the attribute 'gene_space' exists (i.e. not None), the mutation values are selected randomly from the space of values of each gene.
offspring = self.mutation_by_space(offspring)
else:
offspring = self.mutation_randomly(offspring)
else:
# When the 'mutation_probability' parameter exists (i.e. not None), then it is used in the mutation.
if not (self.gene_space is None):
# When the attribute 'gene_space' does not exist (i.e. None), the mutation values are selected randomly based on the continuous range specified by the 2 attributes 'random_mutation_min_val' and 'random_mutation_max_val'.
offspring = self.mutation_probs_by_space(offspring)
else:
offspring = self.mutation_probs_randomly(offspring)
return offspring
def mutation_by_space(self, offspring):
"""
Applies the random mutation using the mutation values' space.
It accepts a single parameter:
-offspring: The offspring to mutate.
It returns an array of the mutated offspring using the mutation space.
"""
# For each offspring, a value from the gene space is selected randomly and assigned to the selected mutated gene.
for offspring_idx in range(offspring.shape[0]):
mutation_indices = numpy.array(random.sample(range(0, self.num_genes), self.mutation_num_genes))
for gene_idx in mutation_indices:
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_idx]
range_max = self.random_mutation_max_val[gene_idx]
if self.gene_space_nested:
# Returning the current gene space from the 'gene_space' attribute.
if type(self.gene_space[gene_idx]) in [numpy.ndarray, list]:
curr_gene_space = self.gene_space[gene_idx].copy()
else:
curr_gene_space = self.gene_space[gene_idx]
# If the gene space has only a single value, use it as the new gene value.
if type(curr_gene_space) in pygad.GA.supported_int_float_types:
value_from_space = curr_gene_space
# If the gene space is None, apply mutation by adding a random value between the range defined by the 2 parameters 'random_mutation_min_val' and 'random_mutation_max_val'.
elif curr_gene_space is None:
rand_val = numpy.random.uniform(low=range_min,
high=range_max,
size=1)[0]
if self.mutation_by_replacement:
value_from_space = rand_val
else:
value_from_space = offspring[offspring_idx, gene_idx] + rand_val
elif type(curr_gene_space) is dict:
# The gene's space of type dict specifies the lower and upper limits of a gene.
if 'step' in curr_gene_space.keys():
# The numpy.random.choice() and numpy.random.uniform() functions return a NumPy array as the output even if the array has a single value.
# We have to return the output at index 0 to force a numeric value to be returned not an object of type numpy.ndarray.
# If numpy.ndarray is returned, then it will cause an issue later while using the set() function.
# Randomly select a value from a discrete range.
value_from_space = numpy.random.choice(numpy.arange(start=curr_gene_space['low'],
stop=curr_gene_space['high'],
step=curr_gene_space['step']),
size=1)[0]
else:
# Return the current gene value.
value_from_space = offspring[offspring_idx, gene_idx]
# Generate a random value to be added to the current gene value.
rand_val = numpy.random.uniform(low=range_min,
high=range_max,
size=1)[0]
# The objective is to have a new gene value that respects the gene_space boundaries.
# The next if-else block checks if adding the random value keeps the new gene value within the gene_space boundaries.
temp_val = value_from_space + rand_val
if temp_val < curr_gene_space['low']:
# Restrict the new value to be > curr_gene_space['low']
# If subtracting the random value makes the new gene value outside the boundaries [low, high), then use the lower boundary the gene value.
if curr_gene_space['low'] <= value_from_space - rand_val < curr_gene_space['high']:
# Because subtracting the random value keeps the new gene value within the boundaries [low, high), then use such a value as the gene value.
temp_val = value_from_space - rand_val
else:
# Because subtracting the random value makes the new gene value outside the boundaries [low, high), then use the lower boundary as the gene value.
temp_val = curr_gene_space['low']
elif temp_val >= curr_gene_space['high']:
# Restrict the new value to be < curr_gene_space['high']
# If subtracting the random value makes the new gene value outside the boundaries [low, high), then use such a value as the gene value.
if curr_gene_space['low'] <= value_from_space - rand_val < curr_gene_space['high']:
# Because subtracting the random value keeps the new value within the boundaries [low, high), then use such a value as the gene value.
temp_val = value_from_space - rand_val
else:
# Because subtracting the random value makes the new gene value outside the boundaries [low, high), then use the lower boundary as the gene value.
temp_val = curr_gene_space['low']
value_from_space = temp_val
else:
# Selecting a value randomly based on the current gene's space in the 'gene_space' attribute.
# If the gene space has only 1 value, then select it. The old and new values of the gene are identical.
if len(curr_gene_space) == 1:
value_from_space = curr_gene_space[0]
# If the gene space has more than 1 value, then select a new one that is different from the current value.
else:
values_to_select_from = list(set(curr_gene_space) - set([offspring[offspring_idx, gene_idx]]))
if len(values_to_select_from) == 0:
value_from_space = offspring[offspring_idx, gene_idx]
else:
value_from_space = random.choice(values_to_select_from)
else:
# Selecting a value randomly from the global gene space in the 'gene_space' attribute.
if type(self.gene_space) is dict:
# When the gene_space is assigned a dict object, then it specifies the lower and upper limits of all genes in the space.
if 'step' in self.gene_space.keys():
value_from_space = numpy.random.choice(numpy.arange(start=self.gene_space['low'],
stop=self.gene_space['high'],
step=self.gene_space['step']),
size=1)[0]
else:
value_from_space = numpy.random.uniform(low=self.gene_space['low'],
high=self.gene_space['high'],
size=1)[0]
else:
# If the space type is not of type dict, then a value is randomly selected from the gene_space attribute.
values_to_select_from = list(set(self.gene_space) - set([offspring[offspring_idx, gene_idx]]))
if len(values_to_select_from) == 0:
value_from_space = offspring[offspring_idx, gene_idx]
else:
value_from_space = random.choice(values_to_select_from)
# value_from_space = random.choice(self.gene_space)
if value_from_space is None:
# TODO: Return index 0.
# TODO: Check if this if statement is necessary.
value_from_space = numpy.random.uniform(low=range_min,
high=range_max,
size=1)[0]
# Assinging the selected value from the space to the gene.
if self.gene_type_single == True:
if not self.gene_type[1] is None:
offspring[offspring_idx, gene_idx] = numpy.round(self.gene_type[0](value_from_space),
self.gene_type[1])
else:
offspring[offspring_idx, gene_idx] = self.gene_type[0](value_from_space)
else:
if not self.gene_type[gene_idx][1] is None:
offspring[offspring_idx, gene_idx] = numpy.round(self.gene_type[gene_idx][0](value_from_space),
self.gene_type[gene_idx][1])
else:
offspring[offspring_idx, gene_idx] = self.gene_type[gene_idx][0](value_from_space)
if self.allow_duplicate_genes == False:
offspring[offspring_idx], _, _ = self.solve_duplicate_genes_by_space(solution=offspring[offspring_idx],
gene_type=self.gene_type,
num_trials=10)
return offspring
def mutation_probs_by_space(self, offspring):
"""
Applies the random mutation using the mutation values' space and the mutation probability. For each gene, if its probability is <= that mutation probability, then it will be mutated based on the mutation space.
It accepts a single parameter:
-offspring: The offspring to mutate.
It returns an array of the mutated offspring using the mutation space.
"""
# For each offspring, a value from the gene space is selected randomly and assigned to the selected mutated gene.
for offspring_idx in range(offspring.shape[0]):
probs = numpy.random.random(size=offspring.shape[1])
for gene_idx in range(offspring.shape[1]):
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_idx]
range_max = self.random_mutation_max_val[gene_idx]
if probs[gene_idx] <= self.mutation_probability:
if self.gene_space_nested:
# Returning the current gene space from the 'gene_space' attribute.
if type(self.gene_space[gene_idx]) in [numpy.ndarray, list]:
curr_gene_space = self.gene_space[gene_idx].copy()
else:
curr_gene_space = self.gene_space[gene_idx]
# If the gene space has only a single value, use it as the new gene value.
if type(curr_gene_space) in pygad.GA.supported_int_float_types:
value_from_space = curr_gene_space
# If the gene space is None, apply mutation by adding a random value between the range defined by the 2 parameters 'random_mutation_min_val' and 'random_mutation_max_val'.
elif curr_gene_space is None:
rand_val = numpy.random.uniform(low=range_min,
high=range_max,
size=1)[0]
if self.mutation_by_replacement:
value_from_space = rand_val
else:
value_from_space = offspring[offspring_idx, gene_idx] + rand_val
elif type(curr_gene_space) is dict:
# Selecting a value randomly from the current gene's space in the 'gene_space' attribute.
if 'step' in curr_gene_space.keys():
value_from_space = numpy.random.choice(numpy.arange(start=curr_gene_space['low'],
stop=curr_gene_space['high'],
step=curr_gene_space['step']),
size=1)[0]
else:
value_from_space = numpy.random.uniform(low=curr_gene_space['low'],
high=curr_gene_space['high'],
size=1)[0]
else:
# Selecting a value randomly from the current gene's space in the 'gene_space' attribute.
# If the gene space has only 1 value, then select it. The old and new values of the gene are identical.
if len(curr_gene_space) == 1:
value_from_space = curr_gene_space[0]
# If the gene space has more than 1 value, then select a new one that is different from the current value.
else:
values_to_select_from = list(set(curr_gene_space) - set([offspring[offspring_idx, gene_idx]]))
if len(values_to_select_from) == 0:
value_from_space = offspring[offspring_idx, gene_idx]
else:
value_from_space = random.choice(values_to_select_from)
else:
# Selecting a value randomly from the global gene space in the 'gene_space' attribute.
if type(self.gene_space) is dict:
if 'step' in self.gene_space.keys():
value_from_space = numpy.random.choice(numpy.arange(start=self.gene_space['low'],
stop=self.gene_space['high'],
step=self.gene_space['step']),
size=1)[0]
else:
value_from_space = numpy.random.uniform(low=self.gene_space['low'],
high=self.gene_space['high'],
size=1)[0]
else:
values_to_select_from = list(set(self.gene_space) - set([offspring[offspring_idx, gene_idx]]))
if len(values_to_select_from) == 0:
value_from_space = offspring[offspring_idx, gene_idx]
else:
value_from_space = random.choice(values_to_select_from)
# Assigning the selected value from the space to the gene.
if self.gene_type_single == True:
if not self.gene_type[1] is None:
offspring[offspring_idx, gene_idx] = numpy.round(self.gene_type[0](value_from_space),
self.gene_type[1])
else:
offspring[offspring_idx, gene_idx] = self.gene_type[0](value_from_space)
else:
if not self.gene_type[gene_idx][1] is None:
offspring[offspring_idx, gene_idx] = numpy.round(self.gene_type[gene_idx][0](value_from_space),
self.gene_type[gene_idx][1])
else:
offspring[offspring_idx, gene_idx] = self.gene_type[gene_idx][0](value_from_space)
if self.allow_duplicate_genes == False:
offspring[offspring_idx], _, _ = self.solve_duplicate_genes_by_space(solution=offspring[offspring_idx],
gene_type=self.gene_type,
num_trials=10)
return offspring
def mutation_randomly(self, offspring):
"""
Applies the random mutation the mutation probability. For each gene, if its probability is <= that mutation probability, then it will be mutated randomly.
It accepts a single parameter:
-offspring: The offspring to mutate.
It returns an array of the mutated offspring.
"""
# Random mutation changes one or more genes in each offspring randomly.
for offspring_idx in range(offspring.shape[0]):
mutation_indices = numpy.array(random.sample(range(0, self.num_genes), self.mutation_num_genes))
for gene_idx in mutation_indices:
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_idx]
range_max = self.random_mutation_max_val[gene_idx]
# Generating a random value.
random_value = numpy.random.uniform(low=range_min,
high=range_max,
size=1)[0]
# If the mutation_by_replacement attribute is True, then the random value replaces the current gene value.
if self.mutation_by_replacement:
if self.gene_type_single == True:
random_value = self.gene_type[0](random_value)
else:
random_value = self.gene_type[gene_idx][0](random_value)
if type(random_value) is numpy.ndarray:
random_value = random_value[0]
# If the mutation_by_replacement attribute is False, then the random value is added to the gene value.
else:
if self.gene_type_single == True:
random_value = self.gene_type[0](offspring[offspring_idx, gene_idx] + random_value)
else:
random_value = self.gene_type[gene_idx][0](offspring[offspring_idx, gene_idx] + random_value)
if type(random_value) is numpy.ndarray:
random_value = random_value[0]
# Round the gene
if self.gene_type_single == True:
if not self.gene_type[1] is None:
random_value = numpy.round(random_value, self.gene_type[1])
else:
if not self.gene_type[gene_idx][1] is None:
random_value = numpy.round(random_value, self.gene_type[gene_idx][1])
offspring[offspring_idx, gene_idx] = random_value
if self.allow_duplicate_genes == False:
offspring[offspring_idx], _, _ = self.solve_duplicate_genes_randomly(solution=offspring[offspring_idx],
min_val=range_min,
max_val=range_max,
mutation_by_replacement=self.mutation_by_replacement,
gene_type=self.gene_type,
num_trials=10)
return offspring
def mutation_probs_randomly(self, offspring):
"""
Applies the random mutation using the mutation probability. For each gene, if its probability is <= that mutation probability, then it will be mutated randomly.
It accepts a single parameter:
-offspring: The offspring to mutate.
It returns an array of the mutated offspring.
"""
# Random mutation changes one or more gene in each offspring randomly.
for offspring_idx in range(offspring.shape[0]):
probs = numpy.random.random(size=offspring.shape[1])
for gene_idx in range(offspring.shape[1]):
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_idx]
range_max = self.random_mutation_max_val[gene_idx]
if probs[gene_idx] <= self.mutation_probability:
# Generating a random value.
random_value = numpy.random.uniform(low=range_min,
high=range_max,
size=1)[0]
# If the mutation_by_replacement attribute is True, then the random value replaces the current gene value.
if self.mutation_by_replacement:
if self.gene_type_single == True:
random_value = self.gene_type[0](random_value)
else:
random_value = self.gene_type[gene_idx][0](random_value)
if type(random_value) is numpy.ndarray:
random_value = random_value[0]
# If the mutation_by_replacement attribute is False, then the random value is added to the gene value.
else:
if self.gene_type_single == True:
random_value = self.gene_type[0](offspring[offspring_idx, gene_idx] + random_value)
else:
random_value = self.gene_type[gene_idx][0](offspring[offspring_idx, gene_idx] + random_value)
if type(random_value) is numpy.ndarray:
random_value = random_value[0]
# Round the gene
if self.gene_type_single == True:
if not self.gene_type[1] is None:
random_value = numpy.round(random_value, self.gene_type[1])
else:
if not self.gene_type[gene_idx][1] is None:
random_value = numpy.round(random_value, self.gene_type[gene_idx][1])
offspring[offspring_idx, gene_idx] = random_value
if self.allow_duplicate_genes == False:
offspring[offspring_idx], _, _ = self.solve_duplicate_genes_randomly(solution=offspring[offspring_idx],
min_val=range_min,
max_val=range_max,
mutation_by_replacement=self.mutation_by_replacement,
gene_type=self.gene_type,
num_trials=10)
return offspring
def swap_mutation(self, offspring):
"""
Applies the swap mutation which interchanges the values of 2 randomly selected genes.
It accepts a single parameter:
-offspring: The offspring to mutate.
It returns an array of the mutated offspring.
"""
for idx in range(offspring.shape[0]):
mutation_gene1 = numpy.random.randint(low=0, high=offspring.shape[1]/2, size=1)[0]
mutation_gene2 = mutation_gene1 + int(offspring.shape[1]/2)
temp = offspring[idx, mutation_gene1]
offspring[idx, mutation_gene1] = offspring[idx, mutation_gene2]
offspring[idx, mutation_gene2] = temp
return offspring
def inversion_mutation(self, offspring):
"""
Applies the inversion mutation which selects a subset of genes and inverts them (in order).
It accepts a single parameter:
-offspring: The offspring to mutate.
It returns an array of the mutated offspring.
"""
for idx in range(offspring.shape[0]):
mutation_gene1 = numpy.random.randint(low=0, high=numpy.ceil(offspring.shape[1]/2 + 1), size=1)[0]
mutation_gene2 = mutation_gene1 + int(offspring.shape[1]/2)
genes_to_scramble = numpy.flip(offspring[idx, mutation_gene1:mutation_gene2])
offspring[idx, mutation_gene1:mutation_gene2] = genes_to_scramble
return offspring
def scramble_mutation(self, offspring):
"""
Applies the scramble mutation which selects a subset of genes and shuffles their order randomly.
It accepts a single parameter:
-offspring: The offspring to mutate.
It returns an array of the mutated offspring.
"""
for idx in range(offspring.shape[0]):
mutation_gene1 = numpy.random.randint(low=0, high=numpy.ceil(offspring.shape[1]/2 + 1), size=1)[0]
mutation_gene2 = mutation_gene1 + int(offspring.shape[1]/2)
genes_range = numpy.arange(start=mutation_gene1, stop=mutation_gene2)
numpy.random.shuffle(genes_range)
genes_to_scramble = numpy.flip(offspring[idx, genes_range])
offspring[idx, genes_range] = genes_to_scramble
return offspring
def adaptive_mutation_population_fitness(self, offspring):
"""
A helper method to calculate the average fitness of the solutions before applying the adaptive mutation.
It accepts a single parameter:
-offspring: The offspring to mutate.
It returns the average fitness to be used in adaptive mutation.
"""
fitness = self.last_generation_fitness.copy()
temp_population = numpy.zeros_like(self.population)
if (self.keep_elitism == 0):
if (self.keep_parents == 0):
parents_to_keep = []
elif (self.keep_parents == -1):
parents_to_keep = self.last_generation_parents.copy()
temp_population[0:len(parents_to_keep), :] = parents_to_keep
elif (self.keep_parents > 0):
parents_to_keep, _ = self.steady_state_selection(self.last_generation_fitness, num_parents=self.keep_parents)
temp_population[0:len(parents_to_keep), :] = parents_to_keep
else:
parents_to_keep, _ = self.steady_state_selection(self.last_generation_fitness, num_parents=self.keep_elitism)
temp_population[0:len(parents_to_keep), :] = parents_to_keep
temp_population[len(parents_to_keep):, :] = offspring
fitness[:self.last_generation_parents.shape[0]] = self.last_generation_fitness[self.last_generation_parents_indices]
first_idx = len(parents_to_keep)
last_idx = fitness.shape[0]
if len(fitness.shape) > 1:
# TODO This is a multi-objective optimization problem.
# fitness[first_idx:last_idx] = [0]*(last_idx - first_idx)
fitness[first_idx:last_idx] = numpy.zeros(shape=(last_idx - first_idx, fitness.shape[1]))
# raise ValueError('Edit adaptive mutation to work with multi-objective optimization problems.')
else:
# This is a single-objective optimization problem.
fitness[first_idx:last_idx] = [0]*(last_idx - first_idx)
# # No parallel processing.
if self.parallel_processing is None:
if self.fitness_batch_size in [1, None]:
# Calculate the fitness for each individual solution.
for idx in range(first_idx, last_idx):
# We cannot return the index of the solution within the population.
# Because the new solution (offspring) does not yet exist in the population.
# The user should handle this situation if the solution index is used anywhere.
fitness[idx] = self.fitness_func(self,
temp_population[idx],
None)
else:
# Calculate the fitness for batch of solutions.
# Number of batches.
num_batches = int(numpy.ceil((last_idx - first_idx) / self.fitness_batch_size))
for batch_idx in range(num_batches):
# The index of the first solution in the current batch.
batch_first_index = first_idx + batch_idx * self.fitness_batch_size
# The index of the last solution in the current batch.
if batch_idx == (num_batches - 1):
batch_last_index = last_idx
else:
batch_last_index = first_idx + (batch_idx + 1) * self.fitness_batch_size
# Calculate the fitness values for the batch.
# We cannot return the index/indices of the solution(s) within the population.
# Because the new solution(s) (offspring) do(es) not yet exist in the population.
# The user should handle this situation if the solution index is used anywhere.
fitness_temp = self.fitness_func(self,
temp_population[batch_first_index:batch_last_index],
None)
# Insert the fitness of each solution at the proper index.
for idx in range(batch_first_index, batch_last_index):
fitness[idx] = fitness_temp[idx - batch_first_index]
else:
# Parallel processing
# Decide which class to use based on whether the user selected "process" or "thread"
# TODO Add ExecutorClass as an instance attribute in the pygad.GA instances. Then retrieve this instance here instead of creating a new one.
if self.parallel_processing[0] == "process":
ExecutorClass = concurrent.futures.ProcessPoolExecutor
else:
ExecutorClass = concurrent.futures.ThreadPoolExecutor
# We can use a with statement to ensure threads are cleaned up promptly (https://docs.python.org/3/library/concurrent.futures.html#threadpoolexecutor-example)
with ExecutorClass(max_workers=self.parallel_processing[1]) as executor:
# Indices of the solutions to calculate its fitness.
solutions_to_submit_indices = list(range(first_idx, last_idx))
# The solutions to calculate its fitness.
solutions_to_submit = [temp_population[sol_idx].copy() for sol_idx in solutions_to_submit_indices]
if self.fitness_batch_size in [1, None]:
# Use parallel processing to calculate the fitness of the solutions.
for index, sol_fitness in zip(solutions_to_submit_indices, executor.map(self.fitness_func, [self]*len(solutions_to_submit_indices), solutions_to_submit, solutions_to_submit_indices)):
if type(sol_fitness) in self.supported_int_float_types:
# The fitness function returns a single numeric value.
# This is a single-objective optimization problem.
fitness[index] = sol_fitness
elif type(sol_fitness) in [list, tuple, numpy.ndarray]:
# The fitness function returns a list/tuple/numpy.ndarray.
# This is a multi-objective optimization problem.
fitness[index] = sol_fitness
else:
raise ValueError(f"The fitness function should return a number or an iterable (list, tuple, or numpy.ndarray) but the value {sol_fitness} of type {type(sol_fitness)} found.")
else:
# Reaching this point means that batch processing is in effect to calculate the fitness values.
# Number of batches.
num_batches = int(numpy.ceil(len(solutions_to_submit_indices) / self.fitness_batch_size))
# Each element of the `batches_solutions` list represents the solutions in one batch.
batches_solutions = []
# Each element of the `batches_indices` list represents the solutions' indices in one batch.
batches_indices = []
# For each batch, get its indices and call the fitness function.
for batch_idx in range(num_batches):
batch_first_index = batch_idx * self.fitness_batch_size
batch_last_index = (batch_idx + 1) * self.fitness_batch_size
batch_indices = solutions_to_submit_indices[batch_first_index:batch_last_index]
batch_solutions = self.population[batch_indices, :]
batches_solutions.append(batch_solutions)
batches_indices.append(batch_indices)
for batch_indices, batch_fitness in zip(batches_indices, executor.map(self.fitness_func, [self]*len(solutions_to_submit_indices), batches_solutions, batches_indices)):
if type(batch_fitness) not in [list, tuple, numpy.ndarray]:
raise TypeError(f"Expected to receive a list, tuple, or numpy.ndarray from the fitness function but the value ({batch_fitness}) of type {type(batch_fitness)}.")
elif len(numpy.array(batch_fitness)) != len(batch_indices):
raise ValueError(f"There is a mismatch between the number of solutions passed to the fitness function ({len(batch_indices)}) and the number of fitness values returned ({len(batch_fitness)}). They must match.")
for index, sol_fitness in zip(batch_indices, batch_fitness):
if type(sol_fitness) in self.supported_int_float_types:
# The fitness function returns a single numeric value.
# This is a single-objective optimization problem.
fitness[index] = sol_fitness
elif type(sol_fitness) in [list, tuple, numpy.ndarray]:
# The fitness function returns a list/tuple/numpy.ndarray.
# This is a multi-objective optimization problem.
fitness[index] = sol_fitness
else:
raise ValueError(f"The fitness function should return a number or an iterable (list, tuple, or numpy.ndarray) but the value ({sol_fitness}) of type {type(sol_fitness)} found.")
if len(fitness.shape) > 1:
# TODO This is a multi-objective optimization problem.
# Calculate the average of each objective's fitness across all solutions in the population.
average_fitness = numpy.mean(fitness, axis=0)
else:
# This is a single-objective optimization problem.
average_fitness = numpy.mean(fitness)
return average_fitness, fitness[len(parents_to_keep):]
def adaptive_mutation(self, offspring):
"""
Applies the adaptive mutation which changes the values of a number of genes randomly. In adaptive mutation, the number of genes to mutate differs based on the fitness value of the solution.
The random value is selected either using the 'gene_space' parameter or the 2 parameters 'random_mutation_min_val' and 'random_mutation_max_val'.
It accepts a single parameter:
-offspring: The offspring to mutate.
It returns an array of the mutated offspring.
"""
# If the attribute 'gene_space' exists (i.e. not None), then the mutation values are selected from the 'gene_space' parameter according to the space of values of each gene. Otherwise, it is selected randomly based on the 2 parameters 'random_mutation_min_val' and 'random_mutation_max_val'.
# When the 'mutation_probability' parameter exists (i.e. not None), then it is used in the mutation. Otherwise, the 'mutation_num_genes' parameter is used.
if self.mutation_probability is None:
# When the 'mutation_probability' parameter does not exist (i.e. None), then the parameter 'mutation_num_genes' is used in the mutation.
if not (self.gene_space is None):
# When the attribute 'gene_space' exists (i.e. not None), the mutation values are selected randomly from the space of values of each gene.
offspring = self.adaptive_mutation_by_space(offspring)
else:
# When the attribute 'gene_space' does not exist (i.e. None), the mutation values are selected randomly based on the continuous range specified by the 2 attributes 'random_mutation_min_val' and 'random_mutation_max_val'.
offspring = self.adaptive_mutation_randomly(offspring)
else:
# When the 'mutation_probability' parameter exists (i.e. not None), then it is used in the mutation.
if not (self.gene_space is None):
# When the attribute 'gene_space' exists (i.e. not None), the mutation values are selected randomly from the space of values of each gene.
offspring = self.adaptive_mutation_probs_by_space(offspring)
else:
# When the attribute 'gene_space' does not exist (i.e. None), the mutation values are selected randomly based on the continuous range specified by the 2 attributes 'random_mutation_min_val' and 'random_mutation_max_val'.
offspring = self.adaptive_mutation_probs_randomly(offspring)
return offspring
def adaptive_mutation_by_space(self, offspring):
"""
Applies the adaptive mutation based on the 2 parameters 'mutation_num_genes' and 'gene_space'.
A number of genes equal are selected randomly for mutation. This number depends on the fitness of the solution.
The random values are selected from the 'gene_space' parameter.
It accepts a single parameter:
-offspring: The offspring to mutate.
It returns an array of the mutated offspring.
"""
# For each offspring, a value from the gene space is selected randomly and assigned to the selected gene for mutation.
average_fitness, offspring_fitness = self.adaptive_mutation_population_fitness(offspring)
# Adaptive mutation changes one or more genes in each offspring randomly.
# The number of genes to mutate depends on the solution's fitness value.
for offspring_idx in range(offspring.shape[0]):
## TODO Make edits to work with multi-objective optimization.
# Compare the fitness of each offspring to the average fitness of each objective function.
fitness_comparison = offspring_fitness[offspring_idx] < average_fitness
# Check if the problem is single or multi-objective optimization.
if type(fitness_comparison) in [bool, numpy.bool_]:
# Single-objective optimization problem.
if offspring_fitness[offspring_idx] < average_fitness:
adaptive_mutation_num_genes = self.mutation_num_genes[0]
else:
adaptive_mutation_num_genes = self.mutation_num_genes[1]
else:
# Multi-objective optimization problem.
# Get the sum of the pool array (result of comparison).
# True is considered 1 and False is 0.
fitness_comparison_sum = sum(fitness_comparison)
# Check if more than or equal to 50% of the objectives have fitness greater than the average.
# If True, then use the first percentage.
# If False, use the second percentage.
if fitness_comparison_sum >= len(fitness_comparison)/2:
adaptive_mutation_num_genes = self.mutation_num_genes[0]
else:
adaptive_mutation_num_genes = self.mutation_num_genes[1]
mutation_indices = numpy.array(random.sample(range(0, self.num_genes), adaptive_mutation_num_genes))
for gene_idx in mutation_indices:
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_idx]
range_max = self.random_mutation_max_val[gene_idx]
if self.gene_space_nested:
# Returning the current gene space from the 'gene_space' attribute.
if type(self.gene_space[gene_idx]) in [numpy.ndarray, list]:
curr_gene_space = self.gene_space[gene_idx].copy()
else:
curr_gene_space = self.gene_space[gene_idx]
# If the gene space has only a single value, use it as the new gene value.
if type(curr_gene_space) in pygad.GA.supported_int_float_types:
value_from_space = curr_gene_space
# If the gene space is None, apply mutation by adding a random value between the range defined by the 2 parameters 'random_mutation_min_val' and 'random_mutation_max_val'.
elif curr_gene_space is None:
rand_val = numpy.random.uniform(low=range_min,
high=range_max,
size=1)[0]
if self.mutation_by_replacement:
value_from_space = rand_val
else:
value_from_space = offspring[offspring_idx, gene_idx] + rand_val
elif type(curr_gene_space) is dict:
# Selecting a value randomly from the current gene's space in the 'gene_space' attribute.
if 'step' in curr_gene_space.keys():
# The numpy.random.choice() and numpy.random.uniform() functions return a NumPy array as the output even if the array has a single value.
# We have to return the output at index 0 to force a numeric value to be returned not an object of type numpy.ndarray.
# If numpy.ndarray is returned, then it will cause an issue later while using the set() function.
value_from_space = numpy.random.choice(numpy.arange(start=curr_gene_space['low'],
stop=curr_gene_space['high'],
step=curr_gene_space['step']),
size=1)[0]
else:
value_from_space = numpy.random.uniform(low=curr_gene_space['low'],
high=curr_gene_space['high'],
size=1)[0]
else:
# Selecting a value randomly from the current gene's space in the 'gene_space' attribute.
# If the gene space has only 1 value, then select it. The old and new values of the gene are identical.
if len(curr_gene_space) == 1:
value_from_space = curr_gene_space[0]
# If the gene space has more than 1 value, then select a new one that is different from the current value.
else:
values_to_select_from = list(set(curr_gene_space) - set([offspring[offspring_idx, gene_idx]]))
if len(values_to_select_from) == 0:
value_from_space = offspring[offspring_idx, gene_idx]
else:
value_from_space = random.choice(values_to_select_from)
else:
# Selecting a value randomly from the global gene space in the 'gene_space' attribute.
if type(self.gene_space) is dict:
if 'step' in self.gene_space.keys():
value_from_space = numpy.random.choice(numpy.arange(start=self.gene_space['low'],
stop=self.gene_space['high'],
step=self.gene_space['step']),
size=1)[0]
else:
value_from_space = numpy.random.uniform(low=self.gene_space['low'],
high=self.gene_space['high'],
size=1)[0]
else:
values_to_select_from = list(set(self.gene_space) - set([offspring[offspring_idx, gene_idx]]))
if len(values_to_select_from) == 0:
value_from_space = offspring[offspring_idx, gene_idx]
else:
value_from_space = random.choice(values_to_select_from)
if value_from_space is None:
value_from_space = numpy.random.uniform(low=range_min,
high=range_max,
size=1)[0]
# Assinging the selected value from the space to the gene.
if self.gene_type_single == True:
if not self.gene_type[1] is None:
offspring[offspring_idx, gene_idx] = numpy.round(self.gene_type[0](value_from_space),
self.gene_type[1])
else:
offspring[offspring_idx, gene_idx] = self.gene_type[0](value_from_space)
else:
if not self.gene_type[gene_idx][1] is None:
offspring[offspring_idx, gene_idx] = numpy.round(self.gene_type[gene_idx][0](value_from_space),
self.gene_type[gene_idx][1])
else:
offspring[offspring_idx, gene_idx] = self.gene_type[gene_idx][0](value_from_space)
if self.allow_duplicate_genes == False:
offspring[offspring_idx], _, _ = self.solve_duplicate_genes_by_space(solution=offspring[offspring_idx],
gene_type=self.gene_type,
num_trials=10)
return offspring
def adaptive_mutation_randomly(self, offspring):
"""
Applies the adaptive mutation based on the 'mutation_num_genes' parameter.
A number of genes equal are selected randomly for mutation. This number depends on the fitness of the solution.
The random values are selected based on the 2 parameters 'random_mutation_min_val' and 'random_mutation_max_val'.
It accepts a single parameter:
-offspring: The offspring to mutate.
It returns an array of the mutated offspring.
"""
average_fitness, offspring_fitness = self.adaptive_mutation_population_fitness(offspring)
# Adaptive random mutation changes one or more genes in each offspring randomly.
# The number of genes to mutate depends on the solution's fitness value.
for offspring_idx in range(offspring.shape[0]):
## TODO Make edits to work with multi-objective optimization.
# Compare the fitness of each offspring to the average fitness of each objective function.
fitness_comparison = offspring_fitness[offspring_idx] < average_fitness
# Check if the problem is single or multi-objective optimization.
if type(fitness_comparison) in [bool, numpy.bool_]:
# Single-objective optimization problem.
if fitness_comparison:
adaptive_mutation_num_genes = self.mutation_num_genes[0]
else:
adaptive_mutation_num_genes = self.mutation_num_genes[1]
else:
# Multi-objective optimization problem.
# Get the sum of the pool array (result of comparison).
# True is considered 1 and False is 0.
fitness_comparison_sum = sum(fitness_comparison)
# Check if more than or equal to 50% of the objectives have fitness greater than the average.
# If True, then use the first percentage.
# If False, use the second percentage.
if fitness_comparison_sum >= len(fitness_comparison)/2:
adaptive_mutation_num_genes = self.mutation_num_genes[0]
else:
adaptive_mutation_num_genes = self.mutation_num_genes[1]
mutation_indices = numpy.array(random.sample(range(0, self.num_genes), adaptive_mutation_num_genes))
for gene_idx in mutation_indices:
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_idx]
range_max = self.random_mutation_max_val[gene_idx]
# Generating a random value.
random_value = numpy.random.uniform(low=range_min,
high=range_max,
size=1)[0]
# If the mutation_by_replacement attribute is True, then the random value replaces the current gene value.
if self.mutation_by_replacement:
if self.gene_type_single == True:
random_value = self.gene_type[0](random_value)
else:
random_value = self.gene_type[gene_idx][0](random_value)
if type(random_value) is numpy.ndarray:
random_value = random_value[0]
# If the mutation_by_replacement attribute is False, then the random value is added to the gene value.
else:
if self.gene_type_single == True:
random_value = self.gene_type[0](offspring[offspring_idx, gene_idx] + random_value)
else:
random_value = self.gene_type[gene_idx][0](offspring[offspring_idx, gene_idx] + random_value)
if type(random_value) is numpy.ndarray:
random_value = random_value[0]
if self.gene_type_single == True:
if not self.gene_type[1] is None:
random_value = numpy.round(random_value, self.gene_type[1])
else:
if not self.gene_type[gene_idx][1] is None:
random_value = numpy.round(random_value, self.gene_type[gene_idx][1])
offspring[offspring_idx, gene_idx] = random_value
if self.allow_duplicate_genes == False:
offspring[offspring_idx], _, _ = self.solve_duplicate_genes_randomly(solution=offspring[offspring_idx],
min_val=range_min,
max_val=range_max,
mutation_by_replacement=self.mutation_by_replacement,
gene_type=self.gene_type,
num_trials=10)
return offspring
def adaptive_mutation_probs_by_space(self, offspring):
"""
Applies the adaptive mutation based on the 2 parameters 'mutation_probability' and 'gene_space'.
Based on whether the solution fitness is above or below a threshold, the mutation is applied diffrently by mutating high or low number of genes.
The random values are selected based on space of values for each gene.
It accepts a single parameter:
-offspring: The offspring to mutate.
It returns an array of the mutated offspring.
"""
# For each offspring, a value from the gene space is selected randomly and assigned to the selected gene for mutation.
average_fitness, offspring_fitness = self.adaptive_mutation_population_fitness(offspring)
# Adaptive random mutation changes one or more genes in each offspring randomly.
# The probability of mutating a gene depends on the solution's fitness value.
for offspring_idx in range(offspring.shape[0]):
## TODO Make edits to work with multi-objective optimization.
# Compare the fitness of each offspring to the average fitness of each objective function.
fitness_comparison = offspring_fitness[offspring_idx] < average_fitness
# Check if the problem is single or multi-objective optimization.
if type(fitness_comparison) in [bool, numpy.bool_]:
# Single-objective optimization problem.
if offspring_fitness[offspring_idx] < average_fitness:
adaptive_mutation_probability = self.mutation_probability[0]
else:
adaptive_mutation_probability = self.mutation_probability[1]
else:
# Multi-objective optimization problem.
# Get the sum of the pool array (result of comparison).
# True is considered 1 and False is 0.
fitness_comparison_sum = sum(fitness_comparison)
# Check if more than or equal to 50% of the objectives have fitness greater than the average.
# If True, then use the first percentage.
# If False, use the second percentage.
if fitness_comparison_sum >= len(fitness_comparison)/2:
adaptive_mutation_probability = self.mutation_probability[0]
else:
adaptive_mutation_probability = self.mutation_probability[1]
probs = numpy.random.random(size=offspring.shape[1])
for gene_idx in range(offspring.shape[1]):
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_idx]
range_max = self.random_mutation_max_val[gene_idx]
if probs[gene_idx] <= adaptive_mutation_probability:
if self.gene_space_nested:
# Returning the current gene space from the 'gene_space' attribute.
if type(self.gene_space[gene_idx]) in [numpy.ndarray, list]:
curr_gene_space = self.gene_space[gene_idx].copy()
else:
curr_gene_space = self.gene_space[gene_idx]
# If the gene space has only a single value, use it as the new gene value.
if type(curr_gene_space) in pygad.GA.supported_int_float_types:
value_from_space = curr_gene_space
# If the gene space is None, apply mutation by adding a random value between the range defined by the 2 parameters 'random_mutation_min_val' and 'random_mutation_max_val'.
elif curr_gene_space is None:
rand_val = numpy.random.uniform(low=range_min,
high=range_max,
size=1)[0]
if self.mutation_by_replacement:
value_from_space = rand_val
else:
value_from_space = offspring[offspring_idx, gene_idx] + rand_val
elif type(curr_gene_space) is dict:
# Selecting a value randomly from the current gene's space in the 'gene_space' attribute.
if 'step' in curr_gene_space.keys():
value_from_space = numpy.random.choice(numpy.arange(start=curr_gene_space['low'],
stop=curr_gene_space['high'],
step=curr_gene_space['step']),
size=1)[0]
else:
value_from_space = numpy.random.uniform(low=curr_gene_space['low'],
high=curr_gene_space['high'],
size=1)[0]
else:
# Selecting a value randomly from the current gene's space in the 'gene_space' attribute.
# If the gene space has only 1 value, then select it. The old and new values of the gene are identical.
if len(curr_gene_space) == 1:
value_from_space = curr_gene_space[0]
# If the gene space has more than 1 value, then select a new one that is different from the current value.
else:
values_to_select_from = list(set(curr_gene_space) - set([offspring[offspring_idx, gene_idx]]))
if len(values_to_select_from) == 0:
value_from_space = offspring[offspring_idx, gene_idx]
else:
value_from_space = random.choice(values_to_select_from)
else:
# Selecting a value randomly from the global gene space in the 'gene_space' attribute.
if type(self.gene_space) is dict:
if 'step' in self.gene_space.keys():
# The numpy.random.choice() and numpy.random.uniform() functions return a NumPy array as the output even if the array has a single value.
# We have to return the output at index 0 to force a numeric value to be returned not an object of type numpy.ndarray.
# If numpy.ndarray is returned, then it will cause an issue later while using the set() function.
value_from_space = numpy.random.choice(numpy.arange(start=self.gene_space['low'],
stop=self.gene_space['high'],
step=self.gene_space['step']),
size=1)[0]