Skip to content

Commit c308eb2

Browse files
committed
Better default name for indicator cons
1 parent c40df4b commit c308eb2

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- Added stage checks to presolve, freereoptsolve, freetransform
77
- Added primal_dual_evolution recipe and a plot recipe
88
### Fixed
9+
- Fixed default name for indicator constraints
910
### Changed
1011
### Removed
1112

src/pyscipopt/scip.pxi

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5330,7 +5330,7 @@ cdef class Model:
53305330

53315331
return pyCons
53325332

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

5384+
if name == '':
5385+
name = 'c'+str(SCIPgetNConss(self._scip)+1)
5386+
53845387
if cons.expr.degree() > 1:
53855388
raise ValueError("expected linear inequality, expression has degree %d" % cons.expr.degree())
53865389

tests/test_cons.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,20 +101,33 @@ def test_SOScons():
101101

102102
def test_cons_indicator():
103103
m = Model()
104-
x = m.addVar(lb=0)
104+
x = m.addVar(lb=0, obj=1)
105105
binvar = m.addVar(vtype="B", lb=1)
106106

107-
c = m.addConsIndicator(x >= 1, binvar)
107+
c1 = m.addConsIndicator(x >= 1, binvar)
108108

109-
slack = m.getSlackVarIndicator(c)
109+
assert c1.name == "c1"
110+
111+
c2 = m.addCons(x <= 3)
112+
113+
c3 = m.addConsIndicator(x >= 0, binvar)
114+
assert c3.name == "c4"
115+
116+
# because addConsIndicator actually adds two constraints
117+
assert m.getNConss() == 5
118+
119+
slack = m.getSlackVarIndicator(c1)
110120

111121
m.optimize()
112122

123+
assert m.getNConss() == 5
113124
assert m.isEQ(m.getVal(slack), 0)
114125
assert m.isEQ(m.getVal(binvar), 1)
115126
assert m.isEQ(m.getVal(x), 1)
116-
assert c.getConshdlrName() == "indicator"
127+
assert c1.getConshdlrName() == "indicator"
128+
117129

130+
test_cons_indicator()
118131

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

0 commit comments

Comments
 (0)