diff --git a/python/cuopt/cuopt/linear_programming/problem.py b/python/cuopt/cuopt/linear_programming/problem.py index 10600a543b..49885acf52 100644 --- a/python/cuopt/cuopt/linear_programming/problem.py +++ b/python/cuopt/cuopt/linear_programming/problem.py @@ -1726,7 +1726,7 @@ def addConstraint(self, constr, name=""): raise ValueError("addConstraint requires a Constraint object") return constr - def updateConstraint(self, constr, coeffs=[], rhs=None): + def updateConstraint(self, constr, coeffs=None, rhs=None): """ Updates a previously added constraint. Values that can be updated are constraint coefficients and RHS. @@ -1751,6 +1751,8 @@ def updateConstraint(self, constr, coeffs=[], rhs=None): >>> problem.updateConstraint(c1, coeffs=[(x, 1)], rhs=10) """ self.reset_solved_values() + if coeffs is None: + coeffs = [] if isinstance(constr, Constraint): if constr.is_quadratic: raise ValueError( @@ -1761,7 +1763,7 @@ def updateConstraint(self, constr, coeffs=[], rhs=None): for var, coeff in coeffs: idx = var.index constr.vindex_coeff_dict[idx] = coeff - if rhs: + if rhs is not None: constr.RHS = rhs else: raise ValueError("Object to update must be a Constraint") @@ -1840,7 +1842,7 @@ def setObjective(self, expr, sense=MINIMIZE): "Objective must be a Variable, Expression or a constant" ) - def updateObjective(self, coeffs=[], constant=None, sense=None): + def updateObjective(self, coeffs=None, constant=None, sense=None): """ Updates the objective of the problem. Values that can be updated are objective coefficients, constant and sense. @@ -1865,13 +1867,15 @@ def updateObjective(self, coeffs=[], constant=None, sense=None): sense=MINIMIZE) """ self.reset_solved_values() + if coeffs is None: + coeffs = [] if isinstance(coeffs, dict): coeffs = coeffs.items() for var, coeff in coeffs: var.setObjectiveCoefficient(coeff) - if constant: + if constant is not None: self.ObjConstant = constant - if sense: + if sense is not None: self.ObjSense = sense def getIncumbentValues(self, solution, vars): diff --git a/python/cuopt/cuopt/tests/linear_programming/test_python_API.py b/python/cuopt/cuopt/tests/linear_programming/test_python_API.py index 860b7aef2a..1b93c53528 100644 --- a/python/cuopt/cuopt/tests/linear_programming/test_python_API.py +++ b/python/cuopt/cuopt/tests/linear_programming/test_python_API.py @@ -566,6 +566,22 @@ def test_problem_update(): assert prob.ObjValue == pytest.approx(5) +def test_problem_update_accepts_zero_values(): + prob = Problem() + x = prob.addVariable(vtype=INTEGER, lb=0, name="x") + c1 = prob.addConstraint(x <= 5, name="c1") + + prob.updateConstraint(c1, rhs=0) + + assert c1.RHS == 0 + + prob.setObjective(x + 5) + + prob.updateObjective(constant=0) + + assert prob.ObjConstant == 0 + + def test_quadratic_expression_and_matrix(): problem = Problem() x = problem.addVariable(lb=9.0, vtype="I", name="x")