Skip to content

Commit 26c8127

Browse files
code quality improvements
1 parent ea7a69d commit 26c8127

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

kernel_tuner/strategies/diff_evo.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ def random_draw(idxs, mutate, best):
9999
return np.random.choice(idxs, draw, replace=draw >= len(idxs))
100100

101101

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 """
103104
if constraint_aware:
104105
samples = LatinHypercube(len(tune_params)).integers(l_bounds=0, u_bounds=max_idx, n=popsize, endpoint=True)
105106
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,
133134
bounds = np.array(bounds)
134135

135136
# 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)
137138

138139
# Override with user-specified starting position
139140
population[0] = cost_func.get_start_pos()
@@ -160,7 +161,7 @@ def differential_evolution(searchspace, cost_func, bounds, popsize, maxiter, F,
160161

161162
# If for two generations there has been no change, generate a new population
162163
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))
164165

165166
# Iterate over each individual in the population
166167
i = 0
@@ -189,19 +190,20 @@ def differential_evolution(searchspace, cost_func, bounds, popsize, maxiter, F,
189190
if constraint_aware:
190191
trial_vector = repair(trial_vector, searchspace)
191192

192-
# Store for selection
193+
# Store for selection, if not in trial_population already
193194
if list(trial_vector) not in trial_population:
194195
trial_population.append(list(trial_vector))
195196
i += 1
196197
stuck = 0
197198
else:
198199
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
205207

206208

207209
# --- c. Selection ---

0 commit comments

Comments
 (0)