Skip to content

cvxpy/mutate-problem-constraints

Pack Severity Fix Versions
CVXPY error none, edit by hand All versions

CVXPY's Problem.constraints returns a shallow copy of the list. Appending to that copy leaves the next solve unaware of the added constraint.

problem.constraints.append(x >= 1)
problem = cp.Problem(problem.objective, [*problem.constraints, x >= 1])

The rule checks standalone list mutators such as append and clear, and writes or deletions through an index or slice of problem.constraints. A list saved to a variable can be edited and passed to a new problem.

Assemble the constraints before constructing the problem, or build a new problem with the updated list. A new problem has its own compilation cache.

CVXPY guide to changing a problem

CVXPY Problem.constraints returns a list copy. Include the updated constraints when constructing a Problem.

See all rules in the CVXPY pack.