title: How to correctly index variables within nested for loops for gurobi with python?
body: <p>I am attempting to add a constraint to an optimisation model with Gurobi. I have some terms in the constraint that reference both the current hour (that is being looped over), the 'next' hour (hour + 1), as well as some fixed constraints (i.e max_power_ramp).</p>
  
  <p>An example of the code is as follows</p>
  
  <pre><code>for hour in hours:
      if hour &gt; hours[0]:
          for i in num_gen:
              model.addConstr((p_upper[hour, i] &lt;= p[hours[hours.index(hour) - 1], i] + 
                               max_ramp_up[i] * v[hours[hours.index(hour) - 1], i] + 
                                  startup_ramp[v[hour, i]] - v[hours[hours.index(hour)-1], i] 
                                      + max_power[i] * (1 - v[hour,i])), name="power ramp up")
  </code></pre>
  
  <p>Both the num_gen and hours variables are range variables. The issue seems to be associated with how I am referencing the variables such as <code>v[hours[hours.index(hour) - 1], i]</code></p>
  
  <p>When I debug the code, everything seems to be fine in terms of the individual cell references, but when I try to complete the commands such as <code>max_ramp_up[i] * v[hours[hours.index(hour) - 1], i]</code> &amp; <code>max_power[i] * (1 - v[hour,i])</code> - it is returning errors such as; </p>
  
  <pre><code>ipdb&gt; max_ramp_up[i] * v[hours[hours.index(hour) - 1], i]
  *** gurobipy.GurobiError: Index out of range for attribute 'VarName'
  </code></pre>