简体   繁体   中英

How to define objective function of CVRP with 3d cost matrix using CPLEX Solver?

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, ie, 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, ie, 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 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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM