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.

Thanks in advance!

  • 1
    $\begingroup$ 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) $\endgroup$ – EhsanK Oct 11 '19 at 15:58
  • $\begingroup$ How did you define your set indices? $\endgroup$ – Oguz Toragay 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.

  • 1
    $\begingroup$ 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! $\endgroup$ – campioni Oct 14 '19 at 11:59
  • $\begingroup$ Glad it helped! $\endgroup$ – 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


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

  • $\begingroup$ 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. $\endgroup$ – campioni Oct 13 '19 at 22:23
  • $\begingroup$ 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. $\endgroup$ – campioni Oct 24 '19 at 16:23
  • 1
    $\begingroup$ @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. $\endgroup$ – Oguz Toragay Oct 24 '19 at 16:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.