Skip to content

Commit 9049228

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 84b29c0 commit 9049228

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

genetic_algorithm/genetic_algorithm_optimization.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def select_parents(
122122

123123
if not population_score:
124124
raise ValueError("Population score is empty, cannot select parents.")
125-
125+
126126
population_score.sort(key=lambda score_tuple: score_tuple[1], reverse=True)
127127
selected_count = min(N_SELECTED, len(population_score))
128128
return [ind for ind, _ in population_score[:selected_count]]
@@ -245,32 +245,37 @@ def evolve(self, verbose: bool = True) -> np.ndarray:
245245
for generation in range(self.generations):
246246
# Evaluate population fitness (multithreaded)
247247
population_score = self.evaluate_population()
248-
248+
249249
# Ensure population_score isn't empty
250250
if not population_score:
251251
raise ValueError("Population score is empty. No individuals evaluated.")
252-
252+
253253
# Check the best individual
254-
best_individual = max(population_score, key=lambda score_tuple: score_tuple[1])[0]
254+
best_individual = max(
255+
population_score, key=lambda score_tuple: score_tuple[1]
256+
)[0]
255257
best_fitness = self.fitness(best_individual)
256-
258+
257259
# Select parents for next generation
258260
parents = self.select_parents(population_score)
259261
next_generation = []
260-
262+
261263
# Generate offspring using crossover and mutation
262264
for i in range(0, len(parents), 2):
263-
parent1, parent2 = parents[i], parents[(i + 1) % len(parents)] # Wrap around for odd cases
265+
parent1, parent2 = (
266+
parents[i],
267+
parents[(i + 1) % len(parents)],
268+
) # Wrap around for odd cases
264269
child1, child2 = self.crossover(parent1, parent2)
265270
next_generation.append(self.mutate(child1))
266271
next_generation.append(self.mutate(child2))
267-
272+
268273
# Ensure population size remains the same
269274
self.population = next_generation[: self.population_size]
270-
275+
271276
if verbose and generation % 10 == 0:
272277
print(f"Generation {generation}: Best Fitness = {best_fitness}")
273-
278+
274279
return best_individual
275280

276281

0 commit comments

Comments
 (0)