我在成本优化问题中与“链接”变量作斗争。总的来说,我有四个变量:varX1
、和varX2
,它们以下列方式配对:varY1
varY2
if 0 <= varX1 < 1 then varY1 = 0
if 1 <= varX1 <= 6 then varY1 = 1
和
if 0 <= varX2 < 1 then varY2 = 0
if 1 <= varX2 <= 5 then varY2 = 1
我尝试使用 pyomos 分段尝试从https://github.com/Pyomo/pyomo/blob/main/examples/pyomo/piecewise/step.py重新创建示例,对 varY1 varX2 varY2`varX1
和 varY1的关系进行建模。as well as
and
完整型号代码为:
import pyomo.environ as po
costsX1 = 4
costsX2 = 6
costsY1 = 2
costsY2 = 1
a1 = 4
a2 = 3
model = po.ConcreteModel()
model.VarX1 = po.Var(bounds=(0,6))
model.VarX2 = po.Var(bounds=(0,5))
model.VarY1 = po.Var(within=po.Binary)
model.VarY2 = po.Var(within=po.Binary)
model.cons1 = po.Constraint(expr=model.VarX1+model.VarX2==5)
model.cons2 = po.Constraint(expr=a1*model.VarY1+ a2*model.VarY2>=3)
model.obj = po.Objective(expr=costsX1*model.VarX1+costsY1*a1*model.VarY1+costsX2*model.VarX2+costsY2*a2*model.VarY2,
sense=po.minimize)
DOMAIN_PTS_X1 = [0, 1, 1, 6]
RANGE_PTS_Y1 = [0, 0, 1, 1]
DOMAIN_PTS_X2 = [0, 1, 1, 5]
RANGE_PTS_Y2 = [0, 0, 1, 1]
model.piece1 = po.Piecewise(model.VarX1, model.VarY1,
pw_pts=DOMAIN_PTS_X1,
pw_constr_type='LB',
f_rule=RANGE_PTS_Y1,
pw_repn='INC')
model.piece2 = po.Piecewise(model.VarX2, model.VarY2,
pw_pts=DOMAIN_PTS_X2,
pw_constr_type='LB',
f_rule=RANGE_PTS_Y2,
pw_repn='INC')
opt = po.SolverFactory('cbc')
result_obj = opt.solve(model, tee=True)
model.pprint()
我得到以下结果
4 Var Declarations
VarX1 : Size=1, Index=None
Key : Lower : Value : Upper : Fixed : Stale : Domain
None : 0 : 5.0 : 6 : False : False : Reals
VarX2 : Size=1, Index=None
Key : Lower : Value : Upper : Fixed : Stale : Domain
None : 0 : 0.0 : 5 : False : False : Reals
VarY1 : Size=1, Index=None
Key : Lower : Value : Upper : Fixed : Stale : Domain
None : 0 : 0.0 : 1 : False : False : Binary
VarY2 : Size=1, Index=None
Key : Lower : Value : Upper : Fixed : Stale : Domain
None : 0 : 1.0 : 1 : False : False : Binary
可以看出,没有varX1
和的配对varY1
。
任何人都可以帮助我吗?
解决方案:
在 AirSquid 的帮助下,我能够为我的问题找到一个简单的解决方案。我放弃了使用 pyomos 分段函数,并为模型引入了额外的约束。以下模型按需要工作
import pyomo.environ as po
costsX1 = 4
costsX2 = 6
costsY1 = 2
costsY2 = 1
a1 = 4
a2 = 3
model = po.ConcreteModel()
model.VarX1 = po.Var(bounds=(0,6))
model.VarX2 = po.Var(bounds=(0,5))
model.VarY1 = po.Var(within=po.Binary)
model.VarY2 = po.Var(within=po.Binary)
model.cons1 = po.Constraint(expr=model.VarX1+model.VarX2==5)
model.cons2 = po.Constraint(expr=a1*model.VarY1+ a2*model.VarY2>=3)
model.con3 = po.Constraint(expr=model.VarY1 <= model.VarX1)
model.con4 = po.Constraint(expr=model.VarY1 >= model.VarX1/6)
model.con5 = po.Constraint(expr=model.VarY2 <= model.VarX2)
model.con6 = po.Constraint(expr=model.VarY2 >= model.VarX2/5)
model.obj = po.Objective(expr=costsX1*model.VarX1+costsY1*a1*model.VarY1+costsX2*model.VarX2+costsY2*a2*model.VarY2,
sense=po.minimize)
opt = po.SolverFactory('cbc')
result_obj = opt.solve(model, tee=True)
model.pprint()