Skip to content

Commit

Permalink
Add new NLPModelMeta constructor (#471)
Browse files Browse the repository at this point in the history
* Add meta constructor

Co-authored-by: Tangi Migot <[email protected]>
  • Loading branch information
MaxenceGollier and tmigot authored Jul 29, 2024
1 parent 1534231 commit 6ebae68
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
53 changes: 51 additions & 2 deletions src/nlp/meta.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ Some of their components may be infinite to indicate that the corresponding boun
---
NLPModelMeta(nvar; kwargs...)
NLPModelMeta(nvar::Integer; kwargs...)
NLPModelMeta(meta::AbstractNLPModelMeta; kwargs...)
Create an `NLPModelMeta` with `nvar` variables.
Alternatively, create an `NLPModelMeta` copy from another `AbstractNLPModelMeta`.
The following keyword arguments are accepted:
- `x0`: initial guess
- `lvar`: vector of lower bounds
Expand Down Expand Up @@ -214,9 +216,56 @@ function NLPModelMeta{T, S}(
)
end

NLPModelMeta(nvar; x0::S = zeros(nvar), kwargs...) where {S} =
NLPModelMeta(nvar::Int; x0::S = zeros(nvar), kwargs...) where {S} =
NLPModelMeta{eltype(S), S}(nvar, x0 = x0; kwargs...)

function NLPModelMeta(
meta::AbstractNLPModelMeta{T, S};
nvar::Int = meta.nvar,
x0::S = meta.x0,
lvar::S = meta.lvar,
uvar::S = meta.uvar,
nlvb = meta.nlvb,
nlvo = meta.nlvo,
nlvc = meta.nlvc,
ncon = meta.ncon,
y0::S = meta.y0,
lcon::S = meta.lcon,
ucon::S = meta.ucon,
nnzo = meta.nnzo,
nnzj = meta.nnzj,
lin_nnzj = meta.lin_nnzj,
nln_nnzj = meta.nln_nnzj,
nnzh = meta.nnzh,
lin = meta.lin,
minimize = meta.minimize,
islp = meta.islp,
name = meta.name,
) where {T, S}
NLPModelMeta{T, S}(
nvar,
x0 = x0,
lvar = lvar,
uvar = uvar,
nlvb = nlvb,
nlvo = nlvo,
nlvc = nlvc,
ncon = ncon,
y0 = y0,
lcon = lcon,
ucon = ucon,
nnzo = nnzo,
nnzj = nnzj,
lin_nnzj = lin_nnzj,
nln_nnzj = nln_nnzj,
nnzh = nnzh,
lin = lin,
minimize = minimize,
islp = islp,
name = name,
)
end

"""
reset_data!(nlp)
Expand Down
21 changes: 21 additions & 0 deletions test/nlp/meta.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
@testset "A problem with zero variables doesn't make sense." begin
@test_throws ErrorException NLPModelMeta(0)
end

@testset "Meta copier." begin
nlp = SimpleNLPModel()

# Check simple copy
meta = NLPModelMeta(nlp.meta)
for field in fieldnames(typeof(nlp.meta))
@test getfield(nlp.meta, field) == getfield(meta, field)
end

modif = Dict(:nnzh => 1, :x0 => [2.0; 2.0; 0.0], :nvar => 3, :lvar => zeros(3), :uvar => [1.0; 1.0; 0.0])
meta = NLPModelMeta(nlp.meta; modif...)

for field in setdiff(fieldnames(typeof(nlp.meta)), union(keys(modif),[:ifix]))
@test getfield(nlp.meta, field) == getfield(meta, field)
end
for field in keys(modif)
@test getfield(meta, field) == modif[field]
end
@test meta.ifix == [3]
end

0 comments on commit 6ebae68

Please sign in to comment.