Skip to content

Commit

Permalink
Improvement of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amontoison authored and dpo committed Jun 6, 2018
1 parent 7137918 commit ed39771
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 88 deletions.
32 changes: 16 additions & 16 deletions test/consistency.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ function consistent_functions(nlps; nloops=100, rtol=1.0e-8, exclude=[])
# Test objcons for unconstrained problems
if m == 0
f, c = objcons(nlps[i], x)
@test fs[i] == f
@test isapprox(fs[i], f, rtol=rtol)
@test c == []
f, tmpc = objcons!(nlps[i], x, c)
@test fs[i] == f
@test isapprox(fs[i], f, rtol=rtol)
@test c == []
@test tmpc == []
end
Expand All @@ -67,16 +67,16 @@ function consistent_functions(nlps; nloops=100, rtol=1.0e-8, exclude=[])
end
tmpg = grad!(nlps[i], x, tmp_n)
@test isapprox(gs[i], tmp_n, atol=rtol * max(gmin, 1.0))
@test tmpg == tmp_n
@test isapprox(tmpg, tmp_n, atol=rtol * max(gmin, 1.0))

if !(objgrad in exclude)
f, g = objgrad(nlps[i], x)
@test isapprox(fs[i], f, atol=rtol * max(abs(f), 1.0))
@test gs[i] == g
@test isapprox(gs[i], g, atol=rtol * max(gmin, 1.0))
f, tmpg = objgrad!(nlps[i], x, g)
@test isapprox(fs[i], f, atol=rtol * max(abs(f), 1.0))
@test gs[i] == g
@test g == tmpg
@test isapprox(gs[i], g, atol=rtol * max(gmin, 1.0))
@test isapprox(g, tmpg, atol=rtol * max(gmin, 1.0))
end
end
end
Expand Down Expand Up @@ -125,7 +125,7 @@ function consistent_functions(nlps; nloops=100, rtol=1.0e-8, exclude=[])
end
tmphv = hprod!(nlps[i], x, v, tmp_n)
@test isapprox(Hvs[i], tmp_n, atol=rtol * max(Hvmin, 1.0))
@test tmphv == tmp_n
@test isapprox(tmphv, tmp_n, atol=rtol * max(Hvmin, 1.0))
fill!(tmp_n, 0)
H = hess_op!(nlps[i], x, tmp_n)
res = H * v
Expand All @@ -143,7 +143,7 @@ function consistent_functions(nlps; nloops=100, rtol=1.0e-8, exclude=[])
for i = 1:N
tmpc = cons!(nlps[i], x, tmp_m)
@test isapprox(cs[i], tmp_m, atol=rtol * max(cmin, 1.0))
@test tmpc == tmp_m
@test isapprox(tmpc, tmp_m, atol=rtol * max(cmin, 1.0))
ci, li, ui = copy(cs[i]), cls[i], cus[i]
for k = 1:m
if li[k] > -Inf
Expand All @@ -166,12 +166,12 @@ function consistent_functions(nlps; nloops=100, rtol=1.0e-8, exclude=[])

