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:
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:
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.