Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move to v0.7 #170

Merged
merged 14 commits into from
May 28, 2018
5 changes: 0 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,8 @@ os:
- linux
- osx
julia:
- 0.6
- nightly

matrix:
allow_failures:
- julia: nightly

notifications:
email: false
# uncomment the following lines to override the default test script
Expand Down
5 changes: 3 additions & 2 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
julia 0.6.0
julia 0.7.0-
DiffEqDiffTools
Distances
ForwardDiff 0.7.0
LineSearches 5.0.0
DiffBase
NLSolversBase 5.0.1
NLSolversBase 6.0.0
Reexport
7 changes: 6 additions & 1 deletion src/NLsolve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ using NLSolversBase
using LineSearches
using ForwardDiff
using DiffEqDiffTools
using LinearAlgebra
using Printf

import Base.show,
Base.push!,
Expand All @@ -16,6 +18,10 @@ import Base.show,
import NLSolversBase: OnceDifferentiable, InplaceObjective, NotInplaceObjective,
only_fj, only_fj!

using Reexport
@reexport using LineSearches
using LinearAlgebra

export OnceDifferentiable,
n_ary,
nlsolve,
Expand All @@ -33,7 +39,6 @@ show(io::IO, e::IsFiniteException) = print(io,
"During the resolution of the non-linear system, the evaluation" *
" of the following equation(s) resulted in a non-finite number: $(e.indices)")

include("objectives/autodiff.jl")
include("objectives/helpers.jl")