if !(objcons in exclude)
f, c = objcons(nlps[i], x)
@test fs[i] == f
@test cs[i] == c
@test isapprox(fs[i], f, atol=rtol * max(abs(f), 1.0))
@test isapprox(cs[i],c, atol=rtol * max(cmin, 1.0))
f, tmpc = objcons!(nlps[i], x, c)
@test fs[i] == f
@test cs[i] == c
@test c == tmpc
@test isapprox(fs[i], f, atol=rtol * max(abs(f), 1.0))
@test isapprox(cs[i],c, atol=rtol * max(cmin, 1.0))
@test isapprox(c, tmpc, atol=rtol * max(cmin, 1.0))
end
end
end
Expand All @@ -197,7 +197,7 @@ function consistent_functions(nlps; nloops=100, rtol=1.0e-8, exclude=[])
@test isapprox(vi, norm(Jps[j]), atol=rtol * max(Jmin, 1.0))
end
tmpjv = jprod!(nlps[i], x, v, tmp_m)
@test tmpjv == tmp_m
@test isapprox(tmpjv, tmp_m, atol=rtol * max(Jmin, 1.0))
@test isapprox(Jps[i], tmp_m, atol=rtol * max(Jmin, 1.0))
fill!(tmp_m, 0)
J = jac_op!(nlps[i], x, tmp_m, tmp_n)
Expand All @@ -218,7 +218,7 @@ function consistent_functions(nlps; nloops=100, rtol=1.0e-8, exclude=[])
end
tmpjtv = jtprod!(nlps[i], x, w, tmp_n)
@test isapprox(Jtps[i], tmp_n, atol=rtol * max(Jmin, 1.0))
@test tmpjtv == tmp_n
@test isapprox(tmpjtv, tmp_n, atol=rtol * max(Jmin, 1.0))
fill!(tmp_n, 0)
J = jac_op!(nlps[i], x, tmp_m, tmp_n)
res = J' * w
Expand Down Expand Up @@ -255,7 +255,7 @@ function consistent_functions(nlps; nloops=100, rtol=1.0e-8, exclude=[])
@test isapprox(Ls[i], Ls[j], atol=rtol * max(Lmin, 1.0))
end
σ = rand() - 0.5
tmp_nn = hess(nlps[i], x, obj_weight=σ, y=σ*y)
tmp_nn = hess(nlps[i], x, obj_weight = σ, y=σ*y)
@test isapprox*Ls[i], tmp_nn, atol=rtol * max(Hmin, 1.0))
end
end
Expand Down
28 changes: 14 additions & 14 deletions test/nls_consistency.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ function consistent_nls_functions(nlss; nloops=10, rtol=1.0e-8)
end

r = residual!(nlss[i], x, tmp_m)
@test r == Fs[i]
@test Fs[i] == tmp_m
@test isapprox(r, Fs[i], rtol=rtol)
@test isapprox(Fs[i], tmp_m, rtol=rtol)
end

Js = Any[jac_residual(nls, x) for nls in nlss]
Expand All @@ -54,10 +54,10 @@ function consistent_nls_functions(nlss; nloops=10, rtol=1.0e-8)
end

jps = jprod_residual!(nlss[i], x, v, tmp_m)
@test jps == Jps[i]
@test Jps[i] == tmp_m
@test Jps[i] == J_ops[i] * v
@test Jps[i] == J_ops_inplace[i] * v
@test isapprox(jps, Jps[i], rtol=rtol)
@test isapprox(Jps[i], tmp_m, rtol=rtol)
@test isapprox(Jps[i], J_ops[i] * v, rtol=rtol)
@test isapprox(Jps[i], J_ops_inplace[i] * v, rtol=rtol)
end

v = rand(m)
Expand All @@ -69,10 +69,10 @@ function consistent_nls_functions(nlss; nloops=10, rtol=1.0e-8)
end

