From 3cc8626813ade86ce0709e3d92fb227e23c4e63e Mon Sep 17 00:00:00 2001 From: Eric <5846501+ericphanson@users.noreply.github.com> Date: Mon, 28 Oct 2019 18:02:04 +0000 Subject: [PATCH] Add tests from #332 --- src/problem_depot/problems/lp.jl | 15 +++++++-- src/problem_depot/problems/socp.jl | 52 ++++++++++++++++++++++++++++++ test/runtests.jl | 2 +- 3 files changed, 65 insertions(+), 4 deletions(-) diff --git a/src/problem_depot/problems/lp.jl b/src/problem_depot/problems/lp.jl index 457407bd2..367039e98 100644 --- a/src/problem_depot/problems/lp.jl +++ b/src/problem_depot/problems/lp.jl @@ -1,4 +1,4 @@ -@add_problem lp function lp_abs_atom(handle_problem!, ::Val{test}, atol, rtol, ::Type{T}) where {T, test} +@add_problem lp function lp_dual_abs_atom(handle_problem!, ::Val{test}, atol, rtol, ::Type{T}) where {T, test} x = Variable() p = minimize(abs(x), x<=-1) if test @@ -19,6 +19,11 @@ if test @test p.optval ≈ 2 atol=atol rtol=rtol @test evaluate(sum(abs(x))) ≈ 2 atol=atol rtol=rtol + @test p.constraints[1].dual ≈ 1 atol=atol rtol=rtol + @test p.constraints[2].dual ≈ 1 atol=atol rtol=rtol + @test p.constraints[3].dual[1,1] ≈ 0 atol=atol rtol=rtol + @test p.constraints[3].dual[2,2] ≈ 0 atol=atol rtol=rtol + @test p.constraints[3].dual[1,2] ≈ p.constraints[3].dual[2,1] atol=atol rtol=rtol end end @@ -207,7 +212,7 @@ end # TODO: @davidlizeng. We should finish this someday. end -@add_problem lp function lp_norm_inf_atom(handle_problem!, ::Val{test}, atol, rtol, ::Type{T}) where {T, test} +@add_problem lp function lp_dual_norm_inf_atom(handle_problem!, ::Val{test}, atol, rtol, ::Type{T}) where {T, test} x = Variable(3) p = minimize(norm_inf(x), [-2 <= x, x <= 1]) if test @@ -217,10 +222,12 @@ end if test @test p.optval ≈ 0 atol=atol rtol=rtol @test evaluate(norm_inf(x)) ≈ 0 atol=atol rtol=rtol + @test norm(p.constraints[1].dual) ≈ 0 atol=atol rtol=rtol + @test norm(p.constraints[2].dual) ≈ 0 atol=atol rtol=rtol end end -@add_problem lp function lp_norm_1_atom(handle_problem!, ::Val{test}, atol, rtol, ::Type{T}) where {T, test} +@add_problem lp function lp_dual_norm_1_atom(handle_problem!, ::Val{test}, atol, rtol, ::Type{T}) where {T, test} x = Variable(3) p = minimize(norm_1(x), [-2 <= x, x <= 1]) if test @@ -230,5 +237,7 @@ end if test @test p.optval ≈ 0 atol=atol rtol=rtol @test evaluate(norm_1(x)) ≈ 0 atol=atol rtol=rtol + @test norm(p.constraints[1].dual) ≈ 0 atol=atol rtol=rtol + @test norm(p.constraints[2].dual) ≈ 0 atol=atol rtol=rtol end end diff --git a/src/problem_depot/problems/socp.jl b/src/problem_depot/problems/socp.jl index 129002a78..5269a6d6e 100644 --- a/src/problem_depot/problems/socp.jl +++ b/src/problem_depot/problems/socp.jl @@ -370,3 +370,55 @@ end @test o1 <= o2 end end + + +@add_problem socp function socp_dual_minimal_norm_solutions(handle_problem!, ::Val{test}, atol, rtol, ::Type{T}) where {T, test} + x = Variable(2) + A = [1 2; 2 4]; + b = [3, 6]; + p = minimize(norm(x, 1), A*x==b) + + if test + @test vexity(p) == ConvexVexity() + end + + handle_problem!(p) + if test + @test p.optval ≈ 1.5 atol=atol rtol=rtol + @test evaluate(x) ≈ [0, 1.5] atol=atol rtol=rtol + @test evaluate(norm(x, 1)) ≈ p.optval atol=atol rtol=rtol + @test dot(b, p.constraints[1].dual) ≈ p.optval atol=atol rtol=rtol + end + + x = Variable(2) + A = [1 2; 2 4]; + b = [3, 6]; + p = minimize(norm(x, 2), A*x==b) + + test && @test vexity(p) == ConvexVexity() + + handle_problem!(p) + + if test + @test p.optval ≈ 3/sqrt(5) atol=atol rtol=rtol + @test evaluate(x) ≈ [3/5, 6/5] atol=atol rtol=rtol + @test evaluate(norm(x, 2)) ≈ p.optval atol=atol rtol=rtol + @test dot(b, p.constraints[1].dual) ≈ p.optval atol=atol rtol=rtol + end + + x = Variable(2) + A = [1 2; 2 4]; + b = [3, 6]; + p = minimize(norm(x, Inf), A*x==b) + + test && @test vexity(p) == ConvexVexity() + + handle_problem!(p) + + if test + @test p.optval ≈ 1.0 atol=atol rtol=rtol + @test evaluate(x) ≈ [1, 1] atol=atol rtol=rtol + @test evaluate(norm(x, Inf)) ≈ p.optval atol=atol rtol=rtol + @test dot(b, p.constraints[1].dual) ≈ p.optval atol=atol rtol=rtol + end +end diff --git a/test/runtests.jl b/test/runtests.jl index a7abe4021..0d9e542ad 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -36,7 +36,7 @@ end end @testset "GLPK MIP" begin - run_tests(; exclude=[r"socp", r"sdp", r"exp"]) do p + run_tests(; exclude=[r"socp", r"sdp", r"exp", r"dual"]) do p solve!(p, GLPKSolverMIP()) end end