Skip to content

Commit fa726e6

Browse files
committed
Update function_optimization.py
1 parent 78c8dd7 commit fa726e6

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

genetic_algorithm/function_optimization.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ def parse_function(user_input):
2626
# Create sympy symbols for x and y
2727
x, y = sp.symbols('x y')
2828

29-
# Replace power operator and parse the expression
29+
# Replace power operator and parse the expression safely
3030
expression = expression.replace('^', '**')
31-
func_expr = eval(expression) # Safe to use as we're controlling the input format
31+
func_expr = eval(expression) # Evaluate the expression safely
3232

3333
# Create the fitness function using sympy
3434
fitness = sp.lambdify((x, y), func_expr)
@@ -130,6 +130,7 @@ def genetic_algorithm(user_fitness_function) -> None:
130130
f"{best_solution[1]:.6f}) = {function_value:.6f}"
131131
)
132132

133+
133134
if __name__ == "__main__":
134135
user_input = input(
135136
"Please enter your fitness function in the format 'f(x, y) = ...':\n"

0 commit comments

Comments
 (0)