英文:
Identify variables in Expression does not work
问题
以下是您要翻译的内容:
import pyomo.environ as pyo
from pyomo.core.expr.current import identify_variables
model = pyo.ConcreteModel()
model.x = pyo.Var(initialize=1.0)
def _e(m,i):
return m.x*i
model.e = pyo.Expression([1,2,3], rule=_e)
assert len(list(identify_variables(model.e, include_fixed=True))) > 0
英文:
Given the following code based on Pyomo docs (https://pyomo.readthedocs.io/en/stable/developer_reference/expressions/managing.html#identifying-components-and-variables and https://pyomo.readthedocs.io/en/stable/pyomo_modeling_components/Expressions.html#expression-objects) I would expect to find a non-empty list, i.e. a list containing the variable model.x
. Where is my misconception here?
import pyomo.environ as pyo
from pyomo.core.expr.current import identify_variables
model = pyo.ConcreteModel()
model.x = pyo.Var(initialize=1.0)
def _e(m,i):
return m.x*i
model.e = pyo.Expression([1,2,3], rule=_e)
assert len(list(identify_variables(model.e, include_fixed=True))) > 0
答案1
得分: 0
作为参考,以下是我得到的内容,以便在任何表达式使用`Var`时都有一个非空列表:
```python
from itertools import chain
import pyomo.environ as pyo
from pyomo.core.expr.current import identify_variables
model = pyo.ConcreteModel()
model.x = pyo.Var(initialize=1.0)
def _e(m,i):
return m.x*i
model.e = pyo.Expression([1,2,3], rule=_e)
assert len(list(chain(*(identify_variables(value) for value in list(model.e.values()))))) > 0
英文:
For reference, this is what I ended up with to have a non-empty list if any expression uses a Var
:
from itertools import chain
import pyomo.environ as pyo
from pyomo.core.expr.current import identify_variables
model = pyo.ConcreteModel()
model.x = pyo.Var(initialize=1.0)
def _e(m,i):
return m.x*i
model.e = pyo.Expression([1,2,3], rule=_e)
assert len(list(chain(*(identify_variables(value) for value in list(model.e.values()))))) > 0
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论