@@ -99,7 +99,8 @@ def random_draw(idxs, mutate, best):
99
99
return np .random .choice (idxs , draw , replace = draw >= len (idxs ))
100
100
101
101
102
- def generate_population (tune_params , min_idx , max_idx , popsize , searchspace , constraint_aware ):
102
+ def generate_population (tune_params , max_idx , popsize , searchspace , constraint_aware ):
103
+ """ Generate new population, returns Numpy array """
103
104
if constraint_aware :
104
105
samples = LatinHypercube (len (tune_params )).integers (l_bounds = 0 , u_bounds = max_idx , n = popsize , endpoint = True )
105
106
population = [indices_to_values (sample , tune_params ) for sample in samples ]
@@ -133,7 +134,7 @@ def differential_evolution(searchspace, cost_func, bounds, popsize, maxiter, F,
133
134
bounds = np .array (bounds )
134
135
135
136
# Initialize the population with random individuals within the bounds
136
- population = generate_population (tune_params , min_idx , max_idx , popsize , searchspace , constraint_aware )
137
+ population = generate_population (tune_params , max_idx , popsize , searchspace , constraint_aware )
137
138
138
139
# Override with user-specified starting position
139
140
population [0 ] = cost_func .get_start_pos ()
@@ -160,7 +161,7 @@ def differential_evolution(searchspace, cost_func, bounds, popsize, maxiter, F,
160
161
161
162
# If for two generations there has been no change, generate a new population
162
163
if stabilized > 2 :
163
- trial_population = list (generate_population (tune_params , min_idx , max_idx , popsize , searchspace , constraint_aware ))
164
+ trial_population = list (generate_population (tune_params , max_idx , popsize , searchspace , constraint_aware ))
164
165
165
166
# Iterate over each individual in the population
166
167
i = 0
@@ -189,19 +190,20 @@ def differential_evolution(searchspace, cost_func, bounds, popsize, maxiter, F,
189
190
if constraint_aware :
190
191
trial_vector = repair (trial_vector , searchspace )
191
192
192
- # Store for selection
193
+ # Store for selection, if not in trial_population already
193
194
if list (trial_vector ) not in trial_population :
194
195
trial_population .append (list (trial_vector ))
195
196
i += 1
196
197
stuck = 0
197
198
else :
198
199
stuck += 1
199
- if stuck >= 20 :
200
- if verbose :
201
- print (f"Differential Evolution got stuck generating new individuals, insert random sample" )
202
- trial_population .append (list (searchspace .get_random_sample (1 )[0 ]))
203
- i += 1
204
- stuck = 0
200
+
201
+ if stuck >= 20 :
202
+ if verbose :
203
+ print ("Differential Evolution got stuck generating new individuals, insert random sample" )
204
+ trial_population .append (list (searchspace .get_random_sample (1 )[0 ]))
205
+ i += 1
206
+ stuck = 0
205
207
206
208
207
209
# --- c. Selection ---
0 commit comments