Pyomo:约束在求解 objective 后不会更改变量值
Pyomo: Constraint doesn't change variable values after solving the objective
简介
这只是一个基本的线性规划问题,我正在尝试解决以了解 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
打印出这些 parameter/variable 产量:
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,所述 variable/combination 变量应具有右侧参数的值。
此具体约束条件为:
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
Objective函数
现在重点来了。在尝试实际解决问题后,变量没有获得由我的约束定义的赋值。他们留空。这对我来说没有意义。
Objective函数
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
注:
objective 函数不应该只是 return 一个值吗?相反,它 return 是这样的:
4.11764705882353*(Pth_gh[0] + Pth_gh[0.25] + Pth_gh[0.5] + Pth_gh[0.75] + Pth_gh[1.0]...
所有这些介绍和你遗漏了显示你如何尝试打印的代码片段? :)
要习惯如何在 pyomo
中提取值需要一点时间。这里有一些尝试可以让你在 求解后得到 objective 函数值 。
# 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
试试看。如果您仍然卡住,请发表评论!
简介
这只是一个基本的线性规划问题,我正在尝试解决以了解 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
打印出这些 parameter/variable 产量:
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,所述 variable/combination 变量应具有右侧参数的值。
此具体约束条件为:
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
Objective函数
现在重点来了。在尝试实际解决问题后,变量没有获得由我的约束定义的赋值。他们留空。这对我来说没有意义。
Objective函数
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
注: objective 函数不应该只是 return 一个值吗?相反,它 return 是这样的:
4.11764705882353*(Pth_gh[0] + Pth_gh[0.25] + Pth_gh[0.5] + Pth_gh[0.75] + Pth_gh[1.0]...
所有这些介绍和你遗漏了显示你如何尝试打印的代码片段? :)
要习惯如何在 pyomo
中提取值需要一点时间。这里有一些尝试可以让你在 求解后得到 objective 函数值 。
# 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
试试看。如果您仍然卡住,请发表评论!