PYOMO抽象建模中的嵌套分离
我正在研究具有一些分析的小型优化模型。我在混凝土模型中所做的方式效果很好:
from pyomo.environ import *
m = ConcreteModel()
m.d1 = Disjunct()
m.d2 = Disjunct()
m.d1.sub1 = Disjunct()
m.d1.sub2 = Disjunct()
m.d1.disj = Disjunction(expr=[m.d1.sub1, m.d1.sub2])
m.disj = Disjunction(expr=[m.d1, m.d2])
但是现在我将混凝土模型转换为抽象的公式。我能够修复所有内容,而不是嵌套分离。我做的方式就像:
#Disjunct 1
def _op_mode1(self, op_mode, t):
m = op_mode.model()
op_mode.c1 = po.Constraint(expr=m.x[t] == True)
#Disjunct 2
def _op_mode2(self, op_mode, t):
m = op_mode.model()
op_mode.c1 = po.Constraint(expr=m.x[t] == False)
#Disjunction 1
def _op_modes(self,m, t):
return [m.mode1[t], m.mode2[t]]
#Adding Components
self.model.del_component("mode1")
self.model.del_component("mode1_index")
self.model.add_component("mode1", pogdp.Disjunct(self.model.T, rule=self._op_mode1))
self.model.del_component("mode2")
self.model.del_component("mode2_index")
self.model.add_component("mode2", pogdp.Disjunct(self.model.T, rule=self._op_mode1))
self.model.del_component("modes")
self.model.del_component("modes_index")
self.model.add_component("modes", pogdp.Disjunction(self.model.T, rule=self._op_modes))`
正如我之前提到的那样,这很好。但是我还没有找到任何方法来嵌套分离。 PYOMO ALSWAYS抱怨析出的第二层,例如“ sub1”。
有人可以给我一个暗示吗?
许多问候
Joerg
I am working on a small optimization model with some disjunctions. The way I did in a concrete model worked well:
from pyomo.environ import *
m = ConcreteModel()
m.d1 = Disjunct()
m.d2 = Disjunct()
m.d1.sub1 = Disjunct()
m.d1.sub2 = Disjunct()
m.d1.disj = Disjunction(expr=[m.d1.sub1, m.d1.sub2])
m.disj = Disjunction(expr=[m.d1, m.d2])
But now I tranfered the concrete model into an abstract formulation. I was able to fix everything instead of nesting the disjunctions. The way I did it was like:
#Disjunct 1
def _op_mode1(self, op_mode, t):
m = op_mode.model()
op_mode.c1 = po.Constraint(expr=m.x[t] == True)
#Disjunct 2
def _op_mode2(self, op_mode, t):
m = op_mode.model()
op_mode.c1 = po.Constraint(expr=m.x[t] == False)
#Disjunction 1
def _op_modes(self,m, t):
return [m.mode1[t], m.mode2[t]]
#Adding Components
self.model.del_component("mode1")
self.model.del_component("mode1_index")
self.model.add_component("mode1", pogdp.Disjunct(self.model.T, rule=self._op_mode1))
self.model.del_component("mode2")
self.model.del_component("mode2_index")
self.model.add_component("mode2", pogdp.Disjunct(self.model.T, rule=self._op_mode1))
self.model.del_component("modes")
self.model.del_component("modes_index")
self.model.add_component("modes", pogdp.Disjunction(self.model.T, rule=self._op_modes))`
As I previously mentioned, this works fine. But I haven`t found any way to nest the disjunctions. Pyomo alsways complains about the second layer of the disjuncts like "sub1".
Would anybody could give me a hint?
Many greetings
Joerg
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以上最新模型的问题是,您正在声明
m.d1
和m.d2
mt
的每个元素,但它们覆盖了每个元素每次都有相同的名字以来。您应该看到为此记录的警告消息。因此,如果您将模型的pprint
删除,您会发现您只有最后声明的(在x [10]
上都有约束)。因此,m.disjunction _
中的前9个分析是不存在的析取。最简单的解决方案是在您声明它们时给出唯一的名称:但是,通过
mt
索引划分的清洁度(可能更容易),因为这基本上是基本上是独特的名称在做什么。The issue with the latest model above is that you are declaring
m.d1
andm.d2
for each element ofm.T
, but they overwrite each other each time since they have the same name. You should be seeing warning messages logged for this. So if you uncomment yourpprint
of the model, you'll see that you only have the last ones you declared (with constraints onx[10]
). So the first 9 Disjunctions inm.disjunction_
are disjunctions of Disjuncts that do not exist. The simplest fix for this is to give the disjuncts unique names when you declare them:However, it would be cleaner (and probably easier down the line) to index the Disjuncts by
m.T
as well, since that's basically what the unique names are doing.block
(因此,ed
规则)被传递给块(或分离)作为第一个参数。因此,您的具体模型也可能看起来像这样:关于为什么您将模型转换为“抽象”形式的更根本的问题。 PYOMO抽象模型主要是设计为在AMPL中建模的人们所熟悉的。尽管它们将与块结构的模型一起使用,但由于AMPL从未真正考虑到块,但类似地面向块的抽象模型往往不必要地繁琐。
Block
(and henceDisjunct
rules) are passed the block (or disjunct) to be populated as the first argument. So, an "abstract" equivalent too your concrete model might look something like this:There is a more fundamental question as to why you are converting your model over to "abstract" form. Pyomo Abstract models were mostly devised to be familiar to people coming from modeling in AMPL. While they will work with block-structured models, as AMPL was never really designed with blocks in mind, similarly block-oriented Abstract models tend to be unnecessarily cumbersome.
这是我们的新模型:
我认为这与Rangeset有关。对于一个步骤,它可以工作,但是在多个步骤中,它会引发错误: attributeError:'nontype'对象没有属性'component',
如果您可以看一下它,那将是很棒的。
非常感谢
Here ist our new model:
I think it has something to do with the RangeSet. For a single step it works, but with more than one steps it throws an error: AttributeError: 'NoneType' object has no attribute 'component'
It would be great if you could have a look on it.
Many thanks
这是与BIGM相当良好的代码,但与MBIGM或Hull Transformation相当不错:
Here is the code which works pretty fine with bigm, but not with mbigm or hull transformation: