Christoph Neumann
Christoph Neumann

Reputation: 115

Set an upper bound of a pyomo constraint

Suppose I have a pyomo constraint con1 of the form:

    from pyomo.environ import *
    model = ConcreteModel()
    model.x_1 = Var()
    model.x_2 = Var()
    model.con1 = Constraint(expr=-exp(model.x_1) -  model.x_2 <= 0)

I want to manipulate the upper bound (in general the right hand side) of model.c1, i.e. subtract some value c.

I can query the upper (lower) bound by using the .upper() (.lower())-method and I found that

    model.con1.set_value(model.con1.body <= model.con1.upper()-c)

seems to work for <=-restrictions and that

    model.con1.set_value(model.con1.body >= model.con1.lower()-c)

seems to work for >=-restrictions.

Does my approach have potential side-effects (which I am currently unaware of)? Is there a more elegant way of manipulating the right hand side of a constraint?

Upvotes: 0

Views: 2111

Answers (1)

Bethany Nicholson
Bethany Nicholson

Reputation: 2818

Another way to do this is to use a mutable parameter:

from pyomo.environ import *
m = ConcreteModel()
m.p_ub = Param(initialize=10, mutable=True)
m.x1 = Var()
m.con1 = Constraint(expr=m.x1 <= m.p_ub)

Then if you want to change the value of the upper bound you can do something like:

m.p_ub = value(m.p_ub)-c

Just remember that you cannot use a Pyomo Variable as an upper or lower bound, they must be numeric constants.

Upvotes: 2

Related Questions