fixes
This commit is contained in:
@ -34,10 +34,10 @@ function NWTN(f;
|
|||||||
#
|
#
|
||||||
# saves the point in lastx, the gradient in lastg, the Hessian in lasth,
|
# saves the point in lastx, the gradient in lastg, the Hessian in lasth,
|
||||||
# and increases feval
|
# and increases feval
|
||||||
|
|
||||||
lastx = x + alpha * d
|
lastx = x + alpha * d
|
||||||
phi, lastg, lastH = f(lastx)
|
phi, lastg, lastH = f(lastx)
|
||||||
|
|
||||||
if Plotf > 2
|
if Plotf > 2
|
||||||
if fStar > - Inf
|
if fStar > - Inf
|
||||||
push!(gap, (phi - fStar) / max(abs(fStar), 1))
|
push!(gap, (phi - fStar) / max(abs(fStar), 1))
|
||||||
@ -56,7 +56,7 @@ function NWTN(f;
|
|||||||
|
|
||||||
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
|
|
||||||
function ArmijoWolfeLS(phi0, phip0, as, m1, m2, tau)
|
function ArmijoWolfeLS(phi0, phip0, as, m1, m2, tau)
|
||||||
# performs an Armijo-Wolfe Line Search.
|
# performs an Armijo-Wolfe Line Search.
|
||||||
#
|
#
|
||||||
@ -70,12 +70,12 @@ function NWTN(f;
|
|||||||
# Wolfe condition is used
|
# Wolfe condition is used
|
||||||
#
|
#
|
||||||
# returns the optimal step and the optimal f-value
|
# returns the optimal step and the optimal f-value
|
||||||
|
|
||||||
lsiter = 1 # count iterations of first phase
|
lsiter = 1 # count iterations of first phase
|
||||||
local phips, phia
|
local phips, phia
|
||||||
while feval ≤ MaxFeval
|
while feval ≤ MaxFeval
|
||||||
phia, phips = f2phi(as, true)
|
phia, phips = f2phi(as, true)
|
||||||
|
|
||||||
if (phia ≤ phi0 + m1 * as * phip0) && (abs(phips) ≤ - m2 * phip0)
|
if (phia ≤ phi0 + m1 * as * phip0) && (abs(phips) ≤ - m2 * phip0)
|
||||||
if printing
|
if printing
|
||||||
@printf(" %2d", lsiter)
|
@printf(" %2d", lsiter)
|
||||||
@ -88,29 +88,29 @@ function NWTN(f;
|
|||||||
end
|
end
|
||||||
as = as / tau
|
as = as / tau
|
||||||
lsiter += 1
|
lsiter += 1
|
||||||
end
|
end
|
||||||
|
|
||||||
if printing
|
if printing
|
||||||
@printf(" %2d ", lsiter)
|
@printf(" %2d ", lsiter)
|
||||||
end
|
end
|
||||||
lsiter = 1 # count iterations of second phase
|
lsiter = 1 # count iterations of second phase
|
||||||
|
|
||||||
am = 0
|
am = 0
|
||||||
a = as
|
a = as
|
||||||
phipm = phip0
|
phipm = phip0
|
||||||
while (feval ≤ MaxFeval ) && (as - am) > mina && (phips > 1e-12)
|
while (feval ≤ MaxFeval ) && (as - am) > mina && (phips > 1e-12)
|
||||||
|
|
||||||
# compute the new value by safeguarded quadratic interpolation
|
# compute the new value by safeguarded quadratic interpolation
|
||||||
a = (am * phips - as * phipm) / (phips - phipm)
|
a = (am * phips - as * phipm) / (phips - phipm)
|
||||||
a = max(am + ( as - am ) * sfgrd, min(as - ( as - am ) * sfgrd, a))
|
a = max(am + ( as - am ) * sfgrd, min(as - ( as - am ) * sfgrd, a))
|
||||||
|
|
||||||
# compute phi(a)
|
# compute phi(a)
|
||||||
phia, phip = f2phi(a, true)
|
phia, phip = f2phi(a, true)
|
||||||
|
|
||||||
if (phia ≤ phi0 + m1 * a * phip0) && (abs(phip) ≤ -m2 * phip0)
|
if (phia ≤ phi0 + m1 * a * phip0) && (abs(phip) ≤ -m2 * phip0)
|
||||||
break # Armijo + strong Wolfe satisfied, we are done
|
break # Armijo + strong Wolfe satisfied, we are done
|
||||||
end
|
end
|
||||||
|
|
||||||
# restrict the interval based on sign of the derivative in a
|
# restrict the interval based on sign of the derivative in a
|
||||||
if phip < 0
|
if phip < 0
|
||||||
am = a
|
am = a
|
||||||
@ -143,7 +143,7 @@ function NWTN(f;
|
|||||||
# m1 is satisfied
|
# m1 is satisfied
|
||||||
#
|
#
|
||||||
# returns the optimal step and the optimal f-value
|
# returns the optimal step and the optimal f-value
|
||||||
|
|
||||||
lsiter = 1 # count ls iterations
|
lsiter = 1 # count ls iterations
|
||||||
while feval ≤ MaxFeval && as > mina
|
while feval ≤ MaxFeval && as > mina
|
||||||
phia, _ = f2phi(as)
|
phia, _ = f2phi(as)
|
||||||
@ -160,7 +160,7 @@ function NWTN(f;
|
|||||||
|
|
||||||
return (as, phia)
|
return (as, phia)
|
||||||
end
|
end
|
||||||
|
|
||||||
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
|
|
||||||
@ -227,7 +227,7 @@ function NWTN(f;
|
|||||||
|
|
||||||
# "global" variables- - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
# "global" variables- - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
|
|
||||||
lastx = zeros(n) # last point visited in the line search
|
lastx = zeros(n) # last point visited in the line search
|
||||||
lastg = zeros(n) # gradient of lastx
|
lastg = zeros(n) # gradient of lastx
|
||||||
lastH = zeros(n, n) # Hessian of lastx
|
lastH = zeros(n, n) # Hessian of lastx
|
||||||
@ -235,7 +235,7 @@ function NWTN(f;
|
|||||||
feval = 0 # f() evaluations count ("common" with LSs)
|
feval = 0 # f() evaluations count ("common" with LSs)
|
||||||
|
|
||||||
status = "error"
|
status = "error"
|
||||||
|
|
||||||
# initializations - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
# initializations - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
|
|
||||||
@ -254,7 +254,7 @@ function NWTN(f;
|
|||||||
@printf("\n\n")
|
@printf("\n\n")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
v, _ = f2phi(0)
|
v, _ = f2phi(0)
|
||||||
ng = norm(lastg)
|
ng = norm(lastg)
|
||||||
if eps < 0
|
if eps < 0
|
||||||
@ -269,7 +269,7 @@ function NWTN(f;
|
|||||||
while true
|
while true
|
||||||
|
|
||||||
# output statistics - - - - - - - - - - - - - - - - - - - - - - - - - -
|
# output statistics - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
|
|
||||||
if fStar > -Inf
|
if fStar > -Inf
|
||||||
gapk = (v - fStar) / max(abs(fStar), 1)
|
gapk = (v - fStar) / max(abs(fStar), 1)
|
||||||
|
|
||||||
@ -282,7 +282,7 @@ function NWTN(f;
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
prevv = v
|
prevv = v
|
||||||
|
|
||||||
if Plotf > 1
|
if Plotf > 1
|
||||||
if Plotf ≥ 2
|
if Plotf ≥ 2
|
||||||
push!(gap, gapk)
|
push!(gap, gapk)
|
||||||
@ -292,7 +292,7 @@ function NWTN(f;
|
|||||||
if printing
|
if printing
|
||||||
@printf("%4d\t%1.8e\t\t%1.4e", feval, v, ng)
|
@printf("%4d\t%1.8e\t\t%1.4e", feval, v, ng)
|
||||||
end
|
end
|
||||||
|
|
||||||
if Plotf > 1
|
if Plotf > 1
|
||||||
if Plotf ≥ 2
|
if Plotf ≥ 2
|
||||||
push!(gap, v)
|
push!(gap, v)
|
||||||
@ -306,12 +306,12 @@ function NWTN(f;
|
|||||||
status = "optimal"
|
status = "optimal"
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
|
|
||||||
if feval > MaxFeval
|
if feval > MaxFeval
|
||||||
status = "stopped"
|
status = "stopped"
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
|
|
||||||
# compute Newton's direction- - - - - - - - - - - - - - - - - - - - - -
|
# compute Newton's direction- - - - - - - - - - - - - - - - - - - - - -
|
||||||
|
|
||||||
lambdan = eigmin(lastH) # smallest eigenvalue
|
lambdan = eigmin(lastH) # smallest eigenvalue
|
||||||
@ -327,45 +327,45 @@ function NWTN(f;
|
|||||||
end
|
end
|
||||||
d = -lastH \ lastg
|
d = -lastH \ lastg
|
||||||
phip0 = lastg' * d
|
phip0 = lastg' * d
|
||||||
|
|
||||||
# compute step size - - - - - - - - - - - - - - - - - - - - - - - - - -
|
# compute step size - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
# in Newton's method, the default initial stepsize is 1
|
# in Newton's method, the default initial stepsize is 1
|
||||||
|
|
||||||
if AWLS
|
if AWLS
|
||||||
a, v = ArmijoWolfeLS(v, phip0, 1, m1, m2, tau)
|
a, v = ArmijoWolfeLS(v, phip0, 1, m1, m2, tau)
|
||||||
else
|
else
|
||||||
a, v = BacktrackingLS(v, phip0, 1, m1, tau)
|
a, v = BacktrackingLS(v, phip0, 1, m1, tau)
|
||||||
end
|
end
|
||||||
|
|
||||||
# output statistics - - - - - - - - - - - - - - - - - - - - - - - - - -
|
# output statistics - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
|
|
||||||
if printing
|
if printing
|
||||||
@printf("\t%1.2e", a)
|
@printf("\t%1.2e", a)
|
||||||
@printf("\n")
|
@printf("\n")
|
||||||
end
|
end
|
||||||
|
|
||||||
if a ≤ mina
|
if a ≤ mina
|
||||||
status = "error"
|
status = "error"
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
|
|
||||||
if v ≤ MInf
|
if v ≤ MInf
|
||||||
status = "unbounded"
|
status = "unbounded"
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
|
|
||||||
# compute new point - - - - - - - - - - - - - - - - - - - - - - - - - -
|
# compute new point - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
|
|
||||||
# possibly plot the trajectory
|
# possibly plot the trajectory
|
||||||
if n == 2 && Plotf == 1
|
if n == 2 && Plotf == 1
|
||||||
PXY = hcat(PXY, hcat(x, lastx))
|
PXY = hcat(PXY, hcat(x, lastx))
|
||||||
end
|
end
|
||||||
|
|
||||||
x = lastx
|
x = lastx
|
||||||
ng = norm(lastg)
|
ng = norm(lastg)
|
||||||
|
|
||||||
# iterate - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
# iterate - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
|
|
||||||
if Interactive
|
if Interactive
|
||||||
readline()
|
readline()
|
||||||
end
|
end
|
||||||
@ -383,4 +383,4 @@ function NWTN(f;
|
|||||||
# end of main loop- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
# end of main loop- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
return (x, status)
|
return (x, status)
|
||||||
end
|
end
|
||||||
|
|||||||
@ -307,16 +307,16 @@ function lasso(x::Union{Nothing, AbstractVecOrMat})
|
|||||||
|
|
||||||
if isnothing(x) # informative call
|
if isnothing(x) # informative call
|
||||||
v = ( 2 - 1/3 )^2 + 10/9 # optimal solution [ 1/9 , 0 ]
|
v = ( 2 - 1/3 )^2 + 10/9 # optimal solution [ 1/9 , 0 ]
|
||||||
return (v, [0, 0])
|
return (v, [0., 0.], nothing)
|
||||||
else
|
else
|
||||||
v = ( 3 * x( 1 ) + 2 * x( 2 ) - 2 )^2 +
|
v = (3 * x[1] + 2 * x[2] - 2)^2 +
|
||||||
10 * ( abs( x( 1 ) ) + abs( x( 2 ) ) ) # f(x)
|
10 * (abs(x[1]) + abs(x[2])) # f(x)
|
||||||
|
|
||||||
g = zeros(2)
|
g = zeros(2)
|
||||||
g[1] = 18 * x[1] + 12 * x[2] - 12 + 10 * sign( x[1] )
|
g[1] = 18 * x[1] + 12 * x[2] - 12 + 10 * sign(x[1])
|
||||||
g[2] = 12 * x[1] + 8 * x[2] - 8 + 10 * sign( x[2] )
|
g[2] = 12 * x[1] + 8 * x[2] - 8 + 10 * sign(x[2])
|
||||||
|
|
||||||
return (v, g)
|
return (v, g, nothing)
|
||||||
end
|
end
|
||||||
end # lasso
|
end # lasso
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user