-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimizer_loop.py
More file actions
75 lines (60 loc) · 2.77 KB
/
optimizer_loop.py
File metadata and controls
75 lines (60 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import numpy as np
from scipy.optimize import minimize
from cost_function import compute_cost
import matplotlib.pyplot as plt
def optimize_cloning_machine(method='COBYLA', max_iter=100000):
"""
Optimizes the cloning machine's parameters and plots the cost function as a function of iterations.
Args:
method (str): Optimization method (e.g., 'Nelder-Mead', 'Powell', 'L-BFGS-B', 'COBYLA').
max_iter (int): Maximum number of iterations.
Returns:
tuple: Optimized parameters and final cost.
And plots the cost function value as a function of the number of iterations.
"""
# Define the initial parameters (random initialization)
initial_thetas = [np.random.uniform(0, 2 * np.pi) for _ in range(12)]
# List to store cost values for plotting
cost_values = []
# Callback function to track the cost after each iteration
def callback(x):
cost = compute_cost(x)
cost_values.append(cost)
print(f"Iteration {len(cost_values)}: Cost = {cost}")
# Define constraints for COBYLA (all thetas must be in [0, 2π])
constraints = []
if method == "COBYLA":
constraints = [
{'type': 'ineq', 'fun': lambda x, i=i: x[i]} for i in range(12) # x[i] >= 0
] + [
{'type': 'ineq', 'fun': lambda x, i=i: 2 * np.pi - x[i]} for i in range(12) # x[i] <= 2π
]
# Optimization using the specified method
result = minimize(
compute_cost, # Function to minimize
initial_thetas, # Initial guess for thetas
method=method, # Optimization method
options={'maxiter': max_iter, 'disp': True}, # Maximum iterations and verbose output
callback=callback, # Track progress
constraints=constraints if method == "COBYLA" else None # Only apply constraints for COBYLA
)
# Optimized parameters and cost
optimized_thetas = result.x
optimized_cost = result.fun
print("\nOptimization Complete")
print(f"Optimized Thetas: {optimized_thetas}")
print(f"Final Cost: {optimized_cost}")
# Plot cost function values as a function of iterations
plt.figure(figsize=(8, 6))
plt.plot(range(1, len(cost_values) + 1), cost_values, marker='o', linestyle='-')
plt.xlabel("Iteration")
plt.ylabel("Cost Function Value")
plt.title(f"Cost Function vs Iterations ({method} Method)")
plt.grid(True)
plt.show()
return optimized_thetas, optimized_cost
## TO TEST THE FUNCTION AND SEE WHAT IT DOES
# thetas = [np.random.uniform(0, 2 * np.pi) for _ in range(12)]
# cost_function = compute_cost(thetas)
# # You can switch the method to 'Nelder-Mead', 'Powell', 'L-BFGS-B', or 'COBYLA'
# optimized_thetas, final_cost = optimize_cloning_machine(method='Nelder-Mead', max_iter=100)