Skip to content

Commit 162792e

Browse files
committed
Update genetic_algorithm_optimization.py
1 parent 0feb04c commit 162792e

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

genetic_algorithm/genetic_algorithm_optimization.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
import sympy as sp
33

44

5-
def parse_function(user_input):
5+
def parse_function(user_input: str) -> callable:
66
"""
77
Convert user input from f(x, y) = x^2 + y^2 to a valid Python function.
88
99
Parameters:
1010
user_input (str): The user-defined fitness function in string format.
1111
1212
Returns:
13-
function: A callable fitness function.
13+
callable: A callable fitness function.
1414
1515
Examples:
1616
>>> parse_function("f(x, y) = x^2 + y^2")
@@ -39,19 +39,19 @@ def parse_function(user_input):
3939
return fitness
4040

4141

42-
def genetic_algorithm(user_fitness_function) -> None:
42+
def genetic_algorithm(user_fitness_function: callable) -> None:
4343
"""
4444
Execute the genetic algorithm to optimize the user-defined fitness function.
4545
4646
Parameters:
47-
user_fitness_function (function): The fitness function to be optimized.
47+
user_fitness_function (callable): The fitness function to be optimized.
4848
4949
Returns:
5050
None
5151
5252
Example:
53-
>>> def user_fitness_function(x):
54-
... return x[0]**2 + x[1]**2
53+
>>> def user_fitness_function(x, y):
54+
... return x**2 + y**2
5555
>>> genetic_algorithm(user_fitness_function) # This will print outputs
5656
"""
5757
rng = np.random.default_rng() # New random number generator
@@ -121,7 +121,6 @@ def genetic_algorithm(user_fitness_function) -> None:
121121
population = offspring
122122

123123
print("\n--- Optimization Results ---")
124-
print(f"User-defined function: f(x, y) = {user_input.split('=')[1].strip()}")
125124
print(f"Best Fitness Value (Minimum): {best_fitness:.6f}")
126125
print(
127126
f"Optimal Solution Found: x = {best_solution[0]:.6f}, "

0 commit comments

Comments
 (0)