update a constraint with Pulp

Viewed 1599

Suppose to have a linear program and a constraint of the form:

4 x_1 + 3 x_2 ≤ 10

and that you want to update it to

4 x_1 + 3 x_2 + 10 x_3 ≤ 10

or to

3 x_2 ≤ 10

In order to do that, I "rewrite" the constraint from scratch, like

prob.constraints[0] = ...

but for a very long constraint this is very inefficient.

Is there a simpler way to add or remove variables from the constraints?

1 Answers

You can add new terms to your constraints by:

prob.constraints[0].addterm(x_3, 10)

Similarly you can remove terms by

prob.constraints[0].pop(x_1)

This accomplishes the two examples you listed.

Related