Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat committed Aug 9, 2024
1 parent 9a3ee3a commit b962ed4
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 40 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[compat]
MathOptInterface = "~0.10"
julia = "^1"
MathOptInterface = "1"
julia = "1"
2 changes: 1 addition & 1 deletion src/Bridges/Constraint/AllDifferent/ad_to_neq.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function MOIBC.bridge_constraint(
Tuple{Int, Int},
MOI.ConstraintIndex{MOI.ScalarAffineFunction{T}, CP.DifferentFrom{T}},
}()
sizehint!(cons, dim * (dim - 1) / 2)
sizehint!(cons, div(dim * (dim - 1), 2))

for i in 1:dim
for j in (i+1):dim
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function MOIBC.bridge_constraint(
# Upper-triangular matrix of constraints: i >= j, i.e. d(d-1)/2 elements:
# \sum_{i=2}^{d} (n - i + 1) = d (d - 1) / 2
cons = Dict{Tuple{Int, Int}, MOI.ConstraintIndex}()
sizehint!(cons, dim * (dim - 1) / 2)
sizehint!(cons, div(dim * (dim - 1), 2))

for i in 1:dim
for j in (i+1):dim
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ function MOIBC.bridge_constraint(
cons_different_reif = Dict{Tuple{Int, Int}, MOI.ConstraintIndex{MOI.VectorAffineFunction{T}, CP.Reification{CP.DifferentFrom{T}}}}()
cons = Dict{Tuple{Int, Int}, MOI.ConstraintIndex{MOI.ScalarAffineFunction{T}, MOI.GreaterThan{T}}}()

sizehint!(vars_different, s.dimension * (s.dimension - 1) / 2)
sizehint!(vars_different_bin, s.dimension * (s.dimension - 1) / 2)
sizehint!(cons_different_reif, s.dimension * (s.dimension - 1) / 2)
sizehint!(cons, s.dimension * (s.dimension - 1) / 2)
sizehint!(vars_different, div(s.dimension * (s.dimension - 1), 2))
sizehint!(vars_different_bin, div(s.dimension * (s.dimension - 1), 2))
sizehint!(cons_different_reif, div(s.dimension * (s.dimension - 1), 2))
sizehint!(cons, div(s.dimension * (s.dimension - 1), 2))

for i in 1:s.dimension
for j in (i+1):s.dimension
Expand Down
14 changes: 6 additions & 8 deletions src/Bridges/Constraint/DifferentFrom_Reify/reif_neq_to_milp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,14 @@ function MOIBC.bridge_constraint(
)

# If the constraint is satisfied, constrain the reified.
smallm = if T <: Int
one(T)
else
T(_REIF_EQTO_FLOAT_EPSILON)
end

con_smallm = MOI.add_constraint(
model,
f_scalars[1] - var_abs / smallm,
MOI.LessThan(zero(T))
if T <: Int
f_scalars[1] - var_abs
else
f_scalars[1] - var_abs / T(_REIF_EQTO_FLOAT_EPSILON)
end,
MOI.LessThan(zero(T)),
)

return ReificationDifferentFrom2MILPBridge{T}(var_abs, var_abs_int, con_abs, con_bigm, con_smallm)
Expand Down
14 changes: 6 additions & 8 deletions src/Bridges/Constraint/EqualTo_Reify/reif_eqto_to_milp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,14 @@ function MOIBC.bridge_constraint(
)

# If the constraint is satisfied, constrain the reified.
smallm = if T <: Int
one(T)
else
T(_REIF_EQTO_FLOAT_EPSILON)
end

con_smallm = MOI.add_constraint(
model,
f_scalars[1] - var_abs / smallm,
MOI.LessThan(zero(T))
if T <: Int
f_scalars[1] - var_abs
else
f_scalars[1] - var_abs / T(_REIF_EQTO_FLOAT_EPSILON)
end,
MOI.LessThan(zero(T)),
)

return ReificationEqualTo2MILPBridge{T}(var_abs, var_abs_int, con_abs, con_bigm, con_smallm)
Expand Down
1 change: 0 additions & 1 deletion src/ConstraintProgrammingExtensions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ include("sets_graph.jl") # Sets that work on graphs.
include("sets_reification.jl") # Sets that rely on the concept of reification (also called logical constraints).

include("Bridges/Bridges.jl")
include("DeprecatedTest/DeprecatedTest.jl")
include("Test/Test.jl")

include("FlatZinc/FlatZinc.jl")
Expand Down
4 changes: 2 additions & 2 deletions test/Bridges/Constraint/AllDifferent/ad_to_neq.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@
@test MOIB.added_constraint_types(typeof(bridge)) == [(MOI.ScalarAffineFunction{T}, CP.DifferentFrom{T})]

@test MOI.get(bridge, MOI.NumberOfVariables()) == 0
@test MOI.get(bridge, MOI.NumberOfConstraints{MOI.ScalarAffineFunction{T}, CP.DifferentFrom{T}}()) == dim * (dim - 1) / 2
@test MOI.get(bridge, MOI.NumberOfConstraints{MOI.ScalarAffineFunction{T}, CP.DifferentFrom{T}}()) == div(dim * (dim - 1), 2)

@test Set(MOI.get(bridge, MOI.ListOfConstraintIndices{MOI.ScalarAffineFunction{T}, CP.DifferentFrom{T}}())) == Set(collect(values(bridge.cons)))
end

@testset "Set of constraints" begin
@test length(bridge.cons) == dim * (dim - 1) / 2
@test length(bridge.cons) == div(dim * (dim - 1), 2)
for i in 1:dim
for j in (i+1):dim
@test MOI.is_valid(model, bridge.cons[i, j])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@
@test MOIB.added_constraint_types(typeof(bridge)) == [(MOI.VectorAffineFunction{T}, CP.Disjunction)]

@test MOI.get(bridge, MOI.NumberOfVariables()) == 0
@test MOI.get(bridge, MOI.NumberOfConstraints{MOI.VectorAffineFunction{T}, CP.Disjunction}()) == dim * (dim - 1) / 2
@test MOI.get(bridge, MOI.NumberOfConstraints{MOI.VectorAffineFunction{T}, CP.Disjunction}()) == div(dim * (dim - 1), 2)

@test Set(MOI.get(bridge, MOI.ListOfConstraintIndices{MOI.VectorAffineFunction{T}, CP.Disjunction}())) == Set(collect(values(bridge.cons)))
end

@testset "Set of constraints" begin
@test length(bridge.cons) == dim * (dim - 1) / 2
@test length(bridge.cons) == div(dim * (dim - 1), 2)
for i in 1:dim
for j in (i+1):dim
@test MOI.is_valid(model, bridge.cons[i, j])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@
(MOI.ScalarAffineFunction{T}, MOI.GreaterThan{T}),
]

@test MOI.get(bridge, MOI.NumberOfVariables()) == dim * length(values_set) + dim * (dim - 1) / 2
@test MOI.get(bridge, MOI.NumberOfConstraints{MOI.VariableIndex, MOI.ZeroOne}()) == dim * length(values_set) + dim * (dim - 1) / 2
@test MOI.get(bridge, MOI.NumberOfVariables()) == dim * length(values_set) + div(dim * (dim - 1), 2)
@test MOI.get(bridge, MOI.NumberOfConstraints{MOI.VariableIndex, MOI.ZeroOne}()) == dim * length(values_set) + div(dim * (dim - 1), 2)
@test MOI.get(bridge, MOI.NumberOfConstraints{MOI.VectorAffineFunction{T}, CP.Reification{MOI.EqualTo{T}}}()) == dim * length(values_set)
@test MOI.get(bridge, MOI.NumberOfConstraints{MOI.ScalarAffineFunction{T}, CP.Reification{CP.DifferentFrom{T}}}()) == dim * (dim - 1) / 2
@test MOI.get(bridge, MOI.NumberOfConstraints{MOI.ScalarAffineFunction{T}, MOI.GreaterThan{T}}()) == dim * (dim - 1) / 2
@test MOI.get(bridge, MOI.NumberOfConstraints{MOI.ScalarAffineFunction{T}, CP.Reification{CP.DifferentFrom{T}}}()) == div(dim * (dim - 1), 2)
@test MOI.get(bridge, MOI.NumberOfConstraints{MOI.ScalarAffineFunction{T}, MOI.GreaterThan{T}}()) == div(dim * (dim - 1), 2)

@test Set(MOI.get(bridge, MOI.ListOfVariableIndices())) == Set([vec(collect(values(bridge.vars_compare)))..., vec(collect(values(bridge.vars_different)))...])
@test Set(MOI.get(bridge, MOI.ListOfConstraintIndices{MOI.VariableIndex, MOI.ZeroOne}())) == Set([vec(collect(values(bridge.vars_compare_bin)))..., vec(collect(values(bridge.vars_different_bin)))...])
Expand All @@ -81,8 +81,8 @@
end
end

@test length(bridge.vars_different) == dim * (dim - 1) / 2
@test length(bridge.vars_different_bin) == dim * (dim - 1) / 2
@test length(bridge.vars_different) == div(dim * (dim - 1), 2)
@test length(bridge.vars_different_bin) == div(dim * (dim - 1), 2)

for i in 1:dim
for j in 1:i
Expand Down Expand Up @@ -121,7 +121,7 @@
end

@testset "Compare array variables" begin
@test length(bridge.cons_different_reif) == dim * (dim - 1) / 2
@test length(bridge.cons_different_reif) == div(dim * (dim - 1), 2)

for i in 1:dim
for j in (i+1):dim
Expand Down Expand Up @@ -149,7 +149,7 @@
end

@testset "Disjunction" begin
@test length(bridge.cons) == dim * (dim - 1) / 2
@test length(bridge.cons) == div(dim * (dim - 1), 2)

for i in 1:dim
for j in (i+1):dim
Expand Down
3 changes: 0 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ using Test

const CP = ConstraintProgrammingExtensions
const COIT = CP.Test
const COIDT = CP.DeprecatedTest
const COIB = CP.Bridges
const MOI = MathOptInterface
const MOIB = MOI.Bridges
const MOIBC = MOIB.Constraint
const MOIT = MOI.DeprecatedTest
const MOIU = MOI.Utilities

include("Bridges/models.jl")
Expand All @@ -19,6 +17,5 @@ include("Bridges/models.jl")
include("moi_traits.jl")
include("Bridges/Bridges.jl")
include("FlatZinc/FlatZinc.jl")
include("DeprecatedTest/DeprecatedTest.jl")
include("Test/Test.jl")
end

0 comments on commit b962ed4

Please sign in to comment.