mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-21 08:42:03 +00:00
Compare commits
3 Commits
d62f39f647
...
9049228ff8
Author | SHA1 | Date | |
---|---|---|---|
|
9049228ff8 | ||
|
84b29c0eed | ||
|
dbd29aed76 |
|
@ -1,6 +1,7 @@
|
||||||
import random
|
import random
|
||||||
from collections.abc import Callable, Sequence
|
from collections.abc import Callable, Sequence
|
||||||
from concurrent.futures import ThreadPoolExecutor
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
# Parameters
|
# Parameters
|
||||||
|
@ -59,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)
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -82,10 +80,10 @@ class GeneticAlgorithm:
|
||||||
... )
|
... )
|
||||||
>>> individual = np.array([1.0, 2.0])
|
>>> individual = np.array([1.0, 2.0])
|
||||||
>>> ga.fitness(individual)
|
>>> ga.fitness(individual)
|
||||||
5.0 # The fitness should be 1^2 + 2^2 = 5
|
-5.0 # The fitness should be -1^2 + 2^2 = 5 for minimizing
|
||||||
>>> ga.maximize = True
|
>>> ga.maximize = True
|
||||||
>>> ga.fitness(individual)
|
>>> ga.fitness(individual)
|
||||||
-5.0 # The fitness should be -5 when maximizing
|
5.0 # The fitness should be 1^2 + 2^2 = 5 when maximizing
|
||||||
"""
|
"""
|
||||||
value = float(self.function(*individual)) # Ensure fitness is a float
|
value = float(self.function(*individual)) # Ensure fitness is a float
|
||||||
return value if self.maximize else -value # If minimizing, invert the fitness
|
return value if self.maximize else -value # If minimizing, invert the fitness
|
||||||
|
@ -114,11 +112,17 @@ class GeneticAlgorithm:
|
||||||
>>> selected_parents = ga.select_parents(population_score)
|
>>> selected_parents = ga.select_parents(population_score)
|
||||||
>>> len(selected_parents)
|
>>> len(selected_parents)
|
||||||
2 # Should select the two parents with the best fitness scores.
|
2 # Should select the two parents with the best fitness scores.
|
||||||
>>> np.array_equal(selected_parents[0], np.array([1.0, 2.0])) # Parent 1 should be [1.0, 2.0]
|
>>> np.array_equal(selected_parents[0], np.array([1.0, 2.0]))
|
||||||
|
# Parent 1 should be [1.0, 2.0]
|
||||||
True
|
True
|
||||||
>>> np.array_equal(selected_parents[1], np.array([-1.0, -2.0])) # Parent 2 should be [-1.0, -2.0]
|
>>> np.array_equal(selected_parents[1], np.array([-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]]
|
||||||
|
@ -237,10 +241,15 @@ class GeneticAlgorithm:
|
||||||
>>> isinstance(best_solution[1], float) # Second element should be a float
|
>>> isinstance(best_solution[1], float) # Second element should be a float
|
||||||
True
|
True
|
||||||
"""
|
"""
|
||||||
|
best_individual = None
|
||||||
for generation in range(self.generations):
|
for generation in range(self.generations):
|
||||||
# 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]
|
population_score, key=lambda score_tuple: score_tuple[1]
|
||||||
|
@ -253,7 +262,10 @@ 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…
Reference in New Issue
Block a user