@@ -29,15 +29,11 @@ def steady_state_selection(self, fitness, num_parents):
2929 fitness_sorted = self .sort_solutions_nsga2 (fitness = fitness )
3030
3131 # Selecting the best individuals in the current generation as parents for producing the offspring of the next generation.
32- if self .gene_type_single == True :
33- parents = numpy .empty ((num_parents , self .population .shape [1 ]), dtype = self .gene_type [0 ])
34- else :
35- parents = numpy .empty ((num_parents , self .population .shape [1 ]), dtype = object )
32+ parents = self .initialize_parents_array ((num_parents , self .population .shape [1 ]))
33+ parents_indices = numpy .array (fitness_sorted [:num_parents ])
34+ parents [:, :] = self .population [parents_indices , :].copy ()
3635
37- for parent_num in range (num_parents ):
38- parents [parent_num , :] = self .population [fitness_sorted [parent_num ], :].copy ()
39-
40- return parents , numpy .array (fitness_sorted [:num_parents ])
36+ return parents , parents_indices
4137
4238 def rank_selection (self , fitness , num_parents ):
4339
@@ -91,15 +87,9 @@ def random_selection(self, fitness, num_parents):
9187 -The indices of the selected solutions.
9288 """
9389
94- if self .gene_type_single == True :
95- parents = numpy .empty ((num_parents , self .population .shape [1 ]), dtype = self .gene_type [0 ])
96- else :
97- parents = numpy .empty ((num_parents , self .population .shape [1 ]), dtype = object )
98-
90+ parents = self .initialize_parents_array ((num_parents , self .population .shape [1 ]))
9991 rand_indices = numpy .random .randint (low = 0.0 , high = fitness .shape [0 ], size = num_parents )
100-
101- for parent_num in range (num_parents ):
102- parents [parent_num , :] = self .population [rand_indices [parent_num ], :].copy ()
92+ parents [:, :] = self .population [rand_indices , :].copy ()
10393
10494 return parents , rand_indices
10595
@@ -119,11 +109,7 @@ def tournament_selection(self, fitness, num_parents):
119109 # This function works with both single- and multi-objective optimization problems.
120110 fitness_sorted = self .sort_solutions_nsga2 (fitness = fitness )
121111
122- if self .gene_type_single == True :
123- parents = numpy .empty ((num_parents , self .population .shape [1 ]), dtype = self .gene_type [0 ])
124- else :
125- parents = numpy .empty ((num_parents , self .population .shape [1 ]), dtype = object )
126-
112+ parents = self .initialize_parents_array ((num_parents , self .population .shape [1 ]))
127113 parents_indices = []
128114
129115 for parent_num in range (num_parents ):
@@ -137,10 +123,11 @@ def tournament_selection(self, fitness, num_parents):
137123
138124 # Append the index of the selected parent.
139125 parents_indices .append (rand_indices [selected_parent_idx ])
140- # Insert the selected parent.
141- parents [parent_num , :] = self .population [rand_indices [selected_parent_idx ], :].copy ()
142126
143- return parents , numpy .array (parents_indices )
127+ parents_indices = numpy .array (parents_indices )
128+ parents [:, :] = self .population [parents_indices , :].copy ()
129+
130+ return parents , parents_indices
144131
145132 def roulette_wheel_selection (self , fitness , num_parents ):
146133
@@ -186,11 +173,13 @@ def roulette_wheel_selection(self, fitness, num_parents):
186173 rand_prob = numpy .random .rand ()
187174 for idx in range (probs .shape [0 ]):
188175 if (rand_prob >= probs_start [idx ] and rand_prob < probs_end [idx ]):
189- parents [parent_num , :] = self .population [idx , :].copy ()
190176 parents_indices .append (idx )
191177 break
192178
193- return parents , numpy .array (parents_indices )
179+ parents_indices = numpy .array (parents_indices )
180+ parents [:, :] = self .population [parents_indices , :].copy ()
181+
182+ return parents , parents_indices
194183
195184 def wheel_cumulative_probs (self , probs , num_parents ):
196185 """
@@ -220,10 +209,7 @@ def wheel_cumulative_probs(self, probs, num_parents):
220209 probs [min_probs_idx ] = float ('inf' )
221210
222211 # Selecting the best individuals in the current generation as parents for producing the offspring of the next generation.
223- if self .gene_type_single == True :
224- parents = numpy .empty ((num_parents , self .population .shape [1 ]), dtype = self .gene_type [0 ])
225- else :
226- parents = numpy .empty ((num_parents , self .population .shape [1 ]), dtype = object )
212+ parents = self .initialize_parents_array ((num_parents , self .population .shape [1 ]))
227213
228214 return probs_start , probs_end , parents
229215
@@ -281,22 +267,21 @@ def stochastic_universal_selection(self, fitness, num_parents):
281267 size = 1 )[0 ] # Location of the first pointer.
282268
283269 # Selecting the best individuals in the current generation as parents for producing the offspring of the next generation.
284- if self .gene_type_single == True :
285- parents = numpy .empty ((num_parents , self .population .shape [1 ]), dtype = self .gene_type [0 ])
286- else :
287- parents = numpy .empty ((num_parents , self .population .shape [1 ]), dtype = object )
270+ parents = self .initialize_parents_array ((num_parents , self .population .shape [1 ]))
288271
289272 parents_indices = []
290273
291274 for parent_num in range (num_parents ):
292275 rand_pointer = first_pointer + parent_num * pointers_distance
293276 for idx in range (probs .shape [0 ]):
294277 if (rand_pointer >= probs_start [idx ] and rand_pointer < probs_end [idx ]):
295- parents [parent_num , :] = self .population [idx , :].copy ()
296278 parents_indices .append (idx )
297279 break
298280
299- return parents , numpy .array (parents_indices )
281+ parents_indices = numpy .array (parents_indices )
282+ parents [:, :] = self .population [parents_indices , :].copy ()
283+
284+ return parents , parents_indices
300285
301286 def tournament_selection_nsga2 (self ,
302287 fitness ,
0 commit comments