jtps = jtprod_residual!(nlss[i], x, v, tmp_n)
@test jtps == Jtps[i]
@test Jtps[i] == tmp_n
@test Jtps[i] == J_ops[i]' * v
@test Jtps[i] == J_ops_inplace[i]' * v
@test isapprox(jtps, Jtps[i], rtol=rtol)
@test isapprox(Jtps[i], tmp_n, rtol=rtol)
@test isapprox(Jtps[i], J_ops[i]' * v, rtol=rtol)
@test isapprox(Jtps[i], J_ops_inplace[i]' * v, rtol=rtol)
end

v = rand(n)
Expand All @@ -90,10 +90,10 @@ function consistent_nls_functions(nlss; nloops=10, rtol=1.0e-8)
end

hvs = hprod_residual!(nlss[i], x, k, v, tmp_n)
@test hvs == Hvs[i]
@test Hvs[i] == tmp_n
@test Hvs[i] == Hops[i] * v
@test Hvs[i] == Hops_inplace[i] * v
@test isapprox(hvs, Hvs[i], rtol=rtol)
@test isapprox(Hvs[i], tmp_n, rtol=rtol)
@test isapprox(Hvs[i], Hops[i] * v, rtol=rtol)
@test isapprox(Hvs[i], Hops_inplace[i] * v, rtol=rtol)
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/test_autodiff_nls_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ function autodiff_nls_test()
F(x) = [x[1] - 1; x[2] - x[1]^2]
nls = ADNLSModel(F, 2, 2)

@test residual(nls, ones(2)) == zeros(2)
@test isapprox(residual(nls, ones(2)), zeros(2), rtol=1e-8)
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/test_feasibility_nls_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function feasibility_nls_test()
lcon=zeros(2), ucon=zeros(2))
nls = FeasibilityResidual(nlp)

@test residual(nls, ones(2)) == zeros(2)
@test isapprox(residual(nls, ones(2)), zeros(2), rtol=1e-8)
end
end

Expand Down
6 changes: 3 additions & 3 deletions test/test_lls_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ function lls_test()
nls = LLSModel(A, b)
x = rand(3)

@test A * x - b == residual(nls, x)
@test A == jac_residual(nls, x)
@test isapprox(A * x - b, residual(nls, x), rtol=1e-8)
@test isapprox(A, jac_residual(nls, x), rtol=1e-8)
for i = 1:10
@test zeros(3, 3) == hess_residual(nls, x, i)
@test isapprox(zeros(3, 3), hess_residual(nls, x, i), rtol=1e-8)
end
end
end
Expand Down
16 changes: 8 additions & 8 deletions test/test_qn_model.jl
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
using LinearOperators

function check_qn_model(qnmodel)

rtol = 1e-8
model = qnmodel.model
@assert typeof(qnmodel) <: NLPModels.QuasiNewtonModel
@assert qnmodel.meta.nvar == model.meta.nvar
@assert qnmodel.meta.ncon == model.meta.ncon

x = rand(qnmodel.meta.nvar)

@assert obj(model, x) == obj(qnmodel, x)
@assert isapprox(obj(model, x), obj(qnmodel, x), rtol=rtol)
@assert neval_obj(model) == 2

@assert grad(model, x) == grad(qnmodel, x)
@assert isapprox(grad(model, x), grad(qnmodel, x), rtol=rtol)
@assert neval_grad(model) == 2

@assert cons(model, x) == cons(qnmodel, x)
@assert isapprox(cons(model, x), cons(qnmodel, x), rtol=rtol)
@assert neval_cons(model) == 2

@assert jac(model, x) == jac(qnmodel, x)
@assert isapprox(jac(model, x), jac(qnmodel, x), rtol=rtol)
@assert neval_jac(model) == 2

v = rand(qnmodel.meta.nvar)
u = rand(qnmodel.meta.ncon)

@assert jprod(model, x, v) == jprod(qnmodel, x, v)
@assert isapprox(jprod(model, x, v), jprod(qnmodel, x, v), rtol=rtol)
@assert neval_jprod(model) == 2

@assert jtprod(model, x, u) == jtprod(qnmodel, x, u)
@assert isapprox(jtprod(model, x, u), jtprod(qnmodel, x, u), rtol=rtol)
@assert neval_jtprod(model) == 2

H = hess_op(qnmodel, x)
@assert typeof(H) <: LinearOperators.AbstractLinearOperator
@assert size(H) == (model.meta.nvar, model.meta.nvar)
@assert H * v == hprod(qnmodel, x, v)
@assert isapprox(H * v, hprod(qnmodel, x, v), rtol=rtol)

g = grad(qnmodel, x)
gp = grad(qnmodel, x - g)
Expand Down
45 changes: 23 additions & 22 deletions test/test_simple_model.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
function test_simple_model()
rtol = 1e-8
x0 = zeros(2)
f(x) = x[1]^4 - 4*x[1]*x[2] + x[2]^2
g(x) = [4*x[1]^3 - 4*x[2]; 2*x[2] - 4*x[1]]
Expand All @@ -12,15 +13,15 @@ function test_simple_model()
nlp = SimpleNLPModel(f, x0, g=g, g! =g!, H=H, Hp=Hp, Hp! =Hp!, Hcoord=Hc)
v = rand(2)
w = zeros(2)
@test obj(nlp, x0) == f(x0)
@test grad(nlp, x0) == g(x0)
@test isapprox(obj(nlp, x0), f(x0), rtol=rtol)
@test isapprox(grad(nlp, x0), g(x0), rtol=rtol)
grad!(nlp, x0, w)
@test w == g!(x0, w)
@test hess(nlp, x0) == H(x0)
@test isapprox(w, g!(x0, w), rtol=rtol)
@test isapprox(hess(nlp, x0), H(x0), rtol=rtol)
@test hess_coord(nlp, x0) == Hc(x0)
@test hprod(nlp, x0, v) == Hp(x0, v)
@test isapprox(hprod(nlp, x0, v), Hp(x0, v), rtol=rtol)
hprod!(nlp, x0, v, w)
@test w == Hp!(x0, v, w)
@test isapprox(w, Hp!(x0, v, w), rtol=rtol)
@test_throws NotImplementedError cons(nlp, x0)

c(x) = [x[1] + x[2] - 1; x[1]*x[2] - 1]
Expand All @@ -39,30 +40,30 @@ function test_simple_model()
c! =c!, J=J, Jp=Jp, Jp! =Jp!, Jtp=Jtp, Jtp! =Jtp!, lcon=zeros(2),
ucon=zeros(2))
y0 = rand(2)
@test obj(nlp, x0) == f(x0)
@test grad(nlp, x0) == g(x0)
@test isapprox(obj(nlp, x0), f(x0), rtol=rtol)
@test isapprox(grad(nlp, x0), g(x0), rtol=rtol)
grad!(nlp, x0, w)
@test w == g!(x0, w)
@test hess(nlp, x0, y=zeros(2)) == W(x0)
@test hess(nlp, x0, y=y0) == W(x0, y=y0)
@test isapprox(w, g!(x0, w), rtol=rtol)
@test isapprox(hess(nlp, x0, y=zeros(2)), W(x0), rtol=rtol)
@test isapprox(hess(nlp, x0, y=y0), W(x0, y=y0), rtol=rtol)
@test hess_coord(nlp, x0, y=zeros(2)) == Wc(x0)
@test hess_coord(nlp, x0, y=y0) == Wc(x0, y=y0)
@test hprod(nlp, x0, v, y=zeros(2)) == Wp(x0, v)
@test hprod(nlp, x0, v, y=y0) == Wp(x0, v, y=y0)
@test isapprox(hprod(nlp, x0, v, y=zeros(2)), Wp(x0, v), rtol=rtol)
@test isapprox(hprod(nlp, x0, v, y=y0), Wp(x0, v, y=y0), rtol=rtol)
hprod!(nlp, x0, v, w, y=zeros(2))
@test w == Wp!(x0, v, w)
@test isapprox(w, Wp!(x0, v, w), rtol=rtol)
hprod!(nlp, x0, v, w, y=y0)
@test w == Wp!(x0, v, w, y=y0)
@test cons(nlp, x0) == c(x0)
@test isapprox(w, Wp!(x0, v, w, y=y0), rtol=rtol)
@test isapprox(cons(nlp, x0), c(x0), rtol=rtol)
cons!(nlp, x0, w)
@test w == c!(x0, w)
@test jac(nlp, x0) == J(x0)
@test jprod(nlp, x0, v) == Jp(x0, v)
@test isapprox(w, c!(x0, w), rtol=rtol)
@test isapprox(jac(nlp, x0), J(x0), rtol=rtol)
@test isapprox(jprod(nlp, x0, v), Jp(x0, v), rtol=rtol)
jprod!(nlp, x0, v, w)
@test w == Jp!(x0, v, w)
@test isapprox(w,Jp!(x0, v, w), rtol=rtol)
jtprod!(nlp, x0, v, w)
@test w == Jtp!(x0, v, w)
@test jtprod(nlp, x0, v) == Jtp(x0, v)
@test isapprox(w, Jtp!(x0, v, w), rtol=rtol)
@test isapprox(jtprod(nlp, x0, v), Jtp(x0, v), rtol=rtol)
end

function test_objgrad_objcons()
Expand Down
2 changes: 1 addition & 1 deletion test/test_simple_nls_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function simple_nls_test()
JF(x) = [1.0 0.0; -2*x[1] 1.0]
nls = SimpleNLSModel(2, 2, F=F, JF=JF)

@test residual(nls, ones(2)) == zeros(2)
@test isapprox(residual(nls, ones(2)), zeros(2), rtol=1e-8)
end
end

Expand Down
Loading

0 comments on commit ed39771

Please sign in to comment.