-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminima.py
473 lines (400 loc) · 14.4 KB
/
minima.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
"""
Small collection of optimization algorithms
ACHTUNG:
iterative optimization algorithms
finds only a local minimum.
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as cm
## Test function
def F(x, y):
"""
Himmelblau's function
function to find the minimum
4 equals minima ( x, y):
I = ( 3.0 , 2.0 )
II = (-2.805118, 3.131312)
III = (-3.779310, -3.283186)
IV = ( 3.584428, -1.848126)
"""
return (x**2 + y - 11)**2 + (x + y**2 - 7)**2
def G(x):
"""
function to find the minimum
local minimun = 0.8375654352833
global minimum = -1.1071598716888
"""
return (x**2 - 1)**2 + x
## gradient descent
def grad_disc(f, x0, tol, step):
"""
implementation of gradient descent
you have to be careful about the values
you pass in x0 if the function has more minima
and also the value of steps is a delicate choice
to be made wisely
Parameters
----------
f : callable
function to find the minimum,
can be f(x), f(x,y) and so on
x0 : 1darray
initial guess, to choose carefully
tol : float
required tollerance
the function stops when all components
of the gradient have smaller than tol
step : float
size of step to do, to choose carefully
Returns
-------
X : ndarray
array with all steps of solution
iter : int
number of iteration
"""
iter = 0 #initialize iteration counter
h = 1e-7 #increment for derivatives
X = [] #to store solution
M = len(x0) #number of variable
s = np.zeros(M) #auxiliary array for derivatives
grad = np.zeros(M) #gradient
while True:
#gradient computation
for i in range(M): #loop over variables
s[i] = 1 #we select one variable at a time
dz1 = x0 + s*h #step forward
dz2 = x0 - s*h #step backward
grad[i] = (f(*dz1) - f(*dz2))/(2*h) #derivative along z's direction
s[:] = 0 #reset to select the other variables
if all(abs(grad) < tol):
break
x0 = x0 - step*grad #move towards the minimum
X.append(x0) #store iteration
iter += 1 #update counter
X = np.array(X)
return X, iter
## gradient descent with momentum
def grad_disc_m(f, x0, tol, alpha, beta):
"""
implementation of gradient descent with momentum
you have to be careful about the values
you pass in x0 if the function has more minima
and also the value of alpha an beta is a
delicate choice to be made wisely
Parameters
----------
f : callable
function to find the minimum,
can be f(x), f(x,y) and so on
x0 : 1darray
initial guess, to choose carefully
tol : float
required tollerance
the function stops when all components
of the gradient have smaller than tol
alpha : float
size of step to do, to choose carefully
beta : float
size of step to do for velocity,
to choose carefully, if beta = 0
we get the method of gradient
descent without momentum
Returns
-------
X : ndarray
array with all steps of solution
iter : int
number of iteration
"""
iter = 0 #initialize iteration counter
h = 1e-7 #increment for derivatives
X = [] #to store solution
M = len(x0) #number of variable
s = np.zeros(M) #auxiliary array for derivatives
grad = np.zeros(M) #gradient
w = np.zeros(M) #velocity, momentum
while True:
#gradient computation
for i in range(M): #loop over variables
s[i] = 1 #we select one variable at a time
dz1 = x0 + s*h #step forward
dz2 = x0 - s*h #step backward
grad[i] = (f(*dz1) - f(*dz2))/(2*h) #derivative along z's direction
s[:] = 0 #reset to select the other variables
if all(abs(grad) < tol):
break
w = beta*w + grad #update velocity
x0 = x0 - alpha*w #update position move towards the minimum
X.append(x0) #store iteration
iter += 1 #update counter
X = np.array(X)
return X, iter
## Adadelta
def adadelta(f, x0, tol, rho, eps):
"""
implementation of adadelta alghoritm
you have to be careful about the values
you pass in x0 if the function has more minima
and also the value of alpha an beta is a
delicate choice to be made wisely
Parameters
----------
f : callable
function to find the minimum,
can be f(x), f(x,y) and so on
x0 : 1darray
initial guess, to choose carefully
tol : float
required tollerance
the function stops when all components
of the gradient have smaller than tol
rho : float
parameter of alghoritm, decay rate, to choose carefully
eps : float
parameter of alghoritm, to choose carefully
Returns
-------
X : ndarray
array with all steps of solution
iter : int
number of iteration
"""
iter = 0 #initialize iteration counter
h = 1e-7 #increment for derivatives
X = [] #to store solution
M = len(x0) #number of variable
s = np.zeros(M) #auxiliary array for derivatives
grad = np.zeros(M) #gradient
E_gr = np.zeros(M) #running everage gradient
E_dx = np.zeros(M) #running everage dx
while True:
#gradient computation
for i in range(M): #loop over variables
s[i] = 1 #we select one variable at a time
dz1 = x0 + s*h #step forward
dz2 = x0 - s*h #step backward
grad[i] = (f(*dz1) - f(*dz2))/(2*h) #derivative along z's direction
s[:] = 0 #reset to select the other variables
if all(abs(grad) < tol):
break
E_gr = E_gr*rho + (1 - rho)*grad**2 #E[g^2]
dx = - np.sqrt(E_dx + eps)/np.sqrt(E_gr + eps) * grad #delta_x
E_dx = E_dx*rho + (1 - rho)*dx**2 #E[dx^2]
x0 = x0 + dx #update position move towards the minimum
X.append(x0) #store iteration
iter += 1 #update counter
X = np.array(X)
return X, iter
## Adam
def adam(f, x0, tol, a, b1, b2, eps):
"""
implementation of Adam alghoritm, Adaptive Moment Estimation
you have to be careful about the values
you pass in x0 if the function has more minima
and also the value of alpha an beta is a
delicate choice to be made wisely
Parameters
----------
f : callable
function to find the minimum,
can be f(x), f(x,y) and so on
x0 : 1darray
initial guess, to choose carefully
tol : float
required tollerance
the function stops when all components
of the gradient have smaller than tol
a : float
size of step to do, to choose carefully, typical value is 0.001
b1 : float
Decay factor for first momentum, typical value is 0.9
b2 : float
Decay factor for second momentum, typical value is 0.999
eps : float
parameter of alghoritm, to choose carefully, typical value is 1e-8
Returns
-------
X : ndarray
array with all steps of solution
iter : int
number of iteration
"""
iter = 0 #initialize iteration counter
h = 1e-7 #increment for derivatives
X = [] #to store solution
M = len(x0) #number of variable
s = np.zeros(M) #auxiliary array for derivatives
grad = np.zeros(M) #gradient
m = np.zeros(M) #first moment
v = np.zeros(M) #second moment
while True:
#gradient computation
for i in range(M): #loop over variables
s[i] = 1 #we select one variable at a time
dz1 = x0 + s*h #step forward
dz2 = x0 - s*h #step backward
grad[i] = (f(*dz1) - f(*dz2))/(2*h) #derivative along z's direction
s[:] = 0 #reset to select the other variables
if all(abs(grad) < tol):
break
m = b1*m + (1 - b1)*grad
v = b2*v + (1 - b2)*grad**2
m_hat = m/(1 - b1**(iter+1))
v_hat = v/(1 - b2**(iter+1))
dx = a*m_hat/(np.sqrt(v_hat) + eps)
x0 = x0 - dx #update position move towards the minimum
X.append(x0) #store iteration
iter += 1 #update counter
X = np.array(X)
return X, iter
##TEST
def test1d():
"""
test for one variable's function
"""
print("test 1D:")
#---------------------------------------------------
# No momentum
#---------------------------------------------------
print("no momentum")
x0 = np.array([1.5])
sol, iter = grad_disc(G, x0, 1e-8, 1e-3)
xs1, = sol.T
x_min = xs1[-1]
min_f = G(x_min)
print(f"Punto di minimo x_min = {x_min:.8f}")
print(f"Valore nel minimo G(x_min) = {min_f:.8f}")
print(f"numero di iterazioni = {iter}\n")
#---------------------------------------------------
# With momentum
#---------------------------------------------------
print("with momentum")
x0 = np.array([1.5])
sol, iter = grad_disc_m(G, x0, 1e-8, 1e-3, 0.953) #global
#sol, iter = grad_disc_m(G, x0, 1e-8, 1e-3, 0.9) #local
xs2, = sol.T
x_min = xs2[-1]
min_f = G(x_min)
print(f"Punto di minimo x_min = {x_min:.8f}")
print(f"Valore nel minimo G(x_min) = {min_f:.8f}")
print(f"numero di iterazioni = {iter}\n")
#---------------------------------------------------
# ADADELTA
#---------------------------------------------------
print("Adadelta")
x0 = np.array([1.5])
sol, iter = adadelta(G, x0, 1e-8, 0.95, 1e-6)
xs3, = sol.T
x_min = xs3[-1]
min_f = G(x_min)
print(f"Punto di minimo x_min = {x_min:.8f}")
print(f"Valore nel minimo G(x_min) = {min_f:.8f}")
print(f"numero di iterazioni = {iter}\n")
#---------------------------------------------------
# ADAM
#---------------------------------------------------
print("Adam")
x0 = np.array([1.5])
#sol, iter = adam(G, x0, 1e-8, 0.001, 0.999, 0.998, 1e-8) #global
sol, iter = adam(G, x0, 1e-8, 0.001, 0.9, 0.999, 1e-8) #local
xs4, = sol.T
x_min = xs4[-1]
min_f = G(x_min)
print(f"Punto di minimo x_min = {x_min:.8f}")
print(f"Valore nel minimo G(x_min) = {min_f:.8f}")
print(f"numero di iterazioni = {iter}\n")
#---------------------------------------------------
# Plot
#---------------------------------------------------
plt.figure(1)
plt.title("Traiettorie soluzioni")
t = np.linspace(-x0[0], x0[0], 1000)
plt.plot(t, G(t), 'k', label='function')
plt.plot(xs1, G(xs1)+0.1, 'r', label='no momentum')
plt.plot(xs2, G(xs2)+0.2, 'b', label='with momentum')
plt.plot(xs3, G(xs3)+0.3, 'g', label='adadelta')
plt.plot(xs4, G(xs4)+0.4, 'y', label='adam')
plt.legend(loc='best')
plt.xlabel('x')
plt.ylabel('G(x)')
plt.grid()
plt.show()
def test2d():
"""
test for two variable's function
"""
print("test 2D:\n")
#---------------------------------------------------
# No momentum
#---------------------------------------------------
print("no momentum")
x0 = np.array([-0.2, -0.9])
sol, iter = grad_disc(F, x0, 1e-8, 1e-3)
xs1, ys1 = sol.T
x_min, y_min = xs1[-1], ys1[-1]
min_f = F(x_min, y_min)
print(f"Punto di minimo (x_min, y_min) = ({x_min:.8f}, {y_min:.8f})")
print(f"Valore nel minimo F(x_min, y_min) = {min_f:.8f}")
print(f"numero di iterazioni = {iter}\n")
#---------------------------------------------------
# With momentum
#---------------------------------------------------
print("with momentum")
x0 = np.array([-0.2, -0.9])
sol, iter = grad_disc_m(F, x0, 1e-8, 1e-3, 0.8)
xs2, ys2 = sol.T
x_min, y_min = xs2[-1], ys2[-1]
min_f = F(x_min, y_min)
print(f"Punto di minimo (x_min, y_min) = ({x_min:.8f}, {y_min:.8f})")
print(f"Valore nel minimo F(x_min, y_min) = {min_f:.8f}")
print(f"numero di iterazioni = {iter}\n")
#---------------------------------------------------
# ADADELTA
#---------------------------------------------------
print("Adadelta")
x0 = np.array([-0.2, -0.9])
sol, iter = adadelta(F, x0, 1e-8, 0.95, 1e-6)
xs3, ys3 = sol.T
x_min, y_min = xs3[-1], ys3[-1]
min_f = F(x_min, y_min)
print(f"Punto di minimo (x_min, y_min) = ({x_min:.8f}, {y_min:.8f})")
print(f"Valore nel minimo F(x_min, y_min) = {min_f:.8f}")
print(f"numero di iterazioni = {iter}\n")
#---------------------------------------------------
# ADAM
#---------------------------------------------------
print("Adam")
x0 = np.array([-0.2, -0.9])
sol, iter = adam(F, x0, 1e-8, 0.001, 0.9, 0.999, 1e-8)
xs4, ys4 = sol.T
x_min, y_min = xs4[-1], ys4[-1]
min_f = F(x_min, y_min)
print(f"Punto di minimo (x_min, y_min) = ({x_min:.8f}, {y_min:.8f})")
print(f"Valore nel minimo F(x_min, y_min) = {min_f:.8f}")
print(f"numero di iterazioni = {iter}")
#---------------------------------------------------
# Plot
#---------------------------------------------------
N = 200
x = np.linspace(-6, 6, N)
y = np.linspace(-6, 6, N)
x, y = np.meshgrid(x, y)
plt.figure(2)
plt.title("Traiettorie soluzioni")
plt.xlabel('x')
plt.ylabel('y')
levels = np.linspace(0, 300, 30)
c=plt.contourf(x, y, F(x, y), levels=levels, cmap='jet')
plt.colorbar(c)
plt.grid()
plt.plot(xs1, ys1, 'r', label='no momentum')
plt.plot(xs2, ys2, 'b', label='with momentum')
plt.plot(xs3, ys3, 'g', label='adadelta')
plt.plot(xs4, ys4, 'y', label='adam')
plt.legend(loc='best')
plt.show()
if __name__ == "__main__":
test1d()
test2d()