-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_rosenbrock.py
More file actions
82 lines (66 loc) · 2.1 KB
/
Copy pathtest_rosenbrock.py
File metadata and controls
82 lines (66 loc) · 2.1 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
76
77
78
79
80
81
82
import numpy as np
import matplotlib.pyplot as plt
import os
gscale = 2 if "ANDROID_STORAGE" in os.environ else 1
plt.rcParams.update({'font.size': 14*gscale})
# Function to minimize: Rosenbrock function
# Minimum at f(1,1) = 0 in case of n = 2
def compute_f(x, y):
f = 100*(y - x**2)**2 + (1 - x)**2
return f
def compute_fp(x, y):
fp = np.array([-400*x*y + 400*x**3 + 2*x - 2, 200*y - 200*x**2])
return fp
def compute_fpp(x, y):
fpp = np.array([
[-400*y + 1200*x**2 + 2, -400*x],
[-400*x, 200]
])
return fpp
# optimization
def run_optimization(ax):
learning_rate = 0.001
num_iterations = 300
term_tolerance = 1e-10
# Initial parameters
param = np.array([-2.0, 2.0])
# Create a text object for loss display
bkg = dict(facecolor="white", edgecolor="white", boxstyle="round,pad=0")
loss_text = ax.text(-1, -2, "", fontsize=18*gscale, color="black", bbox=bkg)
ax.scatter(param[0], param[1], color='red', s=10*gscale)
plt.pause(0.1)
for iter in range(1, num_iterations + 1):
fp = compute_fp(param[0], param[1])
fpp = compute_fpp(param[0], param[1])
# Update parameters
delta = fp
param = param - learning_rate * delta
# Update loss display
loss = compute_f(param[0], param[1])
loss_text.set_text(f"Iter={iter}: loss = {loss:0.3f}")
# Plotting current param
ax.scatter(param[0], param[1], color='red', s=10*gscale)
plt.pause(0.01)
# Check convergence
if np.linalg.norm(delta) < term_tolerance:
break
return param
# Init display
size = 100
x = np.linspace(-3, 3, size)
y = np.linspace(-3, 3, size)
X, Y = np.meshgrid(x, y)
Z = compute_f(X, Y)
fig = plt.figure(figsize=(7*gscale, 7*gscale))
plt.xlim(-3, 3)
plt.ylim(-3, 3)
plt.xlabel('x')
plt.ylabel('y')
plt.title('Optimization')
fig.set_facecolor('white')
ax = fig.add_subplot()
ax.contourf(X, Y, Z, levels=256, cmap='jet')
ax.contour(X, Y, Z, levels=128, colors='k', linewidths=1, linestyles='-')
# Run optimization
param = run_optimization(ax)
plt.show()