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

Pyomo: How do I add all set of a model's constraints to another model?

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

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

谢谢,

如果要将约束和变量复制到全新模型,可以使用模型的 clone 方法,然后删除任何不需要的模型对象(objective 函数、参数等)

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, 2, 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, Index=None, 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, Dimen=1, Size=3, Domain=None, Ordered=False, Bounds=(1, 3)
#         [1, 2, 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, Index=None, 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, Dimen=1, Size=3, Domain=None, Ordered=False, Bounds=(1, 3)
#         [1, 2, 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 Constraint Declarations
#     con1 : Size=1, Index=None, 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,2,3,4,5,6], within=NonNegativeReals)
model2.b = Var([1,2,3,4,5,6], 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())