13
13
# Random number generator
14
14
rng = np .random .default_rng ()
15
15
16
+
16
17
class GeneticAlgorithm :
17
18
def __init__ (
18
19
self ,
@@ -72,7 +73,9 @@ def mutate(self, individual):
72
73
def evaluate_population (self ):
73
74
# Multithreaded evaluation of population fitness
74
75
with ThreadPoolExecutor () as executor :
75
- return list (executor .map (lambda ind : (ind , self .fitness (ind )), self .population ))
76
+ return list (
77
+ executor .map (lambda ind : (ind , self .fitness (ind )), self .population )
78
+ )
76
79
77
80
def evolve (self ):
78
81
for generation in range (self .generations ):
@@ -95,7 +98,7 @@ def evolve(self):
95
98
next_generation .append (self .mutate (child2 ))
96
99
97
100
# Ensure population size remains the same
98
- self .population = next_generation [:self .population_size ]
101
+ self .population = next_generation [: self .population_size ]
99
102
100
103
if generation % 10 == 0 :
101
104
print (f"Generation { generation } : Best Fitness = { best_fitness } " )
@@ -119,7 +122,7 @@ def target_function(x, y):
119
122
generations = N_GENERATIONS ,
120
123
mutation_prob = MUTATION_PROBABILITY ,
121
124
crossover_rate = CROSSOVER_RATE ,
122
- maximize = False # Minimize the function
125
+ maximize = False , # Minimize the function
123
126
)
124
127
125
128
best_solution = ga .evolve ()
0 commit comments