Skip to content

Commit 7b7daf5

Browse files
authored
Add support for ConstraintConflictStatus with SOS constraints (#603)
1 parent bd360c1 commit 7b7daf5

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

src/MOI_wrapper/MOI_wrapper.jl

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3970,7 +3970,7 @@ function _ensure_conflict_computed(model::Optimizer)
39703970
end
39713971

39723972
function _is_feasible(model::Optimizer)
3973-
return MOI.get(model, ConflictStatus()) == GRB_INFEASIBLE
3973+
return MOI.get(model, ConflictStatus()) == GRB_ERROR_IIS_NOT_INFEASIBLE
39743974
end
39753975

39763976
"""
@@ -4102,6 +4102,22 @@ function MOI.get(
41024102
return p[] > 0 ? MOI.IN_CONFLICT : MOI.NOT_IN_CONFLICT
41034103
end
41044104

4105+
function MOI.get(
4106+
model::Optimizer,
4107+
::MOI.ConstraintConflictStatus,
4108+
index::MOI.ConstraintIndex{MOI.VectorOfVariables,S},
4109+
) where {S<:Union{MOI.SOS1,MOI.SOS2}}
4110+
_ensure_conflict_computed(model)
4111+
if _is_feasible(model)
4112+
return MOI.NOT_IN_CONFLICT
4113+
end
4114+
p = Ref{Cint}(0)
4115+
row = Cint(_info(model, index).row - 1)
4116+
ret = GRBgetintattrelement(model, "IISSOS", row, p)
4117+
_check_ret(model, ret)
4118+
return p[] > 0 ? MOI.IN_CONFLICT : MOI.NOT_IN_CONFLICT
4119+
end
4120+
41054121
function MOI.get(
41064122
model::Optimizer,
41074123
::MOI.ConstraintConflictStatus,

test/MOI/MOI_wrapper.jl

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,6 +1527,52 @@ function test_Env()
15271527
return
15281528
end
15291529

1530+
function test_SOS1_no_conflict()
1531+
model = Gurobi.Optimizer(GRB_ENV)
1532+
MOI.set(model, MOI.Silent(), true)
1533+
x = MOI.add_variables(model, 2)
1534+
set = MOI.SOS1([1.0, 2.0])
1535+
c1 = MOI.add_constraint(model, MOI.VectorOfVariables(x), set)
1536+
MOI.optimize!(model)
1537+
MOI.compute_conflict!(model)
1538+
@test MOI.get(model, MOI.ConstraintConflictStatus(), c1) ==
1539+
MOI.NOT_IN_CONFLICT
1540+
return
1541+
end
1542+
1543+
function test_SOS1_in_conflict()
1544+
model = Gurobi.Optimizer(GRB_ENV)
1545+
MOI.set(model, MOI.Silent(), true)
1546+
x = MOI.add_variables(model, 2)
1547+
MOI.add_constraint.(model, x, MOI.GreaterThan(0.0))
1548+
MOI.add_constraint.(model, x, MOI.LessThan(1.0))
1549+
set = MOI.SOS1([1.0, 2.0])
1550+
c1 = MOI.add_constraint(model, MOI.VectorOfVariables(x), set)
1551+
c2 = MOI.add_constraint(model, 1.0 * x[1] + 1.0 * x[2], MOI.EqualTo(2.0))
1552+
MOI.optimize!(model)
1553+
MOI.compute_conflict!(model)
1554+
@test MOI.get(model, MOI.ConstraintConflictStatus(), c1) == MOI.IN_CONFLICT
1555+
@test MOI.get(model, MOI.ConstraintConflictStatus(), c2) == MOI.IN_CONFLICT
1556+
return
1557+
end
1558+
1559+
function test_SOS2_not_in_conflict()
1560+
model = Gurobi.Optimizer(GRB_ENV)
1561+
MOI.set(model, MOI.Silent(), true)
1562+
x = MOI.add_variables(model, 3)
1563+
MOI.add_constraint.(model, x, MOI.GreaterThan(0.0))
1564+
set = MOI.SOS2([1.0, 2.0, 3.0])
1565+
c1 = MOI.add_constraint(model, MOI.VectorOfVariables(x), set)
1566+
f = sum(1.0 * x[i] for i in 1:3)
1567+
c2 = MOI.add_constraint(model, f, MOI.EqualTo(-1.0))
1568+
MOI.optimize!(model)
1569+
MOI.compute_conflict!(model)
1570+
@test MOI.get(model, MOI.ConstraintConflictStatus(), c1) ==
1571+
MOI.NOT_IN_CONFLICT
1572+
@test MOI.get(model, MOI.ConstraintConflictStatus(), c2) == MOI.IN_CONFLICT
1573+
return
1574+
end
1575+
15301576
end # TestMOIWrapper
15311577

15321578
TestMOIWrapper.runtests()

0 commit comments

Comments
 (0)