Skip to content

Commit 93b67eb

Browse files
tmigotprobot-auto-merge[bot]
authored andcommitted
add test with parametric meta
1 parent 2bcdcca commit 93b67eb

7 files changed

Lines changed: 492 additions & 472 deletions

File tree

test/nlp/quasi-newton.jl

Lines changed: 60 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -6,69 +6,71 @@
66
J(x) = [1.0 -2.0; -0.5x[1] -2.0x[2]]
77

88
for (QNM, QNO) in [(LSR1Model, LSR1Operator), (LBFGSModel, LBFGSOperator)]
9-
nlp = QNM(SimpleNLPModel())
10-
n = nlp.meta.nvar
11-
m = nlp.meta.ncon
9+
for dt in [Float64, Float32]
10+
nlp = QNM(SimpleNLPModel(zero(dt)))
11+
n = nlp.meta.nvar
12+
m = nlp.meta.ncon
1213

13-
s, y = randn(n), randn(n)
14-
B = QNO(n)
15-
push!(B, s, y)
16-
push!(nlp, s, y)
17-
H(x) = B
18-
H(x, y) = B
14+
s, y = randn(n), randn(n)
15+
B = QNO(n)
16+
push!(B, s, y)
17+
push!(nlp, s, y)
18+
H(x) = B
19+
H(x, y) = B
1920

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

29-
# Basic methods
30-
@test obj(nlp, x) f(x)
31-
@test grad(nlp, x) ∇f(x)
32-
@test hprod(nlp, x, v) H(x) * v
33-
@test cons(nlp, x) c(x)
34-
@test jac(nlp, x) J(x)
35-
@test jprod(nlp, x, v) J(x) * v
36-
@test jtprod(nlp, x, w) J(x)' * w
30+
# Basic methods
31+
@test obj(nlp, x) f(x)
32+
@test grad(nlp, x) ∇f(x)
33+
@test hprod(nlp, x, v) H(x) * v
34+
@test cons(nlp, x) c(x)
35+
@test jac(nlp, x) J(x)
36+
@test jprod(nlp, x, v) J(x) * v
37+
@test jtprod(nlp, x, w) J(x)' * w
3738

38-
# Increasing coverage
39-
fx, cx = objcons(nlp, x)
40-
@test fx f(x)
41-
@test cx c(x)
42-
fx, _ = objcons!(nlp, x, cx)
43-
@test fx f(x)
44-
@test cx c(x)
45-
fx, gx = objgrad(nlp, x)
46-
@test fx f(x)
47-
@test gx ∇f(x)
48-
fx, _ = objgrad!(nlp, x, gx)
49-
@test fx f(x)
50-
@test gx ∇f(x)
51-
@test jprod!(nlp, jac_structure(nlp)..., jac_coord(nlp, x), v, Jv) J(x) * v
52-
@test jprod!(nlp, x, jac_structure(nlp)..., v, Jv) J(x) * v
53-
@test jtprod!(nlp, jac_structure(nlp)..., jac_coord(nlp, x), w, Jtw) J(x)' * w
54-
@test jtprod!(nlp, x, jac_structure(nlp)..., w, Jtw) J(x)' * w
55-
Jop = jac_op!(nlp, x, Jv, Jtw)
56-
@test Jop * v J(x) * v
57-
@test Jop' * w J(x)' * w
58-
Jop = jac_op!(nlp, jac_structure(nlp)..., jac_coord(nlp, x), Jv, Jtw)
59-
@test Jop * v J(x) * v
60-
@test Jop' * w J(x)' * w
61-
Jop = jac_op!(nlp, x, jac_structure(nlp)..., Jv, Jtw)
62-
@test Jop * v J(x) * v
63-
@test Jop' * w J(x)' * w
64-
Hop = hess_op(nlp, x)
65-
@test Hop * v H(x) * v
66-
Hop = hess_op!(nlp, x, Hv)
67-
@test Hop * v H(x) * v
39+
# Increasing coverage
40+
fx, cx = objcons(nlp, x)
41+
@test fx f(x)
42+
@test cx c(x)
43+
fx, _ = objcons!(nlp, x, cx)
44+
@test fx f(x)
45+
@test cx c(x)
46+
fx, gx = objgrad(nlp, x)
47+
@test fx f(x)
48+
@test gx ∇f(x)
49+
fx, _ = objgrad!(nlp, x, gx)
50+
@test fx f(x)
51+
@test gx ∇f(x)
52+
@test jprod!(nlp, jac_structure(nlp)..., jac_coord(nlp, x), v, Jv) J(x) * v
53+
@test jprod!(nlp, x, jac_structure(nlp)..., v, Jv) J(x) * v
54+
@test jtprod!(nlp, jac_structure(nlp)..., jac_coord(nlp, x), w, Jtw) J(x)' * w
55+
@test jtprod!(nlp, x, jac_structure(nlp)..., w, Jtw) J(x)' * w
56+
Jop = jac_op!(nlp, x, Jv, Jtw)
57+
@test Jop * v J(x) * v
58+
@test Jop' * w J(x)' * w
59+
Jop = jac_op!(nlp, jac_structure(nlp)..., jac_coord(nlp, x), Jv, Jtw)
60+
@test Jop * v J(x) * v
61+
@test Jop' * w J(x)' * w
62+
Jop = jac_op!(nlp, x, jac_structure(nlp)..., Jv, Jtw)
63+
@test Jop * v J(x) * v
64+
@test Jop' * w J(x)' * w
65+
Hop = hess_op(nlp, x)
66+
@test Hop * v H(x) * v
67+
Hop = hess_op!(nlp, x, Hv)
68+
@test Hop * v H(x) * v
6869

