How do I define multiple indices for the same set in python? (using CPLEX to solve a MILP)

I am using docplex in Python to solve a MILP and I am having some trouble defining the indices and sets of my model.

I want to translate $$w,v,a\in W$$ into Python.

In this case, $$W$$ is a set, and $$w, v$$, and $$a$$ are indices of this set. I found a way to define one index for a set but I don't know how to define multiple indices for the same set.

I defined the set $$W$$ like this:

n=3
W = [j for j in range(1,n+1)]


Then I defined the variable: $$Y_{ijws}^{Dt}$$ like this:

Y_Dt_ijws_scope = [(t,i,j,w,s) for t in T for i in I for j in J for w in W for s in S]

Y_Dt_ijws = m.integer_var_dict(Y_Dt_ijws_scope, name='Y_Dt_ijws')


Where $$t$$ belongs to a set $$T$$, $$i$$ belongs to a set $$I$$, etc. I defined these sets previously as I did for $$W$$.

This was working fine until I had to write this part of a constraint: $$\sum_{i\in I}Y_{ijws}^{Dt}+\sum_{i\in I}\sum_{a\in W}Y_{ijas}^{Dt}\cdot S_{aw}$$

We can see that the constraint uses the variable that I defined but also uses a variable with the index $$a$$ instead of the index $$w$$, which is not defined.

I started to write the constraint like this:

m.add_constraints((m.sum(Y_Dt_ijws[t,i,j,w,s] for i in I) + m.sum(m.sum(Y_Dt_ijvs[t,i,j,a,s]*S_aw for a in W) for i in I))*S_DC_wv


But obviously, there's an error saying there is an undefined name Y_Dt_ijvs because I used $$a$$ inside the parenthesis the second time I wrote that variable.

I'm new to all this so I don't know if the way I did things is the best one. I thought of defining Y_Dt_ijas as a new variable but for me, that doesn't make sense since it is referring to the exact same thing. Can someone help me, please?

(P.S.: I know some people use GAMS or other things to solve these MILP problems but I have to use Python.)

• Try something like this in your second term: m.sum(Y_Dt_ijvs[t,i,j,a,s]*S_aw for i in I for a in W)*S_DC_wv (assuming you iterate over all the j, w, s, t
– EhsanK
Jul 7 at 18:23
• @EhsanK I'm sorry, I don't know if I understand your suggestion. You are telling me to remove a sum? The whole constraint is m.add_constraints((m.sum(Y_Dt_ijws[t,i,j,w,s] for i in I) + m.sum(m.sum(Y_Dt_ijvs[t,i,j,a,s]*S_aw for a in W) for i in I))*S_DC_wv == m.sum(Y_DCt_jkwvs[t,j,k,w,v,s] for k in K) for j in J for w in W for t in T for s in S) so in the end I iterate for all those indices right? Jul 8 at 11:42
• You don't need to write m.sum() multiple times for summing over each index. Bring the indexes you are summing over inside of the loop. Like a comprehension. That's what I did. I brought the two indexes you are summing over (I and W) inside of the m.sum().
– EhsanK
Jul 8 at 12:38
• Ok, got it! Thanks! But that doesn't solve my problem with indices. Right know I have this constraint m.add_constraints(m.sum(Y_Dt_ijws[t,i,j,w,s] for i in I) + m.sum(Y_Dt_ijvs[t,i,j,a,s]*S_aw for a in W for i in I)*S_DC_wv == m.sum(Y_DCt_jkwvs[t,j,k,w,v,s] for k in K) for j in J for w in W for t in T for s in S) and the same error (name 'Y_Dt_ijvs' is not defined). Jul 8 at 14:54
• Have you defined Y_Dt_ijws, Y_Dt_ijvs, Y_DCt_jkwvs all as variables? Because the terms in add_constraints should be either variables or constants.
– EhsanK
Jul 9 at 2:16