Skip to main content

Trouble with indexing in Gurobi

Answered

Comments

10 comments

  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    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
  • Maximilian Sletbakk
    Gurobi-versary
    Conversationalist
    First Question

    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 constraints

     

    The error message is the one below, probably refering to the first picture I sent, also attached below

     

     

    2
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    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
  • Maximilian Sletbakk
    Gurobi-versary
    Conversationalist
    First Question
    # 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 time-step 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
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    You could just call the \(\texttt{battery_test_rule}\) method only for \(t < 47\).

    0
  • Maximilian Sletbakk
    Gurobi-versary
    Conversationalist
    First Question

    What is the best way to express this in the constraint format? 

    0
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    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 = None
    0
  • Maximilian Sletbakk
    Gurobi-versary
    Conversationalist
    First Question

    Thanks! That fixed the keyerror! 
    However my 

    model.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 = None

    Is 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
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    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
  • Maximilian Sletbakk
    Gurobi-versary
    Conversationalist
    First Question

    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.