Mustafa
Mustafa

Reputation: 1

DOcplexException: Expecting iterable Error

I am dealing with a vehicle routing problem with multiple working days. I want to add constraints that if a working day is 6 or its multipliers, a worker must work only 240 minutes. Therefore, I put two loops for working days (k) and workers (w) and each k and w, I want to make sure total travel and service times do not exceed a prespesified time (S). As you can see on my codes, I used if else structure for 240 and 480 minutes daily times. However, I got "DOcplexException: Expecting iterable Error" and saw this:

OcplexException: Expecting iterable, got: 480x_0_1_1_1+100x_0_2_1_1+480x_0_3_1_1+20x_0_4_1_1+300x_0_5_1_1+100x_0_6_1_1+200x_0_7_1_1+480x_0_8_1_1+80x_0_9_1_1+200x_0_10_1_1+120x_0_11_1_1+260x_0_12_1_1+280x_0_13_1_1+340x_0_14_1_1+400x_0_15_1_1+120x_0_16_1_1+80x_0_17_1_1+50x_0_18_1_1+20x_0_19_1_1+320x_0_20_1_1+180x_0_21_1_1+50x_0_22_1_1+100x_0_23_1_1+80x_0_24_1_1+140 ...

The constraints are following:

# for w in W:
#     for k in D:
#         if k%6==0:
#             mdl.add_constraints(mdl.sum((distanceList[i][j]*6/7000)*x[i, j, w, k] for i in N for j in N if j != i) + mdl.sum(serviceTime[j-1]*x[i, j, w, k] for i in V for j in N if j != i) <= S*0.5) 
#         else:
#             mdl.add_constraints(mdl.sum((distanceList[i][j]*6/7000)*x[i, j, w, k] for i in N for j in N if j != i) + mdl.sum(serviceTime[j-1]*x[i, j, w, k] for i in V for j in N if j != i) <= S)

I really appreciate for your help! Thanks.

Upvotes: 0

Views: 617

Answers (1)

Philippe Couronne
Philippe Couronne

Reputation: 846

Model.add_constraints() expects either a list or a comprehension (both work), in other terms, something that can be iterated upon. Based on the (non-executable) code snippet you posted, I have the impression the argument of add_constraints is a constraint:

Model.add_constraints( mdl.sum(a[i,w,k]) + mdl.sum(b[i,w,k]) <= S)

which is not accepted. You should transform the argument to add_constraints to a Python comprehension, something like:

Model.add_constraints( mdl.sum(a[i,w,k]+mdl.sum(b[i,w,k] <= S for w in W for k in K)

Thus, add_constraints receives a comprehension as expected. If parts of the constraints depend on the indices w,k use auxiliary functions in the comprehension. Let me give an example with the rhs:

  def my_rhs(wk,k): # rhs of the constraint as a function of w,k
     ... return w+k % 42 # silly example

  Model.add_constraints( mdl.sum(a[i,w,k]+mdl.sum(b[i,w,k] <= my_rhs(w,k) for w in W for k in K)

Upvotes: 1

Related Questions