69-
reset_data!(nlp)
70-
Hop = hess_op!(nlp, x, Hv)
71-
@test Hop * v == v
70+
reset_data!(nlp)
71+
Hop = hess_op!(nlp, x, Hv)
72+
@test Hop * v == v
73+
end
7274
end
7375
end
7476

test/nlp/simple-model.jl

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,22 @@ mutable struct SimpleNLPModel{T, S} <: AbstractNLPModel{T, S}
1616
counters::Counters
1717
end
1818

19-
function SimpleNLPModel()
20-
meta = NLPModelMeta(
19+
function SimpleNLPModel(::T) where {T}
20+
meta = NLPModelMeta{T, Vector{T}}(
2121
2,
2222
nnzh = 2,
2323
ncon = 2,
24-
lvar = zeros(2),
25-
uvar = ones(2),
26-
x0 = [2.0; 2.0],
27-
lcon = [0.0; 0.0],
28-
ucon = [0.0; Inf],
24+
lvar = zeros(T, 2),
25+
uvar = ones(T, 2),
26+
x0 = T.([2.0; 2.0]),
27+
lcon = T.([0.0; 0.0]),
28+
ucon = T.([0.0; Inf]),
2929
name = "Simple NLP Model",
3030
)
3131

3232
return SimpleNLPModel(meta, Counters())
3333
end
34+
SimpleNLPModel() = SimpleNLPModel(zero(Float64))
3435

3536
function NLPModels.obj(nlp::SimpleNLPModel, x::AbstractVector)
3637
@lencheck 2 x

test/nlp/slack-model.jl

Lines changed: 76 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -7,84 +7,86 @@
77
J(x) = [1.0 -2.0 0; -0.5x[1] -2.0x[2] -1]
88
H(x, y) = H(x) + y[2] * [-0.5 0 0; 0 -2.0 0; 0 0 0]
99

10-
nlp = SlackModel(SimpleNLPModel())
11-
n = nlp.meta.nvar
12-
m = nlp.meta.ncon
10+
for dt in [Float64, Float32]
11+
nlp = SlackModel(SimpleNLPModel(zero(dt)))
12+
n = nlp.meta.nvar
13+
m = nlp.meta.ncon
1314

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)
15+
x = randn(n)
16+
y = randn(m)
17+
v = randn(n)
18+
w = randn(m)
19+
Jv = zeros(m)
20+
Jtw = zeros(n)
21+
Hv = zeros(n)
22+
Hvals = zeros(nlp.meta.nnzh)
2223

