-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Create function_optimization.py #11611
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
1c8c133
a91b8d8
8176e0e
da593c4
3864de9
1ad1867
f7bc96e
70f4771
5d82115
b740c01
d28af66
78c8dd7
854b155
fa726e6
08ab755
ad01c9c
b761f25
f4a4831
eb27d35
19d5420
e51da64
0feb04c
162792e
b093145
7f8befa
0697e97
cd4f6f3
282b6c6
85fc983
11876c9
cabb35a
961e1a4
6dd4159
ccbf8a5
8661150
3a654c5
1873f2f
40e4e3c
5ff30a6
0b06dae
796d80b
57feaea
096a033
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import numpy as np | ||
|
||
def parse_function(user_input): | ||
Check failure on line 3 in genetic_algorithm/function_optimization.py
|
||
night-spring marked this conversation as resolved.
Show resolved
Hide resolved
night-spring marked this conversation as resolved.
Show resolved
Hide resolved
night-spring marked this conversation as resolved.
Show resolved
Hide resolved
night-spring marked this conversation as resolved.
Show resolved
Hide resolved
night-spring marked this conversation as resolved.
Show resolved
Hide resolved
night-spring marked this conversation as resolved.
Show resolved
Hide resolved
night-spring marked this conversation as resolved.
Show resolved
Hide resolved
night-spring marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Convert user input from f(x, y) = ... to a valid Python function. | ||
|
||
Check failure on line 6 in genetic_algorithm/function_optimization.py
|
||
Parameters: | ||
user_input (str): A string representing the user-defined fitness function. | ||
|
||
Check failure on line 9 in genetic_algorithm/function_optimization.py
|
||
Returns: | ||
str: A string of valid Python code for the fitness function. | ||
|
||
Check failure on line 12 in genetic_algorithm/function_optimization.py
|
||
Raises: | ||
ValueError: If the input format is invalid. | ||
|
||
Check failure on line 15 in genetic_algorithm/function_optimization.py
|
||
Examples: | ||
>>> parse_function("f(x, y) = x^2 + y^2") | ||
'def fitness(x):\\n return x[0]**2 + x[1]**2\\n' | ||
>>> parse_function("f(x, y) = x^2 - y^2") | ||
'def fitness(x):\\n return x[0]**2 - x[1]**2\\n' | ||
>>> parse_function("f(x, y) = x + y") | ||
'def fitness(x):\\n return x[0] + x[1]\\n' | ||
""" | ||
user_input = user_input.strip() | ||
if "=" in user_input: | ||
_, expression = user_input.split("=", 1) | ||
expression = expression.strip() | ||
else: | ||
raise ValueError("Invalid function format. Please use 'f(x, y) = ...'.") | ||
|
||
Check failure on line 30 in genetic_algorithm/function_optimization.py
|
||
function_code = f"def fitness(x):\n" | ||
Check failure on line 31 in genetic_algorithm/function_optimization.py
|
||
function_code += f" return {expression.replace('^', '**').replace('x', 'x[0]').replace('y', 'x[1]')}\n" | ||
Check failure on line 32 in genetic_algorithm/function_optimization.py
|
||
|
||
Check failure on line 33 in genetic_algorithm/function_optimization.py
|
||
return function_code | ||
|
||
|
||
def genetic_algorithm(user_fitness_function): | ||
night-spring marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Execute the genetic algorithm to optimize the user-defined fitness function. | ||
|
||
Check failure on line 40 in genetic_algorithm/function_optimization.py
|
||
Parameters: | ||
user_fitness_function (function): The fitness function to be optimized. | ||
|
||
Returns: | ||
None | ||
|
||
Example: | ||
>>> def example_fitness_function(x): | ||
... return x[0]**2 + x[1]**2 | ||
>>> genetic_algorithm(example_fitness_function) # This will print outputs | ||
""" | ||
population_size = 100 | ||
num_generations = 500 | ||
mutation_rate = 0.01 | ||
chromosome_length = 2 | ||
best_fitness = np.inf | ||
best_solution = None | ||
|
||
population = np.random.rand(population_size, chromosome_length) | ||
|
||
for generation in range(num_generations): | ||
fitness_values = [] | ||
|
||
for individual in population: | ||
fitness_value = user_fitness_function(individual) | ||
|
||
if fitness_value is None: | ||
print(f"Warning: Fitness function returned None for individual {individual}.") | ||
fitness_value = np.inf | ||
else: | ||
print(f"Evaluating individual {individual}, Fitness: {fitness_value:.6f}") | ||
|
||
fitness_values.append(fitness_value) | ||
|
||
fitness_values = np.array(fitness_values) | ||
|
||
best_idx = np.argmin(fitness_values) | ||
if fitness_values[best_idx] < best_fitness: | ||
best_fitness = fitness_values[best_idx] | ||
best_solution = population[best_idx] | ||
|
||
print(f"Generation {generation + 1}, Best Fitness: {best_fitness:.6f}") | ||
|
||
selected_parents = population[np.random.choice(population_size, population_size)] | ||
|
||
offspring = [] | ||
for i in range(0, population_size, 2): | ||
parent1, parent2 = selected_parents[i], selected_parents[i + 1] | ||
cross_point = np.random.randint(1, chromosome_length) | ||
child1 = np.concatenate((parent1[:cross_point], parent2[cross_point:])) | ||
child2 = np.concatenate((parent2[:cross_point], parent1[cross_point:])) | ||
offspring.append(child1) | ||
offspring.append(child2) | ||
|
||
offspring = np.array(offspring) | ||
mutation_mask = np.random.rand(population_size, chromosome_length) < mutation_rate | ||
offspring[mutation_mask] = np.random.rand(np.sum(mutation_mask)) | ||
|
||
population = offspring | ||
|
||
print("\n--- Optimization Results ---") | ||
print(f"User-defined function: f(x, y) = {user_input.split('=')[1].strip()}") | ||
print(f"Best Fitness Value (Minimum): {best_fitness:.6f}") | ||
print(f"Optimal Solution Found: x = {best_solution[0]:.6f}, y = {best_solution[1]:.6f}") | ||
|
||
function_value = best_fitness | ||
print(f"Function Value at Optimal Solution: f({best_solution[0]:.6f}, {best_solution[1]:.6f}) = {function_value:.6f}") | ||
|
||
if __name__ == "__main__": | ||
user_input = input("Please enter your fitness function in the format 'f(x, y) = ...':\n") | ||
|
||
try: | ||
fitness_function_code = parse_function(user_input) | ||
exec(fitness_function_code, globals()) | ||
except (SyntaxError, ValueError) as e: | ||
print(f"Error: {e}") | ||
except Exception as e: | ||
print(f"Error executing function: {e}") | ||
|
||
if 'fitness' in globals(): | ||
genetic_algorithm(fitness) | ||
else: | ||
print("No valid fitness function provided.") |
Uh oh!
There was an error while loading. Please reload this page.