@@ -661,9 +661,9 @@ def initialize_population(self, low, high):
661
661
self .gene_space [gene_idx ] = numpy .asarray (numpy .random .uniform (low = low ,
662
662
high = high ,
663
663
size = 1 ), dtype = self .gene_type )[0 ]
664
- self .population [sol_idx , gene_idx ] = self .gene_space [gene_idx ]
664
+ self .population [sol_idx , gene_idx ] = self .gene_space [gene_idx ]. copy ()
665
665
elif type (self .gene_space [gene_idx ]) in [int , float , numpy .int , numpy .int8 , numpy .int16 , numpy .int32 , numpy .int64 , numpy .uint , numpy .uint8 , numpy .uint16 , numpy .uint32 , numpy .uint64 , numpy .float , numpy .float16 , numpy .float32 , numpy .float64 ]:
666
- self .population [sol_idx , gene_idx ] = self .gene_space [gene_idx ]
666
+ self .population [sol_idx , gene_idx ] = self .gene_space [gene_idx ]. copy ()
667
667
else :
668
668
# Replace all the None values with random values using the init_range_low, init_range_high, and gene_type attributes.
669
669
for idx , curr_gene_space in enumerate (self .gene_space ):
@@ -727,6 +727,7 @@ def run(self):
727
727
# Appending the best solution to the best_solutions list.
728
728
if self .save_best_solutions :
729
729
self .best_solutions .append (best_solution )
730
+ print ("AAAAAAAAA " , self .best_solutions )
730
731
731
732
# Selecting the best parents in the population for mating.
732
733
parents = self .select_parents (fitness , num_parents = self .num_parents_mating )
@@ -809,7 +810,7 @@ def steady_state_selection(self, fitness, num_parents):
809
810
# Selecting the best individuals in the current generation as parents for producing the offspring of the next generation.
810
811
parents = numpy .empty ((num_parents , self .population .shape [1 ]))
811
812
for parent_num in range (num_parents ):
812
- parents [parent_num , :] = self .population [fitness_sorted [parent_num ], :]
813
+ parents [parent_num , :] = self .population [fitness_sorted [parent_num ], :]. copy ()
813
814
return parents
814
815
815
816
def rank_selection (self , fitness , num_parents ):
@@ -827,7 +828,7 @@ def rank_selection(self, fitness, num_parents):
827
828
# Selecting the best individuals in the current generation as parents for producing the offspring of the next generation.
828
829
parents = numpy .empty ((num_parents , self .population .shape [1 ]))
829
830
for parent_num in range (num_parents ):
830
- parents [parent_num , :] = self .population [fitness_sorted [parent_num ], :]
831
+ parents [parent_num , :] = self .population [fitness_sorted [parent_num ], :]. copy ()
831
832
return parents
832
833
833
834
def random_selection (self , fitness , num_parents ):
@@ -845,7 +846,7 @@ def random_selection(self, fitness, num_parents):
845
846
rand_indices = numpy .random .randint (low = 0.0 , high = fitness .shape [0 ], size = num_parents )
846
847
847
848
for parent_num in range (num_parents ):
848
- parents [parent_num , :] = self .population [rand_indices [parent_num ], :]
849
+ parents [parent_num , :] = self .population [rand_indices [parent_num ], :]. copy ()
849
850
return parents
850
851
851
852
def tournament_selection (self , fitness , num_parents ):
@@ -863,7 +864,7 @@ def tournament_selection(self, fitness, num_parents):
863
864
rand_indices = numpy .random .randint (low = 0.0 , high = len (fitness ), size = self .K_tournament )
864
865
K_fitnesses = fitness [rand_indices ]
865
866
selected_parent_idx = numpy .where (K_fitnesses == numpy .max (K_fitnesses ))[0 ][0 ]
866
- parents [parent_num , :] = self .population [rand_indices [selected_parent_idx ], :]
867
+ parents [parent_num , :] = self .population [rand_indices [selected_parent_idx ], :]. copy ()
867
868
return parents
868
869
869
870
def roulette_wheel_selection (self , fitness , num_parents ):
@@ -897,7 +898,7 @@ def roulette_wheel_selection(self, fitness, num_parents):
897
898
rand_prob = numpy .random .rand ()
898
899
for idx in range (probs .shape [0 ]):
899
900
if (rand_prob >= probs_start [idx ] and rand_prob < probs_end [idx ]):
900
- parents [parent_num , :] = self .population [idx , :]
901
+ parents [parent_num , :] = self .population [idx , :]. copy ()
901
902
break
902
903
return parents
903
904
@@ -935,7 +936,7 @@ def stochastic_universal_selection(self, fitness, num_parents):
935
936
rand_pointer = first_pointer + parent_num * pointers_distance
936
937
for idx in range (probs .shape [0 ]):
937
938
if (rand_pointer >= probs_start [idx ] and rand_pointer < probs_end [idx ]):
938
- parents [parent_num , :] = self .population [idx , :]
939
+ parents [parent_num , :] = self .population [idx , :]. copy ()
939
940
break
940
941
return parents
941
942
@@ -1161,7 +1162,7 @@ def mutation_by_space(self, offspring):
1161
1162
1162
1163
if self .gene_space_nested :
1163
1164
# Returning the current gene space from the 'gene_space' attribute.
1164
- curr_gene_space = self .gene_space [gene_idx ]
1165
+ curr_gene_space = self .gene_space [gene_idx ]. copy ()
1165
1166
1166
1167
# If the gene space has only a single value, use it as the new gene value.
1167
1168
if type (curr_gene_space ) in [int , float , numpy .int , numpy .int8 , numpy .int16 , numpy .int32 , numpy .int64 , numpy .uint , numpy .uint8 , numpy .uint16 , numpy .uint32 , numpy .uint64 , numpy .float , numpy .float16 , numpy .float32 , numpy .float64 ]:
@@ -1203,7 +1204,7 @@ def mutation_probs_by_space(self, offspring):
1203
1204
if probs [gene_idx ] <= self .mutation_probability :
1204
1205
if self .gene_space_nested :
1205
1206
# Returning the current gene space from the 'gene_space' attribute.
1206
- curr_gene_space = self .gene_space [gene_idx ]
1207
+ curr_gene_space = self .gene_space [gene_idx ]. copy ()
1207
1208
1208
1209
# If the gene space has only a single value, use it as the new gene value.
1209
1210
if type (curr_gene_space ) in [int , float , numpy .int , numpy .int8 , numpy .int16 , numpy .int32 , numpy .int64 , numpy .uint , numpy .uint8 , numpy .uint16 , numpy .uint32 , numpy .uint64 , numpy .float , numpy .float16 , numpy .float32 , numpy .float64 ]:
@@ -1422,7 +1423,7 @@ def adaptive_mutation_by_space(self, offspring):
1422
1423
1423
1424
if self .gene_space_nested :
1424
1425
# Returning the current gene space from the 'gene_space' attribute.
1425
- curr_gene_space = self .gene_space [gene_idx ]
1426
+ curr_gene_space = self .gene_space [gene_idx ]. copy ()
1426
1427
1427
1428
# If the gene space has only a single value, use it as the new gene value.
1428
1429
if type (curr_gene_space ) in [int , float , numpy .int , numpy .int8 , numpy .int16 , numpy .int32 , numpy .int64 , numpy .uint , numpy .uint8 , numpy .uint16 , numpy .uint32 , numpy .uint64 , numpy .float , numpy .float16 , numpy .float32 , numpy .float64 ]:
@@ -1510,7 +1511,7 @@ def adaptive_mutation_probs_by_space(self, offspring):
1510
1511
if probs [gene_idx ] <= adaptive_mutation_probability :
1511
1512
if self .gene_space_nested :
1512
1513
# Returning the current gene space from the 'gene_space' attribute.
1513
- curr_gene_space = self .gene_space [gene_idx ]
1514
+ curr_gene_space = self .gene_space [gene_idx ]. copy ()
1514
1515
1515
1516
# If the gene space has only a single value, use it as the new gene value.
1516
1517
if type (curr_gene_space ) in [int , float , numpy .int , numpy .int8 , numpy .int16 , numpy .int32 , numpy .int64 , numpy .uint , numpy .uint8 , numpy .uint16 , numpy .uint32 , numpy .uint64 , numpy .float , numpy .float16 , numpy .float32 , numpy .float64 ]:
@@ -1597,7 +1598,7 @@ def best_solution(self, pop_fitness=None):
1597
1598
# Then return the index of that solution corresponding to the best fitness.
1598
1599
best_match_idx = numpy .where (pop_fitness == numpy .max (pop_fitness ))[0 ][0 ]
1599
1600
1600
- best_solution = self .population [best_match_idx , :]
1601
+ best_solution = self .population [best_match_idx , :]. copy ()
1601
1602
best_solution_fitness = pop_fitness [best_match_idx ]
1602
1603
1603
1604
return best_solution , best_solution_fitness , best_match_idx
0 commit comments