Skip to content

Commit 8365fac

Browse files
tmigotprobot-auto-merge[bot]
authored andcommitted
fix show test for qn models
1 parent 93b67eb commit 8365fac

7 files changed

Lines changed: 486 additions & 495 deletions

File tree

test/nlp/quasi-newton.jl

Lines changed: 64 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -5,72 +5,71 @@
55
c(x) = [x[1] - 2x[2] + 1; -x[1]^2 / 4 - x[2]^2 + 1]
66
J(x) = [1.0 -2.0; -0.5x[1] -2.0x[2]]
77

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

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
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
2020

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)
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)
2929

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
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
3838

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
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
6969

70-
reset_data!(nlp)
71-
Hop = hess_op!(nlp, x, Hv)
72-
@test Hop * v == v
73-
end
70+
reset_data!(nlp)
71+
Hop = hess_op!(nlp, x, Hv)
72+
@test Hop * v == v
7473
end
7574
end
7675

@@ -79,7 +78,8 @@
7978
io = IOBuffer()
8079
show(io, nlp)
8180
showed = String(take!(io))
82-
expected = """LSR1Model{Float64, Vector{Float64}} - A QuasiNewtonModel
81+
storage_type = typeof(nlp)
82+
expected = """$storage_type - A QuasiNewtonModel
8383
Problem name: Simple NLP Model
8484
All variables: ████████████████████ 2 All constraints: ████████████████████ 2
8585
free: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0 free: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0
@@ -104,7 +104,8 @@
104104
io = IOBuffer()
105105
show(io, nlp)
106106
showed = String(take!(io))
107-
expected = """LBFGSModel{Float64, Vector{Float64}} - A QuasiNewtonModel
107+
storage_type = typeof(nlp)
108+
expected = """$storage_type - A QuasiNewtonModel
108109
Problem name: Simple NLP Model
109110
All variables: ████████████████████ 2 All constraints: ████████████████████ 2
110111
free: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0 free: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0

test/nlp/simple-model.jl

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

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

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

3636
function NLPModels.obj(nlp::SimpleNLPModel, x::AbstractVector)
3737
@lencheck 2 x

test/nlp/slack-model.jl

Lines changed: 76 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,91 @@
11
@testset "SlackModel NLP tests" begin
2-
@testset "API" begin
2+
@testset "API" for T in [Float64, Float32]
33
f(x) = (x[1] - 2)^2 + (x[2] - 1)^2
44
∇f(x) = [2 * (x[1] - 2); 2 * (x[2] - 1); 0]
55
H(x) = [2.0 0 0; 0 2.0 0; 0 0 0]
66
c(x) = [x[1] - 2x[2] + 1; -x[1]^2 / 4 - x[2]^2 + 1 - x[3]]
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-
for dt in [Float64, Float32]
11-
nlp = SlackModel(SimpleNLPModel(zero(dt)))
12-
n = nlp.meta.nvar
13-
m = nlp.meta.ncon
10+
11+
nlp = SlackModel(SimpleNLPModel(T))
12+
n = nlp.meta.nvar
13+
m = nlp.meta.ncon
1414

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)
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)
2323

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
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
3535

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
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)
8967
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
9089
end
9190

9291
@testset "Show" begin

0 commit comments

Comments
 (0)