From 6ca5d389754743bf80fcafcda95c967a878c9457 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Tue, 30 Jul 2019 19:23:25 +0200 Subject: [PATCH 1/3] Update tests with add_constrained_variables --- src/Test/contconic.jl | 136 ++++++++++++++++++++++++++++++++--------- src/Test/modellike.jl | 99 +++++++++++++++++++++++++----- test/Test/Test.jl | 2 + test/Test/contconic.jl | 8 ++- 4 files changed, 200 insertions(+), 45 deletions(-) diff --git a/src/Test/contconic.jl b/src/Test/contconic.jl index a83f462d8f..8e98dc4670 100644 --- a/src/Test/contconic.jl +++ b/src/Test/contconic.jl @@ -114,7 +114,20 @@ function _lin2test(model::MOI.ModelLike, config::TestConfig, vecofvars::Bool) MOI.empty!(model) @test MOI.is_empty(model) - x,y,z,s = MOI.add_variables(model, 4) + x = MOI.add_variable(model) + @test MOI.get(model, MOI.NumberOfVariables()) == 1 + + if vecofvars + ys, vc = MOI.add_constrained_variables(model, MOI.Nonpositives(1)) + y = ys[1] + else + y = MOI.add_variable(model) + func = MOI.VectorAffineFunction{Float64}(MOI.VectorOfVariables([y])) + vc = MOI.add_constraint(model, func, MOI.Nonpositives(1)) + end + @test MOI.get(model, MOI.NumberOfVariables()) == 2 + + z, s = MOI.add_variables(model, 2) @test MOI.get(model, MOI.NumberOfVariables()) == 4 MOI.set(model, MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(), MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.([3.0, 2.0, -4.0], [x,y,z]), 0.0)) @@ -122,12 +135,6 @@ function _lin2test(model::MOI.ModelLike, config::TestConfig, vecofvars::Bool) c = MOI.add_constraint(model, MOI.VectorAffineFunction(MOI.VectorAffineTerm.([1,1,2,3,3], MOI.ScalarAffineTerm.([1.0,-1.0,1.0,1.0,1.0], [x,s,y,x,z])), [4.0,3.0,-12.0]), MOI.Zeros(3)) - vov = MOI.VectorOfVariables([y]) - if vecofvars - vc = MOI.add_constraint(model, vov, MOI.Nonpositives(1)) - else - vc = MOI.add_constraint(model, MOI.VectorAffineFunction{Float64}(vov), MOI.Nonpositives(1)) - end if vecofvars # test fallback vz = MOI.add_constraint(model, [z], MOI.Nonnegatives(1)) @@ -256,10 +263,10 @@ function lin4test(model::MOI.ModelLike, config::TestConfig) MOI.empty!(model) @test MOI.is_empty(model) - x = MOI.add_variable(model) + xs, cx = MOI.add_constrained_variables(model, MOI.Nonpositives(1)) + x = xs[1] MOI.add_constraint(model, MOI.VectorAffineFunction([MOI.VectorAffineTerm(1, MOI.ScalarAffineTerm(1.0, x))], [-1.0]), MOI.Nonnegatives(1)) - MOI.add_constraint(model, MOI.VectorOfVariables([x]), MOI.Nonpositives(1)) @test MOI.get(model, MOI.NumberOfConstraints{MOI.VectorAffineFunction{Float64},MOI.Nonnegatives}()) == 1 @test MOI.get(model, MOI.NumberOfConstraints{MOI.VectorOfVariables,MOI.Nonpositives}()) == 1 @@ -603,18 +610,18 @@ function _rotatedsoc1test(model::MOI.ModelLike, config::TestConfig, abvars::Bool MOI.empty!(model) @test MOI.is_empty(model) - x = MOI.add_variables(model, 2) if abvars - a = MOI.add_variable(model) - b = MOI.add_variable(model) + abx, rsoc = MOI.add_constrained_variables(model, MOI.RotatedSecondOrderCone(4)) + a, b, x1, x2 = abx + x = [x1, x2] vc1 = MOI.add_constraint(model, MOI.SingleVariable(a), MOI.EqualTo(0.5)) # We test this after the creation of every `SingleVariable` constraint # to ensure a good coverage of corner cases. @test vc1.value == a.value vc2 = MOI.add_constraint(model, MOI.SingleVariable(b), MOI.EqualTo(1.0)) @test vc2.value == b.value - rsoc = MOI.add_constraint(model, MOI.VectorOfVariables([a; b; x]), MOI.RotatedSecondOrderCone(4)) else + x = MOI.add_variables(model, 2) a = 0.5 b = 1.0 rsoc = MOI.add_constraint(model, MOI.VectorAffineFunction(MOI.VectorAffineTerm.([3, 4], MOI.ScalarAffineTerm.([1., 1.], x)), [a, b, 0., 0.]), MOI.RotatedSecondOrderCone(4)) @@ -702,7 +709,7 @@ function rotatedsoc2test(model::MOI.ModelLike, config::TestConfig) MOI.empty!(model) @test MOI.is_empty(model) - x = MOI.add_variables(model, 3) + x, rsoc = MOI.add_constrained_variables(model, MOI.RotatedSecondOrderCone(3)) vc1 = MOI.add_constraint(model, MOI.SingleVariable(x[1]), MOI.LessThan(1.0)) @test vc1.value == x[1].value @@ -711,8 +718,6 @@ function rotatedsoc2test(model::MOI.ModelLike, config::TestConfig) vc3 = MOI.add_constraint(model, MOI.SingleVariable(x[3]), MOI.GreaterThan(2.0)) @test vc3.value == x[3].value - rsoc = MOI.add_constraint(model, MOI.VectorOfVariables(x), MOI.RotatedSecondOrderCone(3)) - @test MOI.get(model, MOI.NumberOfConstraints{MOI.SingleVariable,MOI.LessThan{Float64}}()) == 1 @test MOI.get(model, MOI.NumberOfConstraints{MOI.SingleVariable,MOI.EqualTo{Float64}}()) == 1 @test MOI.get(model, MOI.NumberOfConstraints{MOI.SingleVariable,MOI.GreaterThan{Float64}}()) == 1 @@ -775,8 +780,11 @@ function rotatedsoc3test(model::MOI.ModelLike, config::TestConfig; n=2, ub=3.0) MOI.empty!(model) @test MOI.is_empty(model) - x = MOI.add_variables(model, n) - u = MOI.add_variable(model) + x, cx = MOI.add_constrained_variables(model, MOI.Nonnegatives(n)) + u, cu1 = MOI.add_constrained_variable(model, MOI.GreaterThan(0.0)) + @test cu1.value == u.value + cu2 = MOI.add_constraint(model, MOI.SingleVariable(u), MOI.LessThan(ub)) + @test cu2.value == u.value v = MOI.add_variable(model) t = MOI.add_variables(model, 2) @@ -784,11 +792,6 @@ function rotatedsoc3test(model::MOI.ModelLike, config::TestConfig; n=2, ub=3.0) @test ct1.value == t[1].value ct2 = MOI.add_constraint(model, MOI.SingleVariable(t[2]), MOI.EqualTo(1.0)) @test ct2.value == t[2].value - cx = MOI.add_constraint(model, MOI.VectorOfVariables(x), MOI.Nonnegatives(n)) - cu1 = MOI.add_constraint(model, MOI.SingleVariable(u), MOI.GreaterThan(0.0)) - @test cu1.value == u.value - cu2 = MOI.add_constraint(model, MOI.SingleVariable(u), MOI.LessThan(ub)) - @test cu2.value == u.value c1 = MOI.add_constraint(model, MOI.VectorAffineFunction(MOI.VectorAffineTerm.(1:(2+n), MOI.ScalarAffineTerm.([1/√2; 1/√2; ones(n)], [t; x])), zeros(2+n)), MOI.RotatedSecondOrderCone(2+n)) c2 = MOI.add_constraint(model, MOI.VectorAffineFunction(MOI.VectorAffineTerm.([1, 2, 3], MOI.ScalarAffineTerm.([1/√2; 1/√2; 1.0], [x[1], u, v])), zeros(3)), MOI.RotatedSecondOrderCone(3)) @@ -847,11 +850,87 @@ function rotatedsoc3test(model::MOI.ModelLike, config::TestConfig; n=2, ub=3.0) end end +function rotatedsoc4test(model::MOI.ModelLike, config::TestConfig; n=2, ub=3.0) + atol = config.atol + rtol = config.rtol + # Problem SOCRotated4 + # max x + y + # s.t. + # t + u ≤ 2 (1) + # [t, u, x, y] in RSOC(4) (2) + # Solution: + # By AM-QM: (x+y)/2 ≤ √((x^2+y^2)/2) with equality iff x = y + # That is, + # (x + y)^2/2 ≤ x^2 + y^2 (3) + # By AM-GM: √tu ≤ (t+u)/2 with equality iff t = u + # That is, + # 2tu ≤ (t + u)^2/2 (4) + # Combining (2), (3) and (4), we have + # |x + y| ≤ t + u (5) + # with equality iff x = y and t = u. + # Combining (1) and (5), we have + # x + y ≤ 2 + # with equality iff x = y. + # We conclude that the optimal solution is x = y = t = u = 1 + # with objective value 2. + + @test MOIU.supports_default_copy_to(model, #=copy_names=# false) + @test MOI.supports(model, MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}()) + @test MOI.supports(model, MOI.ObjectiveSense()) + @test MOI.supports_constraint(model, MOI.ScalarAffineFunction{Float64}, MOI.LessThan{Float64}) + @test MOI.supports_constraint(model, MOI.VectorOfVariables, MOI.RotatedSecondOrderCone) + + MOI.empty!(model) + @test MOI.is_empty(model) + + v, cv = MOI.add_constrained_variables(model, MOI.RotatedSecondOrderCone(4)) + t, u, x, y = v + ft = MOI.SingleVariable(t) + fu = MOI.SingleVariable(u) + c = MOI.add_constraint(model, 1.0ft + 1.0fu, MOI.LessThan(2.0)) + fx = MOI.SingleVariable(x) + fy = MOI.SingleVariable(y) + func = 1.0fx + 1.0fy + MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE) + MOI.set(model, MOI.ObjectiveFunction{typeof(func)}(), func) + + if config.solve + @test MOI.get(model, MOI.TerminationStatus()) == MOI.OPTIMIZE_NOT_CALLED + + MOI.optimize!(model) + + @test MOI.get(model, MOI.TerminationStatus()) == config.optimal_status + + @test MOI.get(model, MOI.PrimalStatus()) == MOI.FEASIBLE_POINT + if config.duals + @test MOI.get(model, MOI.DualStatus()) == MOI.FEASIBLE_POINT + end + + @test MOI.get(model, MOI.ObjectiveValue()) ≈ 2.0 atol=atol rtol=rtol + if config.duals + @test MOI.get(model, MOI.DualObjectiveValue()) ≈ 2.0 atol=atol rtol=rtol + end + + @test MOI.get(model, MOI.VariablePrimal(), t) ≈ 1.0 atol=atol rtol=rtol + @test MOI.get(model, MOI.VariablePrimal(), u) ≈ 1.0 atol=atol rtol=rtol + @test MOI.get(model, MOI.VariablePrimal(), x) ≈ 1.0 atol=atol rtol=rtol + @test MOI.get(model, MOI.VariablePrimal(), y) ≈ 1.0 atol=atol rtol=rtol + + @test MOI.get(model, MOI.ConstraintPrimal(), cv) ≈ ones(4) atol=atol rtol=rtol + @test MOI.get(model, MOI.ConstraintPrimal(), c) ≈ 2.0 atol=atol rtol=rtol + + if config.duals + @test MOI.get(model, MOI.ConstraintDual(), cv) ≈ [1.0, 1.0, -1.0, -1.0] atol=atol rtol=rtol + @test MOI.get(model, MOI.ConstraintDual(), c) ≈ -1.0 atol=atol rtol=rtol + end + end +end const rsoctests = Dict("rotatedsoc1v" => rotatedsoc1vtest, "rotatedsoc1f" => rotatedsoc1ftest, "rotatedsoc2" => rotatedsoc2test, - "rotatedsoc3" => rotatedsoc3test) + "rotatedsoc3" => rotatedsoc3test, + "rotatedsoc4" => rotatedsoc4test) @moitestset rsoc @@ -1094,12 +1173,12 @@ function exp3test(model::MOI.ModelLike, config::TestConfig) @test MOI.is_empty(model) x = MOI.add_variable(model) - y = MOI.add_variable(model) - @test MOI.get(model, MOI.NumberOfVariables()) == 2 + @test MOI.get(model, MOI.NumberOfVariables()) == 1 xc = MOI.add_constraint(model, MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(2.0, x)], 0.0), MOI.LessThan(4.0)) - yc = MOI.add_constraint(model, MOI.SingleVariable(y), MOI.LessThan(5.0)) + y, yc = MOI.add_constrained_variable(model, MOI.LessThan(5.0)) @test yc.value == y.value + @test MOI.get(model, MOI.NumberOfVariables()) == 2 ec = MOI.add_constraint(model, MOI.VectorAffineFunction(MOI.VectorAffineTerm.([1, 3], MOI.ScalarAffineTerm.(1.0, [x, y])), [0.0, 1.0, 0.0]), MOI.ExponentialCone()) MOI.set(model, MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(), MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.0, x)], 0.0)) @@ -1594,8 +1673,7 @@ function _det1test(model::MOI.ModelLike, config::TestConfig, vecofvars::Bool, de @test MOI.get(model, MOI.NumberOfVariables()) == (square ? 5 : 4) if logdet - u = MOI.add_variable(model) - vc = MOI.add_constraint(model, MOI.SingleVariable(u), MOI.EqualTo(1.0)) + u, vc = MOI.add_constrained_variable(model, MOI.EqualTo(1.0)) @test vc.value == u.value vov = MOI.VectorOfVariables([t; u; Q]) else diff --git a/src/Test/modellike.jl b/src/Test/modellike.jl index 7c4853df2e..a8d8620545 100644 --- a/src/Test/modellike.jl +++ b/src/Test/modellike.jl @@ -37,9 +37,21 @@ function nametest(model::MOI.ModelLike) @test MOI.supports(model, MOI.VariableName(), MOI.VariableIndex) v = MOI.add_variables(model, 2) @test MOI.get(model, MOI.VariableName(), v[1]) == "" + x, cx = MOI.add_constrained_variable(model, MOI.GreaterThan(0.0)) + @test MOI.get(model, MOI.VariableName(), x) == "" + @test MOI.get(model, MOI.ConstraintName(), cx) == "" + y, cy = MOI.add_constrained_variables(model, MOI.Nonpositives(4)) + for yi in y + @test MOI.get(model, MOI.VariableName(), yi) == "" + end + @test MOI.get(model, MOI.ConstraintName(), cy) == "" MOI.set(model, MOI.VariableName(), v[1], "") MOI.set(model, MOI.VariableName(), v[2], "") # Shouldn't error with duplicate empty name + MOI.set(model, MOI.VariableName(), x, "") + for yi in y + MOI.set(model, MOI.VariableName(), yi, "") + end MOI.set(model, MOI.VariableName(), v[1], "Var1") MOI.set(model, MOI.VariableName(), v[2], "Var1") @@ -52,30 +64,45 @@ function nametest(model::MOI.ModelLike) @test MOI.get(model, MOI.VariableIndex, "Var2") == v[2] @test MOI.get(model, MOI.VariableIndex, "Var3") === nothing - MOI.set(model, MOI.VariableName(), v, ["VarX","Var2"]) - @test MOI.get(model, MOI.VariableName(), v) == ["VarX", "Var2"] + MOI.set(model, MOI.VariableName(), x, "Var1") + @test_throws Exception MOI.get(model, MOI.VariableIndex, "Var1") + + MOI.set(model, MOI.VariableName(), x, "Varx") + @test MOI.get(model, MOI.VariableIndex, "Var1") == v[1] + @test MOI.get(model, MOI.VariableIndex, "Var2") == v[2] + @test MOI.get(model, MOI.VariableIndex, "Varx") == x + @test MOI.get(model, MOI.VariableIndex, "Var3") === nothing + + vynames = ["VarX", "Var2", "Vary1", "Vary2", "Vary3", "Vary4"] + MOI.set(model, MOI.VariableName(), [v; y], vynames) + @test MOI.get(model, MOI.VariableName(), v) == vynames[1:2] + @test MOI.get(model, MOI.VariableName(), y) == vynames[3:6] + @test MOI.get(model, MOI.VariableName(), [v; y]) == vynames @test MOI.supports_constraint(model, MOI.ScalarAffineFunction{Float64}, MOI.LessThan{Float64}) c = MOI.add_constraint(model, MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.([1.0,1.0], v), 0.0), MOI.LessThan(1.0)) @test MOI.supports_constraint(model, MOI.ScalarAffineFunction{Float64}, MOI.EqualTo{Float64}) c2 = MOI.add_constraint(model, MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.([-1.0,1.0], v), 0.0), MOI.EqualTo(0.0)) @test MOI.get(model, MOI.ConstraintName(), c) == "" + @test MOI.get(model, MOI.ConstraintName(), c2) == "" + @test MOI.get(model, MOI.ConstraintName(), cx) == "" + @test MOI.get(model, MOI.ConstraintName(), cy) == "" @test MOI.supports(model, MOI.ConstraintName(), typeof(c)) MOI.set(model, MOI.ConstraintName(), c, "") @test MOI.supports(model, MOI.ConstraintName(), typeof(c2)) MOI.set(model, MOI.ConstraintName(), c2, "") # Shouldn't error with duplicate empty name + @test MOI.supports(model, MOI.ConstraintName(), typeof(cx)) + MOI.set(model, MOI.ConstraintName(), cx, "") + @test MOI.supports(model, MOI.ConstraintName(), typeof(cy)) + MOI.set(model, MOI.ConstraintName(), cy, "") MOI.set(model, MOI.ConstraintName(), c, "Con0") @test MOI.get(model, MOI.ConstraintName(), c) == "Con0" MOI.set(model, MOI.ConstraintName(), c2, "Con0") # Lookup must fail when multiple constraints have the same name. @test_throws Exception MOI.get(model, MOI.ConstraintIndex, "Con0") - @test_throws Exception MOI.get(model, - MOI.ConstraintIndex{ - MOI.ScalarAffineFunction{Float64}, - MOI.LessThan{Float64}}, - "Con0") + @test_throws Exception MOI.get(model, typeof(c), "Con0") MOI.set(model, MOI.ConstraintName(), [c], ["Con1"]) @test MOI.get(model, MOI.ConstraintName(), [c]) == ["Con1"] @@ -87,21 +114,63 @@ function nametest(model::MOI.ModelLike) @test MOI.get(model, MOI.ConstraintIndex, "Con1") == c @test MOI.get(model, MOI.ConstraintIndex, "Con2") === nothing - MOI.set(model, MOI.ConstraintName(), c2, "Con0") + MOI.set(model, MOI.ConstraintName(), [c], ["Con1"]) - @test MOI.get(model, MOI.ConstraintIndex{MOI.ScalarAffineFunction{Float64},MOI.LessThan{Float64}}, "Con0") === nothing - @test MOI.get(model, MOI.ConstraintIndex{MOI.ScalarAffineFunction{Float64},MOI.EqualTo{Float64}}, "Con1") === nothing - @test MOI.get(model, MOI.ConstraintIndex{MOI.ScalarAffineFunction{Float64},MOI.LessThan{Float64}}, "Con1") == c - @test MOI.get(model, MOI.ConstraintIndex{MOI.ScalarAffineFunction{Float64},MOI.EqualTo{Float64}}, "Con0") == c2 - @test MOI.get(model, MOI.ConstraintIndex, "Con0") == c2 - @test MOI.get(model, MOI.ConstraintIndex, "Con1") == c + MOI.set(model, MOI.ConstraintName(), [c2, cx], ["Con2", "Con2"]) + @test_throws Exception MOI.get(model, MOI.ConstraintIndex, "Con2") + @test_throws Exception MOI.get(model, typeof(c2), "Con2") + @test_throws Exception MOI.get(model, typeof(cx), "Con2") + + MOI.set(model, MOI.ConstraintName(), [cx, cy], ["Con3", "Con3"]) + @test_throws Exception MOI.get(model, MOI.ConstraintIndex, "Con3") + @test_throws Exception MOI.get(model, typeof(cx), "Con3") + @test_throws Exception MOI.get(model, typeof(cy), "Con3") + + MOI.set(model, MOI.ConstraintName(), cy, "Con4") + + for (i, ca) in enumerate([c, c2, cx, cy]) + namea = "Con$i" + @test MOI.get(model, MOI.ConstraintName(), ca) == namea + @test MOI.get(model, typeof(ca), namea) == ca + @test MOI.get(model, MOI.ConstraintIndex, namea) == ca + for cb in [c, c2, cx, cy] + if ca === cb + continue + end + nameb = MOI.get(model, MOI.ConstraintName(), cb) + @test MOI.get(model, typeof(cb), namea) == nothing + @test MOI.get(model, typeof(ca), nameb) == nothing + end + end MOI.delete(model, v[2]) @test MOI.get(model, MOI.VariableIndex, "Var2") === nothing MOI.delete(model, c) - @test MOI.get(model, MOI.ConstraintIndex{MOI.ScalarAffineFunction{Float64},MOI.LessThan{Float64}}, "Con1") === nothing + @test MOI.get(model, typeof(c), "Con1") === nothing @test MOI.get(model, MOI.ConstraintIndex, "Con1") === nothing + + MOI.set(model, MOI.ConstraintName(), cx, "Con2") + @test_throws Exception MOI.get(model, MOI.ConstraintIndex, "Con2") + @test_throws Exception MOI.get(model, typeof(c2), "Con2") + @test_throws Exception MOI.get(model, typeof(cx), "Con2") + + MOI.delete(model, x) + @test MOI.get(model, MOI.VariableIndex, "Varx") === nothing + @test MOI.get(model, typeof(cx), "Con3") === nothing + @test MOI.get(model, MOI.ConstraintIndex, "Con3") === nothing + @test MOI.get(model, typeof(c2), "Con2") === c2 + @test MOI.get(model, MOI.ConstraintIndex, "Con2") === c2 + + MOI.delete(model, y) + @test MOI.get(model, typeof(cy), "Con4") === nothing + @test MOI.get(model, MOI.ConstraintIndex, "Con4") === nothing + for i in 1:4 + @test MOI.get(model, MOI.VariableIndex, "Vary$i") === nothing + end + MOI.set(model, MOI.ConstraintName(), c2, "Con4") + @test MOI.get(model, typeof(c2), "Con4") === c2 + @test MOI.get(model, MOI.ConstraintIndex, "Con4") === c2 end end diff --git a/test/Test/Test.jl b/test/Test/Test.jl index 98c0e387da..e40ee09f2b 100644 --- a/test/Test/Test.jl +++ b/test/Test/Test.jl @@ -1,3 +1,5 @@ +using Test + @testset "Config" begin include("config.jl") end diff --git a/test/Test/contconic.jl b/test/Test/contconic.jl index eea437c2b4..cf3b413fa6 100644 --- a/test/Test/contconic.jl +++ b/test/Test/contconic.jl @@ -61,7 +61,7 @@ end MOIT.soc4test(mock, config) end @testset "RSOC" begin - mock.optimize! = (mock::MOIU.MockOptimizer) -> MOIU.mock_optimize!(mock, [1/√2, 1/√2, 0.5, 1.0], + mock.optimize! = (mock::MOIU.MockOptimizer) -> MOIU.mock_optimize!(mock, [0.5, 1.0, 1/√2, 1/√2], (MOI.SingleVariable, MOI.EqualTo{Float64}) => [-√2, -1/√2], (MOI.VectorOfVariables, MOI.RotatedSecondOrderCone) => [[√2, 1/√2, -1.0, -1.0]]) # double variable bounds on a and b variables @@ -92,6 +92,12 @@ end mock.eval_variable_constraint_dual = false MOIT.rotatedsoc3test(mock, config) mock.eval_variable_constraint_dual = true + + mock.optimize! = (mock::MOIU.MockOptimizer) -> MOIU.mock_optimize!(mock, ones(4), + (MOI.ScalarAffineFunction{Float64}, MOI.LessThan{Float64}) => [-1.0], + (MOI.VectorOfVariables, MOI.RotatedSecondOrderCone) => [[1.0, 1.0, -1.0, -1.0]]) + MOIT.rotatedsoc4test(mock, config) + end @testset "GeoMean" begin mock.optimize! = (mock::MOIU.MockOptimizer) -> MOIU.mock_optimize!(mock, ones(4)) From 1284ac4db726d58c446981a7a97287280e5fc671 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Tue, 30 Jul 2019 20:13:02 +0200 Subject: [PATCH 2/3] Update Bridges tests --- test/Bridges/Constraint/flip_sign.jl | 4 +--- test/Bridges/Constraint/functionize.jl | 4 +--- test/Bridges/Constraint/interval.jl | 12 ++++++++---- test/Bridges/Constraint/quad_to_soc.jl | 4 +--- test/Bridges/Constraint/rsoc.jl | 6 ++---- test/Bridges/Constraint/scalarize.jl | 4 +--- test/Bridges/Constraint/slack.jl | 4 +--- test/Bridges/Constraint/soc_to_psd.jl | 6 ++---- test/Bridges/Constraint/square.jl | 4 +--- test/Bridges/Constraint/vectorize.jl | 4 +--- 10 files changed, 19 insertions(+), 33 deletions(-) diff --git a/test/Bridges/Constraint/flip_sign.jl b/test/Bridges/Constraint/flip_sign.jl index 804057d638..9606cdd66b 100644 --- a/test/Bridges/Constraint/flip_sign.jl +++ b/test/Bridges/Constraint/flip_sign.jl @@ -8,9 +8,7 @@ const MOIB = MathOptInterface.Bridges include("../utilities.jl") -include("../simple_model.jl") - -mock = MOIU.MockOptimizer(SimpleModel{Float64}()) +mock = MOIU.MockOptimizer(MOIU.Model{Float64}()) config = MOIT.TestConfig() @testset "GreaterToLess" begin diff --git a/test/Bridges/Constraint/functionize.jl b/test/Bridges/Constraint/functionize.jl index db403bed35..c83b7860ba 100644 --- a/test/Bridges/Constraint/functionize.jl +++ b/test/Bridges/Constraint/functionize.jl @@ -8,9 +8,7 @@ const MOIB = MathOptInterface.Bridges include("../utilities.jl") -include("../simple_model.jl") - -mock = MOIU.MockOptimizer(SimpleModel{Float64}()) +mock = MOIU.MockOptimizer(MOIU.Model{Float64}()) config = MOIT.TestConfig() config_with_basis = MOIT.TestConfig(basis = true) diff --git a/test/Bridges/Constraint/interval.jl b/test/Bridges/Constraint/interval.jl index dc09dae336..92988712df 100644 --- a/test/Bridges/Constraint/interval.jl +++ b/test/Bridges/Constraint/interval.jl @@ -8,9 +8,7 @@ const MOIB = MathOptInterface.Bridges include("../utilities.jl") -include("../simple_model.jl") - -mock = MOIU.MockOptimizer(SimpleModel{Float64}()) +mock = MOIU.MockOptimizer(MOIU.Model{Float64}()) config = MOIT.TestConfig() config_with_basis = MOIT.TestConfig(basis = true) @@ -61,9 +59,15 @@ config_with_basis = MOIT.TestConfig(basis = true) MOIT.linear10btest(bridged_mock, config_with_basis) ci = first(MOI.get(bridged_mock, MOI.ListOfConstraintIndices{MOI.ScalarAffineFunction{Float64}, MOI.Interval{Float64}}())) - newf = MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.([1.0, -1.0], MOI.get(bridged_mock, MOI.ListOfVariableIndices())), 0.0) + vis = MOI.get(bridged_mock, MOI.ListOfVariableIndices()) + newf = MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.([1.0, -1.0], vis), 0.0) MOI.set(bridged_mock, MOI.ConstraintFunction(), ci, newf) @test MOI.get(bridged_mock, MOI.ConstraintFunction(), ci) ≈ newf + + MOI.modify(bridged_mock, ci, MOI.ScalarCoefficientChange(vis[2], 1.0)) + modified_f = MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.(ones(2), vis), 0.0) + @test MOI.get(bridged_mock, MOI.ConstraintFunction(), ci) ≈ modified_f + test_delete_bridge(bridged_mock, ci, 2, ((MOI.ScalarAffineFunction{Float64}, MOI.GreaterThan{Float64}, 0), (MOI.ScalarAffineFunction{Float64}, MOI.LessThan{Float64}, 0))) end diff --git a/test/Bridges/Constraint/quad_to_soc.jl b/test/Bridges/Constraint/quad_to_soc.jl index a30f1bb1e8..29226a9b5e 100644 --- a/test/Bridges/Constraint/quad_to_soc.jl +++ b/test/Bridges/Constraint/quad_to_soc.jl @@ -8,9 +8,7 @@ const MOIB = MathOptInterface.Bridges include("../utilities.jl") -include("../simple_model.jl") - -mock = MOIU.MockOptimizer(SimpleModel{Float64}()) +mock = MOIU.MockOptimizer(MOIU.Model{Float64}()) config = MOIT.TestConfig() @testset "QuadtoSOC" begin diff --git a/test/Bridges/Constraint/rsoc.jl b/test/Bridges/Constraint/rsoc.jl index f2c7d35400..85441fe9cd 100644 --- a/test/Bridges/Constraint/rsoc.jl +++ b/test/Bridges/Constraint/rsoc.jl @@ -8,9 +8,7 @@ const MOIB = MathOptInterface.Bridges include("../utilities.jl") -include("../simple_model.jl") - -mock = MOIU.MockOptimizer(SimpleModel{Float64}()) +mock = MOIU.MockOptimizer(MOIU.Model{Float64}()) config = MOIT.TestConfig() @testset "RSOC" begin @@ -23,7 +21,7 @@ config = MOIT.TestConfig() MOI.VectorQuadraticFunction{Float64}] for S in [MOI.RotatedSecondOrderCone]]) - mock.optimize! = (mock::MOIU.MockOptimizer) -> MOIU.mock_optimize!(mock, [1/√2, 1/√2, 0.5, 1.0], + mock.optimize! = (mock::MOIU.MockOptimizer) -> MOIU.mock_optimize!(mock, [0.5, 1.0, 1/√2, 1/√2], (MOI.SingleVariable, MOI.EqualTo{Float64}) => [-√2, -1/√2], (MOI.VectorAffineFunction{Float64}, MOI.SecondOrderCone) => [[3/2, 1/2, -1.0, -1.0]]) MOIT.rotatedsoc1vtest(bridged_mock, config) diff --git a/test/Bridges/Constraint/scalarize.jl b/test/Bridges/Constraint/scalarize.jl index 5bf99f2de5..b08343177f 100644 --- a/test/Bridges/Constraint/scalarize.jl +++ b/test/Bridges/Constraint/scalarize.jl @@ -8,9 +8,7 @@ const MOIB = MathOptInterface.Bridges include("../utilities.jl") -include("../simple_model.jl") - -mock = MOIU.MockOptimizer(SimpleModel{Float64}()) +mock = MOIU.MockOptimizer(MOIU.Model{Float64}()) config = MOIT.TestConfig() @testset "Scalarize" begin diff --git a/test/Bridges/Constraint/slack.jl b/test/Bridges/Constraint/slack.jl index b3e9713fb0..f172d3d921 100644 --- a/test/Bridges/Constraint/slack.jl +++ b/test/Bridges/Constraint/slack.jl @@ -8,9 +8,7 @@ const MOIB = MathOptInterface.Bridges include("../utilities.jl") -include("../simple_model.jl") - -mock = MOIU.MockOptimizer(SimpleModel{Float64}()) +mock = MOIU.MockOptimizer(MOIU.Model{Float64}()) config = MOIT.TestConfig() @testset "Scalar slack" begin diff --git a/test/Bridges/Constraint/soc_to_psd.jl b/test/Bridges/Constraint/soc_to_psd.jl index 1a51673e54..b77351fc54 100644 --- a/test/Bridges/Constraint/soc_to_psd.jl +++ b/test/Bridges/Constraint/soc_to_psd.jl @@ -8,9 +8,7 @@ const MOIB = MathOptInterface.Bridges include("../utilities.jl") -include("../simple_model.jl") - -mock = MOIU.MockOptimizer(SimpleModel{Float64}()) +mock = MOIU.MockOptimizer(MOIU.Model{Float64}()) config = MOIT.TestConfig() @testset "SOCtoPSD" begin @@ -33,7 +31,7 @@ end for F in [MOI.VectorOfVariables, MOI.VectorAffineFunction{Float64}] for S in [MOI.RotatedSecondOrderCone]]) - mock.optimize! = (mock::MOIU.MockOptimizer) -> MOIU.mock_optimize!(mock, [1/√2, 1/√2, 0.5, 1.0], + mock.optimize! = (mock::MOIU.MockOptimizer) -> MOIU.mock_optimize!(mock, [0.5, 1.0, 1/√2, 1/√2], (MOI.SingleVariable, MOI.EqualTo{Float64}) => [-√2, -1/√2], (MOI.VectorAffineFunction{Float64}, MOI.PositiveSemidefiniteConeTriangle) => [[√2, -1/2, √2/8, -1/2, √2/8, √2/8]]) MOIT.rotatedsoc1vtest(bridged_mock, config) diff --git a/test/Bridges/Constraint/square.jl b/test/Bridges/Constraint/square.jl index cab53937b8..0151c8d1ca 100644 --- a/test/Bridges/Constraint/square.jl +++ b/test/Bridges/Constraint/square.jl @@ -8,9 +8,7 @@ const MOIB = MathOptInterface.Bridges include("../utilities.jl") -include("../simple_model.jl") - -mock = MOIU.MockOptimizer(SimpleModel{Float64}()) +mock = MOIU.MockOptimizer(MOIU.Model{Float64}()) config = MOIT.TestConfig() @testset "Square" begin diff --git a/test/Bridges/Constraint/vectorize.jl b/test/Bridges/Constraint/vectorize.jl index d267194b7f..1b0a8dbfce 100644 --- a/test/Bridges/Constraint/vectorize.jl +++ b/test/Bridges/Constraint/vectorize.jl @@ -8,9 +8,7 @@ const MOIB = MathOptInterface.Bridges include("../utilities.jl") -include("../simple_model.jl") - -mock = MOIU.MockOptimizer(SimpleModel{Float64}()) +mock = MOIU.MockOptimizer(MOIU.Model{Float64}()) config = MOIT.TestConfig() @testset "Vectorize" begin From a733d97486416ef7d2ff55308f0c61f1745d8a7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Tue, 30 Jul 2019 19:30:57 +0200 Subject: [PATCH 3/3] Remove add_constrained_variable in contconic --- src/Test/contconic.jl | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Test/contconic.jl b/src/Test/contconic.jl index 8e98dc4670..7a44bab367 100644 --- a/src/Test/contconic.jl +++ b/src/Test/contconic.jl @@ -781,10 +781,7 @@ function rotatedsoc3test(model::MOI.ModelLike, config::TestConfig; n=2, ub=3.0) @test MOI.is_empty(model) x, cx = MOI.add_constrained_variables(model, MOI.Nonnegatives(n)) - u, cu1 = MOI.add_constrained_variable(model, MOI.GreaterThan(0.0)) - @test cu1.value == u.value - cu2 = MOI.add_constraint(model, MOI.SingleVariable(u), MOI.LessThan(ub)) - @test cu2.value == u.value + u = MOI.add_variable(model) v = MOI.add_variable(model) t = MOI.add_variables(model, 2) @@ -792,6 +789,10 @@ function rotatedsoc3test(model::MOI.ModelLike, config::TestConfig; n=2, ub=3.0) @test ct1.value == t[1].value ct2 = MOI.add_constraint(model, MOI.SingleVariable(t[2]), MOI.EqualTo(1.0)) @test ct2.value == t[2].value + cu1 = MOI.add_constraint(model, MOI.SingleVariable(u), MOI.GreaterThan(0.0)) + @test cu1.value == u.value + cu2 = MOI.add_constraint(model, MOI.SingleVariable(u), MOI.LessThan(ub)) + @test cu2.value == u.value c1 = MOI.add_constraint(model, MOI.VectorAffineFunction(MOI.VectorAffineTerm.(1:(2+n), MOI.ScalarAffineTerm.([1/√2; 1/√2; ones(n)], [t; x])), zeros(2+n)), MOI.RotatedSecondOrderCone(2+n)) c2 = MOI.add_constraint(model, MOI.VectorAffineFunction(MOI.VectorAffineTerm.([1, 2, 3], MOI.ScalarAffineTerm.([1/√2; 1/√2; 1.0], [x[1], u, v])), zeros(3)), MOI.RotatedSecondOrderCone(3)) @@ -1173,12 +1174,12 @@ function exp3test(model::MOI.ModelLike, config::TestConfig) @test MOI.is_empty(model) x = MOI.add_variable(model) - @test MOI.get(model, MOI.NumberOfVariables()) == 1 + y = MOI.add_variable(model) + @test MOI.get(model, MOI.NumberOfVariables()) == 2 xc = MOI.add_constraint(model, MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(2.0, x)], 0.0), MOI.LessThan(4.0)) - y, yc = MOI.add_constrained_variable(model, MOI.LessThan(5.0)) + yc = MOI.add_constraint(model, MOI.SingleVariable(y), MOI.LessThan(5.0)) @test yc.value == y.value - @test MOI.get(model, MOI.NumberOfVariables()) == 2 ec = MOI.add_constraint(model, MOI.VectorAffineFunction(MOI.VectorAffineTerm.([1, 3], MOI.ScalarAffineTerm.(1.0, [x, y])), [0.0, 1.0, 0.0]), MOI.ExponentialCone()) MOI.set(model, MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(), MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.0, x)], 0.0)) @@ -1673,7 +1674,8 @@ function _det1test(model::MOI.ModelLike, config::TestConfig, vecofvars::Bool, de @test MOI.get(model, MOI.NumberOfVariables()) == (square ? 5 : 4) if logdet - u, vc = MOI.add_constrained_variable(model, MOI.EqualTo(1.0)) + u = MOI.add_variable(model) + vc = MOI.add_constraint(model, MOI.SingleVariable(u), MOI.EqualTo(1.0)) @test vc.value == u.value vov = MOI.VectorOfVariables([t; u; Q]) else