Skip to content

Commit 24df5d1

Browse files
authored
Update genetic_algorithm_optimization.py
1 parent 21bcad9 commit 24df5d1

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

genetic_algorithm/genetic_algorithm_optimization.py

+11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import random
33
from concurrent.futures import ThreadPoolExecutor
44

5+
56
# Parameters
67
N_POPULATION = 100 # Population size
78
N_GENERATIONS = 500 # Maximum number of generations
@@ -10,6 +11,7 @@
1011
CROSSOVER_RATE = 0.8 # Probability of crossover
1112
SEARCH_SPACE = (-10, 10) # Search space for the variables
1213

14+
1315
# Random number generator
1416
rng = np.random.default_rng()
1517

@@ -25,6 +27,7 @@ def __init__(
2527
crossover_rate,
2628
maximize=True,
2729
):
30+
2831
self.function = function # Target function to optimize
2932
self.bounds = bounds # Search space bounds (for each variable)
3033
self.population_size = population_size
@@ -44,16 +47,19 @@ def initialize_population(self):
4447
for i in range(self.population_size)
4548
]
4649

50+
4751
def fitness(self, individual):
4852
# Calculate the fitness value (function value)
4953
value = self.function(*individual)
5054
return value if self.maximize else -value # If minimizing, invert the fitness
5155

56+
5257
def select_parents(self, population_score):
5358
# Select top N_SELECTED parents based on fitness
5459
population_score.sort(key=lambda x: x[1], reverse=True)
5560
return [ind for ind, _ in population_score[:N_SELECTED]]
5661

62+
5763
def crossover(self, parent1, parent2):
5864
# Perform uniform crossover
5965
if random.random() < self.crossover_rate:
@@ -63,20 +69,23 @@ def crossover(self, parent1, parent2):
6369
return child1, child2
6470
return parent1, parent2
6571

72+
6673
def mutate(self, individual):
6774
# Apply mutation to an individual using the new random generator
6875
for i in range(self.dim):
6976
if random.random() < self.mutation_prob:
7077
individual[i] = rng.uniform(self.bounds[i][0], self.bounds[i][1])
7178
return individual
7279

80+
7381
def evaluate_population(self):
7482
# Multithreaded evaluation of population fitness
7583
with ThreadPoolExecutor() as executor:
7684
return list(
7785
executor.map(lambda ind: (ind, self.fitness(ind)), self.population)
7886
)
7987

88+
8089
def evolve(self):
8190
for generation in range(self.generations):
8291
# Evaluate population fitness (multithreaded)
@@ -114,6 +123,7 @@ def target_function(x, y):
114123
# Set bounds for the variables (x, y)
115124
bounds = [(-10, 10), (-10, 10)] # Both x and y range from -10 to 10
116125

126+
117127
# Instantiate and run the genetic algorithm
118128
ga = GeneticAlgorithm(
119129
function=target_function,
@@ -125,6 +135,7 @@ def target_function(x, y):
125135
maximize=False, # Minimize the function
126136
)
127137

138+
128139
best_solution = ga.evolve()
129140
print(f"Best solution found: {best_solution}")
130141
print(f"Best fitness (minimum value of function): {target_function(*best_solution)}")

0 commit comments

Comments
 (0)