0

I have a simple example of Pyomo model as below:

import pyomo.environ as pyo
import pandas as pd

df = pd.DataFrame({'x': [0, 1, 2]})

solver = pyo.SolverFactory('gams')
solver.options['solver'] = 'SBB'
model = pyo.ConcreteModel()


model.x = pyo.Var(df.index.tolist(), domain=pyo.Binary, initialize=1)
total_x = sum(model.x[i] for i in df.index.tolist())
model.constraint1 = pyo.Constraint(total_x <= 3)

# Solve:
solver.solve(model)

While accessing model.constraint1:

constraint1 : Size=1
    Key  : Lower : Body               : Upper
    None :  None : x[0] + x[1] + x[2] :   3.0

Due to some infeasibility of the actual objective (which I didn't include here), if that happens, I would like to change the lower or upper bound of this particular existing constraint to make it to 3.1, 3.2 3.3...

Is there any way I can change the bounds of existing constraint?

Ang Yiwei
  • 91
  • 1
  • 1
  • 12

1 Answers1

1

Because Pyomo places requirements on the lower and upper bounds of a constraint expression (namely that they be "non potentially variable" expressions), you cannot directly set the LHS/RHS of the constraint expression. The easiest thing to do is to just reassign the expression, e.g.:

model.constraint1.set_value(model.constraint1.body <= 3.1)

Alternatively, you can make use of a mutable Param to leave a placeholder in the expression tree that can be changed without having to re-process the expression:

model.con1_rhs = Param(mutable=True, initialize=3)
total_x = sum(model.x[i] for i in df.index.tolist())
model.constraint1 = pyo.Constraint(total_x <= model.con1_rhs)

# then to change the RHS,
model.con1_rhs = 3.1
# or
model.con1.rhs.set_value(3.1)
jsiirola
  • 2,259
  • 2
  • 9
  • 12