Skip to content

Commit

Permalink
Remove support for SingleVariable ConstraintName
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed May 18, 2021
1 parent bc9ad0e commit 97a80a5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 168 deletions.
2 changes: 1 addition & 1 deletion src/MOI_wrapper/MOI_copy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ function _add_all_variables(
# We started from empty model.variable_info, hence we assume ordering
index = CleverDicts.add_item(
model.variable_info,
VariableInfo(MOI.VariableIndex(i), i, bound, var_type[i]),
VariableInfo(MOI.VariableIndex(i), i, bound, var_type[i], ""),
)
glp_bound_type = get_glp_bound_type(lower[i], upper[i])
glp_set_col_bnds(model, i, glp_bound_type, lower[i], upper[i])
Expand Down
112 changes: 7 additions & 105 deletions src/MOI_wrapper/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,13 @@ mutable struct VariableInfo
bound::BoundEnum
type::TypeEnum
name::String
# Storage for constraint names associated with variables because GLPK
# can only store names for variables and proper constraints.
# We can perform an optimization and only store three strings for the
# constraint names because, at most, there can be three SingleVariable
# constraints, e.g., LessThan, GreaterThan, and Integer.
lessthan_name::String
greaterthan_interval_or_equalto_name::String
type_constraint_name::String
function VariableInfo(index::MOI.VariableIndex, column::Int)
return new(index, column, NONE, CONTINUOUS, "", "", "", "")
end
function VariableInfo(
index::MOI.VariableIndex,
column::Int,
bound::BoundEnum,
type::TypeEnum,
)
return new(index, column, bound, type, "", "", "", "")
end
end

function VariableInfo(index::MOI.VariableIndex, column::Int)
return VariableInfo(index, column, NONE, CONTINUOUS, "")
end


struct ConstraintKey
value::Int64
end
Expand Down Expand Up @@ -355,10 +341,11 @@ const _SCALAR_SETS = Union{
}

MOI.supports(::Optimizer, ::MOI.VariableName, ::Type{MOI.VariableIndex}) = true

function MOI.supports(
::Optimizer,
::MOI.ConstraintName,
::Type{<:MOI.ConstraintIndex},
::Type{<:MOI.ConstraintIndex{MOI.ScalarAffineFunction{Float64}}},
)
return true
end
Expand Down Expand Up @@ -907,7 +894,6 @@ function MOI.delete(
else
info.bound = NONE
end
info.lessthan_name = ""
model.name_to_constraint_index = nothing
return
end
Expand All @@ -920,7 +906,6 @@ function MOI.delete(
info = _info(model, c)
_set_variable_bound(model, info.column, -Inf, nothing)
info.bound = info.bound == LESS_AND_GREATER_THAN ? LESS_THAN : NONE
info.greaterthan_interval_or_equalto_name = ""
model.name_to_constraint_index = nothing
return
end
Expand All @@ -933,7 +918,6 @@ function MOI.delete(
info = _info(model, c)
_set_variable_bound(model, info.column, -Inf, Inf)
info.bound = NONE
info.greaterthan_interval_or_equalto_name = ""
model.name_to_constraint_index = nothing
return
end
Expand All @@ -946,7 +930,6 @@ function MOI.delete(
info = _info(model, c)
_set_variable_bound(model, info.column, -Inf, Inf)
info.bound = NONE
info.greaterthan_interval_or_equalto_name = ""
model.name_to_constraint_index = nothing
return
end
Expand Down Expand Up @@ -1077,46 +1060,6 @@ function MOI.get(
return MOI.Integer()
end

function MOI.get(
model::Optimizer,
::MOI.ConstraintName,
c::MOI.ConstraintIndex{MOI.SingleVariable,S},
) where {S}
MOI.throw_if_not_valid(model, c)
info = _info(model, c)
if S <: MOI.LessThan
return info.lessthan_name
elseif S <: Union{MOI.GreaterThan,MOI.Interval,MOI.EqualTo}
return info.greaterthan_interval_or_equalto_name
else
@assert S <: Union{MOI.ZeroOne,MOI.Integer}
return info.type_constraint_name
end
end

function MOI.set(
model::Optimizer,
::MOI.ConstraintName,
c::MOI.ConstraintIndex{MOI.SingleVariable,S},
name::String,
) where {S}
MOI.throw_if_not_valid(model, c)
info = _info(model, c)
if S <: MOI.LessThan
old_name = info.lessthan_name
info.lessthan_name = name
elseif S <: Union{MOI.GreaterThan,MOI.Interval,MOI.EqualTo}
old_name = info.greaterthan_interval_or_equalto_name
info.greaterthan_interval_or_equalto_name = name
else
@assert S <: Union{MOI.ZeroOne,MOI.Integer}
old_name = info.type_constraint_name
info.type_constraint_name = name
end
model.name_to_constraint_index = nothing
return
end

###
### ScalarAffineFunction-in-Set
###
Expand Down Expand Up @@ -1363,47 +1306,6 @@ function _rebuild_name_to_constraint_index(model::Optimizer)
),
)
end
for (key, info) in model.variable_info
if !isempty(info.lessthan_name)
_set_name_to_constraint_index(
model,
info.lessthan_name,
MOI.ConstraintIndex{MOI.SingleVariable,MOI.LessThan{Float64}}(
key.value,
),
)
end
if !isempty(info.greaterthan_interval_or_equalto_name)
S =
if info.bound == GREATER_THAN ||
info.bound == LESS_AND_GREATER_THAN
MOI.GreaterThan{Float64}
elseif info.bound == EQUAL_TO
MOI.EqualTo{Float64}
else
@assert info.bound == INTERVAL
MOI.Interval{Float64}
end
_set_name_to_constraint_index(
model,
info.greaterthan_interval_or_equalto_name,
MOI.ConstraintIndex{MOI.SingleVariable,S}(key.value),
)
end
if !isempty(info.type_constraint_name)
S = if info.type == BINARY
MOI.ZeroOne
else
@assert info.type == INTEGER
MOI.Integer
end
_set_name_to_constraint_index(
model,
info.type_constraint_name,
MOI.ConstraintIndex{MOI.SingleVariable,S}(key.value),
)
end
end
return
end

Expand Down
78 changes: 16 additions & 62 deletions test/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ const CONFIG = MOIT.TestConfig()
"""
variables: x
maxobjective: 2.0x
c1: x in ZeroOne()
c2: x >= 0.2
c3: x <= 0.5
x in ZeroOne()
x >= 0.2
x <= 0.5
""",
)
MOI.optimize!(OPTIMIZER)
Expand Down Expand Up @@ -189,8 +189,8 @@ end
"""
variables: x, y
minobjective: -5.0x + y
c1: x in Integer()
c2: x in LessThan(1.0)
x in Integer()
x in LessThan(1.0)
""",
)
MOI.optimize!(model)
Expand All @@ -204,8 +204,8 @@ end
"""
variables: x
minobjective: -5.0x
c1: x in Integer()
c2: x in LessThan(1.0)
x in Integer()
x in LessThan(1.0)
c3: 1.0x in GreaterThan(2.0)
""",
)
Expand Down Expand Up @@ -322,8 +322,8 @@ end
"""
variables: x
minobjective: 1.0x
c1: x in Integer()
c2: x in GreaterThan(1.5)
x in Integer()
x in GreaterThan(1.5)
""",
)
MOI.optimize!(model)
Expand All @@ -335,7 +335,7 @@ end
"""
variables: x
minobjective: 1.0x
c1: x in GreaterThan(1.5)
x in GreaterThan(1.5)
""",
)
MOI.optimize!(model)
Expand All @@ -357,27 +357,6 @@ end
MOI.set(model, MOI.VariableName(), x[1], "x1")
@test_throws ErrorException MOI.get(model, MOI.VariableIndex, "x1")
end

