1
1
import numpy as np
2
2
3
+
3
4
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
+ ):
5
14
"""
6
15
Initialize the Genetic Algorithm optimizer.
7
16
@@ -24,7 +33,11 @@ def initialize_population(self):
24
33
"""
25
34
Initialize a population of random solutions within the bounds.
26
35
"""
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
+ )
28
41
29
42
def fitness (self , individual ):
30
43
"""
@@ -37,7 +50,9 @@ def select_parents(self, population, fitness_scores):
37
50
"""
38
51
Select parents using tournament selection.
39
52
"""
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
+ )
41
56
return population [selected_indices [np .argmin (fitness_scores [selected_indices ])]]
42
57
43
58
def crossover (self , parent1 , parent2 ):
@@ -47,9 +62,11 @@ def crossover(self, parent1, parent2):
47
62
"""
48
63
if self .num_variables == 1 :
49
64
return parent1 , parent2 # No crossover needed for single-variable functions
50
-
65
+
51
66
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
53
70
child1 = np .concatenate ((parent1 [:point ], parent2 [point :]))
54
71
child2 = np .concatenate ((parent2 [:point ], parent1 [point :]))
55
72
return child1 , child2
@@ -61,7 +78,9 @@ def mutate(self, individual):
61
78
"""
62
79
if np .random .rand () < self .mutation_prob :
63
80
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
+ )
65
84
return individual
66
85
67
86
def evolve (self ):
@@ -70,10 +89,12 @@ def evolve(self):
70
89
"""
71
90
population = self .initialize_population ()
72
91
best_solution = None
73
- best_fitness = float (' inf' )
92
+ best_fitness = float (" inf" )
74
93
75
94
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
+ )
77
98
78
99
new_population = []
79
100
for _ in range (self .population_size // 2 ):
0 commit comments