|
| 1 | +# Copyright (c) 2017: Guilherme Bodin, and contributors |
| 2 | +# |
| 3 | +# Use of this source code is governed by an MIT-style license that can be found |
| 4 | +# in the LICENSE.md file or at https://opensource.org/licenses/MIT. |
| 5 | + |
| 6 | +module TestAttributes |
| 7 | + |
| 8 | +using Test |
| 9 | + |
| 10 | +import MathOptInterface as MOI |
| 11 | +import MathOptInterface.Utilities as MOIU |
| 12 | +import Dualization |
| 13 | + |
| 14 | +function runtests() |
| 15 | + for name in names(@__MODULE__; all = true) |
| 16 | + if startswith("$(name)", "test_") |
| 17 | + @testset "$(name)" begin |
| 18 | + getfield(@__MODULE__, name)() |
| 19 | + end |
| 20 | + end |
| 21 | + end |
| 22 | + return |
| 23 | +end |
| 24 | + |
| 25 | +### |
| 26 | +### Helper structs |
| 27 | +### |
| 28 | + |
| 29 | +struct DummyModelAttribute <: MOI.AbstractModelAttribute end |
| 30 | + |
| 31 | +struct DummyVariableAttribute <: MOI.AbstractVariableAttribute end |
| 32 | + |
| 33 | +struct DummyConstraintAttribute <: MOI.AbstractConstraintAttribute end |
| 34 | + |
| 35 | +### |
| 36 | +### The tests |
| 37 | +### |
| 38 | + |
| 39 | +function test_constraint_attribute() |
| 40 | + T = Float64 |
| 41 | + mock = MOI.Utilities.MockOptimizer( |
| 42 | + MOI.Utilities.UniversalFallback(MOI.Utilities.Model{T}()); |
| 43 | + eval_variable_constraint_dual = false, |
| 44 | + ) |
| 45 | + dual = MOI.instantiate(() -> Dualization.DualOptimizer(mock), with_cache_type = T) |
| 46 | + x = MOI.add_variable(dual) |
| 47 | + set_constant = -4 |
| 48 | + set = MOI.GreaterThan(T(set_constant)) |
| 49 | + ci = MOI.add_constraint(dual, T(1) * x, set) |
| 50 | + MOI.set(dual, MOI.ObjectiveSense(), MOI.MIN_SENSE) |
| 51 | + obj = T(2) * x |
| 52 | + MOI.set(dual, MOI.ObjectiveFunction{typeof(obj)}(), obj) |
| 53 | + MOI.optimize!(dual) |
| 54 | + for attr in [MOI.ConstraintDualStart(), MOI.ConstraintPrimalStart()] |
| 55 | + attr = MOI.ConstraintDualStart() |
| 56 | + @test MOI.supports(dual, attr, typeof(ci)) |
| 57 | + value = rand(T) |
| 58 | + MOI.set(dual, attr, ci, value) |
| 59 | + @test MOI.get(dual, attr, ci) == value |
| 60 | + end |
| 61 | + |
| 62 | + value = rand(T) |
| 63 | + mock_vi = MOI.get(mock, MOI.ListOfVariableIndices())[] |
| 64 | + MOI.set(mock, MOI.VariablePrimal(), mock_vi, value) |
| 65 | + @test MOI.get(dual.optimizer, MOI.ConstraintDual(), ci) ≈ value |
| 66 | + |
| 67 | + value = rand(T) |
| 68 | + mock_ci = MOI.get(mock, MOI.ListOfConstraintIndices{MOI.VariableIndex,typeof(set)}())[] |
| 69 | + MOI.set(mock, MOI.ConstraintDual(), mock_ci, value) |
| 70 | + @test MOI.get(dual.optimizer, MOI.ConstraintPrimal(), ci) ≈ value + set_constant |
| 71 | + return |
| 72 | +end |
| 73 | + |
| 74 | +end # module |
| 75 | + |
| 76 | +TestAttributes.runtests() |
0 commit comments