diff --git a/CHANGELOG.md b/CHANGELOG.md index aeab43ace..36feae611 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/pyscipopt/scip.pxi b/src/pyscipopt/scip.pxi index bf575ba65..6a5513cd1 100644 --- a/src/pyscipopt/scip.pxi +++ b/src/pyscipopt/scip.pxi @@ -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): @@ -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 @@ -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()) diff --git a/tests/test_cons.py b/tests/test_cons.py index 8b11250ae..42801bd93 100644 --- a/tests/test_cons.py +++ b/tests/test_cons.py @@ -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."