Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions python/cuopt/cuopt/linear_programming/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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(
Expand All @@ -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")
Expand Down Expand Up @@ -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.
Expand All @@ -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):
Expand Down
16 changes: 16 additions & 0 deletions python/cuopt/cuopt/tests/linear_programming/test_python_API.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down