mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-03-01 20:38:40 +00:00
Update genetic_algorithm_optimization.py
This commit is contained in:
parent
dbd29aed76
commit
84b29c0eed
@ -60,10 +60,7 @@ class GeneticAlgorithm:
|
|||||||
True
|
True
|
||||||
"""
|
"""
|
||||||
return [
|
return [
|
||||||
rng.uniform(
|
np.array([rng.uniform(b[0], b[1]) for b in self.bounds])
|
||||||
low=[self.bounds[j][0] for j in range(self.dim)],
|
|
||||||
high=[self.bounds[j][1] for j in range(self.dim)],
|
|
||||||
)
|
|
||||||
for _ in range(self.population_size)
|
for _ in range(self.population_size)
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -122,6 +119,10 @@ class GeneticAlgorithm:
|
|||||||
# Parent 2 should be [-1.0, -2.0]
|
# Parent 2 should be [-1.0, -2.0]
|
||||||
True
|
True
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
if not population_score:
|
||||||
|
raise ValueError("Population score is empty, cannot select parents.")
|
||||||
|
|
||||||
population_score.sort(key=lambda score_tuple: score_tuple[1], reverse=True)
|
population_score.sort(key=lambda score_tuple: score_tuple[1], reverse=True)
|
||||||
selected_count = min(N_SELECTED, len(population_score))
|
selected_count = min(N_SELECTED, len(population_score))
|
||||||
return [ind for ind, _ in population_score[:selected_count]]
|
return [ind for ind, _ in population_score[:selected_count]]
|
||||||
@ -245,10 +246,12 @@ class GeneticAlgorithm:
|
|||||||
# Evaluate population fitness (multithreaded)
|
# Evaluate population fitness (multithreaded)
|
||||||
population_score = self.evaluate_population()
|
population_score = self.evaluate_population()
|
||||||
|
|
||||||
|
# Ensure population_score isn't empty
|
||||||
|
if not population_score:
|
||||||
|
raise ValueError("Population score is empty. No individuals evaluated.")
|
||||||
|
|
||||||
# Check the best individual
|
# Check the best individual
|
||||||
best_individual = max(
|
best_individual = max(population_score, key=lambda score_tuple: score_tuple[1])[0]
|
||||||
population_score, key=lambda score_tuple: score_tuple[1]
|
|
||||||
)[0]
|
|
||||||
best_fitness = self.fitness(best_individual)
|
best_fitness = self.fitness(best_individual)
|
||||||
|
|
||||||
# Select parents for next generation
|
# Select parents for next generation
|
||||||
@ -257,7 +260,7 @@ class GeneticAlgorithm:
|
|||||||
|
|
||||||
# Generate offspring using crossover and mutation
|
# Generate offspring using crossover and mutation
|
||||||
for i in range(0, len(parents), 2):
|
for i in range(0, len(parents), 2):
|
||||||
parent1, parent2 = parents[i], parents[(i + 1) % len(parents)]
|
parent1, parent2 = parents[i], parents[(i + 1) % len(parents)] # Wrap around for odd cases
|
||||||
child1, child2 = self.crossover(parent1, parent2)
|
child1, child2 = self.crossover(parent1, parent2)
|
||||||
next_generation.append(self.mutate(child1))
|
next_generation.append(self.mutate(child1))
|
||||||
next_generation.append(self.mutate(child2))
|
next_generation.append(self.mutate(child2))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user