|
| 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("simple_model.jl") |
| 10 | +include("utilities.jl") |
| 11 | + |
| 12 | +struct UnknownConstraintAttribute <: MOI.AbstractConstraintAttribute end |
| 13 | +MOI.is_set_by_optimize(::UnknownConstraintAttribute) = true |
| 14 | + |
| 15 | +mock = MOIU.MockOptimizer(SimpleModel{Float64}()) |
| 16 | +bridged_mock = MOIB.SplitInterval{Float64}(mock) |
| 17 | + |
| 18 | +@testset "Unsupported constraint attribute" begin |
| 19 | + attr = UnknownConstraintAttribute() |
| 20 | + err = ArgumentError( |
| 21 | + "Constraint bridge of type `MathOptInterface.Bridges.SplitIntervalBridge{Float64,MathOptInterface.SingleVariable}` " * |
| 22 | + "does not support accessing the attribute `$attr`.") |
| 23 | + x = MOI.add_variable(bridged_mock) |
| 24 | + ci = MOI.add_constraint(bridged_mock, MOI.SingleVariable(x), |
| 25 | + MOI.Interval(0.0, 1.0)) |
| 26 | + @test_throws err MOI.get(bridged_mock, attr, ci) |
| 27 | +end |
| 28 | + |
| 29 | +@testset "Issue #453" begin |
| 30 | + MOI.empty!(bridged_mock) |
| 31 | + MOIU.loadfromstring!(bridged_mock, """ |
| 32 | + variables: x |
| 33 | + maxobjective: 3.0x |
| 34 | + c: 2.0x in Interval(1.0, 4.0) |
| 35 | + d: x in LessThan(1.5) |
| 36 | + """) |
| 37 | + x = MOI.get(bridged_mock, MOI.VariableIndex, "x") |
| 38 | + @test isa(x, MOI.VariableIndex) |
| 39 | + c1 = MOI.get(bridged_mock, MOI.ConstraintIndex{MOI.ScalarAffineFunction{Float64}, MOI.Interval{Float64}}, "c") |
| 40 | + @test isa(c1, MOI.ConstraintIndex{MOI.ScalarAffineFunction{Float64}, MOI.Interval{Float64}}) |
| 41 | + c2 = MOI.get(bridged_mock, MOI.ConstraintIndex, "c") |
| 42 | + @test c1 == c2 |
| 43 | + d1 = MOI.get(bridged_mock, MOI.ConstraintIndex{MOI.SingleVariable, MOI.LessThan{Float64}}, "d") |
| 44 | + @test isa(d1, MOI.ConstraintIndex{MOI.SingleVariable, MOI.LessThan{Float64}}) |
| 45 | + d2 = MOI.get(bridged_mock, MOI.ConstraintIndex, "d") |
| 46 | + @test d1 == d2 |
| 47 | +end |
| 48 | + |
| 49 | +MOI.empty!(bridged_mock) |
| 50 | + |
| 51 | +@testset "Name test" begin |
| 52 | + MOIT.nametest(bridged_mock) |
| 53 | +end |
| 54 | + |
| 55 | +@testset "Copy test" begin |
| 56 | + MOIT.failcopytestc(bridged_mock) |
| 57 | + MOIT.failcopytestia(bridged_mock) |
| 58 | + MOIT.failcopytestva(bridged_mock) |
| 59 | + MOIT.failcopytestca(bridged_mock) |
| 60 | + MOIT.copytest(bridged_mock, SimpleModel{Float64}()) |
| 61 | +end |
| 62 | + |
| 63 | +@testset "Custom test" begin |
| 64 | + model = MOIB.SplitInterval{Int}(SimpleModel{Int}()) |
| 65 | + @test !MOIB.supports_bridging_constraint(model, MOI.VectorAffineFunction{Float64}, MOI.Interval{Float64}) |
| 66 | + |
| 67 | + x, y = MOI.add_variables(model, 2) |
| 68 | + @test MOI.get(model, MOI.NumberOfVariables()) == 2 |
| 69 | + |
| 70 | + f1 = MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(3, x)], 7) |
| 71 | + c1 = MOI.add_constraint(model, f1, MOI.Interval(-1, 1)) |
| 72 | + |
| 73 | + @test MOI.get(model, MOI.ListOfConstraints()) == [(MOI.ScalarAffineFunction{Int},MOI.Interval{Int})] |
| 74 | + test_noc(model, MOI.ScalarAffineFunction{Int}, MOI.GreaterThan{Int}, 0) |
| 75 | + test_noc(model, MOI.ScalarAffineFunction{Int}, MOI.Interval{Int}, 1) |
| 76 | + @test (@inferred MOI.get(model, MOI.ListOfConstraintIndices{MOI.ScalarAffineFunction{Int},MOI.Interval{Int}}())) == [c1] |
| 77 | + |
| 78 | + f2 = MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.([2, -1], [x, y]), 2) |
| 79 | + c2 = MOI.add_constraint(model, f1, MOI.GreaterThan(-2)) |
| 80 | + |
| 81 | + @test MOI.get(model, MOI.ListOfConstraints()) == [(MOI.ScalarAffineFunction{Int},MOI.GreaterThan{Int}), (MOI.ScalarAffineFunction{Int},MOI.Interval{Int})] |
| 82 | + test_noc(model, MOI.ScalarAffineFunction{Int}, MOI.GreaterThan{Int}, 1) |
| 83 | + test_noc(model, MOI.ScalarAffineFunction{Int}, MOI.Interval{Int}, 1) |
| 84 | + @test (@inferred MOI.get(model, MOI.ListOfConstraintIndices{MOI.ScalarAffineFunction{Int},MOI.Interval{Int}}())) == [c1] |
| 85 | + @test (@inferred MOI.get(model, MOI.ListOfConstraintIndices{MOI.ScalarAffineFunction{Int},MOI.GreaterThan{Int}}())) == [c2] |
| 86 | + |
| 87 | + @test MOI.is_valid(model, c2) |
| 88 | + MOI.delete(model, c2) |
| 89 | + |
| 90 | + @test MOI.get(model, MOI.ListOfConstraints()) == [(MOI.ScalarAffineFunction{Int},MOI.Interval{Int})] |
| 91 | + test_noc(model, MOI.ScalarAffineFunction{Int}, MOI.GreaterThan{Int}, 0) |
| 92 | + test_noc(model, MOI.ScalarAffineFunction{Int}, MOI.Interval{Int}, 1) |
| 93 | + @test (@inferred MOI.get(model, MOI.ListOfConstraintIndices{MOI.ScalarAffineFunction{Int},MOI.Interval{Int}}())) == [c1] |
| 94 | +end |
| 95 | + |
| 96 | +@testset "Continuous Linear" begin |
| 97 | + exclude = ["partial_start"] # VariablePrimalStart not supported. |
| 98 | + MOIT.contlineartest(bridged_mock, MOIT.TestConfig(solve=false), exclude) |
| 99 | +end |
0 commit comments