Skip to content

Commit e7bf03c

Browse files
authored
Merge pull request #930 from scipopt/generic-indicator-names
Generic indicator names
2 parents d05b049 + 6cc9ccc commit e7bf03c

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
- Added stage checks to presolve, freereoptsolve, freetransform
88
- Added primal_dual_evolution recipe and a plot recipe
99
### Fixed
10-
- Only redirect stdout and stderr in redirectOutput() so that file output still works afterwards
10+
- Added default names to indicator constraints
1111
### Changed
1212
- GitHub actions using Mac now use precompiled SCIP from latest release
1313
### Removed

src/pyscipopt/scip.pxi

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

53405340
return pyCons
53415341

5342-
def addConsIndicator(self, cons, binvar=None, activeone=True, name="IndicatorCons",
5342+
def addConsIndicator(self, cons, binvar=None, activeone=True, name="",
53435343
initial=True, separate=True, enforce=True, check=True,
53445344
propagate=True, local=False, dynamic=False,
53455345
removable=False, stickingatnode=False):
@@ -5357,7 +5357,7 @@ cdef class Model:
53575357
activeone : bool, optional
53585358
constraint should active if binvar is 1 (0 if activeone = False)
53595359
name : str, optional
5360-
name of the constraint (Default value = "IndicatorCons")
5360+
name of the constraint (Default value = "")
53615361
initial : bool, optional
53625362
should the LP relaxation of constraint be in the initial LP? (Default value = True)
53635363
separate : bool, optional
@@ -5390,6 +5390,9 @@ cdef class Model:
53905390
if cons._lhs is not None and cons._rhs is not None:
53915391
raise ValueError("expected inequality that has either only a left or right hand side")
53925392

5393+
if name == '':
5394+
name = 'c'+str(SCIPgetNConss(self._scip)+1)
5395+
53935396
if cons.expr.degree() > 1:
53945397
raise ValueError("expected linear inequality, expression has degree %d" % cons.expr.degree())
53955398

tests/test_cons.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,20 +101,30 @@ 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(transformed=False) == 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"
117-
127+
assert c1.getConshdlrName() == "indicator"
118128

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

0 commit comments

Comments
 (0)