访问在pyomo约束中出现的所有变量 [英] Access all variables occurring in a pyomo constraint

查看:349
本文介绍了访问在pyomo约束中出现的所有变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python算法,该算法需要修改具体的(混合整数非线性)pyomo模型. 特别是,我需要知道一般代数约束中存在哪些变量. 例如.为了约束

I am working on an algorithm in python that needs to modify concrete (mixed-integer nonlinear) pyomo models. In particular, I need to know which variables are present in a general algebraic constraint. E.g. for a constraint

model.con1 = Constraint(expr=exp(model.x_1) + 2*model.x_2 <= 2)

我想进行一个查询(如model.con1.variables),该查询返回变量([model.x_1,model.x_2])(的列表).

I would like to make a a query (like model.con1.variables ) which returns (a list of) the variables ([model.x_1,model.x_2]).

此文档中,我发现 linear 约束,参数variables完全符合我的目的.但是,我正在使用的模型还将包含一般的代数约束.

In this documentation I find that for linear constraints, the parameter variables exactly serves my purpose. However, the models I'm working with will also contain general algebraic constraints.

本书的第14章提供了一些有关高级开发的详细信息使用pyomo的算法,但是我在那里找不到我的问题的答案.我能想到的唯一非常笨拙的方法是使用表达式的to_string()方法(在我们的示例中返回exp( x_1 ) + x_2 <= 1.0),然后在该字符串中搜索所有变量的出现.我敢肯定,使用pyomo可以更好地访问约束中出现的变量.

Chapter 14 of this book provides several details for the development of high-level algorithms using pyomo, but I did not find an answer to my question there. The only very clumsy approach I can think of is to use the to_string() method of the expression (returning exp( x_1 ) + x_2 <= 1.0 in our example), and then to search this string for the appearence of all variables. I'm sure there is a much better way using pyomo to access the variables occurring in a constraint.

请注意,已经提出了一个类似但不太详细且未回答的问题

Note that a similar, but less detailed and unanswered question has already been asked here.

推荐答案

您不想直接查询model.con1.body返回的表达式的_args属性.以下划线开头的方法和属性被认为是 private ,一般用户不应使用(它们没有文档记录,可以随时更改,恕不另行通知或弃用警告).其次,_args属性仅返回表达式树中该节点的子级.对于线性表达式,这些变量很有可能是变量,但不能保证.对于非线性表达式(和通用表达式),_args的成员几乎可以保证是其他表达式对象.

You do not want to directly query the _args attribute of the expression returned by model.con1.body. Methods and attributes beginning with an underscore are considered private and should not be used by general users (they are undocumented and subject to change with no notice or deprecation warning). Second, the _args attribute only returns the children of that node in the expression tree. For linear expressions, there is a high probability that those are the variables, but it is not guaranteed. For nonlinear expressions (and general expressions), the members of _args are almost guaranteed to be other expression objects.

您可以使用identify_variables生成器获取出现在任何Pyomo表达式中的变量:

You can get the variables that appear in any Pyomo expression using the identify_variables generator:

from pyomo.environ import *
from pyomo.core.base.expr import identify_variables

m = ConcreteModel()
m.x_1 = Var()
m.x_2 = Var()
m.c = Constraint(expr=exp(model.x_1) + 2*model.x_2 <= 2)
vars = list(identify_variables(m.c.body))

这篇关于访问在pyomo约束中出现的所有变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