include("solvers/newton.jl")
Expand Down
4 changes: 2 additions & 2 deletions src/nlsolve/nlsolve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function nlsolve(df::TDF,
end
end

function nlsolve{T}(f,
function nlsolve(f,
initial_x::AbstractArray{T};
method::Symbol = :trust_region,
xtol::Real = zero(T),
Expand All @@ -46,7 +46,7 @@ function nlsolve{T}(f,
m::Integer = 0,
beta::Real = 1.0,
autodiff = :central,
inplace = true)
inplace = true) where T
if typeof(f) <: Union{InplaceObjective, NotInplaceObjective}
df = OnceDifferentiable(f, initial_x, initial_x)
else
Expand Down
6 changes: 3 additions & 3 deletions src/nlsolve/solver_state_results.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ struct SolverState{T}
metadata::Dict
end

function SolverState(i::Integer, fnorm::Real)
function SolverState(i, fnorm)
SolverState(Int(i), fnorm, oftype(fnorm, NaN), Dict())
end

function SolverState{T<:Real}(i::Integer, fnorm::T, stepnorm::T)
function SolverState(i, fnorm, stepnorm)
SolverState(Int(i), fnorm, stepnorm, Dict())
end

struct SolverTrace
states::Vector{SolverState}
end

SolverTrace() = SolverTrace(Array{SolverState}(0))
SolverTrace() = SolverTrace(Array{SolverState}(undef, 0))

function Base.show(io::IO, t::SolverState)
@printf io "%6d %14e %14e\n" t.iteration t.fnorm t.stepnorm
Expand Down
8 changes: 4 additions & 4 deletions src/nlsolve/utils.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
function wdot{T}(wx::AbstractArray{T}, x::AbstractArray{T},
wy::AbstractArray{T}, y::AbstractArray{T})
function wdot(wx::AbstractArray{T}, x::AbstractArray{T},
wy::AbstractArray{T}, y::AbstractArray{T}) where T
out = zero(T)
@inbounds @simd for i in 1:length(x)
out += wx[i]*x[i] * wy[i]*y[i]
end
return out
end

wnorm{T}(w::AbstractArray{T}, x::AbstractArray{T}) = sqrt(wdot(w, x, w, x))
wnorm(w, x) = sqrt(wdot(w, x, w, x))
assess_convergence(f, ftol) = assess_convergence(NaN, NaN, f, NaN, ftol)
function assess_convergence(x,
x_previous,
Expand All @@ -31,7 +31,7 @@ end

function check_isfinite(x::AbstractArray)
if any((y)->!isfinite(y),x)
i = find((!).(isfinite.(x)))
i = findall((!).(isfinite.(x)))
throw(IsFiniteException(i))
end
end
32 changes: 0 additions & 32 deletions src/objectives/autodiff.jl

This file was deleted.

8 changes: 4 additions & 4 deletions src/objectives/helpers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Helpers for functions that do not modify arguments in place but return
function not_in_place(f)
function f!(F, x)
copy!(F, f(x))
copyto!(F, f(x))
end
end

Expand All @@ -14,14 +14,14 @@ end
function not_in_place(f, j, fj)
function fj!(F, J, x)
f, j = fj(x)
copy!(F, f)
copy!(J, j)
copyto!(F, f)
copyto!(J, j)
end
not_in_place(f, j)..., fj!
end


# Helper for functions that take several scalar arguments and return a tuple
function n_ary(f)
f!(fx, x) = copy!(fx, [f(x...)... ])
f!(fx, x) = copyto!(fx, [f(x...)... ])
end
24 changes: 12 additions & 12 deletions src/solvers/anderson.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function AndersonCache(df, method::Anderson)
AndersonCache(xs, gs, old_x, residuals, alphas, fx)
end

@views function anderson_{T}(df::OnceDifferentiable,
@views function anderson_(df::OnceDifferentiable,
x0::AbstractArray{T},
xtol::T,
ftol::T,
Expand All @@ -38,18 +38,18 @@ end
extended_trace::Bool,
m::Integer,
β::Real,
cache = AndersonCache(df, Anderson(m, β)))
cache = AndersonCache(df, Anderson(m, β))) where T


copy!(cache.xs[:,1], x0)
n = 1
copyto!(cache.xs[:,1], x0)
iters = 0
tr = SolverTrace()
tracing = store_trace || show_trace || extended_trace
x_converged, f_converged, converged = false, false, false

errs = zeros(iterations)

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

Expand Down Expand Up @@ -90,25 +90,25 @@ end
cache.xs .= circshift(cache.xs,(0,1)) # no in-place circshift, unfortunately...
cache.gs .= circshift(cache.gs,(0,1))
if m > 1
copy!(cache.old_x, cache.xs[:,2])
copyto!(cache.old_x, cache.xs[:,2])
else
copy!(cache.old_x, cache.xs[:,1])
copyto!(cache.old_x, cache.xs[:,1])
end
copy!(cache.xs[:,1], new_x)
copyto!(cache.xs[:,1], new_x)
end

# returning gs[:,1] rather than xs[:,1] would be better here if
# xn+1 = xn+beta*f(xn) is convergent, but the convergence
# criterion is not guaranteed
x = similar(x0)
copy!(x, cache.xs[:,1])
copyto!(x, cache.xs[:,1])
return SolverResults("Anderson m=$m β=$β",
x0, x, maximum(abs,cache.fx),
n, x_converged, xtol, f_converged, ftol, tr,
iters, x_converged, xtol, f_converged, ftol, tr,
first(df.f_calls), 0)
end

function anderson{T}(df::OnceDifferentiable,
function anderson(df::OnceDifferentiable,
initial_x::AbstractArray{T},
xtol::Real,
ftol::Real,
Expand All @@ -118,6 +118,6 @@ function anderson{T}(df::OnceDifferentiable,
extended_trace::Bool,
m::Integer,
beta::Real,
cache = AndersonCache(df, Anderson(m, beta)))
cache = AndersonCache(df, Anderson(m, beta))) where T
anderson_(df, initial_x, convert(T, xtol), convert(T, ftol), iterations, store_trace, show_trace, extended_trace, m, beta)
end
2 changes: 1 addition & 1 deletion src/solvers/mcp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function mcp_smooth(df::OnceDifferentiable,

sqminus = sqrt.(phiplus.^2 .+ (x .- lower).^2)

dminus_du = 1.-phiplus./sqminus
dminus_du = 1 .- phiplus ./ sqminus

dminus_dv = similar(x)
for i = 1:length(x)
Expand Down
24 changes: 12 additions & 12 deletions src/solvers/mcp_func_defs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ macro reformulate(df)
end)
end

function mcpsolve(df::TDF,
lower::Vector,
upper::Vector,
function mcpsolve(df::OnceDifferentiable,
lower::AbstractArray{T},
upper::AbstractArray{T},
initial_x::AbstractArray{T};
method::Symbol = :trust_region,
reformulation::Symbol = :smooth,
Expand All @@ -26,7 +26,7 @@ function mcpsolve(df::TDF,
extended_trace::Bool = false,
linesearch = LineSearches.BackTracking(),
factor::Real = one(T),
autoscale::Bool = true) where {TDF <: OnceDifferentiable, T}
autoscale::Bool = true) where T

@reformulate df
nlsolve(rf,
Expand All @@ -36,10 +36,10 @@ function mcpsolve(df::TDF,
linesearch = linesearch, factor = factor, autoscale = autoscale)
end

function mcpsolve{T}(f,
function mcpsolve(f,
j,
lower::Vector,
upper::Vector,
lower::AbstractArray{T},
upper::AbstractArray{T},
initial_x::AbstractArray{T};
method::Symbol = :trust_region,
reformulation::Symbol = :smooth,
Expand All @@ -52,7 +52,7 @@ function mcpsolve{T}(f,
linesearch = LineSearches.BackTracking(),
factor::Real = one(T),
autoscale = true,
inplace = true)
inplace = true) where T
if inplace
df = OnceDifferentiable(f, initial_x, initial_x)
else
Expand All @@ -66,9 +66,9 @@ function mcpsolve{T}(f,
linesearch = linesearch, factor = factor, autoscale = autoscale)
end

function mcpsolve{T}(f,
lower::Vector,
upper::Vector,
function mcpsolve(f,
lower::AbstractArray{T},
upper::AbstractArray{T},
initial_x::AbstractArray{T};
method::Symbol = :trust_region,
reformulation::Symbol = :smooth,
Expand All @@ -82,7 +82,7 @@ function mcpsolve{T}(f,
factor::Real = one(T),
autoscale::Bool = true,
autodiff = :central,
inplace = true)
inplace = true) where T
if inplace
df = OnceDifferentiable(f, initial_x, initial_x, autodiff)
else
Expand Down
Loading