Trouble with indexing in Gurobi
AnsweredHello!
I am trying to optimize a "microgrid" with regards to cost, the model will take a fixed demand, a flexible demand, spot price for electricity, along with fixed production data for a solar power system. Additionally power can be stored in a battery to be discharged during optimal times, as well as charged. The time period is 48 hours.
I have most of this defined and working, but I am struggling with getting the battery to work, I have tried different ways of defining it, but i am getting keyerrors due to my indexing:
Here I have tried to define the battery as a constraint, the charge and discharge models seem to work, they both have values for the 48 timesteps in model t. I have also tried to define it using the same formula, except using batterylevel[t] = everything else of [t1]. Sadly none of this works. How can I implement this? Any help would be greatly appreciated!

Hi Maximilian,
Could you please provide a minimal reproducible example showing the issue? This means that in your case, you would need to at least define 3 variables with an index t. Could you also show the error message you get?
Best regards,
Jaromił2 
Thank you for your reply!
So my variables are:The objective function is to minimize the sum of net_transformer_load (pictured below) multiplied by the spot price.
There are also many more constraintsThe error message is the one below, probably refering to the first picture I sent, also attached below
2 
Please note that posting screenshots is not a good way to provide a reproducible example. In particular, the value of \(\texttt{model.t}\) is missing.
Anyway, I suppose that the maximum value for \(\texttt{model.t}\) is 47 and you are trying to execute
battery_test_rule(model,47)
which leads to to the code trying to access
model.batteryLevel[48]
which is not defined.
1 
# Set representing the time steps, 48 hours
model.t = Set(initialize=np.arange(0, 48), ordered=True)
# Set representing the time steps until all vehicles must be fully charged, 32 hours
model.c = Set(initialize = np.arange(0,32), ordered=True)The model c refers to vehicles, this is the flexible demand that I mentioned previously.
So yes it is trying to access a value that it is not indexed for I suppose. I am wondering how I can fix this, as I need the value of the battery level for the previous timestep to carry over to the beginning of the next time step. The usual way I would solve this with an array does not seem to work inside the model.1 
You could just call the \(\texttt{battery_test_rule}\) method only for \(t < 47\).
0 
What is the best way to express this in the constraint format?
0 
What is the best way to express this in the constraint format?
Could you elaborate what exactly you mean?
You could go with
if t < 47:
model.battery_level2 = Constraint(model.t, rule = battery_test_rule)
else
model.battery_level2 = None0 
Thanks! That fixed the keyerror!
However mymodel.battery_level
values do not change, they stay at 1000 which is the upper bound
def battery_level_rule(model,t):
return aml.inequality(0,model.batteryLevel[t],1000)
model.battery_level = Constraint(model.t, rule = battery_level_rule)How can I assign the variable
model.batteryLevel = Var(model.t, within = NonNegativeReals)
to be a result of other variables? I suppose my constraint
def battery_test_rule(model,t):
return model.batteryLevel[t+1] == model.batteryLevel[t]+model.batteryCharge[t]model.batteryDischarge[t]
if t < 47:
model.battery_level = Constraint(model.t, rule = battery_test_rule)
else:
model.battery_level = NoneIs just checking if the equality is true, so how can I get
model.batteryLevel
to update according to
model.batteryLevel[t+1] = model.batteryLevel[t]+model.batteryCharge[t]model.batteryDischarge[t]
Thanks in advance!
0 
Is just checking if the equality is true, so how can I get
If you add the constraint to your model, it is not "just checking" whether the equality holds. When the constraint is added to your model, every feasible solution point has to satisfy this constraint.
I suggest, you write your model to an LP file via
model.write("myLP.lp")
This will generate a file called \(\texttt{myLP.lp}\) which you can inspect and check whether all constraints look as you expect them to.
0 
Okay, I am suspecting that the errror is due to the fact that the "battery" can both charge and discharge at the same time, so I will try to fix that
Thank you very much for your time!0
Please sign in to leave a comment.
Comments
10 comments