|
| 1 | +# Copyright 2017, Iain Dunning, Joey Huchette, Miles Lubin, and contributors |
| 2 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | +# file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 5 | + |
| 6 | +module TestFeasibilityChecker |
| 7 | + |
| 8 | +using JuMP |
| 9 | +using Test |
| 10 | + |
| 11 | +function test_distance_to_set() |
| 12 | + @test JuMP._distance_to_set(1.0, MOI.LessThan(2.0)) ≈ 0.0 |
| 13 | + @test JuMP._distance_to_set(1.0, MOI.LessThan(0.5)) ≈ 0.5 |
| 14 | + @test JuMP._distance_to_set(1.0, MOI.GreaterThan(2.0)) ≈ 1.0 |
| 15 | + @test JuMP._distance_to_set(1.0, MOI.GreaterThan(0.5)) ≈ 0.0 |
| 16 | + @test JuMP._distance_to_set(1.0, MOI.EqualTo(2.0)) ≈ 1.0 |
| 17 | + @test JuMP._distance_to_set(1.0, MOI.EqualTo(0.5)) ≈ 0.5 |
| 18 | + @test JuMP._distance_to_set(1.0, MOI.Interval(1.0, 2.0)) ≈ 0.0 |
| 19 | + @test JuMP._distance_to_set(0.5, MOI.Interval(1.0, 2.0)) ≈ 0.5 |
| 20 | + @test JuMP._distance_to_set(2.75, MOI.Interval(1.0, 2.0)) ≈ 0.75 |
| 21 | + @test JuMP._distance_to_set(0.6, MOI.ZeroOne()) ≈ 0.4 |
| 22 | + @test JuMP._distance_to_set(-0.01, MOI.ZeroOne()) ≈ 0.01 |
| 23 | + @test JuMP._distance_to_set(1.01, MOI.ZeroOne()) ≈ 0.01 |
| 24 | + @test JuMP._distance_to_set(0.6, MOI.Integer()) ≈ 0.4 |
| 25 | + @test JuMP._distance_to_set(3.1, MOI.Integer()) ≈ 0.1 |
| 26 | + @test JuMP._distance_to_set(-0.01, MOI.Integer()) ≈ 0.01 |
| 27 | + @test JuMP._distance_to_set(1.01, MOI.Integer()) ≈ 0.01 |
| 28 | +end |
| 29 | + |
| 30 | +function test_feasible() |
| 31 | + model = Model() |
| 32 | + @variable(model, x, Bin) |
| 33 | + @variable(model, 0 <= y <= 2, Int) |
| 34 | + @variable(model, z == 0.5) |
| 35 | + @constraint(model, x + y + z >= 0.5) |
| 36 | + @test primal_feasibility_report(model, Dict(z => 0.5)) === nothing |
| 37 | +end |
| 38 | + |
| 39 | +function test_bounds() |
| 40 | + model = Model() |
| 41 | + @variable(model, x, Bin) |
| 42 | + @variable(model, 0 <= y <= 2, Int) |
| 43 | + @variable(model, z == 0.5) |
| 44 | + report = primal_feasibility_report(model, Dict(x => 0.1, y => 2.1)) |
| 45 | + @test report[BinaryRef(x)] ≈ 0.1 |
| 46 | + @test report[UpperBoundRef(y)] ≈ 0.1 |
| 47 | + @test report[IntegerRef(y)] ≈ 0.1 |
| 48 | + @test report[FixRef(z)] ≈ 0.5 |
| 49 | + @test length(report) == 4 |
| 50 | +end |
| 51 | + |
| 52 | +function test_affine() |
| 53 | + model = Model() |
| 54 | + @variable(model, x) |
| 55 | + @constraint(model, c1, x <= 0.5) |
| 56 | + @constraint(model, c2, x >= 1.25) |
| 57 | + @constraint(model, c3, x == 1.1) |
| 58 | + @constraint(model, c4, 0 <= x <= 0.5) |
| 59 | + report = primal_feasibility_report(model, Dict(x => 1.0)) |
| 60 | + @test report[c1] ≈ 0.5 |
| 61 | + @test report[c2] ≈ 0.25 |
| 62 | + @test report[c3] ≈ 0.1 |
| 63 | + @test report[c4] ≈ 0.5 |
| 64 | + @test length(report) == 4 |
| 65 | +end |
| 66 | + |
| 67 | +function runtests() |
| 68 | + for name in names(@__MODULE__; all = true) |
| 69 | + if !startswith("$(name)", "test_") |
| 70 | + continue |
| 71 | + end |
| 72 | + @testset "$(name)" begin |
| 73 | + getfield(@__MODULE__, name)() |
| 74 | + end |
| 75 | + end |
| 76 | +end |
| 77 | + |
| 78 | +end |
| 79 | + |
| 80 | +TestFeasibilityChecker.runtests() |
0 commit comments