Identify variables in Expression does not work.

huangapple go评论49阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年2月14日 21:18:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448423.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定