0

I'm using the CPLEX solver in Python through DOCPLEX.

Among other constraints, I would like to state the following one:

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

I also tried to write the following one. In which I juste separeted the sum() of each variable:

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

But in both cases I am getting a KeyError. I suppose that in this case it means that I am looking for a key that does not exist.

I stated a similar constraint, which does not require the function sum and it worked well, without returning errors:

cnrt_11 = {
    (w, c, c1, j-1, j): opt_model.add_constraint(ct=X_var[p, w, c, j-1] + X_var[p1, w, c1, j] <= 1 + A_var[w, c, c1, j-1, j],
                                                 ctname="cnrt10_{0}_{1}_{2}_{3}_{4}".format(w, c, c1, j-1, j))
    for w in range(1, len(operation_cost) + 1) 
    for w1 in range(1, len(operation_cost) + 1)
    for c in range(1, len(operation_cost[w-1]) + 1) 
    for c1 in range(1, len(operation_cost[w-1]) + 1)
    for p in range(1, len(operation_cost[w-1][c-1])+1) 
    for p1 in range(1, len(operation_cost[w-1][c-1])+1) 
    for j in range(2, len(operation_cost[w-1][c-1]) + 1)
    }

Due to that I am supposing that the problem is when using sum() function for summing two variables.

Could someone help me with this issue? I saw that there are other types of sum() functions in docplex, but I am not able to know which one is more appropriated for my case.

Thanks in advance,

campioni
  • 157
  • 1
  • 8

1 Answers1

1

The KeyError means you try to access the X_var dictionary with an unknown tuple key, so it would help to see how you built the X_var dictionary. Model.sum() is not the problem here.

It would also help to see the message with the KeyError: you should see the tuple which cause the error.

Philippe Couronne
  • 826
  • 1
  • 5
  • 6