# How to establish constraint between variables with multiple indexes using CPLEX in Python

I am new in CPLEX and I am using docplex in Python to solve an ILP.

I would like to translate the following constraint in docplex:

$$\sum_{c}(X_p{_w}_{cj}+X_{p+1}{_{w'}}_{cj+1})\leqslant T_w{_{w'}}_{,jj+1} + 1$$

Knowing that the binary variables are:

• $$X_p{_w}_{cj}=1$$ if an operation $$p$$ is done by a machine $$w$$ with a configuration $$c$$ at process plan position $$j$$, and zero otherwise

• $$T_w{_{w'}}_{,jj+1}=1$$ if there is a change of machine $$w$$ between position $$j$$ and $$j+1$$, and zero otherwise.

I've tried to code a dictionary:

cnrt_10 = {(w, w1, j-1, j): opt_model.add_constraint(ct=opt_model.sum(X_var[p-1, w, c, j-1] + X_var[p, w1, c, j] for c in set_C) <= 1 + T_var[w, w1, j-1, j], ctname="cnrt10_{0}_{1}_{2}_{3}".format(w, w1, j-1, j)) for p in set_OP for w in set_W for w1 in set_W for j in set_J}


And also a list:

opt_model.add_constraints(opt_model.sum(X_var[p-1, w, c, j-1] + X_var[p, w1, c, j] for c in set_C) <= 1 + T_var[w, w1, j-1, j] for p in set_OP for w in set_W for w1 in set_W for j in set_J)


But both are returning an error.

I am sorry for my elementary question, but I am a beginner in Python and CPLEX, so I would really appreciate if someone can help me with these problems.

• What is the error you're getting here? Also, I see that you're doing for j in set_J and you are accessing index j-1 in your constraint (which I guess can lead to IndexError or KeyError)
– EhsanK
Oct 11 '19 at 15:58
• How did you define your set indices? Oct 11 '19 at 18:27

Based on a clarification in the comment, you have KeyError and more or less, this is what's happening:

You're looking at an index or key (in your dictionary) that doesn't exist and those are keys related to indices j-1 and p-1. What you need to do is not to loop over all set_J and set_OP. So, for example, assuming set_J = {1,2,3}, you can't have an index of j=0 which happens for the first time when p=1 and j=1 in your loop and X_var[p-1, w, c, j-1]= X_var[0, 1, 1, 0]. One way to avoid that is to say something like for j in range(2, len(set_J)+1) (Note that the idea is the same even if your set_J or set_OP are defined using other Python objects). Do the same thing for set_OP too.

• Hello @EhsanK. Thank you very much for your reply. I tried to change the range of J, as you suggested me and it worked well. I really appreciated your help, thanks a lot! Oct 14 '19 at 11:59
– EhsanK
Oct 14 '19 at 13:05

I believe there is a problem in the indices that you used in the construction of constraints. For example, you defined $$T_w{_{w'}}_{,jj+1}$$ which should have had 4 indices in your constraint while you put 5 of them.

T_var[w, c, c1, j-1, j]

I am not an expert in DOcplex but I am familiar with Pyomo in which you can first define a "ConstraintList()" and then through a for loop, you can add instances of that type of constraint in the constraint list. I tried to model that part of the code even though it isn't exact but may give you some hints.

model.cons_list = ConstraintList()
for c in set_C:
for p in set_OP:
for w, w1 in set_W:
for j in set_J:
model.cons_list.add(sum(X_var[p-1, w, c, j-1] + X_var[p, w1, c, j]) <= 1 + T_var[w, c, c1, j-1, j])


Also you can use

Constraint.Skip


for those combinations that you don't want to have a constraint.

• Thanks for your reply, @Oguz. I edited my post to rectify my mistake by entering T_var[w, w1, j-1, j]. Even when I try this one, the code does not run correctly, it returns a " KeyError: (0, 1, 1, 0)". I think it is related to dictionary key, but I really do not know how to rectify that, since I must use j-1. Oct 13 '19 at 22:23
• Hello @Oguz Toragay, have you ever tried to work with a similar constraint in Pyomo? I implemented this constraint in docplex but the model is not able to understand the relation between X and T. Oct 24 '19 at 16:23
• @campioni, unfortunately not, but try it in Pyomo, if you stack again, ask another question with all the clarifications that are necessary to understand your situation. I will definitely answer that if I can or I am sure someone will help you. Oct 24 '19 at 16:29