0

I am using docplex in Python to solve a ILP.

I would like to translate the following constraint in docplex.

[Constraint][1]

https://i.sstatic.net/OPhwe.png

I tried the following code:

#decision variables 

X_var = {(p, w, c, j): opt_model.binary_var(name="X_{0}_{1}_{2}_{3}".format(p, w, c, j))
          for p in set_OP for w in set_W for c in set_C for j in set_J}

T_var = {(w, w1, j-1, j): opt_model.binary_var(name="T_{0}_{1}_{2}_{3}".format(w, w1, j-1, j))
         for w in set_W for w1 in set_W for j in set_J}

#constraint
 cnrt_10 = {(p, w, w1, j-1, j): opt_model.add_constraint(ct=opt_model.sum(X_var[p-1, w, c, j-1] for c in set_C) + opt_model.sum(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}_{4}".format(p, 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 it is giving the following error:

KeyError: (0, 1, 1, 0)

I've searched about, and it seems that this is an error related to an empty dictionary. I've tried many different ways to solve it, bus I was not able. Since I am new in Python and CPLEX, I think it is not really complicated. If someone could help me, I will be really grateful.

Thanks in advance,

1 Answer 1

1

The problem is this: You are trying to access a variable with index (0, 1, 1, 0) but such a variable does not exist.

First of all, check whether the variable you reference is an X_var or a T_var. Remove one of the two from the constraint definition and see which one causes the error.

Then look hard at the indices with which you reference variables in the constraint and which variables you actually define.

From staring at your code, I guess the problem is this term in your constraint: X_var[p-1, w, c, j-1] p is taken from set_OP and j is taken from set_J. But variable X_var is defined with p from set_OP and j from set_J. So if you take the first element p0 from set_P then you have a variable X_var[p0, ., ., .] but you don't have a variable X_var[p0-1, ., ., .]. The latter is referenced by your constraint, though. Hence you will get a key error.

Sign up to request clarification or add additional context in comments.

2 Comments

Hello Daniel, thanks. Actually your were right, the problem is on the X_var[p-1, w, c, j-1]. But I must consider the sum of X_var[p, w, c, j] and X_var[p-1, w, c, j-1]. How could I do to ensure the sum of followed p and j (j -1 and j / p-1 and p) without having errors ? Also, I do not understand why when I use j-1 for T_var it works well but with X_var it does not work. Could you explain me, please?
I am not quite sure what you are asking. If you need to sum over a set of variables then of course all these variables have to exist. Maybe you have to also create the variables for p=0? Maybe they model some initial stock or something? Then you could create them and fix them to the initial stock value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.