23-
# Basic methods
24-
@test obj(nlp, x) f(x)
25-
@test grad(nlp, x) ∇f(x)
26-
@test hess(nlp, x) tril(H(x))
27-
@test hprod(nlp, x, v) H(x) * v
28-
@test cons(nlp, x) c(x)
29-
@test jac(nlp, x) J(x)
30-
@test jprod(nlp, x, v) J(x) * v
31-
@test jtprod(nlp, x, w) J(x)' * w
32-
@test hess(nlp, x, y) tril(H(x, y))
33-
@test hprod(nlp, x, y, v) H(x, y) * v
24+
# Basic methods
25+
@test obj(nlp, x) f(x)
26+
@test grad(nlp, x) ∇f(x)
27+
@test hess(nlp, x) tril(H(x))
28+
@test hprod(nlp, x, v) H(x) * v
29+
@test cons(nlp, x) c(x)
30+
@test jac(nlp, x) J(x)
31+
@test jprod(nlp, x, v) J(x) * v
32+
@test jtprod(nlp, x, w) J(x)' * w
33+
@test hess(nlp, x, y) tril(H(x, y))
34+
@test hprod(nlp, x, y, v) H(x, y) * v
3435

35-
# Increasing coverage
36-
fx, cx = objcons(nlp, x)
37-
@test fx f(x)
38-
@test cx c(x)
39-
fx, _ = objcons!(nlp, x, cx)
40-
@test fx f(x)
41-
@test cx c(x)
42-
fx, gx = objgrad(nlp, x)
43-
@test fx f(x)
44-
@test gx ∇f(x)
45-
fx, _ = objgrad!(nlp, x, gx)
46-
@test fx f(x)
47-
@test gx ∇f(x)
48-
@test jprod!(nlp, jac_structure(nlp)..., jac_coord(nlp, x), v, Jv) J(x) * v
49-
@test jprod!(nlp, x, jac_structure(nlp)..., v, Jv) J(x) * v
50-
@test jtprod!(nlp, jac_structure(nlp)..., jac_coord(nlp, x), w, Jtw) J(x)' * w
51-
@test jtprod!(nlp, x, jac_structure(nlp)..., w, Jtw) J(x)' * w
52-
Jop = jac_op!(nlp, x, Jv, Jtw)
53-
@test Jop * v J(x) * v
54-
@test Jop' * w J(x)' * w
55-
Jop = jac_op!(nlp, jac_structure(nlp)..., jac_coord(nlp, x), Jv, Jtw)
56-
@test Jop * v J(x) * v
57-
@test Jop' * w J(x)' * w
58-
Jop = jac_op!(nlp, x, jac_structure(nlp)..., Jv, Jtw)
59-
@test Jop * v J(x) * v
60-
@test Jop' * w J(x)' * w
61-
ghjv = zeros(m)
62-
for j = 1:m
63-
eⱼ = [i == j ? 1.0 : 0.0 for i = 1:m]
64-
Cⱼ(x) = H(x, eⱼ) - H(x)
65-
ghjv[j] = dot(gx, Cⱼ(x) * v)
36+
# Increasing coverage
37+
fx, cx = objcons(nlp, x)
38+
@test fx f(x)
39+
@test cx c(x)
40+
fx, _ = objcons!(nlp, x, cx)
41+
@test fx f(x)
42+
@test cx c(x)
43+
fx, gx = objgrad(nlp, x)
44+
@test fx f(x)
45+
@test gx ∇f(x)
46+
fx, _ = objgrad!(nlp, x, gx)
47+
@test fx f(x)
48+
@test gx ∇f(x)
49+
@test jprod!(nlp, jac_structure(nlp)..., jac_coord(nlp, x), v, Jv) J(x) * v
50+
@test jprod!(nlp, x, jac_structure(nlp)..., v, Jv) J(x) * v
51+
@test jtprod!(nlp, jac_structure(nlp)..., jac_coord(nlp, x), w, Jtw) J(x)' * w
52+
@test jtprod!(nlp, x, jac_structure(nlp)..., w, Jtw) J(x)' * w
53+
Jop = jac_op!(nlp, x, Jv, Jtw)
54+
@test Jop * v J(x) * v
55+
@test Jop' * w J(x)' * w
56+
Jop = jac_op!(nlp, jac_structure(nlp)..., jac_coord(nlp, x), Jv, Jtw)
57+
@test Jop * v J(x) * v
58+
@test Jop' * w J(x)' * w
59+
Jop = jac_op!(nlp, x, jac_structure(nlp)..., Jv, Jtw)
60+
@test Jop * v J(x) * v
61+
@test Jop' * w J(x)' * w
62+
ghjv = zeros(m)
63+
for j = 1:m
64+
eⱼ = [i == j ? 1.0 : 0.0 for i = 1:m]
65+
Cⱼ(x) = H(x, eⱼ) - H(x)
66+
ghjv[j] = dot(gx, Cⱼ(x) * v)
67+
end
68+
@test ghjvprod(nlp, x, gx, v) ghjv
69+
@test hess_coord!(nlp, x, Hvals) == hess_coord!(nlp, x, y * 0, Hvals)
70+
@test hprod!(nlp, hess_structure(nlp)..., hess_coord(nlp, x), v, Hv) H(x) * v
71+
@test hprod!(nlp, x, hess_structure(nlp)..., v, Hv) H(x) * v
72+
@test hprod!(nlp, x, y, hess_structure(nlp)..., v, Hv) H(x, y) * v
73+
Hop = hess_op(nlp, x)
74+
@test Hop * v H(x) * v
75+
Hop = hess_op!(nlp, x, Hv)
76+
@test Hop * v H(x) * v
77+
Hop = hess_op!(nlp, hess_structure(nlp)..., hess_coord(nlp, x), Hv)
78+
@test Hop * v H(x) * v
79+
Hop = hess_op!(nlp, x, hess_structure(nlp)..., Hv)
80+
@test Hop * v H(x) * v
81+
Hop = hess_op(nlp, x, y)
82+
@test Hop * v H(x, y) * v
83+
Hop = hess_op!(nlp, x, y, Hv)
84+
@test Hop * v H(x, y) * v
85+
Hop = hess_op!(nlp, hess_structure(nlp)..., hess_coord(nlp, x, y), Hv)
86+
@test Hop * v H(x, y) * v
87+
Hop = hess_op!(nlp, x, y, hess_structure(nlp)..., Hv)
88+
@test Hop * v H(x, y) * v
6689
end
67-
@test ghjvprod(nlp, x, gx, v) ghjv
68-
@test hess_coord!(nlp, x, Hvals) == hess_coord!(nlp, x, y * 0, Hvals)
69-
@test hprod!(nlp, hess_structure(nlp)..., hess_coord(nlp, x), v, Hv) H(x) * v
70-
@test hprod!(nlp, x, hess_structure(nlp)..., v, Hv) H(x) * v
71-
@test hprod!(nlp, x, y, hess_structure(nlp)..., v, Hv) H(x, y) * v
72-
Hop = hess_op(nlp, x)
73-
@test Hop * v H(x) * v
74-
Hop = hess_op!(nlp, x, Hv)
75-
@test Hop * v H(x) * v
76-
Hop = hess_op!(nlp, hess_structure(nlp)..., hess_coord(nlp, x), Hv)
77-
@test Hop * v H(x) * v
78-
Hop = hess_op!(nlp, x, hess_structure(nlp)..., Hv)
79-
@test Hop * v H(x) * v
80-
Hop = hess_op(nlp, x, y)
81-
@test Hop * v H(x, y) * v
82-
Hop = hess_op!(nlp, x, y, Hv)
83-
@test Hop * v H(x, y) * v
84-
Hop = hess_op!(nlp, hess_structure(nlp)..., hess_coord(nlp, x, y), Hv)
85-
@test Hop * v H(x, y) * v
86-
Hop = hess_op!(nlp, x, y, hess_structure(nlp)..., Hv)
87-
@test Hop * v H(x, y) * v
8890
end
8991

9092
@testset "Show" begin

0 commit comments

Comments
 (0)