Skip to content

Commit

Permalink
Merge pull request #930 from scipopt/generic-indicator-names
Browse files Browse the repository at this point in the history
Generic indicator names
  • Loading branch information
Opt-Mucca authored Dec 3, 2024
2 parents d05b049 + 6cc9ccc commit e7bf03c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
- Added stage checks to presolve, freereoptsolve, freetransform
- Added primal_dual_evolution recipe and a plot recipe
### Fixed
- Only redirect stdout and stderr in redirectOutput() so that file output still works afterwards
- Added default names to indicator constraints
### Changed
- GitHub actions using Mac now use precompiled SCIP from latest release
### 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 @@ -5339,7 +5339,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 @@ -5357,7 +5357,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 @@ -5390,6 +5390,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
20 changes: 15 additions & 5 deletions tests/test_cons.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,30 @@ 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(transformed=False) == 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"

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

0 comments on commit e7bf03c

Please sign in to comment.