Skip to main content

Define an index set that is dependent on another two index sets - in Python

Answered

Comments

2 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff
    This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support.
  • David Torres Sanchez
    • Gurobi Staff

    Hi,

    Assuming your variables are appropriately defined, the syntax is a bit similar to AMPL.
    You just need to ensure that the order of the indices is correct.

    m.addConstrs(
      (gp.quicksum(x[j, f + 1] for j in J for f in range(1, F[a][j])) >= 1 for a in A),
      name="set_cov",
    )

    In this case, the outer for-loop defines \(\texttt{a}\), the first in-line for loop of the sum defines \(\texttt{j}\) and the second defines \(\texttt{f}\) (which uses both \(\texttt{a}\) and \(\texttt{j}\)).

    I suggest you take a look at the examples, there are some simple cases when this is done: e.g. diet.py  or workforce1.py. The latter uses tupledict.sum().
    You will need some basic Python knowledge to understand these, particularly Python Generators and list comprehensions.

    Cheers, 
    David

    0

Post is closed for comments.