@testset "Variable bounds" begin
MOI.empty!(model)
x = MOI.add_variable(model)
c1 = MOI.add_constraint(
model,
MOI.SingleVariable(x),
MOI.GreaterThan(0.0),
)
c2 = MOI.add_constraint(model, MOI.SingleVariable(x), MOI.LessThan(1.0))
MOI.set(model, MOI.ConstraintName(), c1, "c1")
@test MOI.get(model, MOI.ConstraintIndex, "c1") == c1
MOI.set(model, MOI.ConstraintName(), c1, "c2")
@test MOI.get(model, MOI.ConstraintIndex, "c1") === nothing
@test MOI.get(model, MOI.ConstraintIndex, "c2") == c1
MOI.set(model, MOI.ConstraintName(), c2, "c1")
@test MOI.get(model, MOI.ConstraintIndex, "c1") == c2
MOI.set(model, MOI.ConstraintName(), c1, "c1")
@test_throws ErrorException MOI.get(model, MOI.ConstraintIndex, "c1")
end

@testset "Affine constraints" begin
MOI.empty!(model)
x = MOI.add_variable(model)
Expand Down Expand Up @@ -468,31 +447,6 @@ end
MOI.delete(model, x)
@test MOI.get(model, MOI.VariableIndex, "x") == z
end
@testset "SingleVariable" begin
model = GLPK.Optimizer()
x = MOI.add_variables(model, 3)
c = MOI.add_constraints(
model,
MOI.SingleVariable.(x),
MOI.GreaterThan(0.0),
)
MOI.set(model, MOI.ConstraintName(), c[1], "x")
MOI.set(model, MOI.ConstraintName(), c[2], "x")
MOI.set(model, MOI.ConstraintName(), c[3], "z")
@test MOI.get(model, MOI.ConstraintIndex, "z") == c[3]
@test_throws ErrorException MOI.get(model, MOI.ConstraintIndex, "x")
MOI.set(model, MOI.ConstraintName(), c[2], "y")
@test MOI.get(model, MOI.ConstraintIndex, "x") == c[1]
@test MOI.get(model, MOI.ConstraintIndex, "y") == c[2]
MOI.set(model, MOI.ConstraintName(), c[3], "x")
@test_throws ErrorException MOI.get(model, MOI.ConstraintIndex, "x")
MOI.delete(model, c[1])
@test MOI.get(model, MOI.ConstraintIndex, "x") == c[3]
MOI.set(model, MOI.ConstraintName(), c[2], "x")
@test_throws ErrorException MOI.get(model, MOI.ConstraintIndex, "x")
MOI.delete(model, x[3])
@test MOI.get(model, MOI.ConstraintIndex, "x") == c[2]
end
@testset "ScalarAffineFunction" begin
model = GLPK.Optimizer()
x = MOI.add_variables(model, 3)
Expand Down Expand Up @@ -579,12 +533,12 @@ end
"""
variables: a, b, c, d
minobjective: a + b + c + d
c1: a >= 1.0
c2: b <= 2.0
c3: c == 3.0
c4: d in Interval(-4.0, 4.0)
c5: a in Integer()
c6: b in ZeroOne()
a >= 1.0
b <= 2.0
c == 3.0
d in Interval(-4.0, 4.0)
a in Integer()
b in ZeroOne()
c7: a + b >= -1.1
c8: a + b <= 2.2
c8: c + d == 2.2
Expand Down

0 comments on commit 97a80a5

Please sign in to comment.