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

Add new NLPModelMeta constructor #471

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading