0

The following is an extract from https://medium.com/cmsa-algorithm-for-the-service-of-the-capacitated/using-cplex-and-python-for-finding-an-exact-solution-for-the-cvrp-ac789ee0d8c4, which of course, works fine when using a 2d cost matrix:

#Intializing the set of arcs A.
A = [(i,j) for i in V for j in V if i!=j]
#Calculating the distance between each node.
c= {(i,j):np.hypot(loc_x[i]-loc_x[j],loc_y[i]-loc_y[j]) for i,j in A}
#Importing the docplex.mp.model from the CPLEX as Model
from docplex.mp.model import Model
mdl = Model('CVRP')
#Initializing our binary variable x_i,j
x=mdl.binary_var_dict (A,name='x')
#Initializing our cumulative demand u
u=mdl.continuous_var_dict (N,ub=Q ,name = 'u')
#Initializing the objectif function
mdl.minimize(mdl.sum(c[i,j]*x[i,j]for i,j in A))

For my solution approach, however, I want to use a 3d cost matrix, i.e., each element is denoted by c[i,j,k] and I want to minimize the sum of:

c[i,j,k]*x[i,j]*x[j,k]

(Long story. My cost of getting to k from j is dependent on where the vehicle came from (node i); but I still want the decision variables to be x_ij instead of x_ijk because I want to have the constraints as is, i.e., defined for x_ij's.)

I have tried the following:

# objective function    
mdl.maximize(mdl.sum(c[i,j,k])*x[i,j]*x[j,k] for i,j in A for j,k in A if i!=j)

But I get the following error message.

DOcplexException: cannot convert to expression: <generator object <genexpr> at 0x000001BE51777348>

Can anybody please help me define my objective function? Any help will be appreciated. Thanks!

  • It would be much easier to help you if you provide a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – rkersh Nov 18 '19 at 18:32

1 Answers1

1

It seems you have misplaced parenthesis in this statement:

mdl.maximize(mdl.sum(c[i,j,k])*x[i,j]*x[j,k] for i,j in A for j,k in A if i!=j)

The first closing parenthesis should not be right after c[i,j,k] but should be at the end of the line. I think the correct statement is this:

mdl.maximize(mdl.sum(c[i,j,k]*x[i,j]*x[j,k] for i,j in A for j,k in A if i!=j))
Daniel Junglas
  • 5,830
  • 1
  • 5
  • 22
  • Indeed! Sorry, I was careless because I was not confident with my approach. So apparently, it was correct all along, except for the misplaced parenthesis. I've already tried a dozen ways to do it and each one failed so I'm so glad this one worked. Thank you so much!! – rc97496 Nov 19 '19 at 11:05