Skip to content

Commit 40e4e3c

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 1873f2f commit 40e4e3c

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

genetic_algorithm/genetic_algorithm_optimization.py

+23-15
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import numpy as np
22
from typing import Callable, List, Tuple
33

4+
45
class GeneticAlgorithmOptimizer:
56
def __init__(
67
self,
7-
objective_function: Callable[..., float],
8-
variable_bounds: List[Tuple[float, float]],
8+
objective_function: Callable[..., float],
9+
variable_bounds: List[Tuple[float, float]],
910
population_size: int = 100,
1011
max_generations: int = 500,
1112
crossover_probability: float = 0.9,
@@ -27,7 +28,7 @@ def generate_initial_population(self) -> np.ndarray:
2728
Generate a population of random solutions within the given variable bounds.
2829
2930
>>> optimizer = GeneticAlgorithmOptimizer(
30-
... objective_function=lambda x: x**2,
31+
... objective_function=lambda x: x**2,
3132
... variable_bounds=[(-10, 10)]
3233
... )
3334
>>> population = optimizer.generate_initial_population()
@@ -45,7 +46,7 @@ def evaluate_fitness(self, individual: List[float]) -> float:
4546
Evaluate the fitness of an individual by computing the value of the objective function.
4647
4748
>>> optimizer = GeneticAlgorithmOptimizer(
48-
... objective_function=lambda x: x**2,
49+
... objective_function=lambda x: x**2,
4950
... variable_bounds=[(-10, 10)]
5051
... )
5152
>>> optimizer.evaluate_fitness([2])
@@ -62,7 +63,7 @@ def select_parent(
6263
Select a parent using tournament selection based on fitness values.
6364
6465
>>> optimizer = GeneticAlgorithmOptimizer(
65-
... objective_function=lambda x: x**2,
66+
... objective_function=lambda x: x**2,
6667
... variable_bounds=[(-10, 10)]
6768
... )
6869
>>> population = optimizer.generate_initial_population()
@@ -80,11 +81,11 @@ def perform_crossover(
8081
self, parent1: np.ndarray, parent2: np.ndarray
8182
) -> Tuple[np.ndarray, np.ndarray]:
8283
"""
83-
Perform one-point crossover between two parents to create offspring.
84+
Perform one-point crossover between two parents to create offspring.
8485
Skip crossover for single-variable functions.
8586
8687
>>> optimizer = GeneticAlgorithmOptimizer(
87-
... objective_function=lambda x: x**2,
88+
... objective_function=lambda x: x**2,
8889
... variable_bounds=[(-10, 10)]
8990
... )
9091
>>> parent1 = [1]
@@ -98,8 +99,12 @@ def perform_crossover(
9899

99100
if self.rng.random() < self.crossover_probability:
100101
crossover_point = self.rng.integers(1, self.num_variables)
101-
child1 = np.concatenate((parent1[:crossover_point], parent2[crossover_point:]))
102-
child2 = np.concatenate((parent2[:crossover_point], parent1[crossover_point:]))
102+
child1 = np.concatenate(
103+
(parent1[:crossover_point], parent2[crossover_point:])
104+
)
105+
child2 = np.concatenate(
106+
(parent2[:crossover_point], parent1[crossover_point:])
107+
)
103108
return child1, child2
104109
return parent1, parent2
105110

@@ -108,7 +113,7 @@ def apply_mutation(self, individual: np.ndarray) -> np.ndarray:
108113
Apply mutation to an individual based on the mutation probability.
109114
110115
>>> optimizer = GeneticAlgorithmOptimizer(
111-
... objective_function=lambda x: x**2,
116+
... objective_function=lambda x: x**2,
112117
... variable_bounds=[(-10, 10)]
113118
... )
114119
>>> individual = [1]
@@ -119,8 +124,8 @@ def apply_mutation(self, individual: np.ndarray) -> np.ndarray:
119124
if self.rng.random() < self.mutation_probability:
120125
mutation_index = self.rng.integers(0, self.num_variables)
121126
individual[mutation_index] = self.rng.uniform(
122-
self.variable_bounds[mutation_index, 0],
123-
self.variable_bounds[mutation_index, 1]
127+
self.variable_bounds[mutation_index, 0],
128+
self.variable_bounds[mutation_index, 1],
124129
)
125130
return individual
126131

@@ -154,11 +159,15 @@ def optimize(self) -> Tuple[np.ndarray, float]:
154159
best_fitness_value = fitness_values[min_fitness_index]
155160
best_solution = population[min_fitness_index]
156161

157-
print(f"Generation {generation + 1}, Best Fitness Value: {best_fitness_value}")
162+
print(
163+
f"Generation {generation + 1}, Best Fitness Value: {best_fitness_value}"
164+
)
158165

159166
return best_solution, best_fitness_value
160167

168+
161169
if __name__ == "__main__":
170+
162171
def objective_function(x: float, y: float) -> float:
163172
"""
164173
Example objective function to minimize x^2 + y^2
@@ -175,8 +184,7 @@ def objective_function(x: float, y: float) -> float:
175184
variable_bounds: List[Tuple[float, float]] = [(-10, 10), (-10, 10)]
176185

177186
optimizer = GeneticAlgorithmOptimizer(
178-
objective_function=objective_function,
179-
variable_bounds=variable_bounds
187+
objective_function=objective_function, variable_bounds=variable_bounds
180188
)
181189
best_solution, best_fitness_value = optimizer.optimize()
182190

0 commit comments

Comments
 (0)