-
Notifications
You must be signed in to change notification settings - Fork 0
/
algos.py
367 lines (292 loc) · 11.3 KB
/
algos.py
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
from typing import Callable, Literal
import matplotlib.pyplot as plt
import numpy as np
import numpy.typing as npt
import os
dict_steapest_desent = {
"Backtracking": {
"alptha_0": 10.0,
"rho": 0.75,
"k": 0,
"epsilon": .000001,
"max_iter": 10000,
"c": 0.001
},
"Bisection": {
"c1": 0.001,
"c2": 0.1,
"alpha": 0,
"t": 1,
"beta": 1000000,
"k": 0,
"epsilon": .000001,
"max_iter": 10000,
}
}
dict_newton_methods = {
"max_iter": 10000,
"epsilon": .000001,
"Damped": {
"alpha": 0.001,
"beta": 0.75
}
}
def plot_fx_vs_iterations(
fx_val: list[float],
k: int,
f: Callable[[npt.NDArray[np.float64]], np.float64 | float],
initial_point: npt.NDArray[np.float64],
condition: Literal["Backtracking", "Bisection","Pure", "Damped", "Levenberg-Marquardt", "Combined"],
x_axis_lable: str,
y_axis_lable: str,
graph_type: str
):
plt.figure(figsize=(12, 5))
plt.plot(range(0, k), fx_val, marker='o')
plt.xlabel(x_axis_lable)
plt.ylabel(y_axis_lable)
plt.title(f'{f.__name__} vs iteration of {condition}_{graph_type}')
# Check if the plot folder exists, if not create it
if not os.path.exists('plots'):
os.makedirs('plots')
# Save the plot in the plots folder
plt.savefig(f'plots/{f.__name__}_{np.array2string(initial_point)}_{condition}_{graph_type}.png')
plt.close()
def plot_contour(condition: Literal["Backtracking", "Bisection","Pure", "Damped", "Levenberg-Marquardt", "Combined"],
f: Callable[[npt.NDArray[np.float64]], np.float64 | float],
inital_point: npt.NDArray[np.float64],
coordinates: list[float],
):
array = np.array(coordinates)
Z_cord = []
stepx=(np.round(np.max(array[:,0]))+.4-(np.round(np.min(array[:,0]))-.4))/50.0
stepy=(np.round(np.max(array[:,1]))+.4-(np.round(np.min(array[:,1]))-.4))/50.0
arrayx= np.arange(np.round(np.min(array[:,0]))-.4, np.round(np.max(array[:,0]))+.4, stepx)
x=arrayx
arrayy= np.arange(np.round(np.min(array[:,1]))-.4, np.round(np.max(array[:,1]))+.4, stepy)
y=arrayy
X, Y = np.meshgrid( x, y)
n1 = len(X)
n2 = len(Y)
for i in range(n1):
temp = []
for j in range(n2):
val = f(np.array([X[i][j], Y[i][j]]))
temp.append(val)
Z_cord.append(temp)
plt.figure(figsize=(20,10))
plt.contour(X, Y, np.array(Z_cord),levels=20)
# array=np.concatenate((array[:60], array[-60:]))
for i in range(len(array)-1):
plt.arrow(array[i][0], array[i][1], array[i+1][0]-array[i][0], array[i+1]
[1]-array[i][1], head_width=0.020, head_length=0.025, fc='cyan', ec='orange')
# Mark the initial point with yellow dot
plt.scatter(array[0][0], array[0][1], color='black', label='Initial Point')
plt.scatter(array[-1][0], array[-1][1], color='red', label='Final Point')
plt.xlabel('X_axis')
plt.ylabel('Y_axis')
plt.title(f'Contour Plot with Update Arrows ({f.__name__})')
plt.colorbar()
plt.legend()
plt.savefig(
f"plots/{f.__name__}_{np.array2string(inital_point)}_{condition}_cont.png")
plt.close()
# Do not rename or delete this function
def steepest_descent(
f: Callable[[npt.NDArray[np.float64]], np.float64 | float],
d_f: Callable[[npt.NDArray[np.float64]], npt.NDArray[np.float64]],
inital_point: npt.NDArray[np.float64],
condition: Literal["Backtracking", "Bisection"],
) -> npt.NDArray[np.float64]:
# print(condition,"up",f)
X_y_cord = []
X_y_cord.append(inital_point)
if condition == "Backtracking":
alpha_0 = dict_steapest_desent[condition]["alptha_0"]
k = dict_steapest_desent[condition]["k"]
epsilon = dict_steapest_desent[condition]["epsilon"]
rho = dict_steapest_desent[condition]["rho"]
maxIteration = dict_steapest_desent[condition]["max_iter"]
c = dict_steapest_desent[condition]["c"]
X = inital_point
fx_val = []
fx_grad_norm_val = []
while k < maxIteration:
gradiant = d_f(X)
if np.linalg.norm(gradiant) > epsilon:
fx_val.append(f(X))
fx_grad_norm_val.append(np.linalg.norm(gradiant))
alpha = alpha_0
dk = -gradiant
f_x = f(X)
updated_X = X+alpha*dk
f_x_alpha_d = f(updated_X)
while f_x_alpha_d > f_x+c*alpha*np.dot(gradiant, dk):
alpha = alpha*rho
updated_X = X+alpha*dk
f_x_alpha_d = f(updated_X)
X = updated_X
X_y_cord.append(X)
k = k+1
else:
break
x_axis_lable = 'Iterations'
y_axis_lable = 'f(x)'
graph_type = 'vals'
plot_fx_vs_iterations(fx_val, k, f, inital_point,condition, x_axis_lable, y_axis_lable, graph_type)
y_axis_lable = "|f'(x)|"
graph_type = 'grad'
plot_fx_vs_iterations(fx_grad_norm_val, k, f, inital_point,condition, x_axis_lable, y_axis_lable, graph_type)
if len(X_y_cord[0]) == 2:
plot_contour(condition, f, inital_point, X_y_cord)
print(k,condition,inital_point,f)
return X
if condition == "Bisection":
c1 = dict_steapest_desent[condition]["c1"]
c2 = dict_steapest_desent[condition]["c2"]
alpha_0 = dict_steapest_desent[condition]["alpha"]
t = dict_steapest_desent[condition]["t"]
beta_0 = dict_steapest_desent[condition]["beta"]
k = dict_steapest_desent[condition]["k"]
epsilon = dict_steapest_desent[condition]["epsilon"]
maxIteration = dict_steapest_desent[condition]["max_iter"]
X = inital_point
fx_val = []
fx_grad_norm_val = []
while k < maxIteration:
gradiant = d_f(X)
if np.linalg.norm(gradiant) > epsilon:
fx_val.append(f(X))
fx_grad_norm_val.append(np.linalg.norm(gradiant))
alpha = alpha_0
beta = beta_0
dk = -gradiant
while (True):
if f(X+t*dk) > f(X)+c1*t*(np.dot(gradiant, dk)):
beta = t
t = (alpha+beta)/2
elif np.dot(d_f(X+t*dk), dk) < c2*(np.dot(gradiant, dk)):
alpha = t
t = (alpha+beta)/2
else:
break
X = X+t*dk
X_y_cord.append(X)
k = k+1
else:
break
x_axis_lable = 'Iterations'
y_axis_lable = 'f(x)'
graph_type = 'vals'
plot_fx_vs_iterations(fx_val, k, f, inital_point,condition, x_axis_lable, y_axis_lable, graph_type)
y_axis_lable = "|f'(x)|"
graph_type = 'grad'
plot_fx_vs_iterations(fx_grad_norm_val, k, f, inital_point,condition, x_axis_lable, y_axis_lable, graph_type)
if len(X_y_cord[0]) == 2:
plot_contour(condition, f, inital_point, X_y_cord)
print(k,condition,inital_point,f)
return X
# Do not rename or delete this function
def newton_method(
f: Callable[[npt.NDArray[np.float64]], np.float64 | float],
d_f: Callable[[npt.NDArray[np.float64]], npt.NDArray[np.float64]],
d2_f: Callable[[npt.NDArray[np.float64]], npt.NDArray[np.float64]],
inital_point: npt.NDArray[np.float64],
condition: Literal["Pure", "Damped", "Levenberg-Marquardt", "Combined"],
) -> npt.NDArray[np.float64]:
max_iteration = dict_newton_methods["max_iter"]
epsilon = dict_newton_methods["epsilon"]
k = 0
X = inital_point
fx_val = []
fx_grad_norm_val = []
X_y_cord = []
X_y_cord.append(inital_point)
if condition == "Pure":
while k < max_iteration:
gradiant = d_f(X)
if np.linalg.norm(gradiant) > epsilon:
fx_val.append(f(X))
fx_grad_norm_val.append(np.linalg.norm(gradiant))
dk = np.linalg.solve(d2_f(X), gradiant)
X = X-dk
X_y_cord.append(X)
k = k+1
else:
break
elif condition == "Damped":
alpha = dict_newton_methods[condition]["alpha"]
beta = dict_newton_methods[condition]["beta"]
while k < max_iteration:
gradiant = d_f(X)
if np.linalg.norm(gradiant) > epsilon:
fx_val.append(f(X))
fx_grad_norm_val.append(np.linalg.norm(gradiant))
t = 1
dk = -np.linalg.solve(d2_f(X), gradiant)
while f(X) < f(X+t*dk)-alpha*t*(np.dot(gradiant, dk)):
t = beta*t
X = X+t*dk
X_y_cord.append(X)
k = k+1
else:
break
elif condition == "Levenberg-Marquardt":
while k < max_iteration:
gradiant = d_f(X)
if np.linalg.norm(gradiant) > epsilon:
fx_val.append(f(X))
fx_grad_norm_val.append(np.linalg.norm(gradiant))
hessian = d2_f(X)
lambda_min = min(np.linalg.eigvals(hessian))
if lambda_min <= 0:
mue_k = -lambda_min+0.1
temp = np.linalg.inv(hessian + mue_k * np.eye(len(X)))
dk = -np.dot(temp, gradiant)
else:
dk = -np.linalg.solve(d2_f(X), gradiant)
X = X+dk
X_y_cord.append(X)
k = k+1
else:
break
elif condition == "Combined":
rho = 0.75
c = 0.001
while k < max_iteration:
gradiant = d_f(X)
if np.linalg.norm(gradiant) > epsilon:
fx_val.append(f(X))
fx_grad_norm_val.append(np.linalg.norm(gradiant))
hessian = d2_f(X)
lambda_min = min(np.linalg.eigvals(hessian))
if lambda_min <= 0:
mue_k = -lambda_min+0.1
temp = np.linalg.inv(hessian + mue_k * np.eye(len(X)))
dk = -np.dot(temp, gradiant)
else:
dk = -np.linalg.solve(d2_f(X), gradiant)
alpha = 10.0
updated_X = X+alpha*dk
f_x_alpha_d = f(updated_X)
f_x = f(X)
while f_x_alpha_d > f_x+c*alpha*np.dot(gradiant, dk):
alpha = alpha*rho
updated_X = X+alpha*dk
f_x_alpha_d = f(updated_X)
X = updated_X
X_y_cord.append(X)
k = k+1
else:
break
x_axis_lable = 'Iterations'
y_axis_lable = 'f(x)'
graph_type = 'vals'
plot_fx_vs_iterations(fx_val, k, f, inital_point,condition, x_axis_lable, y_axis_lable, graph_type)
y_axis_lable = "|f'(x)|"
graph_type = 'grad'
plot_fx_vs_iterations(fx_grad_norm_val, k, f, inital_point,condition, x_axis_lable, y_axis_lable, graph_type)
if len(X_y_cord[0]) == 2:
plot_contour(condition, f, inital_point, X_y_cord)
print(k,condition,inital_point,f)
return X