From 137bf5e6c1905197cd270d772635508f2a9c4f88 Mon Sep 17 00:00:00 2001 From: tmigot Date: Wed, 4 Sep 2024 19:58:32 -0400 Subject: [PATCH] Remove dead tests --- test/runtests.jl | 5 -- test/test_feasibility_form_nls.jl | 91 --------------------- test/test_feasibility_nls_model.jl | 28 ------- test/test_qn_model.jl | 82 ------------------- test/test_slack_model.jl | 124 ----------------------------- 5 files changed, 330 deletions(-) delete mode 100644 test/test_feasibility_form_nls.jl delete mode 100644 test/test_feasibility_nls_model.jl delete mode 100644 test/test_qn_model.jl delete mode 100644 test/test_slack_model.jl diff --git a/test/runtests.jl b/test/runtests.jl index 281269e..4696795 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -19,8 +19,3 @@ end if (v"1.7" <= VERSION) include("allocs_test.jl") end - -# include("test_feasibility_form_nls.jl") -# include("test_feasibility_nls_model.jl") -# include("test_qn_model.jl") -# include("test_slack_model.jl") diff --git a/test/test_feasibility_form_nls.jl b/test/test_feasibility_form_nls.jl deleted file mode 100644 index c2e3cd4..0000000 --- a/test/test_feasibility_form_nls.jl +++ /dev/null @@ -1,91 +0,0 @@ -function test_nls_to_cons() - @testset "Test FeasibilityFormNLS consistency" begin - F1(x) = [x[1] - 1; 10 * (x[2] - x[1]^2)] - F2(x) = [x[1] * x[2] * x[3] * x[4] * x[5] - 1] - F3(x) = [x[1] + x[2] - 1; x[1]^2 + x[2]^2 - 2; x[1]^3 + x[2]^3 - 3] - c1(x) = [sum(x); x[1] * x[2] - 2] - for (F, n, ne) in [(F1, 2, 2), (F2, 5, 1), (F3, 2, 3)], (c, m) in [(x -> zeros(0), 0), (c1, 2)] - x0 = [-(1.0)^i for i = 1:n] - nls = ADNLSModel(F, x0, ne, c, zeros(m), zeros(m)) - - nlpcon = FeasibilityFormNLS(nls) - adnlp = ADNLPModel( - x -> sum(x[(n + 1):end] .^ 2) / 2, - [x0; zeros(ne)], - x -> [F(x[1:n]) - x[(n + 1):end]; c(x[1:n])], - zeros(ne + m), - zeros(ne + m), - ) - TestUtils.consistent_functions([nlpcon; adnlp], exclude = [ghjvprod]) - - adnls = ADNLSModel( - x -> x[(n + 1):end], - [x0; zeros(ne)], - ne, - x -> [F(x[1:n]) - x[(n + 1):end]; c(x[1:n])], - zeros(ne + m), - zeros(ne + m), - ) - TestUtils.consistent_functions([nlpcon; adnls], exclude = [ghjvprod]) - TestUtils.consistent_nls_functions([nlpcon; adnls]) - end - end - - @testset "Test FeasibilityFormNLS with LLSModel" begin - for n in [10; 30], ne in [10; 20; 30], m in [0; 20] - for T in [ - (rows, cols) -> Matrix(1.0I, rows, cols) .+ 1, - (rows, cols) -> sparse(1.0I, rows, cols) .+ 1, - (rows, cols) -> sparse(1.0I, rows, cols), - ] - A = T(ne, n) - b = collect(1:ne) - C = m > 0 ? T(m, n) : zeros(0, n) - lls = LLSModel(A, b, C = C, lcon = zeros(m), ucon = zeros(m)) - nlpcon = FeasibilityFormNLS(lls) - Ine = spdiagm(0 => ones(ne)) - lls2 = LLSModel( - [spzeros(ne, n) Ine], - zeros(ne), - C = [A -Ine; C spzeros(m, ne)], - lcon = [b; zeros(m)], - ucon = [b; zeros(m)], - ) - - TestUtils.consistent_functions([nlpcon; lls2], exclude = [hess, hess_coord, ghjvprod]) - TestUtils.consistent_nls_functions([nlpcon; lls2]) - end - end - end - - @testset "Test FeasibilityFormNLS of a FeasibilityResidual" begin - c(x) = [x[1]^2 + x[2]^2 - 5; x[1] * x[2] - 2; x[1] - 1; x[2] - 1] - x0 = [0.5; 1.5] - nlp = ADNLPModel(x -> 0, x0, c, zeros(4), zeros(4)) - ffnls = FeasibilityFormNLS(FeasibilityResidual(nlp)) - nlp2 = ADNLSModel(x -> x[3:6], [x0; zeros(4)], 4, x -> c(x[1:2]) - x[3:6], zeros(4), zeros(4)) - TestUtils.consistent_functions([ffnls; nlp2]) - TestUtils.consistent_nls_functions([ffnls; nlp2], exclude = [ghjvprod]) - - # The test belows verifies that the nnzj and nnzh information are not lost - n = 10 - m = 2n - A = [spdiagm(0 => 2 * ones(n), 1 => -ones(n - 1), -1 => -ones(n - 1)); -I] - b = zeros(m) - nlp = LLSModel(spzeros(0, n), zeros(0), C = A, lcon = b, ucon = b) - ffnls = FeasibilityFormNLS(FeasibilityResidual(nlp), name = "feas-of-feas") - nlp2 = LLSModel([spzeros(m, n) I], zeros(m), C = [A -I], lcon = b, ucon = b) - TestUtils.consistent_functions([ffnls; nlp2], exclude = [hess, hess_coord, ghjvprod]) - TestUtils.consistent_nls_functions([ffnls; nlp2]) - end - - @testset "FeasibilityFormNLS of an LLSModel should handle hess related function" begin - lls = LLSModel(rand(10, 5), rand(10), C = rand(2, 5), lcon = zeros(2), ucon = zeros(2)) - nls = FeasibilityFormNLS(lls) - @test hess_structure(nls) == (6:15, 6:15) - @test hess_coord(nls, zeros(15)) == ones(10) - @test hess_coord(nls, zeros(15), obj_weight = 0.3) == 0.3 * ones(10) - end -end - -test_nls_to_cons() diff --git a/test/test_feasibility_nls_model.jl b/test/test_feasibility_nls_model.jl deleted file mode 100644 index 78ae04f..0000000 --- a/test/test_feasibility_nls_model.jl +++ /dev/null @@ -1,28 +0,0 @@ -function feasibility_nls_test() - @testset "feasibility_nls_test" begin - nlp = ADNLPModel(x -> 0, zeros(2), x -> [x[1] - 1; x[2] - x[1]^2], zeros(2), zeros(2)) - nls = FeasibilityResidual(nlp) - - @test isapprox(residual(nls, ones(2)), zeros(2), rtol = 1e-8) - - nlp = ADNLPModel( - x -> 0, - zeros(2), - [-0.3; -0.5], - [1.2; 3.4], - x -> [x[1] - 1; x[2] - x[1]^2], - -ones(2), - 2 * ones(2), - ) - nls = FeasibilityResidual(nlp) - - @test nls.meta.nvar == 4 - @test nls.nls_meta.nequ == 2 - @test nls.meta.lvar == [-0.3; -0.5; -1.0; -1.0] - @test nls.meta.uvar == [1.2; 3.4; 2.0; 2.0] - @test isapprox(residual(nls, [1.0; 1.0; 0.0; 0.0]), zeros(2), rtol = 1e-8) - @test isapprox(residual(nls, [0.0; 1.0; 2.0; 3.0]), [-3.0; -2.0], rtol = 1e-8) - end -end - -feasibility_nls_test() diff --git a/test/test_qn_model.jl b/test/test_qn_model.jl deleted file mode 100644 index a6cb35c..0000000 --- a/test/test_qn_model.jl +++ /dev/null @@ -1,82 +0,0 @@ -function check_qn_model(qnmodel) - rtol = 1e-8 - model = qnmodel.model - @assert typeof(qnmodel) <: QuasiNewtonModel - @assert qnmodel.meta.nvar == model.meta.nvar - @assert qnmodel.meta.ncon == model.meta.ncon - - x = [-(-1.0)^i for i = 1:(qnmodel.meta.nvar)] - - @assert isapprox(obj(model, x), obj(qnmodel, x), rtol = rtol) - @assert neval_obj(model) == 2 - - @assert isapprox(grad(model, x), grad(qnmodel, x), rtol = rtol) - @assert neval_grad(model) == 2 - - @assert isapprox(cons(model, x), cons(qnmodel, x), rtol = rtol) - @assert neval_cons(model) == 2 - - @assert isapprox(jac(model, x), jac(qnmodel, x), rtol = rtol) - @assert neval_jac(model) == 2 - - v = [-(-1.0)^i for i = 1:(qnmodel.meta.nvar)] - u = [-(-1.0)^i for i = 1:(qnmodel.meta.ncon)] - - @assert isapprox(jprod(model, x, v), jprod(qnmodel, x, v), rtol = rtol) - @assert neval_jprod(model) == 2 - - @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 isapprox(H * v, hprod(qnmodel, x, v), rtol = rtol) - - g = grad(qnmodel, x) - gp = grad(qnmodel, x - g) - push!(qnmodel, -g, gp - g) # only testing that the call succeeds, not that the update is valid - # the quasi-Newton operator itself is tested in LinearOperators - - reset!(qnmodel) -end - -nlp = SimpleNLPModel() -qn_model = LBFGSModel(nlp) -check_qn_model(qn_model) -qn_model = LBFGSModel(nlp, mem = 2) -check_qn_model(qn_model) -qn_model = LSR1Model(nlp) -check_qn_model(qn_model) -qn_model = LSR1Model(nlp, mem = 2) -check_qn_model(qn_model) -qn_model = DiagonalPSBModel(nlp) -check_qn_model(qn_model) -qn_model = DiagonalAndreiModel(nlp) -check_qn_model(qn_model) -qn_model = SpectralGradientModel(nlp) -check_qn_model(qn_model) - -@testset "objgrad of a qnmodel" begin - struct OnlyObjgradModel <: AbstractNLPModel - meta::NLPModelMeta - counters::Counters - end - - function OnlyObjgradModel() - meta = NLPModelMeta(2) - OnlyObjgradModel(meta, Counters()) - end - - function NLPModels.objgrad!(::OnlyObjgradModel, x::AbstractVector, g::AbstractVector) - f = (x[1] - 1)^2 + 100 * (x[2] - x[1]^2)^2 - g[1] = 2 * (x[1] - 1) - 400 * x[1] * (x[2] - x[1]^2) - g[2] = 200 * (x[2] - x[1]^2) - f, g - end - - nlp = LBFGSModel(OnlyObjgradModel()) - - @test objgrad!(nlp, nlp.meta.x0, zeros(2)) == objgrad!(nlp.model, nlp.meta.x0, zeros(2)) - @test objgrad(nlp, nlp.meta.x0) == objgrad(nlp.model, nlp.meta.x0) -end diff --git a/test/test_slack_model.jl b/test/test_slack_model.jl deleted file mode 100644 index 98d881d..0000000 --- a/test/test_slack_model.jl +++ /dev/null @@ -1,124 +0,0 @@ -@testset "Slack model tests" begin - # an unconstrained problem should be returned unchanged - @printf("Checking slack formulation of genrose\t") - model = genrose_autodiff() - smodel = SlackModel(model) - @test smodel == model - @printf("✓\n") - - # a bound-constrained problem should be returned unchanged - @printf("Checking slack formulation of hs5\t") - model = hs5_autodiff() - smodel = SlackModel(model) - @test smodel == model - @printf("✓\n") - - # an equality-constrained problem should be returned unchanged - @printf("Checking slack formulation of hs6\t") - model = hs6_autodiff() - smodel = SlackModel(model) - @test smodel == model - @printf("✓\n") - - # test problems that actually have inequality constraints - - function check_slack_model(smodel) - rtol = 1e-8 - model = smodel.model - @test typeof(smodel) == NLPModels.SlackModel - n = model.meta.nvar # number of variables in original model - N = smodel.meta.nvar # number of variables in slack model - jlow = model.meta.jlow - nlow = length(jlow) - jupp = model.meta.jupp - nupp = length(jupp) - jrng = model.meta.jrng - nrng = length(jrng) - jfix = model.meta.jfix - nfix = length(jfix) - - @test N == n + model.meta.ncon - nfix - @test smodel.meta.ncon == model.meta.ncon - - x = [-(-1.0)^i for i = 1:N] - s = x[(n + 1):N] - y = [-(-1.0)^i for i = 1:(smodel.meta.ncon)] - - # slack variables do not influence objective value - @test isapprox(obj(model, x[1:n]), obj(smodel, x), rtol = rtol) - @test neval_obj(model) == 2 - - g = grad(model, x[1:n]) - G = grad(smodel, x) - @test isapprox(g, G[1:n], rtol = rtol) - @test all(i -> (i ≈ 0), G[(n + 1):N]) - @test neval_grad(model) == 2 - - h = hess(model, x[1:n], y) - H = hess(smodel, x, y) - @test isapprox(H[1:n, 1:n], h, rtol = rtol) - @test all(i -> (i ≈ 0), H[1:n, (n + 1):N]) - @test all(i -> (i ≈ 0), H[(n + 1):N, 1:n]) - @test all(i -> (i ≈ 0), H[(n + 1):N, (n + 1):N]) - @test neval_hess(model) == 2 - - v = [-(-1.0)^i for i = 1:N] - hv = hprod(model, x[1:n], y, v[1:n]) - HV = hprod(smodel, x, y, v) - @test isapprox(HV[1:n], hv, rtol = rtol) - @test all(i -> (i ≈ 0), HV[(n + 1):N]) - @test neval_hprod(model) == 2 - - c = cons(model, x[1:n]) - C = cons(smodel, x) - - # slack variables do not influence equality constraints - @test all(C[jfix] ≈ c[jfix]) - @test all(C[jlow] ≈ c[jlow] - s[1:nlow]) - @test all(C[jupp] ≈ c[jupp] - s[(nlow + 1):(nlow + nupp)]) - @test all(C[jrng] ≈ c[jrng] - s[(nlow + nupp + 1):(nlow + nupp + nrng)]) - @test neval_cons(model) == 2 - - j = jac(model, x[1:n]) - J = jac(smodel, x) - K = J[:, (n + 1):N] - @test all(J[:, 1:n] ≈ j) - k = 1 - for l in collect([jlow; jupp; jrng]) - @test J[l, n + k] ≈ -1 - K[l, k] = 0 - k += 1 - end - @test all(i -> (i ≈ 0), K) - @test neval_jac(model) == 2 - - v = [-(-1.0)^i for i = 1:N] - Jv = J * v - @test all(jprod(smodel, x, v) ≈ Jv) - jv = zeros(smodel.meta.ncon) - @test all(jprod!(smodel, x, v, jv) ≈ Jv) - - u = [-(-1.0)^i for i = 1:(smodel.meta.ncon)] - Jtu = J' * u - @test all(jtprod(smodel, x, u) ≈ Jtu) - jtu = zeros(N) - @test all(jtprod!(smodel, x, u, jtu) ≈ Jtu) - - reset!(smodel) - end - - for problem in ["hs10", "hs11", "hs14", "lincon", "linsv"] - @printf("Checking slack formulation of %-8s\t", problem) - problem_f = eval(Symbol(problem * "_autodiff")) - nlp = problem_f() - slack_model = SlackModel(nlp) - check_slack_model(slack_model) - @printf("✓\n") - end -end - -@testset "Test that type is maintained (#217)" begin - nlp = ADNLPModel(x -> dot(x, x), ones(Float16, 2), x -> sum(x), [-1.0], [1.0]) - snlp = SlackModel(nlp) - @test eltype(snlp.meta.x0) == Float16 -end