From 79a40accd9f23eaf6314623102e43f8782cada0c Mon Sep 17 00:00:00 2001 From: Maxence Gollier Date: Thu, 25 Jul 2024 12:43:02 +0200 Subject: [PATCH 1/4] Add meta constructor --- src/nlp/meta.jl | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/src/nlp/meta.jl b/src/nlp/meta.jl index 88d5cba2..910bd284 100644 --- a/src/nlp/meta.jl +++ b/src/nlp/meta.jl @@ -214,9 +214,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::NLPModelMeta{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) From 9d384dfbdb11fd64eaee12d208d7f22a6f542f3f Mon Sep 17 00:00:00 2001 From: MaxenceGollier <134112149+MaxenceGollier@users.noreply.github.com> Date: Fri, 26 Jul 2024 09:52:31 +0200 Subject: [PATCH 2/4] Apply suggestions from code review Co-authored-by: Tangi Migot --- src/nlp/meta.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/nlp/meta.jl b/src/nlp/meta.jl index 910bd284..8550938d 100644 --- a/src/nlp/meta.jl +++ b/src/nlp/meta.jl @@ -218,7 +218,7 @@ NLPModelMeta(nvar::Int; x0::S = zeros(nvar), kwargs...) where {S} = NLPModelMeta{eltype(S), S}(nvar, x0 = x0; kwargs...) function NLPModelMeta( - meta::NLPModelMeta{T, S}; + meta::AbstractNLPModelMeta{T, S}; nvar::Int = meta.nvar, x0::S = meta.x0, lvar::S = meta.lvar, @@ -260,7 +260,7 @@ function NLPModelMeta( lin = lin, minimize = minimize, islp = islp, - name = name + name = name, ) end From f319e8eacfc7206c007e162d6866be34d8c850d3 Mon Sep 17 00:00:00 2001 From: Maxence Gollier Date: Fri, 26 Jul 2024 11:01:36 +0200 Subject: [PATCH 3/4] Add documentation and unit test --- src/nlp/meta.jl | 4 +++- test/nlp/meta.jl | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/nlp/meta.jl b/src/nlp/meta.jl index 910bd284..507d0ddd 100644 --- a/src/nlp/meta.jl +++ b/src/nlp/meta.jl @@ -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` with the ability to change the fields defined in the keyword arguments. The following keyword arguments are accepted: - `x0`: initial guess - `lvar`: vector of lower bounds diff --git a/test/nlp/meta.jl b/test/nlp/meta.jl index 3f2d8110..4a378d10 100644 --- a/test/nlp/meta.jl +++ b/test/nlp/meta.jl @@ -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 From a609118213878efa280d54f1bd25d00dba9fc264 Mon Sep 17 00:00:00 2001 From: Tangi Migot Date: Mon, 29 Jul 2024 17:49:27 -0400 Subject: [PATCH 4/4] Update src/nlp/meta.jl --- src/nlp/meta.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nlp/meta.jl b/src/nlp/meta.jl index ab7639f1..224740d0 100644 --- a/src/nlp/meta.jl +++ b/src/nlp/meta.jl @@ -30,7 +30,7 @@ Some of their components may be infinite to indicate that the corresponding boun NLPModelMeta(meta::AbstractNLPModelMeta; kwargs...) Create an `NLPModelMeta` with `nvar` variables. -Alternatively, create an `NLPModelMeta` copy from another `AbstractNLPModelMeta` with the ability to change the fields defined in the keyword arguments. +Alternatively, create an `NLPModelMeta` copy from another `AbstractNLPModelMeta`. The following keyword arguments are accepted: - `x0`: initial guess - `lvar`: vector of lower bounds