Skip to content

Commit

Permalink
add unconstrained_api
Browse files Browse the repository at this point in the history
  • Loading branch information
tmigot committed Jul 26, 2021
1 parent d728aa5 commit 02d5edd
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/nlp/consistency.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function consistent_nlps(
)
consistent_counters(nlps)
test_meta && consistent_meta(nlps, rtol = rtol)
unconstrained_api(nlps, exclude = exclude)
consistent_functions(nlps, rtol = rtol, exclude = exclude)
consistent_counters(nlps)
for nlp in nlps
Expand Down Expand Up @@ -59,6 +60,23 @@ function consistent_nlps(
end
end

function unconstrained_api(nlps; exclude = [])
for nlp in nlps
if nlp.meta.ncon == 0
x0, x1 = nlp.meta.x0, ones(nlp.meta.nvar)
@test nlp.meta.nnzj == 0
@test cons in exclude || cons(nlp, x0) == []
@test jac_coord in exclude || jac_coord(nlp, x0) == []
@test jac_coord in exclude || jac_structure(nlp) == ([], [])
@test jac in exclude || jac(nlp, x0) == zeros(0, nlp.meta.nvar)
@test jtprod in exclude || all(jtprod(nlp, x0, nlp.meta.y0) .== 0)
@test jprod in exclude || jprod(nlp, x0, x1) == []
@test hess in exclude || hess(nlp, x0, ones(0)) == hess(nlp, x0)
@test hprod in exclude || hprod(nlp, x0, ones(0), x1) == hprod(nlp, x0, x1)
end
end
end

function consistent_meta(nlps; rtol = 1.0e-8)
fields = [:nvar, :x0, :lvar, :uvar, :ifix, :ilow, :iupp, :irng, :ifree, :ncon, :y0]
N = length(nlps)
Expand Down
36 changes: 36 additions & 0 deletions src/nlp/problems/brownden.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,39 @@ function NLPModels.hprod!(
end
return Hv
end

function NLPModels.cons!(nlp::BROWNDEN, x::AbstractVector{T}, c) where {T}
@lencheck nlp.meta.ncon c
increment!(nlp, :neval_cons)
return c
end
function NLPModels.jac_structure!(
nlp::BROWNDEN,
rows::AbstractVector{T},
cols::AbstractVector{T},
) where {T}
@lencheck nlp.meta.nnzj rows cols
return rows, cols
end
function NLPModels.jac_coord!(nlp::BROWNDEN, x::AbstractVector{T}, vals) where {T}
@lencheck nlp.meta.nnzj vals
increment!(nlp, :neval_jac)
return vals
end
function NLPModels.jprod!(nlp::BROWNDEN, x::AbstractVector{T}, v, Jv) where {T}
@lencheck nlp.meta.nvar v
@lencheck nlp.meta.ncon Jv
increment!(nlp, :neval_jprod)
return Jv
end
function NLPModels.jtprod!(nlp::BROWNDEN, x::AbstractVector{T}, v, Jtv) where {T}
@lencheck nlp.meta.nvar Jtv
@lencheck nlp.meta.ncon v
increment!(nlp, :neval_jtprod)
fill!(Jtv, zero(T))
return Jtv
end
function NLPModels.hess_coord!(nlp::BROWNDEN, x, y, vals; obj_weight=1.0)
return hess_coord!(nlp, x, vals; obj_weight=obj_weight)
end
NLPModels.hprod!(nlp::BROWNDEN, x, y, v, Hv; obj_weight=1.0) = hprod!(nlp, x, v, Hv; obj_weight=obj_weight)
36 changes: 36 additions & 0 deletions src/nlp/problems/hs5.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,39 @@ function NLPModels.hprod!(
(-sin(x[1] + x[2]) * (v[1] + v[2]) * ones(T, 2) + 2 * [v[1] - v[2]; v[2] - v[1]]) * obj_weight
return Hv
end

function NLPModels.cons!(nlp::HS5, x::AbstractVector{T}, c) where {T}
@lencheck nlp.meta.ncon c
increment!(nlp, :neval_cons)
return c
end
function NLPModels.jac_structure!(
nlp::HS5,
rows::AbstractVector{T},
cols::AbstractVector{T},
) where {T}
@lencheck nlp.meta.nnzj rows cols
return rows, cols
end
function NLPModels.jac_coord!(nlp::HS5, x::AbstractVector{T}, vals) where {T}
@lencheck nlp.meta.nnzj vals
increment!(nlp, :neval_jac)
return vals
end
function NLPModels.jprod!(nlp::HS5, x::AbstractVector{T}, v, Jv) where {T}
@lencheck nlp.meta.nvar v
@lencheck nlp.meta.ncon Jv
increment!(nlp, :neval_jprod)
return Jv
end
function NLPModels.jtprod!(nlp::HS5, x::AbstractVector{T}, v, Jtv) where {T}
@lencheck nlp.meta.nvar Jtv
@lencheck nlp.meta.ncon v
increment!(nlp, :neval_jtprod)
fill!(Jtv, zero(T))
return Jtv
end
function NLPModels.hess_coord!(nlp::HS5, x, y, vals; obj_weight=1.0)
return hess_coord!(nlp, x, vals; obj_weight=obj_weight)
end
NLPModels.hprod!(nlp::HS5, x, y, v, Hv; obj_weight=1.0) = hprod!(nlp, x, v, Hv; obj_weight=obj_weight)
1 change: 1 addition & 0 deletions src/nls/consistency.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function consistent_nlss(
reset!(nls)
end
consistent_functions(nlss, exclude = exclude)
unconstrained_api(nlss, exclude = exclude)

if test_slack && has_inequalities(nlss[1])
reset!.(nlss)
Expand Down
36 changes: 36 additions & 0 deletions src/nls/problems/mgh01.jl
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,39 @@ function NLPModels.hprod!(
Hv .= obj_weight * [T(1) - 200 * x[2]+600 * x[1]^2 -200*x[1]; -200*x[1] T(100)] * v
return Hv
end

function NLPModels.cons!(nls::MGH01, x::AbstractVector{T}, c) where {T}
@lencheck nls.meta.ncon c
increment!(nls, :neval_cons)
return c
end
function NLPModels.jac_structure!(
nls::MGH01,
rows::AbstractVector{T},
cols::AbstractVector{T},
) where {T}
@lencheck nls.meta.nnzj rows cols
return rows, cols
end
function NLPModels.jac_coord!(nls::MGH01, x::AbstractVector{T}, vals) where {T}
@lencheck nls.meta.nnzj vals
increment!(nls, :neval_jac)
return vals
end
function NLPModels.jprod!(nls::MGH01, x::AbstractVector{T}, v, Jv) where {T}
@lencheck nls.meta.nvar v
@lencheck nls.meta.ncon Jv
increment!(nls, :neval_jprod)
return Jv
end
function NLPModels.jtprod!(nls::MGH01, x::AbstractVector{T}, v, Jtv) where {T}
@lencheck nls.meta.nvar Jtv
@lencheck nls.meta.ncon v
increment!(nls, :neval_jtprod)
fill!(Jtv, zero(T))
return Jtv
end
function NLPModels.hess_coord!(nls::MGH01, x, y, vals; obj_weight=1.0)
return hess_coord!(nls, x, vals; obj_weight=obj_weight)
end
NLPModels.hprod!(nls::MGH01, x, y, v, Hv; obj_weight=1.0) = hprod!(nls, x, v, Hv; obj_weight=obj_weight)

0 comments on commit 02d5edd

Please sign in to comment.