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, 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())

相关内容

  • 没有找到相关文章

最新更新