首页 > 解决方案 > Pyomo:约束在解决目标后不会改变变量值

问题描述

介绍


这只是一个基本的线性规划问题,我试图解决以了解 pyomo 的工作原理。稍后我将添加更多约束并将其转换为 MILP。我的索引 t 代表 0.25 个时间步长的一天。使用 ConcreteModel() 和 CPLEX 作为求解器。

参数和变量


Dth(t) 的实际函数对我的问题并不感兴趣,它只是为了完整性而存在。

model.t = RangeSet(0, 24, 0.25)               #timesteps

model.Pth_gh = Var(model.t)                   #variable

Dth = {}
for i in model.i:
   Dth[model.t[i]] = 2 + 4 * exp(-(model.t[i] - 9) ** 2 / 3 ** 2) + 2 * exp(-(model.t[i] - 17) ** 2 / 3 ** 2)

model.Dth = Param(model.t, initialize=Dth     #actual parameter

打印出这些参数/变量产量:

Pth_gh : Size=97, Index=t
    Key   : Lower : Value : Upper : Fixed : Stale : Domain
        0 :  None :  None :  None : False :  True :  Reals
     0.25 :  None :  None :  None : False :  True :  Reals
      0.5 :  None :  None :  None : False :  True :  Reals
     0.75 :  None :  None :  None : False :  True :  Reals
      1.0 :  None :  None :  None : False :  True :  Reals
     

Dth : Size=97, Index=t, Domain=Any, Default=None, Mutable=False
    Key   : Value
        0 : 2.0004936392163692
     0.25 :  2.000808241156262
      0.5 : 2.0013050898153586
     0.75 :  2.002078298728981
      1.0 : 2.0032639513411756

我不知道您是否可以更轻松地做到这一点,如果我想稍后导出我的解决方案,我觉得时间步可能非常有用。所以我对这部分很满意。

约束


文字中的一般约束:
对于每个索引 t,所述变量/变量组合应具有右侧参数的值。
这个特定的约束条件:
Pth_gh 在每个时间步中应该与 Dth 具有相同的值。
代码中的约束:

def ThPowerBalance(model):
    for t in model.t:
        return model.Pth_gh[t] == model.Dth[t]

model.ThPowerBalanceEq = Constraint(model.t, rule=ThPowerBalance)

打印约束会产生以下结果:
将其与我的变量 Dth 进行比较,这几乎就是我想要看到的,不是吗?

ThPowerBalanceEq : Size=97, Index=t, Active=True
    Key   : Lower              : Body          : Upper              : Active
        0 : 2.0004936392163692 :     Pth_gh[0] : 2.0004936392163692 :   True
     0.25 :  2.000808241156262 :  Pth_gh[0.25] :  2.000808241156262 :   True
      0.5 : 2.0013050898153586 :   Pth_gh[0.5] : 2.0013050898153586 :   True
     0.75 :  2.002078298728981 :  Pth_gh[0.75] :  2.002078298728981 :   True

目标函数


现在这是问题所在。在尝试实际解决问题后,变量没有获得由我的约束定义的分配值。他们空着。这对我来说没有意义。
目标函数

model.OBJ = Objective(expr=p_gas * dt * 1 / gh_eff * summation(model.Pth_gh)\
                       + summation(model.pel, model.Pel_buy) * dt)

注意:Pel_buy 是一个变量。Pth_gh 的模拟定义。pel 是一个参数。Dth 的模拟定义。
求解器设置

opt = SolverFactory('cplex')
opt.solve(model)

求解模型后打印 Pth_gh

Pth_gh : Size=97, Index=t
    Key   : Lower : Value : Upper : Fixed : Stale : Domain
        0 :  None :  None :  None : False :  True :  Reals
     0.25 :  None :  None :  None : False :  True :  Reals
      0.5 :  None :  None :  None : False :  True :  Reals
     0.75 :  None :  None :  None : False :  True :  Reals
      1.0 :  None :  None :  None : False :  True :  Reals

注意: 目标函数不应该只返回一个值吗?相反,它返回:

4.11764705882353*(Pth_gh[0] + Pth_gh[0.25] + Pth_gh[0.5] + Pth_gh[0.75] + Pth_gh[1.0]...

标签: pythonoptimizationconstraintscplexpyomo

解决方案


所有这些介绍,而您遗漏了显示您如何尝试打印的代码片段?:)

需要一点时间来习惯如何在pyomo. 这里有一些可以尝试的方法,可以在求解后为您提供目标函数值。

# whatever solver you use above here ...
result = solver.solve(model)
print(result)        # will show solver info, including the OBJ value
model.display()      # will show you the values of ALL of the model variables and expressions, including the OBJ value

model.OBJ.display()  # will show you the evaluation of the OBJ function
print(model.OBJ.expr())   # will print the evaluation of the OBJ function, and give direct access to the value

试试那些。如果您仍然卡住,请回复评论!


推荐阅读