Skip to content

Commit

Permalink
Better default name for indicator cons
Browse files Browse the repository at this point in the history
  • Loading branch information
Joao-Dionisio committed Nov 30, 2024
1 parent c40df4b commit c308eb2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Added stage checks to presolve, freereoptsolve, freetransform
- Added primal_dual_evolution recipe and a plot recipe
### Fixed
- Fixed default name for indicator constraints
### Changed
### Removed

Expand Down
7 changes: 5 additions & 2 deletions src/pyscipopt/scip.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -5330,7 +5330,7 @@ cdef class Model:

return pyCons

def addConsIndicator(self, cons, binvar=None, activeone=True, name="IndicatorCons",
def addConsIndicator(self, cons, binvar=None, activeone=True, name="",
initial=True, separate=True, enforce=True, check=True,
propagate=True, local=False, dynamic=False,
removable=False, stickingatnode=False):
Expand All @@ -5348,7 +5348,7 @@ cdef class Model:
activeone : bool, optional
constraint should active if binvar is 1 (0 if activeone = False)
name : str, optional
name of the constraint (Default value = "IndicatorCons")
name of the constraint (Default value = "")
initial : bool, optional
should the LP relaxation of constraint be in the initial LP? (Default value = True)
separate : bool, optional
Expand Down Expand Up @@ -5381,6 +5381,9 @@ cdef class Model:
if cons._lhs is not None and cons._rhs is not None:
raise ValueError("expected inequality that has either only a left or right hand side")

if name == '':
name = 'c'+str(SCIPgetNConss(self._scip)+1)

if cons.expr.degree() > 1:
raise ValueError("expected linear inequality, expression has degree %d" % cons.expr.degree())

Expand Down
21 changes: 17 additions & 4 deletions tests/test_cons.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,33 @@ def test_SOScons():

def test_cons_indicator():
m = Model()
x = m.addVar(lb=0)
x = m.addVar(lb=0, obj=1)
binvar = m.addVar(vtype="B", lb=1)

c = m.addConsIndicator(x >= 1, binvar)
c1 = m.addConsIndicator(x >= 1, binvar)

slack = m.getSlackVarIndicator(c)
assert c1.name == "c1"

c2 = m.addCons(x <= 3)

c3 = m.addConsIndicator(x >= 0, binvar)
assert c3.name == "c4"

# because addConsIndicator actually adds two constraints
assert m.getNConss() == 5

slack = m.getSlackVarIndicator(c1)

m.optimize()

assert m.getNConss() == 5
assert m.isEQ(m.getVal(slack), 0)
assert m.isEQ(m.getVal(binvar), 1)
assert m.isEQ(m.getVal(x), 1)
assert c.getConshdlrName() == "indicator"
assert c1.getConshdlrName() == "indicator"


test_cons_indicator()

@pytest.mark.xfail(
reason="addConsIndicator doesn't behave as expected when binary variable is False. See Issue #717."
Expand Down

0 comments on commit c308eb2

Please sign in to comment.