|
| 1 | +using Test |
| 2 | + |
| 3 | +using MathOptInterface |
| 4 | +const MOI = MathOptInterface |
| 5 | +const MOIT = MathOptInterface.Test |
| 6 | +const MOIU = MathOptInterface.Utilities |
| 7 | +const MOIB = MathOptInterface.Bridges |
| 8 | + |
| 9 | +include("../utilities.jl") |
| 10 | + |
| 11 | +mock = MOIU.MockOptimizer(MOIU.Model{Float64}()) |
| 12 | +config = MOIT.TestConfig() |
| 13 | + |
| 14 | +bridged_mock = MOIB.Variable.Zeros{Float64}(mock) |
| 15 | + |
| 16 | +x, cx = MOI.add_constrained_variable(bridged_mock, MOI.GreaterThan(0.0)) |
| 17 | +yz, cyz = MOI.add_constrained_variables(bridged_mock, MOI.Zeros(2)) |
| 18 | +y, z = yz |
| 19 | +fx = MOI.SingleVariable(x) |
| 20 | +fy = MOI.SingleVariable(y) |
| 21 | +fz = MOI.SingleVariable(z) |
| 22 | +c1, c2 = MOI.add_constraints( |
| 23 | + bridged_mock, [1.0fy + 1.0fz, 1.0fx + 1.0fy + 1.0fz], |
| 24 | + [MOI.EqualTo(0.0), MOI.GreaterThan(1.0)] |
| 25 | +) |
| 26 | +#c2 = MOI.add_constraint(bridged_mock, , ) |
| 27 | +MOI.set(bridged_mock, MOI.ObjectiveSense(), MOI.MIN_SENSE) |
| 28 | +obj = 1.0fx - 1.0fy - 1.0fz |
| 29 | +MOI.set(bridged_mock, MOI.ObjectiveFunction{typeof(obj)}(), obj) |
| 30 | + |
| 31 | +@test MOIB.Variable.unbridged_map(MOIB.bridge(bridged_mock, y), y, MOIB.Variable.IndexInVector(1)) === nothing |
| 32 | +@test MOIB.Variable.unbridged_map(MOIB.bridge(bridged_mock, z), z, MOIB.Variable.IndexInVector(2)) === nothing |
| 33 | + |
| 34 | +err = ErrorException( |
| 35 | + "Cannot delete constraint index of bridged constrained variables. Delete" * |
| 36 | + " the scalar variable or the vector of variables instead." |
| 37 | +) |
| 38 | +@test_throws err MOI.delete(bridged_mock, cyz) |
| 39 | + |
| 40 | +err = ErrorException( |
| 41 | + "Cannot add two `VectorOfVariables`-in-`MathOptInterface.Zeros` on the" * |
| 42 | + " same first variable MathOptInterface.VariableIndex(-1)." |
| 43 | +) |
| 44 | +@test_throws err MOI.add_constraint(bridged_mock, MOI.VectorOfVariables(yz), MOI.Zeros(2)) |
| 45 | + |
| 46 | +err = ErrorException( |
| 47 | + "Cannot `VectorOfVariables`-in-`MathOptInterface.Zeros` for" * |
| 48 | + " which some variables are bridged but not the first one" * |
| 49 | + " `MathOptInterface.VariableIndex(12345679)`." |
| 50 | +) |
| 51 | +@test_throws err MOI.add_constraint(bridged_mock, MOI.VectorOfVariables([x, y]), MOI.Zeros(2)) |
| 52 | + |
| 53 | +err = ErrorException( |
| 54 | + "Cannot unbridge function because some variables are bridged by" * |
| 55 | + " variable bridges that do not support reverse mapping, e.g.," * |
| 56 | + " `ZerosBridge`." |
| 57 | +) |
| 58 | +@test_throws err MOI.get(bridged_mock, MOI.ObjectiveFunction{typeof(obj)}()) |
| 59 | +# With `c1`, the function does not contain any variable so it tests that it |
| 60 | +# also throws an error even if it never calls `variable_unbridged_function`. |
| 61 | +@test_throws err MOI.get(bridged_mock, MOI.ConstraintFunction(), c1) |
| 62 | +@test_throws err MOI.get(bridged_mock, MOI.ConstraintFunction(), c2) |
| 63 | + |
| 64 | +err = ArgumentError( |
| 65 | + "Variable bridge of type `MathOptInterface.Bridges.Variable.ZerosBridge{Float64}`" * |
| 66 | + " does not support accessing the attribute `MathOptInterface.Test.UnknownVariableAttribute()`." |
| 67 | +) |
| 68 | +@test_throws err MOI.get(bridged_mock, MOIT.UnknownVariableAttribute(), y) |
| 69 | + |
| 70 | +@testset "Results" begin |
| 71 | + MOIU.set_mock_optimize!(mock, |
| 72 | + (mock::MOIU.MockOptimizer) -> MOIU.mock_optimize!( |
| 73 | + mock, [1.0], |
| 74 | + (MOI.ScalarAffineFunction{Float64}, MOI.EqualTo{Float64}) => 0.0, |
| 75 | + (MOI.ScalarAffineFunction{Float64}, MOI.GreaterThan{Float64}) => 1.0) |
| 76 | + ) |
| 77 | + MOI.optimize!(bridged_mock) |
| 78 | + @test MOI.get(bridged_mock, MOI.VariablePrimal(), x) == 1.0 |
| 79 | + @test MOI.get(bridged_mock, MOI.VariablePrimal(), y) == 0.0 |
| 80 | + @test MOI.get(bridged_mock, MOI.VariablePrimal(), z) == 0.0 |
| 81 | + |
| 82 | + @test MOI.get(bridged_mock, MOI.ConstraintPrimal(), cyz) == zeros(2) |
| 83 | + |
| 84 | + @test MOI.get(bridged_mock, MOI.ConstraintDual(), cx) == 0.0 |
| 85 | + @test MOI.get(bridged_mock, MOI.ConstraintDual(), c1) == 0.0 |
| 86 | + @test MOI.get(bridged_mock, MOI.ConstraintDual(), c2) == 1.0 |
| 87 | + |
| 88 | + err = ArgumentError( |
| 89 | + "Bridge of type `MathOptInterface.Bridges.Variable.ZerosBridge{Float64}`" * |
| 90 | + " does not support accessing the attribute" * |
| 91 | + " `MathOptInterface.ConstraintDual(1)`." |
| 92 | + ) |
| 93 | + @test_throws err MOI.get(bridged_mock, MOI.ConstraintDual(), cyz) |
| 94 | +end |
| 95 | + |
| 96 | +@testset "Query" begin |
| 97 | + @test MOI.get(bridged_mock, MOI.ConstraintFunction(), cyz).variables == yz |
| 98 | + @test MOI.get(mock, MOI.NumberOfVariables()) == 1 |
| 99 | + @test MOI.get(mock, MOI.ListOfVariableIndices()) == [x] |
| 100 | + @test MOI.get(bridged_mock, MOI.NumberOfVariables()) == 3 |
| 101 | + @test MOI.get(bridged_mock, MOI.ListOfVariableIndices()) == [x, y, z] |
| 102 | + @test MOI.get(mock, MOI.NumberOfConstraints{MOI.VectorOfVariables, MOI.Zeros}()) == 0 |
| 103 | + @test MOI.get(bridged_mock, MOI.NumberOfConstraints{MOI.VectorOfVariables, MOI.Zeros}()) == 1 |
| 104 | + @test MOI.get(bridged_mock, MOI.ListOfConstraintIndices{MOI.VectorOfVariables, MOI.Zeros}()) == [cyz] |
| 105 | +end |
| 106 | + |
| 107 | +@testset "SingleVariable objective" begin |
| 108 | + err = ErrorException("Using bridged variable in `SingleVariable` function.") |
| 109 | + @test_throws err MOI.set(bridged_mock, MOI.ObjectiveFunction{typeof(fy)}(), fy) |
| 110 | + MOI.set(bridged_mock, MOI.ObjectiveFunction{typeof(fx)}(), fx) |
| 111 | + @test MOI.get(bridged_mock, MOI.ObjectiveFunction{typeof(fx)}()) == fx |
| 112 | +end |
| 113 | + |
| 114 | +@testset "Delete" begin |
| 115 | + test_delete_bridged_variables(bridged_mock, yz, MOI.Zeros, 3, ( |
| 116 | + (MOI.SingleVariable, MOI.GreaterThan{Float64}, 1), |
| 117 | + )) |
| 118 | + @test MOI.is_valid(bridged_mock, x) |
| 119 | + @test !MOI.is_valid(bridged_mock, y) |
| 120 | + @test !MOI.is_valid(bridged_mock, z) |
| 121 | +end |
0 commit comments