How to add variables to existing constraints?

622 views
Skip to first unread message

terenc...@gmail.com

unread,
May 14, 2017, 7:26:01 PM5/14/17
to Pyomo Forum
Does anyone know how to add new variables to an existing constraint?
For example, suppose I have the constraint x1 + x2 <=1 in an abstract model. I want to add another variable x3 to the instance and then add x3 the constraint. The constraint would become x1 + x2 + x3 <= 1. I believe we need to do a lot of operations like this in implementing algorithms like column generation. I have no idea if pyomo support this. 

Thanks a lot!

Gabriel Hackebeil

unread,
May 15, 2017, 12:03:12 PM5/15/17
to pyomo...@googlegroups.com
When you initialize a constraint with a relational expression, Pyomo will break it apart into a LB, UB, and body. Any expression that looks like it might contain variables will be moved into the body (it is not safe to assume that any constants will be moved to the bounds). The .body attribute on a constructed constraint will point to the body expression, so you can modify it by doing something like “constraint[i].body += c*v”.

However, be sure to watch out for cases where you define constraint expressions like “x <= y”, where both x and y are variables. It’s hard to know if the body will become “x - y” or “y - x”, and unless you are certain what happens, you might be adding variables to the body with the wrong sign.

You can also use the Expression component to accomplish this, but the caveat above still holds. Just make sure to write the constraint expressions in a way where it is obvious what the body will be (i.e., the body of “2 <= x - y” will never be stored as “y - x”).

Gabe

--
You received this message because you are subscribed to the Google Groups "Pyomo Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyomo-forum...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted
Message has been deleted

Gabriel Hackebeil

unread,
Oct 1, 2017, 5:32:27 PM10/1/17
to pyomo...@googlegroups.com
That looks like a bug. In the short term, use “._body += “. I’ll push a fix.

Gabe

On Sep 25, 2017, at 8:58 AM, Manny <manuel....@gmail.com> wrote:

Hi,

When trying to do this with a concrete model I receive the error message - AttributeError: can't set attribute. Below is the model I'm working with:

from pyomo.environ import *

model = ConcreteModel()

model.x = Var([1,2,3],within=NonNegativeReals)
model.y = Var([1,2],within=NonNegativeReals)

model.land = Constraint(expr = model.x[1]+model.x[2]+model.x[3]<=500)

model.land.body += model.y[1] 

If you could please advise on the correct form of implementation that would be greatly appreciated.

Thanks,

Manuel

Mehdi Golari

unread,
Jul 13, 2018, 4:55:40 PM7/13/18
to Pyomo Forum
Hi Gabe,

This bug seems to be still there. I thought I'll remind you as I just came across such a bug. By the way, I can set the body attribute after I solve the optimization model with .body, not sure what that happens. 

Thanks,
Mehdi
Reply all
Reply to author
Forward
0 new messages