-
Notifications
You must be signed in to change notification settings - Fork 1
/
example3.py
45 lines (37 loc) · 1.21 KB
/
example3.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
from algormeter import *
from numpy.linalg import norm
from math import sqrt
from algormeter.tools import counter, dbx
TMAX = 3.
TMIN = .05
EPS = .01
# Nonsmooth Barzilai-Borwein (NSBB) algorithm
def NSBB(p, **kwargs):
def t():
d = p.Xk - Xprev
t = norm(p.gfXk)*norm(d)**2/(EPS + 2*(p.f(Xprev)- p.fXk + p.gfXk @ d))
dbx.print('t:',t, 'Xprev:',Xprev, 'f(Xprev):',p.f(Xprev) )
m = 1./sqrt(p.K+1)
if t < TMIN*m:
t = TMIN*m
counter.up('min',cls='t')
if t > TMAX*m:
t = TMAX*m
counter.up('max',cls='t')
return t
def halt():
return np.isclose(p.fXk,p.optimumValue,atol=1.E-6)
p.isHalt = halt
Xprev = p.XStart + .1
counter.log('hi', 'msg',cls='Welcome')
for k in p.loop():
# if np.isclose(p.fXk,p.optimumValue,atol=1.E-6): # alternative at stop redefine used above
# break
p.Xkp1 = p.Xk - t() * p.gfXk / norm(p.gfXk)
Xprev = p.Xk
df, pv = algorMeter(algorithms = [NSBB], problems = probList_base, iterations = 100,
# trace=True,
# dbprint = True
)
print('\n', df)
print('\n', pv)