简体   繁体   中英

Eliminate equality constraints in a pyomo model

I want to eliminate linear equality constraints on integral variables in a pyomo model by substitution. For instance, I wish to transform the model

在此处输入图片说明

by substituting

在此处输入图片说明 ( * )

to

在此处输入图片说明

Is there a way to perfom such a substitution in a pyomo model? I will be able to obtain ( * ) by computing the solution space of the corresponding system of linear diophantine equations in the form y = const_vec + susbtitution_matrix * eta , where in our example we have

const_vec = np.array([1,0,0])
substitution_matrix = np.array([[-1,0],
                                [1,0],
                                [0,1]])

What you are describing is generally referred to as "variable aggregation." As you indicate, there are four basic steps:

  1. Identify the linear equality equations you want to remove
  2. Compute the substitution map
  3. Deactivate the equality constraints that you want to remove
  4. Substitute variables on all remaining constraints

It sounds like you have 1 and 2 under control. For 3, assuming you identified a Constraint mc you want to deactivate, you just need to call mcdeactivate() .

For 4, you will want to generate new expressions for the remaining Constraint " body " expressions (variables only appear in the body and not in the lower/upper bounds). For current Pyomo releases (through 5.4.x), you can perform variable substitution by leveraging the clone_expression() . You need to generate a "substitution map": a dict that maps the id() of the variables you want to the new expression you want to use. For example:

from pyomo.core.base.expr import clone_expression

m = ConcreteModel()
m.y = Var([1,2,3])
m.eta = Var([1,2])
# ...
m.c = Constraint(expr=m.y[1]**2 + m.y[3]**2 <= 4)
# ...

substitution_map = {
    id(m.y[1]): 1 - m.eta[1],
    id(m.y[2]): m.eta[1],
    id(m.y[3]): m.eta[2],
}
m.c = (m.c.lower, clone_expression(m.c.body, substitute=substitution_map), m.c.upper)

Finally, the disclaimers:

  1. Setting the constraint with this syntax should work with recent Pyomo releases (I tested back through 5.1)
  2. This approach technically violates one of the assumptions in the current Pyomo expression system (it generates potentially "entangled" expressions: expressions that share common sub-trees). While not "good", it shouldn't cause troubles, unless you do additional transformations / expression manipulation.
  3. Pyomo 5.5 will have a new expression system that will likely have a different mechanism for manipulating / substituting variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM