提问者:小点点

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]...

共1个答案

匿名用户

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

需要一点时间来习惯如何在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

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