为什么决策变量域声明在与 v5.6.9 一起使用的 Pyomo v5.7 中不起作用
Why does a decision variable domain declaration not work in Pyomo v5.7 that worked with v5.6.9
我有一个在 Pyomo 5.7 中不起作用但在 5.6.9 中起作用的决策变量声明。
相关行是构建(具体)模型的一部分:
model.v = pe.Var(within = pe.RealSet) #pe = pyomo.environ
这 运行 在我使用 Pyomo 5.6.9 版时很好,但现在在提供的代码块中出现如下所述的错误。
为了 运行 5.7 我不得不删除 within 语句所以我剩下:
model.v = pe.Var()
不再支持定义域包含所有实数的明确声明的原因是什么?
当 运行 使用 Pyomo 5.7 的原始语句时,出现此错误(Windows 10 上的 CPython 3.7.1):
ERROR: Constructing component 'v' from data=None failed: TypeError: __new__()
takes 1 positional argument but 2 were given
Traceback (most recent call last):
File "runscript.py", line 216, in <module>
payoff_matrix = my_matrix
File "C:\Users\myusername\Documents\modelbuildscript.py", line 24, in __init__
model.v = pe.Var(within = pe.RealSet)
File "C:\EngTools\Anaconda318.12\lib\site-packages\pyomo\core\base\block.py", line 543, in __setattr__
self.add_component(name, val)
File "C:\EngTools\Anaconda318.12\lib\site-packages\pyomo\core\base\block.py", line 1081, in add_component
val.construct(data)
File "C:\EngTools\Anaconda318.12\lib\site-packages\pyomo\core\base\var.py", line 613, in construct
self._initialize_members((None,))
File "C:\EngTools\Anaconda318.12\lib\site-packages\pyomo\core\base\var.py", line 682, in_initialize_members
self.domain = self._domain_init_rule(self._parent())
TypeError: __new__() takes 1 positional argument but 2 were given
简短的回答是你应该使用
model.v = pe.Var(within=pe.Reals)
稍长的答案是 Pyomo 全局集(Reals
、NonNegativeReals
、Integers
、PositiveIntegers
、Binary
等)是最初使用独立于 Set
和 RangeSet
组件的 class 层次结构定义。不幸的是,这导致了许多问题(正确检测离散集、使用全局集执行集操作等)。 Pyomo 5.7 切换到 Set
/ RangeSet
组件的新重写,在许多其他错误修复中移动到一个新的范例,其中 Pyomo 中的 all 集是 Set
或 RangeSet
组件(甚至是全局集)。我们放入了一个向后兼容层,该层捕获了测试套件中 RealSet
class 的所有遗留用途,但您的用例(虽然有效)不是记录/测试用例。 PR 1619 将恢复此功能(可能会包含在 Pyomo 5.7.1 中),并为使用 RealSet
.
添加官方弃用警告
我有一个在 Pyomo 5.7 中不起作用但在 5.6.9 中起作用的决策变量声明。
相关行是构建(具体)模型的一部分:
model.v = pe.Var(within = pe.RealSet) #pe = pyomo.environ
这 运行 在我使用 Pyomo 5.6.9 版时很好,但现在在提供的代码块中出现如下所述的错误。
为了 运行 5.7 我不得不删除 within 语句所以我剩下:
model.v = pe.Var()
不再支持定义域包含所有实数的明确声明的原因是什么?
当 运行 使用 Pyomo 5.7 的原始语句时,出现此错误(Windows 10 上的 CPython 3.7.1):
ERROR: Constructing component 'v' from data=None failed: TypeError: __new__()
takes 1 positional argument but 2 were given
Traceback (most recent call last):
File "runscript.py", line 216, in <module>
payoff_matrix = my_matrix
File "C:\Users\myusername\Documents\modelbuildscript.py", line 24, in __init__
model.v = pe.Var(within = pe.RealSet)
File "C:\EngTools\Anaconda318.12\lib\site-packages\pyomo\core\base\block.py", line 543, in __setattr__
self.add_component(name, val)
File "C:\EngTools\Anaconda318.12\lib\site-packages\pyomo\core\base\block.py", line 1081, in add_component
val.construct(data)
File "C:\EngTools\Anaconda318.12\lib\site-packages\pyomo\core\base\var.py", line 613, in construct
self._initialize_members((None,))
File "C:\EngTools\Anaconda318.12\lib\site-packages\pyomo\core\base\var.py", line 682, in_initialize_members
self.domain = self._domain_init_rule(self._parent())
TypeError: __new__() takes 1 positional argument but 2 were given
简短的回答是你应该使用
model.v = pe.Var(within=pe.Reals)
稍长的答案是 Pyomo 全局集(Reals
、NonNegativeReals
、Integers
、PositiveIntegers
、Binary
等)是最初使用独立于 Set
和 RangeSet
组件的 class 层次结构定义。不幸的是,这导致了许多问题(正确检测离散集、使用全局集执行集操作等)。 Pyomo 5.7 切换到 Set
/ RangeSet
组件的新重写,在许多其他错误修复中移动到一个新的范例,其中 Pyomo 中的 all 集是 Set
或 RangeSet
组件(甚至是全局集)。我们放入了一个向后兼容层,该层捕获了测试套件中 RealSet
class 的所有遗留用途,但您的用例(虽然有效)不是记录/测试用例。 PR 1619 将恢复此功能(可能会包含在 Pyomo 5.7.1 中),并为使用 RealSet
.