13
13
MUTATION_PROBABILITY = 0.1
14
14
NUM_GENERATIONS = 50
15
15
16
+
16
17
def evaluate (func : Callable , params : List [float ]) -> float :
17
18
"""
18
19
Evaluate the fitness of an individual based on the provided function.
@@ -25,12 +26,14 @@ def evaluate(func: Callable, params: List[float]) -> float:
25
26
"""
26
27
return func (* params )
27
28
29
+
28
30
def crossover (parent_1 : List [float ], parent_2 : List [float ]) -> List [float ]:
29
31
"""Perform crossover between two parents."""
30
32
crossover_point = random .randint (1 , len (parent_1 ) - 1 )
31
33
child = parent_1 [:crossover_point ] + parent_2 [crossover_point :]
32
34
return child
33
35
36
+
34
37
def mutate (individual : List [float ], mutation_rate : float ) -> List [float ]:
35
38
"""Mutate an individual with a certain probability."""
36
39
mutated_individual = []
@@ -43,21 +46,37 @@ def mutate(individual: List[float], mutation_rate: float) -> List[float]:
43
46
mutated_individual .append (gene )
44
47
return mutated_individual
45
48
46
- def select (population : List [Tuple [List [float ], float ]], num_selected : int ) -> List [List [float ]]:
49
+
50
+ def select (
51
+ population : List [Tuple [List [float ], float ]], num_selected : int
52
+ ) -> List [List [float ]]:
47
53
"""Select individuals based on their fitness scores."""
48
54
sorted_population = sorted (population , key = lambda x : x [1 ])
49
- selected_parents = [individual [0 ] for individual in sorted_population [:num_selected ]]
55
+ selected_parents = [
56
+ individual [0 ] for individual in sorted_population [:num_selected ]
57
+ ]
50
58
return selected_parents
51
59
52
- def optimize (func : Callable , num_params : int , param_ranges : List [Tuple [float , float ]], optimization_goal : str ) -> Tuple [List [float ], float ]:
60
+
61
+ def optimize (
62
+ func : Callable ,
63
+ num_params : int ,
64
+ param_ranges : List [Tuple [float , float ]],
65
+ optimization_goal : str ,
66
+ ) -> Tuple [List [float ], float ]:
53
67
"""Optimize the given function using a genetic algorithm."""
54
68
# Initialize the population
55
- population = [[random .uniform (param_range [0 ], param_range [1 ]) for param_range in param_ranges ] for _ in range (N_POPULATION )]
69
+ population = [
70
+ [random .uniform (param_range [0 ], param_range [1 ]) for param_range in param_ranges ]
71
+ for _ in range (N_POPULATION )
72
+ ]
56
73
57
74
# Main optimization loop
58
75
for generation in range (NUM_GENERATIONS ):
59
76
# Evaluate the fitness of each individual in the population
60
- population_fitness = [(individual , evaluate (func , individual )) for individual in population ]
77
+ population_fitness = [
78
+ (individual , evaluate (func , individual )) for individual in population
79
+ ]
61
80
62
81
# Select parents for crossover
63
82
selected_parents = select (population_fitness , N_SELECTED )
@@ -79,16 +98,19 @@ def optimize(func: Callable, num_params: int, param_ranges: List[Tuple[float, fl
79
98
80
99
return best_individual , best_fitness
81
100
101
+
82
102
if __name__ == "__main__" :
83
103
# Set random seed for reproducibility
84
104
random .seed (123 )
85
105
86
106
# Example usage:
87
107
def quadratic_function (x , y ):
88
108
"""Example function to optimize."""
89
- return x ** 2 + y ** 2
109
+ return x ** 2 + y ** 2
90
110
91
111
param_ranges = [(- 10 , 10 ), (- 10 , 10 )]
92
- best_params , best_fitness = optimize (quadratic_function , 2 , param_ranges , "minimize" )
112
+ best_params , best_fitness = optimize (
113
+ quadratic_function , 2 , param_ranges , "minimize"
114
+ )
93
115
print ("Best parameters:" , best_params )
94
116
print ("Best fitness:" , best_fitness )
0 commit comments