Skip to content

Commit 7f8befa

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

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

genetic_algorithm/genetic_algorithm_optimization.py

+29-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
import numpy as np
22

3+
34
class GeneticAlgorithmOptimizer:
4-
def __init__(self, func, bounds, population_size=100, generations=500, crossover_prob=0.9, mutation_prob=0.01):
5+
def __init__(
6+
self,
7+
func,
8+
bounds,
9+
population_size=100,
10+
generations=500,
11+
crossover_prob=0.9,
12+
mutation_prob=0.01,
13+
):
514
"""
615
Initialize the Genetic Algorithm optimizer.
716
@@ -24,7 +33,11 @@ def initialize_population(self):
2433
"""
2534
Initialize a population of random solutions within the bounds.
2635
"""
27-
return np.random.uniform(low=self.bounds[:, 0], high=self.bounds[:, 1], size=(self.population_size, self.num_variables))
36+
return np.random.uniform(
37+
low=self.bounds[:, 0],
38+
high=self.bounds[:, 1],
39+
size=(self.population_size, self.num_variables),
40+
)
2841

2942
def fitness(self, individual):
3043
"""
@@ -37,7 +50,9 @@ def select_parents(self, population, fitness_scores):
3750
"""
3851
Select parents using tournament selection.
3952
"""
40-
selected_indices = np.random.choice(range(self.population_size), size=2, replace=False)
53+
selected_indices = np.random.choice(
54+
range(self.population_size), size=2, replace=False
55+
)
4156
return population[selected_indices[np.argmin(fitness_scores[selected_indices])]]
4257

4358
def crossover(self, parent1, parent2):
@@ -47,9 +62,11 @@ def crossover(self, parent1, parent2):
4762
"""
4863
if self.num_variables == 1:
4964
return parent1, parent2 # No crossover needed for single-variable functions
50-
65+
5166
if np.random.rand() < self.crossover_prob:
52-
point = np.random.randint(1, self.num_variables) # Updated to handle the edge case
67+
point = np.random.randint(
68+
1, self.num_variables
69+
) # Updated to handle the edge case
5370
child1 = np.concatenate((parent1[:point], parent2[point:]))
5471
child2 = np.concatenate((parent2[:point], parent1[point:]))
5572
return child1, child2
@@ -61,7 +78,9 @@ def mutate(self, individual):
6178
"""
6279
if np.random.rand() < self.mutation_prob:
6380
index = np.random.randint(0, self.num_variables)
64-
individual[index] = np.random.uniform(self.bounds[index, 0], self.bounds[index, 1])
81+
individual[index] = np.random.uniform(
82+
self.bounds[index, 0], self.bounds[index, 1]
83+
)
6584
return individual
6685

6786
def evolve(self):
@@ -70,10 +89,12 @@ def evolve(self):
7089
"""
7190
population = self.initialize_population()
7291
best_solution = None
73-
best_fitness = float('inf')
92+
best_fitness = float("inf")
7493

7594
for gen in range(self.generations):
76-
fitness_scores = np.array([self.fitness(individual) for individual in population])
95+
fitness_scores = np.array(
96+
[self.fitness(individual) for individual in population]
97+
)
7798

7899
new_population = []
79100
for _ in range(self.population_size // 2):

0 commit comments

Comments
 (0)