Define an index set that is dependent on another two index sets - in Python
AnsweredHi,
I'm transferring my model from AMPL to python now, but I could not figure out how to build the index sets that rely on other indices.
s.t. c1{a in A}: sum{j in J, f in 1..F[a,j]} x[j,f+1] >= 1;
where J={0,1,2}, A is a non-continuous set ({0,1,5} for example) and F gives the max value that f can take under each (a,j) pair (for example, F=[ 1 1 2; 1 2 3; 1 1 3])
I've found a solution posted here but did not work it out when the dependency is on two indices. https://support.gurobi.com/hc/en-us/community/posts/5989957537553-Defining-a-set-dependent-on-an-other-set?sort_by=created_at
Thank you so much.
-
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,
David0
Please sign in to leave a comment.
Comments
1 comment