Skip to content

Commit

Permalink
Update IntervalArithmetic syntax (#138)
Browse files Browse the repository at this point in the history
This enables us to upgrade the version to `0.21`.
  • Loading branch information
vtjeng authored Sep 5, 2023
1 parent e1d08af commit 7255ab3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ DataFrames = "0.21, 0.22, 1"
DelimitedFiles = "1"
DocStringExtensions = "0.8, 0.9"
HiGHS = "1.4"
IntervalArithmetic = "0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.20"
IntervalArithmetic = "0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.20, 0.21"
JuMP = "0.21.4, 0.21.5, 0.22, 0.23, 1"
MAT = "0.5, 0.6, 0.7, 0.8, 0.9, 0.10"
MathOptInterface = "0.9, 0.10, 1"
Expand Down
19 changes: 11 additions & 8 deletions src/vendor/ConditionalJuMP.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using JuMP
using IntervalArithmetic: Interval
using IntervalArithmetic

# We vendor ConditionalJuMP (https://github.com/rdeits/ConditionalJuMP.jl/blob/e0c406077c0b07be76e02f72c3a7a7aa650df82f/src/ConditionalJuMP.jl)
# so that we can use JuMP >= 0.2.0
Expand All @@ -23,12 +23,15 @@ function owner_model(
return nothing
end

interval(x::Number) = Interval(x, x)
interval(x::JuMP.VariableRef) = Interval(lower_bound(x), upper_bound(x))
function interval(e::JuMP.GenericAffExpr)
result = Interval(e.constant, e.constant)
# Supplements constructors in https://github.com/JuliaIntervals/IntervalArithmetic.jl/blob/master/src/intervals/construction.jl.
IntervalArithmetic.interval(x::JuMP.VariableRef) =
IntervalArithmetic.interval(lower_bound(x), upper_bound(x))
function IntervalArithmetic.interval(e::JuMP.GenericAffExpr)
result = IntervalArithmetic.interval(e.constant)
for (var, coef) in e.terms
result += Interval(coef, coef) * Interval(lower_bound(var), upper_bound(var))
result +=
IntervalArithmetic.interval(coef) *
IntervalArithmetic.interval(lower_bound(var), upper_bound(var))
end
return result
end
Expand All @@ -37,7 +40,7 @@ lower_bound(x::Number) = x
upper_bound(x::Number) = x
lower_bound(x::JuMP.VariableRef) = JuMP.lower_bound(x)
upper_bound(x::JuMP.VariableRef) = JuMP.upper_bound(x)
lower_bound(e::JuMP.GenericAffExpr) = lower_bound(interval(e))
upper_bound(e::JuMP.GenericAffExpr) = upper_bound(interval(e))
lower_bound(e::JuMP.GenericAffExpr) = lower_bound(IntervalArithmetic.interval(e))
upper_bound(e::JuMP.GenericAffExpr) = upper_bound(IntervalArithmetic.interval(e))
lower_bound(i::Interval) = i.lo
upper_bound(i::Interval) = i.hi
11 changes: 6 additions & 5 deletions test/vendor/ConditionalJuMP.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using Test

using JuMP
using MIPVerify: owner_model, interval, lower_bound, upper_bound
using IntervalArithmetic
using MIPVerify: owner_model, lower_bound, upper_bound
@isdefined(TestHelpers) || include("../TestHelpers.jl")

TestHelpers.@timed_testset "ConditionalJuMP.jl" begin
Expand All @@ -23,7 +24,7 @@ TestHelpers.@timed_testset "ConditionalJuMP.jl" begin
e = 2 * x + 1

@testset "Number" begin
i = interval(5.0)
i = IntervalArithmetic.interval(5.0)
@test lower_bound(i) == 5.0
@test upper_bound(i) == 5.0
end
Expand All @@ -32,7 +33,7 @@ TestHelpers.@timed_testset "ConditionalJuMP.jl" begin
@test lower_bound(x) == 1
@test upper_bound(x) == 3

i = interval(x)
i = IntervalArithmetic.interval(x)
@test lower_bound(i) == 1
@test upper_bound(i) == 3
end
Expand All @@ -50,13 +51,13 @@ TestHelpers.@timed_testset "ConditionalJuMP.jl" begin
@test lower_bound(AffExpr(2)) == 2
@test upper_bound(AffExpr(2)) == 2

i = interval(e)
i = IntervalArithmetic.interval(e)
@test lower_bound(i) == lower_bound(e)
@test upper_bound(i) == upper_bound(e)
end

@testset "Interval" begin
i = interval(5.0)
i = IntervalArithmetic.interval(5.0)
@test lower_bound(i) == 5.0
@test upper_bound(i) == 5.0
end
Expand Down

0 comments on commit 7255ab3

Please sign in to comment.