Skip to main content

Constraint Programming




  • Eli Towle
    Gurobi Staff Gurobi Staff

    As the error suggests, your code attempts to access variable \(\texttt{X[0, 25, 0]}\), which does not exist. I suspect the issue is as follows:

    • The outer \(\texttt{for}\) loop of the \(\texttt{R4}\) constraints iterates over \( \texttt{t} \) in \(\texttt{range(1, len(T) - 2)}\). Consequently, a constraint will be added for \( \texttt{t = len(T) - 3} \).
    • The inner \(\texttt{for}\) loop sums over \(\texttt{X[e, t_prima, s]}\) for \(\texttt{t_prima} = \texttt{t}\), \(\texttt{t+1}\), \(\texttt{t+2}\), and \(\texttt{t+3}\). When \( \texttt{t = len(T) - 3} \), this means the code attempts to access \(\texttt{x[e, len(T), s]}\), which does not exist in Python's zero-based indexing.

    Try changing your outer \(\texttt{for}\) loop to stop at \( \texttt{t} = \texttt{len(T) - 4} \):

    for t in range(1, len(T) - 3)

    (EDIT: or possibly \(\texttt{range(len(T) - 3)}\), as Michel suggests below.)

    Once the code is working, you can inspect the model for correctness by writing an LP file to disk with Model.write():


    Lastly, the mathematical formulation of the constraint isn't totally clear. There are constraints for all \( t \in \{1, \ldots, T-3\} \), but \( t \) is also used as the summation index in the constraint. From your code, it looks like the summation should instead sum over \(t'\): \( \sum_{t'=t}^{t+3} x_{e,t,s} \leq 3 \).

  • Michel Soares
    Thought Leader

    It seems to me like there is a small confusion between the modelling (1-indexed) and the code (python is 0-indexed). It might make more sense to use range(0, len(T)-3) in your constraint.



Please sign in to leave a comment.