Skip to content

Commit 7cdca7d

Browse files
Bump to LinearOperators 2 and NLPModels 0.17 (#21)
* compat linop and NLPModels * add more tests * fix issue test nls * type issue hess multi precision * return res * simplify test slacknls
1 parent 26c136e commit 7cdca7d

10 files changed

Lines changed: 188 additions & 121 deletions

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
1212

1313
[compat]
1414
FastClosures = "0.2.1, 0.3.0"
15-
LinearOperators = "1.1.0"
16-
NLPModels = "0.16"
15+
LinearOperators = "2.0"
16+
NLPModels = "0.17"
1717
julia = "^1.3.0"
1818

1919
[extras]

src/feasibility-form-nls.jl

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ function NLPModels.hess_coord!(
233233
xr::AbstractVector,
234234
y::AbstractVector,
235235
vals::AbstractVector;
236-
obj_weight::Float64 = 1.0,
236+
obj_weight::Real = one(eltype(xr)),
237237
)
238238
@lencheck nlp.meta.nvar xr
239239
@lencheck nlp.meta.ncon y
@@ -261,16 +261,17 @@ function NLPModels.hprod!(
261261
y::AbstractVector,
262262
v::AbstractVector,
263263
hv::AbstractVector;
264-
obj_weight::Float64 = 1.0,
264+
obj_weight::Real = one(eltype(xr)),
265265
)
266266
@lencheck nlp.meta.nvar xr v hv
267267
@lencheck nlp.meta.ncon y
268268
n, m, ne = nlp.internal.meta.nvar, nlp.internal.meta.ncon, nlp.internal.nls_meta.nequ
269269
x = @view xr[1:n]
270+
T = eltype(xr)
270271
if m > 0
271-
@views hprod!(nlp.internal, x, y[(ne + 1):end], v[1:n], hv[1:n], obj_weight = 0.0)
272+
@views hprod!(nlp.internal, x, y[(ne + 1):end], v[1:n], hv[1:n], obj_weight = zero(T))
272273
else
273-
fill!(hv, 0.0)
274+
fill!(hv, zero(T))
274275
end
275276
for i = 1:ne
276277
@views hv[1:n] .+= hprod_residual(nlp.internal, x, i, v[1:n]) * y[i]
@@ -356,7 +357,7 @@ function NLPModels.jtprod_residual!(
356357
@lencheck nlp.nls_meta.nequ v
357358
increment!(nlp, :neval_jtprod_residual)
358359
n, ne = nlp.internal.meta.nvar, nlp.internal.nls_meta.nequ
359-
Jtv[1:n] .= 0.0
360+
Jtv[1:n] .= zero(eltype(x))
360361
Jtv[(n + 1):end] .= v
361362
return Jtv
362363
end
@@ -387,7 +388,7 @@ function NLPModels.jth_hess_residual(nlp::FeasibilityFormNLS, x::AbstractVector,
387388
@lencheck nlp.meta.nvar x
388389
increment!(nlp, :neval_jhess_residual)
389390
n = nlp.meta.nvar
390-
return spzeros(n, n)
391+
return spzeros(eltype(x), n, n)
391392
end
392393

393394
function NLPModels.hprod_residual!(
@@ -399,6 +400,6 @@ function NLPModels.hprod_residual!(
399400
)
400401
@lencheck nlp.meta.nvar x v Hiv
401402
increment!(nlp, :neval_hprod_residual)
402-
fill!(Hiv, 0.0)
403+
fill!(Hiv, zero(eltype(x)))
403404
return Hiv
404405
end

src/feasibility-residual.jl

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ end
128128

129129
function NLPModels.hess_residual(nls::FeasibilityResidual, x::AbstractVector, v::AbstractVector)
130130
increment!(nls, :neval_hess_residual)
131-
return hess(nls.nlp, x, v, obj_weight = 0.0)
131+
return hess(nls.nlp, x, v, obj_weight = zero(eltype(x)))
132132
end
133133

134134
function NLPModels.hess_structure_residual!(
@@ -146,14 +146,15 @@ function NLPModels.hess_coord_residual!(
146146
vals::AbstractVector,
147147
)
148148
increment!(nls, :neval_hess_residual)
149-
return hess_coord!(nls.nlp, x, v, vals, obj_weight = 0.0)
149+
return hess_coord!(nls.nlp, x, v, vals, obj_weight = zero(eltype(x)))
150150
end
151151

152152
function NLPModels.jth_hess_residual(nls::FeasibilityResidual, x::AbstractVector, i::Int)
153153
increment!(nls, :neval_jhess_residual)
154-
y = zeros(nls.nls_meta.nequ)
155-
y[i] = 1.0
156-
return hess(nls.nlp, x, y, obj_weight = 0.0)
154+
T = eltype(x)
155+
y = zeros(T, nls.nls_meta.nequ)
156+
y[i] = one(T)
157+
return hess(nls.nlp, x, y, obj_weight = zero(T))
157158
end
158159

159160
function NLPModels.hprod_residual!(
@@ -164,9 +165,10 @@ function NLPModels.hprod_residual!(
164165
Hiv::AbstractVector,
165166
)
166167
increment!(nls, :neval_hprod_residual)
167-
y = zeros(nls.nls_meta.nequ)
168-
y[i] = 1.0
169-
return hprod!(nls.nlp, x, y, v, Hiv, obj_weight = 0.0)
168+
T = eltype(x)
169+
y = zeros(T, nls.nls_meta.nequ)
170+
y[i] = one(T)
171+
return hprod!(nls.nlp, x, y, v, Hiv, obj_weight = zero(T))
170172
end
171173

172174
function NLPModels.hess(
@@ -178,7 +180,7 @@ function NLPModels.hess(
178180
cx = cons(nls.nlp, x)
179181
Jx = jac(nls.nlp, x)
180182
Hx = tril(Jx' * Jx)
181-
Hx .+= hess(nls.nlp, x, cx, obj_weight = 0.0).data
183+
Hx .+= hess(nls.nlp, x, cx, obj_weight = zero(eltype(x))).data
182184
return Symmetric(obj_weight * Hx, :L)
183185
end
184186

@@ -193,8 +195,9 @@ function NLPModels.hprod!(
193195
cx = cons(nls.nlp, x)
194196
Jv = jprod(nls.nlp, x, v)
195197
jtprod!(nls.nlp, x, Jv, Hv)
196-
Hiv = zeros(eltype(x), nls.meta.nvar)
197-
hprod!(nls.nlp, x, cx, v, Hiv, obj_weight = 0.0)
198+
T = eltype(x)
199+
Hiv = zeros(T, nls.meta.nvar)
200+
hprod!(nls.nlp, x, cx, v, Hiv, obj_weight = zero(T))
198201
Hv .+= Hiv
199202
Hv .*= obj_weight
200203
return Hv

src/quasi-newton.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ function NLPModels.hprod!(
8484
Hv::AbstractVector;
8585
kwargs...,
8686
)
87-
Hv[1:(nlp.meta.nvar)] .= nlp.op * v
87+
@views mul!(Hv[1:(nlp.meta.nvar)], nlp.op, v)
8888
return Hv
8989
end
9090

src/slack-model.jl

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ function slack_meta(meta::NLPModelMeta{T, S}; name = meta.name * "-slack") where
108108
lin = meta.lin,
109109
nln = meta.nln,
110110
name = name,
111+
minimize = meta.minimize,
111112
)
112113
end
113114

@@ -465,16 +466,32 @@ function NLPModels.jac_op_residual!(
465466
)
466467
@lencheck nls.meta.nvar x Jtv
467468
@lencheck nls.nls_meta.nequ Jv
468-
prod = @closure v -> jprod_residual!(nls, x, v, Jv)
469-
ctprod = @closure v -> jtprod_residual!(nls, x, v, Jtv)
469+
prod! = @closure (res, v, α, β) -> begin
470+
jprod_residual!(nls, x, v, Jv)
471+
if β == 0
472+
@. res = α * Jv
473+
else
474+
@. res = α * Jv + β * res
475+
end
476+
return res
477+
end
478+
ctprod! = @closure (res, v, α, β) -> begin
479+
jtprod_residual!(nls, x, v, Jtv)
480+
if β == 0
481+
@. res = α * Jtv
482+
else
483+
@. res = α * Jtv + β * res
484+
end
485+
return res
486+
end
470487
return LinearOperator{eltype(x)}(
471488
nls_meta(nls).nequ,
472489
nls_meta(nls).nvar,
473490
false,
474491
false,
475-
prod,
476-
ctprod,
477-
ctprod,
492+
prod!,
493+
ctprod!,
494+
ctprod!,
478495
)
479496
end
480497

@@ -550,14 +567,22 @@ function NLPModels.hess_op_residual!(
550567
Hiv::AbstractVector,
551568
)
552569
@lencheck nls.meta.nvar x Hiv
553-
prod = @closure v -> hprod_residual!(nls, x, i, v, Hiv)
570+
prod! = @closure (res, v, α, β) -> begin
571+
hprod_residual!(nls, x, i, v, Hiv)
572+
if β == 0
573+
@. res = α * Hiv
574+
else
575+
@. res = α * Hiv + β * res
576+
end
577+
return res
578+
end
554579
return LinearOperator{eltype(x)}(
555580
nls_meta(nls).nvar,
556581
nls_meta(nls).nvar,
557582
true,
558583
true,
559-
prod,
560-
prod,
561-
prod,
584+
prod!,
585+
prod!,
586+
prod!,
562587
)
563588
end

test/nlp/quasi-newton.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@
1212
n = nlp.meta.nvar
1313
m = nlp.meta.ncon
1414

15-
s, y = randn(n), randn(n)
15+
s, y = randn(T, n), randn(T, n)
1616
B = QNO(n)
1717
push!(B, s, y)
1818
push!(nlp, s, y)
1919
H(x) = B
2020
H(x, y) = B
2121

22-
y = randn(m)
23-
x = randn(n)
24-
v = randn(n)
25-
w = randn(m)
26-
Jv = zeros(m)
27-
Jtw = zeros(n)
28-
Hv = zeros(n)
29-
Hvals = zeros(nlp.meta.nnzh)
22+
y = randn(T, m)
23+
x = randn(T, n)
24+
v = randn(T, n)
25+
w = randn(T, m)
26+
Jv = zeros(T, m)
27+
Jtw = zeros(T, n)
28+
Hv = zeros(T, n)
29+
Hvals = zeros(T, nlp.meta.nnzh)
3030

3131
# Basic methods
3232
@test obj(nlp, x) f(x)

test/nlp/slack-model.jl

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
@testset "SlackModel NLP tests" begin
22
@testset "API" for T in [Float64, Float32]
33
f(x) = (x[1] - 2)^2 + (x[2] - 1)^2
4-
∇f(x) = [2 * (x[1] - 2); 2 * (x[2] - 1); 0]
5-
H(x) = [2.0 0 0; 0 2.0 0; 0 0 0]
6-
c(x) = [x[1] - 2x[2] + 1; -x[1]^2 / 4 - x[2]^2 + 1 - x[3]]
7-
J(x) = [1.0 -2.0 0; -0.5x[1] -2.0x[2] -1]
8-
H(x, y) = H(x) + y[2] * [-0.5 0 0; 0 -2.0 0; 0 0 0]
4+
∇f(x) = T[2 * (x[1] - 2); 2 * (x[2] - 1); 0]
5+
H(x) = T[2.0 0 0; 0 2.0 0; 0 0 0]
6+
c(x) = T[x[1] - 2x[2] + 1; -x[1]^2 / 4 - x[2]^2 + 1 - x[3]]
7+
J(x) = T[1.0 -2.0 0; -0.5x[1] -2.0x[2] -1]
8+
H(x, y) = H(x) + y[2] * T[-0.5 0 0; 0 -2.0 0; 0 0 0]
99

1010
nlp = SlackModel(SimpleNLPModel(T))
1111
n = nlp.meta.nvar
1212
m = nlp.meta.ncon
1313

14-
x = randn(n)
15-
y = randn(m)
16-
v = randn(n)
17-
w = randn(m)
18-
Jv = zeros(m)
19-
Jtw = zeros(n)
20-
Hv = zeros(n)
21-
Hvals = zeros(nlp.meta.nnzh)
14+
x = randn(T, n)
15+
y = randn(T, m)
16+
v = randn(T, n)
17+
w = randn(T, m)
18+
Jv = zeros(T, m)
19+
Jtw = zeros(T, n)
20+
Hv = zeros(T, n)
21+
Hvals = zeros(T, nlp.meta.nnzh)
2222

2323
# Basic methods
2424
@test obj(nlp, x) f(x)
@@ -52,15 +52,23 @@
5252
Jop = jac_op!(nlp, x, Jv, Jtw)
5353
@test Jop * v J(x) * v
5454
@test Jop' * w J(x)' * w
55+
res = J(x) * v - w
56+
@test mul!(w, Jop, v, one(T), -one(T)) res
57+
res = J(x)' * w - v
58+
@test mul!(v, Jop', w, one(T), -one(T)) res
5559
Jop = jac_op!(nlp, jac_structure(nlp)..., jac_coord(nlp, x), Jv, Jtw)
5660
@test Jop * v J(x) * v
5761
@test Jop' * w J(x)' * w
62+
res = J(x) * v - w
63+
@test mul!(w, Jop, v, one(T), -one(T)) res
64+
res = J(x)' * w - v
65+
@test mul!(v, Jop', w, one(T), -one(T)) res
5866
Jop = jac_op!(nlp, x, jac_structure(nlp)..., Jv, Jtw)
5967
@test Jop * v J(x) * v
6068
@test Jop' * w J(x)' * w
61-
ghjv = zeros(m)
69+
ghjv = zeros(T, m)
6270
for j = 1:m
63-
eⱼ = [i == j ? 1.0 : 0.0 for i = 1:m]
71+
eⱼ = [i == j ? one(T) : zero(T) for i = 1:m]
6472
Cⱼ(x) = H(x, eⱼ) - H(x)
6573
ghjv[j] = dot(gx, Cⱼ(x) * v)
6674
end
@@ -73,14 +81,21 @@
7381
@test Hop * v H(x) * v
7482
Hop = hess_op!(nlp, x, Hv)
7583
@test Hop * v H(x) * v
84+
z = ones(T, nlp.meta.nvar)
85+
res = H(x) * v - z
86+
@test mul!(z, Hop, v, one(T), -one(T)) res
7687
Hop = hess_op!(nlp, hess_structure(nlp)..., hess_coord(nlp, x), Hv)
7788
@test Hop * v H(x) * v
89+
res = H(x) * v - z
90+
@test mul!(z, Hop, v, one(T), -one(T)) res
7891
Hop = hess_op!(nlp, x, hess_structure(nlp)..., Hv)
7992
@test Hop * v H(x) * v
8093
Hop = hess_op(nlp, x, y)
8194
@test Hop * v H(x, y) * v
8295
Hop = hess_op!(nlp, x, y, Hv)
8396
@test Hop * v H(x, y) * v
97+
res = H(x, y) * v - z
98+
@test mul!(z, Hop, v, one(T), -one(T)) res
8499
Hop = hess_op!(nlp, hess_structure(nlp)..., hess_coord(nlp, x, y), Hv)
85100
@test Hop * v H(x, y) * v
86101
Hop = hess_op!(nlp, x, y, hess_structure(nlp)..., Hv)

0 commit comments

Comments
 (0)