Skip to main content

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

Answered

Comments

1 comment

  • David Torres Sanchez
    Gurobi Staff 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

Please sign in to leave a comment.