Skip to content

Commit 2bcdcca

Browse files
tmigotprobot-auto-merge[bot]
authored andcommitted
add parametric meta
1 parent 038ccd8 commit 2bcdcca

9 files changed

Lines changed: 67 additions & 56 deletions

src/feasibility-form-nls.jl

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ to
2222
If you rather have the first problem, the `nls` model already works as an NLPModel of
2323
that format.
2424
"""
25-
mutable struct FeasibilityFormNLS{M <: AbstractNLSModel} <: AbstractNLSModel
26-
meta::NLPModelMeta
27-
nls_meta::NLSMeta
25+
mutable struct FeasibilityFormNLS{T, S, M <: AbstractNLSModel{T, S}} <: AbstractNLSModel{T, S}
26+
meta::NLPModelMeta{T, S}
27+
nls_meta::NLSMeta{T, S}
2828
internal::M
2929
counters::NLSCounters
3030
end
@@ -39,38 +39,41 @@ end
3939
Converts a nonlinear least-squares problem with residual `F(x)` to a nonlinear
4040
optimization problem with constraints `F(x) = r` and objective `¹/₂‖r‖²`.
4141
"""
42-
function FeasibilityFormNLS(nls::AbstractNLSModel; name = "$(nls.meta.name)-ffnls")
42+
function FeasibilityFormNLS(
43+
nls::AbstractNLSModel{T, S};
44+
name = "$(nls.meta.name)-ffnls",
45+
) where {T,S}
4346
nequ = nls.nls_meta.nequ
4447
meta = nls.meta
4548
nvar = meta.nvar + nequ
4649
ncon = meta.ncon + nequ
4750
nnzh = nls.nls_meta.nnzh + nequ + (meta.ncon == 0 ? 0 : meta.nnzh) # Some indexes can be repeated
48-
meta = NLPModelMeta(
51+
meta = NLPModelMeta{T, S}(
4952
nvar,
50-
x0 = [meta.x0; zeros(nequ)],
51-
lvar = [meta.lvar; fill(-Inf, nequ)],
52-
uvar = [meta.uvar; fill(Inf, nequ)],
53+
x0 = [meta.x0; zeros(T, nequ)],
54+
lvar = [meta.lvar; fill(T(-Inf), nequ)],
55+
uvar = [meta.uvar; fill(T(Inf), nequ)],
5356
ncon = ncon,
54-
lcon = [zeros(nequ); meta.lcon],
55-
ucon = [zeros(nequ); meta.ucon],
56-
y0 = [zeros(nequ); meta.y0],
57+
lcon = [zeros(T, nequ); meta.lcon],
58+
ucon = [zeros(T, nequ); meta.ucon],
59+
y0 = [zeros(T, nequ); meta.y0],
5760
lin = [nls.nls_meta.lin; meta.lin .+ nequ],
5861
nln = [nls.nls_meta.nln; meta.nln .+ nequ],
5962
nnzj = meta.nnzj + nls.nls_meta.nnzj + nequ,
6063
nnzh = nnzh,
6164
name = name,
6265
)
63-
nls_meta = NLSMeta(
66+
nls_meta = NLSMeta{T, S}(
6467
nequ,
6568
nvar,
66-
x0 = [meta.x0; zeros(nequ)],
69+
x0 = [meta.x0; zeros(T, nequ)],
6770
nnzj = nequ,
6871
nnzh = 0,
6972
lin = 1:nequ,
7073
nln = Int[],
7174
)
7275

73-
nlp = FeasibilityFormNLS{typeof(nls)}(meta, nls_meta, nls, NLSCounters())
76+
nlp = FeasibilityFormNLS{T, S, typeof(nls)}(meta, nls_meta, nls, NLSCounters())
7477
finalizer(nlp -> finalize(nlp.internal), nlp)
7578

7679
return nlp

src/feasibility-residual.jl

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ The slack variables are created using SlackModel.
2525
If ``\\ell_i = u_i``, no slack variable is created.
2626
In particular, if there are only equality constrained of the form ``c(x) = 0``, the resulting NLS is simply ``\\min_x \\tfrac{1}{2}\\|c(x)\\|^2``.
2727
"""
28-
mutable struct FeasibilityResidual <: AbstractNLSModel
29-
meta::NLPModelMeta
30-
nls_meta::NLSMeta
28+
mutable struct FeasibilityResidual{T, S} <: AbstractNLSModel{T, S}
29+
meta::NLPModelMeta{T, S}
30+
nls_meta::NLSMeta{T, S}
3131
counters::NLSCounters
32-
nlp::AbstractNLPModel
32+
nlp::AbstractNLPModel{T, S}
3333
end
3434

3535
function NLPModels.show_header(io::IO, nls::FeasibilityResidual)
@@ -39,7 +39,10 @@ function NLPModels.show_header(io::IO, nls::FeasibilityResidual)
3939
)
4040
end
4141

42-
function FeasibilityResidual(nlp::AbstractNLPModel; name = "$(nlp.meta.name)-feasres")
42+
function FeasibilityResidual(
43+
nlp::AbstractNLPModel{T, S};
44+
name = "$(nlp.meta.name)-feasres",
45+
) where {T, S}
4346
if !equality_constrained(nlp)
4447
if unconstrained(nlp)
4548
throw(ErrorException("Can't handle unconstrained problem"))
@@ -52,15 +55,15 @@ function FeasibilityResidual(nlp::AbstractNLPModel; name = "$(nlp.meta.name)-fea
5255

5356
m, n = nlp.meta.ncon, nlp.meta.nvar
5457
# TODO: What is copied?
55-
meta = NLPModelMeta(
58+
meta = NLPModelMeta{T, S}(
5659
n,
5760
x0 = nlp.meta.x0,
5861
name = name,
5962
lvar = nlp.meta.lvar,
6063
uvar = nlp.meta.uvar,
6164
nnzj = 0,
6265
)
63-
nls_meta = NLSMeta(
66+
nls_meta = NLSMeta{T, S}(
6467
m,
6568
n,
6669
nnzj = nlp.meta.nnzj,

src/model-interaction.jl

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
function FeasibilityFormNLS(nls::FeasibilityResidual; name = "$(nls.meta.name)-ffnls")
1+
function FeasibilityFormNLS(
2+
nls::FeasibilityResidual{T, S};
3+
name = "$(nls.meta.name)-ffnls",
4+
) where {T, S}
25
meta = nls.nlp.meta
36
nequ = meta.ncon
47
nvar = meta.nvar + nequ
58
ncon = meta.ncon
69
nnzj = meta.nnzj + nequ
710
nnzh = meta.nnzh + nequ
8-
meta = NLPModelMeta(
11+
meta = NLPModelMeta{T, S}(
912
nvar,
10-
x0 = [meta.x0; zeros(nequ)],
11-
lvar = [meta.lvar; fill(-Inf, nequ)],
12-
uvar = [meta.uvar; fill(Inf, nequ)],
13+
x0 = [meta.x0; zeros(T, nequ)],
14+
lvar = [meta.lvar; fill(T(-Inf), nequ)],
15+
uvar = [meta.uvar; fill(T(Inf), nequ)],
1316
ncon = ncon,
1417
lcon = meta.lcon,
1518
ucon = meta.ucon,
@@ -20,9 +23,9 @@ function FeasibilityFormNLS(nls::FeasibilityResidual; name = "$(nls.meta.name)-f
2023
nnzh = nnzh,
2124
name = name,
2225
)
23-
nls_meta = NLSMeta(nequ, nvar, x0 = [meta.x0; zeros(nequ)], nnzj = nequ, nnzh = 0)
26+
nls_meta = NLSMeta{T, S}(nequ, nvar, x0 = [meta.x0; zeros(T, nequ)], nnzj = nequ, nnzh = 0)
2427

25-
nlp = FeasibilityFormNLS{FeasibilityResidual}(meta, nls_meta, nls, NLSCounters())
28+
nlp = FeasibilityFormNLS{T, S, FeasibilityResidual{T, S}}(meta, nls_meta, nls, NLSCounters())
2629
finalizer(nlp -> finalize(nlp.internal), nlp)
2730

2831
return nlp

src/quasi-newton.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
export QuasiNewtonModel, LBFGSModel, LSR1Model
22

3-
abstract type QuasiNewtonModel <: AbstractNLPModel end
3+
abstract type QuasiNewtonModel{T, S} <: AbstractNLPModel{T, S} end
44

5-
mutable struct LBFGSModel <: QuasiNewtonModel
6-
meta::NLPModelMeta
7-
model::AbstractNLPModel
5+
mutable struct LBFGSModel{T, S} <: QuasiNewtonModel{T, S}
6+
meta::NLPModelMeta{T, S}
7+
model::AbstractNLPModel{T, S}
88
op::LBFGSOperator
99
end
1010

11-
mutable struct LSR1Model <: QuasiNewtonModel
12-
meta::NLPModelMeta
13-
model::AbstractNLPModel
11+
mutable struct LSR1Model{T, S} <: QuasiNewtonModel{T, S}
12+
meta::NLPModelMeta{T, S}
13+
model::AbstractNLPModel{T, S}
1414
op::LSR1Operator
1515
end
1616

src/slack-model.jl

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ The slack variables are implicitly ordered as `[s(low), s(upp), s(rng)]`, where
4444
``c_L ≤ c(x) < ∞``, ``-∞ < c(x) ≤ c_U`` and
4545
``c_L ≤ c(x) ≤ c_U``, respectively.
4646
"""
47-
mutable struct SlackModel <: AbstractNLPModel
48-
meta::NLPModelMeta
49-
model::AbstractNLPModel
47+
mutable struct SlackModel{T, S} <: AbstractNLPModel{T, S}
48+
meta::NLPModelMeta{T, S}
49+
model::AbstractNLPModel{T, S}
5050
end
5151

5252
NLPModels.show_header(io::IO, nlp::SlackModel) =
@@ -60,10 +60,10 @@ end
6060

6161
"""Like `SlackModel`, this model converts inequalities into equalities and bounds.
6262
"""
63-
mutable struct SlackNLSModel <: AbstractNLSModel
64-
meta::NLPModelMeta
65-
nls_meta::NLSMeta
66-
model::AbstractNLPModel
63+
mutable struct SlackNLSModel{T, S} <: AbstractNLSModel{T, S}
64+
meta::NLPModelMeta{T, S}
65+
nls_meta::NLSMeta{T, S}
66+
model::AbstractNLPModel{T, S}
6767
end
6868

6969
NLPModels.show_header(io::IO, nls::SlackNLSModel) =
@@ -75,12 +75,11 @@ function Base.show(io::IO, nls::SlackNLSModel)
7575
show(io, nls.model.counters)
7676
end
7777

78-
function slack_meta(meta::NLPModelMeta; name = meta.name * "-slack")
78+
function slack_meta(meta::NLPModelMeta{T, S}; name = meta.name * "-slack") where {T, S}
7979
ns = meta.ncon - length(meta.jfix)
8080
jlow = meta.jlow
8181
jupp = meta.jupp
8282
jrng = meta.jrng
83-
T = eltype(meta.x0)
8483

8584
# Don't introduce slacks for equality constraints!
8685
lvar = [meta.lvar; meta.lcon[[jlow; jupp; jrng]]] # l ≤ x and cₗ ≤ s
@@ -119,12 +118,15 @@ function SlackModel(model::AbstractNLPModel; name = model.meta.name * "-slack")
119118
return snlp
120119
end
121120

122-
function SlackNLSModel(model::AbstractNLSModel; name = model.meta.name * "-slack")
121+
function SlackNLSModel(
122+
model::AbstractNLSModel{T, S};
123+
name = model.meta.name * "-slack",
124+
) where {T, S}
123125
ns = model.meta.ncon - length(model.meta.jfix)
124126
ns == 0 && return model
125127

126128
meta = slack_meta(model.meta, name = name)
127-
nls_meta = NLSMeta(
129+
nls_meta = NLSMeta{T, S}(
128130
model.nls_meta.nequ,
129131
model.meta.nvar + ns,
130132
x0 = [model.meta.x0; zeros(eltype(model.meta.x0), ns)],

test/nlp/quasi-newton.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
io = IOBuffer()
7878
show(io, nlp)
7979
showed = String(take!(io))
80-
expected = """LSR1Model - A QuasiNewtonModel
80+
expected = """LSR1Model{Float64, Vector{Float64}} - A QuasiNewtonModel
8181
Problem name: Simple NLP Model
8282
All variables: ████████████████████ 2 All constraints: ████████████████████ 2
8383
free: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0 free: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0
@@ -102,7 +102,7 @@
102102
io = IOBuffer()
103103
show(io, nlp)
104104
showed = String(take!(io))
105-
expected = """LBFGSModel - A QuasiNewtonModel
105+
expected = """LBFGSModel{Float64, Vector{Float64}} - A QuasiNewtonModel
106106
Problem name: Simple NLP Model
107107
All variables: ████████████████████ 2 All constraints: ████████████████████ 2
108108
free: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0 free: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0

test/nlp/simple-model.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ Modified problem 14 in the Hock-Schittkowski suite
1111
1212
x₀ = [2.0, 2.0].
1313
"""
14-
mutable struct SimpleNLPModel <: AbstractNLPModel
15-
meta::NLPModelMeta
14+
mutable struct SimpleNLPModel{T, S} <: AbstractNLPModel{T, S}
15+
meta::NLPModelMeta{T, S}
1616
counters::Counters
1717
end
1818

test/nls/feasibility-residual.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595

9696
@testset "Show" begin
9797
nls = FeasibilityResidual(SimpleNLSModel())
98-
@test typeof(nls.nlp) == SlackNLSModel
98+
@test typeof(nls.nlp) == SlackNLSModel{Float64, Vector{Float64}}
9999
io = IOBuffer()
100100
show(io, nls)
101101
showed = String(take!(io))
@@ -125,8 +125,8 @@
125125
end
126126

127127
@testset "FeasibilityResidual of an unconstrained problem" begin
128-
mutable struct UncModel <: AbstractNLPModel
129-
meta::NLPModelMeta
128+
mutable struct UncModel{T, S} <: AbstractNLPModel{T, S}
129+
meta::NLPModelMeta{T, S}
130130
end
131131
nlp = UncModel(NLPModelMeta(2))
132132
@test_throws ErrorException FeasibilityResidual(nlp)

test/nls/simple-model.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ x₀ = ones(n).
1818
1919
Modified SimpleNLSModel.
2020
"""
21-
mutable struct SimpleNLSModel <: AbstractNLSModel
22-
meta::NLPModelMeta
23-
nls_meta::NLSMeta
21+
mutable struct SimpleNLSModel{T, S} <: AbstractNLSModel{T, S}
22+
meta::NLPModelMeta{T, S}
23+
nls_meta::NLSMeta{T, S}
2424
counters::NLSCounters
2525
end
2626

0 commit comments

Comments
 (0)