Skip to content

Commit

Permalink
NLPModel use meta type
Browse files Browse the repository at this point in the history
  • Loading branch information
tmigot committed Apr 1, 2024
1 parent 4fe997c commit 9a74ab1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
26 changes: 20 additions & 6 deletions src/ADNLPModels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,19 @@ function ADNLPModel!(model::AbstractNLPModel; kwargs...)
end

function ADNLPModel(model::AbstractNLPModel; kwargs...)
function model_c(x; model = model)
cx = similar(x, model.meta.ncon)
return cons!(model, x, cx)
end

return if model.meta.nlin > 0
ADNLPModel(
x -> obj(model, x),
model.meta.x0,
model.meta.lvar,
model.meta.uvar,
jac_lin(model, model.meta.x0),
x -> cons(model, x),
model_c,
model.meta.lcon,
model.meta.ucon;
kwargs...,
Expand All @@ -99,7 +104,7 @@ function ADNLPModel(model::AbstractNLPModel; kwargs...)
model.meta.x0,
model.meta.lvar,
model.meta.uvar,
x -> cons(model, x),
model_c,
model.meta.lcon,
model.meta.ucon;
kwargs...,
Expand All @@ -110,27 +115,36 @@ end
include("nls.jl")

function ADNLSModel(model::AbstractNLSModel; kwargs...)
function model_c(x; model = model)
cx = similar(x, model.meta.ncon)
return cons!(model, x, cx)
end
function model_F(x; model = model)
Fx = similar(x, model.nls_meta.nequ)
return residual!(model, x, Fx)
end

return if model.meta.nlin > 0
ADNLSModel(
x -> residual(model, x),
model_F,
model.meta.x0,
model.nls_meta.nequ,
model.meta.lvar,
model.meta.uvar,
jac_lin(model, model.meta.x0),
x -> cons(model, x),
model_c,
model.meta.lcon,
model.meta.ucon;
kwargs...,
)
else
ADNLSModel(
x -> residual(model, x),
model_F,
model.meta.x0,
model.nls_meta.nequ,
model.meta.lvar,
model.meta.uvar,
x -> cons(model, x),
model_c,
model.meta.lcon,
model.meta.ucon;
kwargs...,
Expand Down
2 changes: 1 addition & 1 deletion src/nlp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ ADNLPModel(
adbackend::ADModelBackend,
f,
c,
) where {T, S} = ADNLPModel(meta, counters, adbackend, f, Int[], Int[], similar(meta.x0, 0), c)
) where {T, S} = ADNLPModel(meta, counters, adbackend, f, Int[], Int[], S(undef, 0), c)

ADNLPModels.show_header(io::IO, nlp::ADNLPModel) =
println(io, "ADNLPModel - Model with automatic differentiation backend $(nlp.adbackend)")
Expand Down
2 changes: 1 addition & 1 deletion src/nls.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ ADNLSModel(
F,
c,
) where {T, S} =
ADNLSModel(meta, nls_meta, counters, adbackend, F, Int[], Int[], similar(meta.x0, 0), c)
ADNLSModel(meta, nls_meta, counters, adbackend, F, Int[], Int[], S(undef, 0), c)

ADNLPModels.show_header(io::IO, nls::ADNLSModel) = println(
io,
Expand Down

0 comments on commit 9a74ab1

Please sign in to comment.