问题
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)
My constraint definition looks like as follows:
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"])
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..
I would appreciate any help or comment!
回答1:
Would something like this work?
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)
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.
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.
来源:https://stackoverflow.com/questions/62248598/pyomo-creating-a-variable-time-index