Skip to content

Commit

Permalink
Fix nlsolve
Browse files Browse the repository at this point in the history
  • Loading branch information
pkofod committed Dec 18, 2017
1 parent 0bb85a5 commit 2c99592
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/differentiable_vectors/autodiff.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function OnceDifferentiable(f!, F::AbstractArray, x::AbstractArray, autodiff = :central)
function OnceDifferentiable(f!, x::AbstractArray, F::AbstractArray, autodiff::Union{Symbol, Bool} = :central)
if autodiff == :central
function fj!(F, J, x)
f!(F, x)
Expand All @@ -13,7 +13,7 @@ function OnceDifferentiable(f!, F::AbstractArray, x::AbstractArray, autodiff = :
F = similar(x)
fj!(F, J, x)
end
return OnceDifferentiable(f!, j!, fj!, similar(x), similar(x))
return OnceDifferentiable(f!, j!, fj!, x, x)
elseif autodiff == :forward || autodiff == true
jac_cfg = ForwardDiff.JacobianConfig(f!, x, x)
ForwardDiff.checktag(jac_cfg, f!, x)
Expand All @@ -28,7 +28,7 @@ function OnceDifferentiable(f!, F::AbstractArray, x::AbstractArray, autodiff = :
DiffBase.value(jac_res)
end

return OnceDifferentiable(f!, g!, fg!, similar(x), x)
return OnceDifferentiable(f!, g!, fg!, x, x)
else
error("The autodiff value $(autodiff) is not supported. Use :central or :forward.")
end
Expand Down
2 changes: 1 addition & 1 deletion src/nlsolve/nlsolve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function nlsolve(f!,
autoscale::Bool = true,
m::Integer = 0,
beta::Real = 1.0) where T
nlsolve(OnceDifferentiable(f!, j!, similar(initial_x), initial_x),
nlsolve(OnceDifferentiable(f!, j!, initial_x, initial_x),
initial_x, method = method, xtol = xtol, ftol = ftol,
iterations = iterations, store_trace = store_trace,
show_trace = show_trace, extended_trace = extended_trace,
Expand Down
2 changes: 1 addition & 1 deletion src/solvers/anderson.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

for n = 1:iterations
# fixed-point iteration
value!(df, fx, xs[:,1])
value!!(df, fx, xs[:,1])

gs[:,1] .= xs[:,1] .+ β.*fx

Expand Down
12 changes: 6 additions & 6 deletions src/solvers/mcp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function mcp_smooth(df::OnceDifferentiable,
lower::Vector, upper::Vector)

function f!(F, x)
value!(df, F, x)
value!!(df, F, x)
for i = 1:length(x)
if isfinite.(upper[i])
F[i] += (x[i]-upper[i]) + sqrt(F[i]^2+(x[i]-upper[i])^2)
Expand All @@ -29,7 +29,7 @@ function mcp_smooth(df::OnceDifferentiable,

function j!(J, x)
F = similar(x)
value_jacobian!(df, F, J, x)
value_jacobian!!(df, F, J, x)

# Derivatives of phiplus
sqplus = sqrt.(F.^2 .+ (x .- upper).^2)
Expand Down Expand Up @@ -74,7 +74,7 @@ function mcp_smooth(df::OnceDifferentiable,
J[i,i] += dminus_dv[i] + dminus_du[i]*dplus_dv[i]
end
end
return OnceDifferentiable(f!, j!, similar(df.F), similar(df.x_f))
return OnceDifferentiable(f!, j!, similar(df.x_f), similar(df.F))
end

# Generate a function whose roots are the solutions of the MCP.
Expand All @@ -87,7 +87,7 @@ end
function mcp_minmax(df::OnceDifferentiable,
lower::Vector, upper::Vector)
function f!(F, x)
value!(df, F, x)
value!!(df, F, x)
for i = 1:length(x)
if F[i] < x[i]-upper[i]
F[i] = x[i]-upper[i]
Expand All @@ -100,7 +100,7 @@ function mcp_minmax(df::OnceDifferentiable,

function j!(J, x)
F = similar(x)
value_jacobian!(df, F, J, x)
value_jacobian!!(df, F, J, x)
for i = 1:length(x)
if F[i] < x[i]-upper[i] || F[i] > x[i]-lower[i]
for j = 1:length(x)
Expand All @@ -109,5 +109,5 @@ function mcp_minmax(df::OnceDifferentiable,
end
end
end
return OnceDifferentiable(f!, j!, similar(df.F), similar(df.x_f))
return OnceDifferentiable(f!, j!, similar(df.x_f), similar(df.F))
end
2 changes: 1 addition & 1 deletion src/solvers/mcp_func_defs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function mcpsolve{T}(f!,
linesearch! = LineSearches.BackTracking(),
factor::Real = one(T),
autoscale::Bool = true)
@reformulate OnceDifferentiable(f!, j!, similar(initial_x), initial_x)
@reformulate OnceDifferentiable(f!, j!, initial_x, initial_x)
nlsolve(rf,
initial_x, method = method, xtol = xtol, ftol = ftol,
iterations = iterations, store_trace = store_trace,
Expand Down
2 changes: 1 addition & 1 deletion src/solvers/newton.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function newton_{T}(df::OnceDifferentiable,
go!(storage, xlin)
vecdot(value(df), value(df)) / 2
end
dfo = OnceDifferentiable(fo, go!, fgo!, real(zero(T)), x)
dfo = OnceDifferentiable(fo, go!, fgo!, x, real(zero(T)))

while !converged && it < iterations

Expand Down
2 changes: 1 addition & 1 deletion test/minpack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ for (df, initial, name) in alltests
r.f_calls, r.g_calls, r.residual_norm, tot_time)
@test converged(r)
# with autodiff
tot_time = @elapsed r_AD = nlsolve(df.f, initial, method = method, autodiff = true)
tot_time2 = @elapsed r_AD = nlsolve(df.f, initial, method = method, autodiff = true)
@printf("%-45s %5d %5d %5d %14e %10e\n", name*"-"*string(method)*"-AD",
length(initial), r_AD.f_calls, r_AD.g_calls, r_AD.residual_norm, tot_time)
if PRINT_FILE
Expand Down
4 changes: 2 additions & 2 deletions test/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function g_sparse!(J, x)
J[2, 2] = u
end

df = OnceDifferentiable(f_sparse!, g_sparse!, rand(2), spzeros(2, 2), rand(2))
df = OnceDifferentiable(f_sparse!, g_sparse!, rand(2), rand(2), spzeros(2, 2))

# Test trust region
r = nlsolve(df, [ -0.5; 1.4], method = :trust_region, autoscale = true)
Expand All @@ -39,7 +39,7 @@ r = mcpsolve(df, [-Inf;-Inf], [Inf; Inf], [-0.5; 1.4], reformulation = :minmax)
@test norm(r.zero - [ 0; 1]) < 1e-8

# Test given sparse
df = OnceDifferentiable(f_sparse!, g_sparse!, rand(2), spzeros(2, 2), rand(2))
df = OnceDifferentiable(f_sparse!, g_sparse!, rand(2), rand(2), spzeros(2, 2))
r = nlsolve(df, [ -0.5; 1.4], method = :trust_region, autoscale = true)
@test converged(r)
@test norm(r.zero - [ 0; 1]) < 1e-8
Expand Down

0 comments on commit 2c99592

Please sign in to comment.