Pyomo Abstract Mod的印刷版组件

2024-06-06 23:34:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图展示一个非常基本的使用块的Pyomo抽象模型。但是,当我使用model.pprint()时,它不会显示我在块中创建的组件

model = pe.AbstractModel()
model.t = pe.Set()
model.x = pe.Param(model.t)
model.b = pe.Block()
model.b.y = pe.Var()

model.pprint()
# 1 Set Declarations
#     t : Dim=0, Dimen=1, Size=0, Domain=None, Ordered=False, Bounds=None
#         Not constructed

# 1 Param Declarations
#     x : Size=0, Index=t, Domain=Any, Default=None, Mutable=False
#         Not constructed

# 1 Block Declarations
#     b : Size=1, Index=None, Active=True
#         Not constructed

# 3 Declarations: t x b

我想打印我的抽象模型,并在调用pprint()时查看我的块中的所有元素,因此它看起来如下所示:

# 1 Set Declarations
#     t : Dim=0, Dimen=1, Size=0, Domain=None, Ordered=False, Bounds=None
#         Not constructed

# 1 Param Declarations
#     x : Size=0, Index=t, Domain=Any, Default=None, Mutable=False
#         Not constructed

# 1 Block Declarations
#     b : Size=1, Index=None, Active=True
#         Not constructed

#     1 Var Declarations
#         y : ...

# 3 Declarations: t x b

有没有办法做到这一点?我知道它在处理具体模型(或创建抽象模型的实例)时会这样做

目前我找到的唯一解决方法是调用块的component_objects()方法并打印它返回的所有元素,但显然我更希望像调用pprint()时那样将模型中的所有内容都放在一起

for i in model.b.component_objects():
    print(i)
# b.y

Tags: 模型nonefalsesizeindexmodelparamdomain