我正在尝试使用以下简单的示例代码连接两个带有在pyomo中实现的“Connector”类的块。
from pyomo.environ import *
m = ConcreteModel()
# Block 01
m.block_01 = Block()
m.block_01.flow = Var(within=NonNegativeReals, bounds=(2, 10))
m.block_01.OUT = Connector(initialize= {'flow': m.block_01.flow})
# Block 02
m.block_02 = Block()
m.block_02.flow = Var(within=NonNegativeReals)
m.block_02.IN = Connector(initialize= {'flow': m.block_02.flow})
m.con = Constraint(expr=m.block_01.OUT == m.block_02.IN)
def _obj(_m):
return _m.block_01.flow + _m.block_02.flow
m.obj = Objective(rule=_obj)
在“优化”之后,所有变量都采用其下限值(m.block_01.flow = 2和m.block_02.flow = 0)。所以Connector似乎不会为变量传输任何数据。
如果我正在使用:
m.con = Constraint(expr=m.block_01.flow == m.block_02.flow)
相反,它的工作原理。然而,这不是连接器的想法,对吗?
关于问题原因的任何想法?
答案 0 :(得分:1)
在将模型发送给求解器之前,您是否应用了expand_connectors
转换?
TransformationFactory('core.expand_connectors').apply_to(m)