Identify variables in Expression does not work.

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

Identify variables in Expression does not work

问题

以下是您要翻译的内容:

  1. import pyomo.environ as pyo
  2. from pyomo.core.expr.current import identify_variables
  3. model = pyo.ConcreteModel()
  4. model.x = pyo.Var(initialize=1.0)
  5. def _e(m,i):
  6. return m.x*i
  7. model.e = pyo.Expression([1,2,3], rule=_e)
  8. 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?

  1. import pyomo.environ as pyo
  2. from pyomo.core.expr.current import identify_variables
  3. model = pyo.ConcreteModel()
  4. model.x = pyo.Var(initialize=1.0)
  5. def _e(m,i):
  6. return m.x*i
  7. model.e = pyo.Expression([1,2,3], rule=_e)
  8. assert len(list(identify_variables(model.e, include_fixed=True))) > 0

答案1

得分: 0

  1. 作为参考以下是我得到的内容以便在任何表达式使用`Var`时都有一个非空列表
  2. ```python
  3. from itertools import chain
  4. import pyomo.environ as pyo
  5. from pyomo.core.expr.current import identify_variables
  6. model = pyo.ConcreteModel()
  7. model.x = pyo.Var(initialize=1.0)
  8. def _e(m,i):
  9. return m.x*i
  10. model.e = pyo.Expression([1,2,3], rule=_e)
  11. 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:

  1. from itertools import chain
  2. import pyomo.environ as pyo
  3. from pyomo.core.expr.current import identify_variables
  4. model = pyo.ConcreteModel()
  5. model.x = pyo.Var(initialize=1.0)
  6. def _e(m,i):
  7. return m.x*i
  8. model.e = pyo.Expression([1,2,3], rule=_e)
  9. 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:

确定