1212CROSSOVER_RATE = 0.8 # Probability of crossover
1313SEARCH_SPACE = (- 10 , 10 ) # Search space for the variables
1414
15-
1615# Random number generator
1716rng = np .random .default_rng ()
1817
@@ -41,12 +40,7 @@ def __init__(
4140 self .population = self .initialize_population ()
4241
4342 def initialize_population (self ) -> list [np .ndarray ]:
44- """
45- Initialize the population with random individuals within the search space.
46-
47- Returns:
48- list[np.ndarray]: A list of individuals represented as numpy arrays.
49- """
43+ """Initialize the population with random individuals within the search space."""
5044 return [
5145 rng .uniform (
5246 low = [self .bounds [j ][0 ] for j in range (self .dim )],
@@ -56,18 +50,14 @@ def initialize_population(self) -> list[np.ndarray]:
5650 ]
5751
5852 def fitness (self , individual : np .ndarray ) -> float :
59- """
60- Calculate the fitness value (function value) for an individual.
61- """
53+ """Calculate the fitness value (function value) for an individual."""
6254 value = float (self .function (* individual )) # Ensure fitness is a float
6355 return value if self .maximize else - value # If minimizing, invert the fitness
6456
6557 def select_parents (
6658 self , population_score : list [tuple [np .ndarray , float ]]
6759 ) -> list [np .ndarray ]:
68- """
69- Select top N_SELECTED parents based on fitness.
70- """
60+ """Select top N_SELECTED parents based on fitness."""
7161 population_score .sort (key = lambda score_tuple : score_tuple [1 ], reverse = True )
7262 selected_count = min (N_SELECTED , len (population_score ))
7363 return [ind for ind , _ in population_score [:selected_count ]]
@@ -87,10 +77,10 @@ def crossover(
8777
8878 Example:
8979 >>> ga = GeneticAlgorithm(
90- lambda x, y: -(x**2 + y**2),
91- [(-10, 10), (-10, 10)],
92- 10, 100, 0.1, 0.8, True
93- )
80+ ... lambda x, y: -(x**2 + y**2),
81+ ... [(-10, 10), (-10, 10)],
82+ ... 10, 100, 0.1, 0.8, True
83+ ... )
9484 >>> parent1, parent2 = np.array([1, 2]), np.array([3, 4])
9585 >>> len(ga.crossover(parent1, parent2)) == 2
9686 True
@@ -114,10 +104,10 @@ def mutate(self, individual: np.ndarray) -> np.ndarray:
114104
115105 Example:
116106 >>> ga = GeneticAlgorithm(
117- lambda x, y: -(x**2 + y**2),
118- [(-10, 10), (-10, 10)],
119- 10, 100, 0.1, 0.8, True
120- )
107+ ... lambda x, y: -(x**2 + y**2),
108+ ... [(-10, 10), (-10, 10)],
109+ ... 10, 100, 0.1, 0.8, True
110+ ... )
121111 >>> ind = np.array([1.0, 2.0])
122112 >>> mutated = ga.mutate(ind)
123113 >>> len(mutated) == 2 # Ensure it still has the correct number of dimensions
@@ -133,22 +123,22 @@ def evaluate_population(self) -> list[tuple[np.ndarray, float]]:
133123 Evaluate the fitness of the entire population in parallel.
134124
135125 Returns:
136- list[tuple[np.ndarray, float]]: # Trailing whitespace here
126+ list[tuple[np.ndarray, float]]:
137127 The population with their respective fitness values.
138128
139129 Example:
140130 >>> ga = GeneticAlgorithm(
141- lambda x, y: -(x**2 + y**2),
142- [(-10, 10), (-10, 10)],
143- 10, 100, 0.1, 0.8, True
144- )
131+ ... lambda x, y: -(x**2 + y**2),
132+ ... [(-10, 10), (-10, 10)],
133+ ... 10, 100, 0.1, 0.8, True
134+ ... )
145135 >>> eval_population = ga.evaluate_population()
146136 >>> len(eval_population) == ga.population_size # Ensure population size
147137 True
148138 >>> all(
149- isinstance(ind, tuple) and isinstance(ind[1], float)
150- for ind in eval_population
151- )
139+ ... isinstance(ind, tuple) and isinstance(ind[1], float)
140+ ... for ind in eval_population
141+ ... )
152142 True
153143 """
154144 with ThreadPoolExecutor () as executor :
@@ -220,7 +210,6 @@ def target_function(var_x: float, var_y: float) -> float:
220210# Set bounds for the variables (var_x, var_y)
221211bounds = [(- 10 , 10 ), (- 10 , 10 )] # Both var_x and var_y range from -10 to 10
222212
223-
224213# Instantiate and run the genetic algorithm
225214ga = GeneticAlgorithm (
226215 function = target_function ,
@@ -232,7 +221,6 @@ def target_function(var_x: float, var_y: float) -> float:
232221 maximize = False , # Minimize the function
233222)
234223
235-
236224best_solution = ga .evolve ()
237225print (f"Best solution found: { best_solution } " )
238226print (f"Best fitness (minimum value of function): { target_function (* best_solution )} " )
0 commit comments