微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Pyomo:如何将模型的所有约束集添加到另一个模型?

如何解决Pyomo:如何将模型的所有约束集添加到另一个模型?

我想将模型中的所有约束和变量添加到 Pyomo 中的不同模型中,但我不知道如何进行。用例是当我想将我的模型转换为它的对偶时,我需要将所有对偶约束和变量添加到原始问题中。当我们想将某个模型的最优性条件添加到另一个问题时,这实际上很有用。

也许 Pyomo 中还有其他功能转换可以做同样的事情,我不知道它们,所以如果存在这样的功能,如果有人能提供帮助,我会非常高兴。

谢谢,

解决方法

如果您想将约束和变量复制到一个全新的模型中,您可以使用模型的 clone 方法,然后删除所有不需要的模型对象(目标函数、参数等)>

import pyomo.environ as pyo

#%% Build initial model
my_set = [1,2,3]
m1 = pyo.ConcreteModel()
m1.x = pyo.Var(my_set,within=pyo.NonNegativeReals)
m1.y = pyo.Var(within=pyo.Binary)
m1.con1 = pyo.Constraint(expr = sum([m1.x[i] for i in my_set]) <= 3)
m1.obj = pyo.Objective(expr = m1.y + sum([m1.x[i] for i in my_set]),sense=-1)

#%% Solve initial model
solver = pyo.SolverFactory('glpk')
res1 = solver.solve(m1)

#%% Clone initial model
m2 = m1.clone()

#%% Verify objects copied to other model
m1.pprint()

# 1 Set Declarations
#     x_index : Dim=0,Dimen=1,Size=3,Domain=None,Ordered=False,Bounds=(1,3)
#         [1,3]

# 2 Var Declarations
#     x : Size=3,Index=x_index
#         Key : Lower : Value : Upper : Fixed : Stale : Domain
#           1 :     0 :   3.0 :  None : False : False : NonNegativeReals
#           2 :     0 :   0.0 :  None : False : False : NonNegativeReals
#           3 :     0 :   0.0 :  None : False : False : NonNegativeReals
#     y : Size=1,Index=None
#         Key  : Lower : Value : Upper : Fixed : Stale : Domain
#         None :     0 :   1.0 :     1 : False : False : Binary

# 1 Objective Declarations
#     obj : Size=1,Index=None,Active=True
#         Key  : Active : Sense    : Expression
#         None :   True : maximize : x[1] + x[2] + x[3] + y

# 1 Constraint Declarations
#     con1 : Size=1,Active=True
#         Key  : Lower : Body               : Upper : Active
#         None :  -Inf : x[1] + x[2] + x[3] :   3.0 :   True

# 5 Declarations: x_index x y con1 obj

m2.pprint()
# 1 Set Declarations
#     x_index : Dim=0,Active=True
#         Key  : Lower : Body               : Upper : Active
#         None :  -Inf : x[1] + x[2] + x[3] :   3.0 :   True

# 5 Declarations: x_index x y con1 obj

#%% Remove objective if desired
for obj in m2.component_objects(pyo.Objective):
    m2.del_component(obj)

#%% See that objective is removed
m2.pprint()

# 1 Set Declarations
#     x_index : Dim=0,Index=None
#         Key  : Lower : Value : Upper : Fixed : Stale : Domain
#         None :     0 :   1.0 :     1 : False : False : Binary

# 1 Constraint Declarations
#     con1 : Size=1,Active=True
#         Key  : Lower : Body               : Upper : Active
#         None :  -Inf : x[1] + x[2] + x[3] :   3.0 :   True

# 4 Declarations: x_index x y con1
,

我将模型“model2”的约束添加到另一个模型“model1”,首先从初始模型中删除项目,然后将其添加到另一个。

这里是一个示例代码来表明:

from pyomo.environ import *

model1 = ConcreteModel()
model2 = ConcreteModel()

model1.a = Var([1,3,4,5,6],within=NonNegativeReals)
model2.b = Var([1,within=NonNegativeReals)

model1.c = Constraint(expr= model1.a[1] + model1.a[3] == 3)
model2.d = Constraint(expr= model2.b[1] + model2.b[3] == 3)

  
for con in model2.component_objects(Constraint):
    model2.del_component(con)
    model1.add_component('d',con)

for var in model2.component_objects(Var):
    model2.del_component(var)
    model1.add_component('b',var)

model1.objective = Objective(expr = model1.a[1] + model1.b[3],sense=minimize)

solver = SolverFactory('glpk')
res = solver.solve(model1)

print(model1.display())

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。