11

Once a model is implemented in PuLP, how do you change a coefficient (e.g., $a_{ij}$, $b_i$ or $c_j$) of a program of the from $\min\{c^{\top}x: Ax=b, x\geq0\}$?

Specifically:

  1. How to update coefficients or RHSs?
  2. Once it is updated, do we have to tell PuLP to update (e.g. in Gurobi we would do model.update()) ?
  3. Does PuLP re-optimize or start from scratch?
Daniel Duque
  • 1,355
  • 6
  • 20

1 Answers1

10

If the model in PuLP is:

from pulp import LpProblem, LpVariable, LpMaximize, lpSum
m = LpProblem(name='example', sense = LpMaximize)
x = LpVariable.dicts(name='x',indexs=[1,2,3])
m += lpSum(x) <= 3, 'c1'
m += lpSum(i*x[i] for i in [1,2,3]), 'obj'

We can access the coefficient of $x_1$ in 'C1' with:

m.constraints['c1'][x[1]] # This the coefficient => 1

and further set it in a pythonic way:

m.constraints['c1'][x[1]] = 2 # Now the coefficient is 2

The same is true for the objective function with:

m.objective[x[1]] = 0 # objective coefficient of x_1 is zero

To chance the RHS, one way is to add or subtract the difference. If the new RHS is 4, we proceed as:

m.constrains['c1'][x[1]] += 1

A new call of m.solve() would be required, the value() function for all the objects won't change until the new call of solve().

Not sure about 3, but perhaps a PuLP developer knows.

Daniel Duque
  • 1,355
  • 6
  • 20
  • I do not understand the last part about how to change the RHS of the constraint, why is a variable given in the indices of the constraint? Can I use any variable present in the constraint? – Philippe Jun 15 '20 at 20:59