pyomo创建一个可变的时间索引 [英] Pyomo creating a variable time index
问题描述
I'm trying to bring this constraint in my pyomo model [1
我定义了一个随时间变化的索引集,我想在下面优化相应的能量变量
I define a set for indexing over time and I want to optimize the corresponding energy variable below
model.grid_time = Set(initialize=range(0, 23)))
model.charging_energy = Var(model.grid_time, initialize=0)
我的约束定义如下:
model.limits = ConstraintList()
for t in model.grid_time:
model.limits.add(sum(model.charging_energy[t] for t in model.grid >= energy_demand.at[t,"total_energy_demand"])
这些代码行的问题在于,我正在对整个索引集model.grid_time求和,而不只是对t求和.我想我需要第二个变量索引集(替换for t in model.grid
),但是在创建变量索引集之后,搜索失败.
The problem with these codelines is that I'm summing over the whole indexing set model.grid_time and not just up to t. I think I need a second variable indexing set (replacing for t in model.grid
), but I'm searching unsuccessfully after how creating a variable index set..
我将不胜感激!
推荐答案
这样的作品行吗?
def Sum_rule(model, v, t):
return sum(model.Ech[t2] for t2 in model.grid_time if t2 <= t) <= model.Edem[v,t]
model.Sum_constraint = Constraint(model.grid_time, model.V, rule=Sum_rule)
本质上,发生的是Sum_rule(model, v, t)
中的t
确保对model.grid_times
中的每个t
调用约束.总和中的t2
也是model.grid_times
的一部分,但只会采用小于调用约束的t
的值.
Essentially, what happens is that the t
in the Sum_rule(model, v, t)
makes sure that the constraint is called for each t
in model.grid_times
. The t2
in the sum is also part of model.grid_times
, but it will only take values that are smaller than the t
at which the constraint is called.
我不确定我的约束条件是否完全符合您的表示法,因为您没有提供所有必需的信息(例如,关于E^dem
变量的下标v
的信息,但基本上可以用总和来完成
I am not sure if my constraint matches exactly your notation, as you have not provided all the information required (e.g. regarding the subscript v
of the E^dem
variable, but it will basically do what you want with the sum.
这篇关于pyomo创建一个可变的时间索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!