VRP异构站点依赖性

时间:2019-06-03 08:32:27

标签: python jupyter-notebook docplex

在我的代码中,我设法实现了不同的车辆类型(我认为),并指出了站点依赖性。但是,似乎在我的优化输出中,车辆可以行驶多于一条的路线。我要实现的目标是,一旦我的车辆返回仓库(节点0),便要为其分配新的车辆来执行另一条路线。你能帮我吗? :)

我正在使用Docplex解算器在Python Jupyter笔记本上运行

all_units = [0,1,2,3,4,5,6,7,8,9]
ucp_raw_unit_data = {
    "customer": all_units,
    "loc_x": [40,45,45,42,42,42,40,40,38,38],
    "loc_y" : [50,68,70,66,68,65,69,66,68,70],
   "demand": [0,10,30,10,10,10,20,20,20,10],
    "req_vehicle":[[0,1,2], [0], [0], [0],[0], [0], [0], [0], [0], [0]],
    }

df_units = DataFrame(ucp_raw_unit_data, index=all_units)

# Display the 'df_units' Data Frame
df_units

Q = 50
N = list(df_units.customer[1:])
V = [0] + N
k = 15
# n.o. vehicles
K = range(1,k+1)
# vehicle 1 = type 1 vehicle 6 = type 2 and vehicle 11 = type 0
vehicle_types = {1:[1],2:[1],3:[1],4:[1],5:[2],6:[2],7:[2],8:[2],9: 
[2],10:[2],11:[0],12:[0],13:[0],14:[0],15:[0]}
lf = 0.5
R = range(1,11)

# Create arcs and costs
A = [(i,j,k,r) for i in V for j in V for k in K for r in R if i!=j]
Y = [(k,r) for k in K for r in R]
c = {(i,j):np.hypot(df_units.loc_x[i]-df_units.loc_x[j], 
df_units.loc_y[i]-df_units.loc_y[j]) for i,j,k,r in A}

from docplex.mp.model import Model
import docplex

mdl = Model('SDCVRP')

# decision variables
x = mdl.binary_var_dict(A, name = 'x')
u = mdl.continuous_var_dict(df_units.customer, ub = Q, name = 'u')
y = mdl.binary_var_dict(Y, name = 'y')
# objective function
mdl.minimize(mdl.sum(c[i,j]*x[i,j,k,r] for i,j,k,r in A))

#constraint 1 each node only visited once
mdl.add_constraints(mdl.sum(x[i,j,k,r] for k in K for r in R for j in V 
if j != i and vehicle_types[k][0] in df_units.req_vehicle[j]) == 1 for i 
in N)
##contraint 2 each node only exited once
mdl.add_constraints(mdl.sum(x[i,j,k, r] for k in K for r in R for i in V 
if i != j and vehicle_types[k][0] in df_units.req_vehicle[j]) == 1 for j 
in N )

##constraint 3 -- Vehicle type constraint (site-dependency)
mdl.add_constraints(mdl.sum(x[i,j,k,r] for k in K for r in R for i in V 
if i != j and vehicle_types[k][0] not in 
df_units.req_vehicle[j]) == 0 for j in N)

#Correcte constraint 4 -- Flow constraint
mdl.add_constraints((mdl.sum(x[i, j, k,r] for j in V if j != i)  - 
                mdl.sum(x[j, i, k,r] for j in V if i != j)) == 0 for i in 
N for k in K for r in R)

#constraint 5 -- Cumulative load of visited nodes
mdl.add_indicator_constraints([mdl.indicator_constraint(x[i,j,k,r],u[i] + 
df_units.demand[j]==u[j]) for i,j,k,r in A if i!=0 and j!=0])

## constraint 6 -- one vehicle to one route
mdl.add_constraints(mdl.sum(y[k,r] for r in R) <= 1 for k in K)
mdl.add_indicator_constraints([mdl.indicator_constraint(x[i,j,k,r],y[k,r] 
== 1) for i,j,k,r in A if i!=0 and j!=0])

##constraint 7 -- cumulative load must be equal or higher than demand in 
this node
mdl.add_constraints(u[i] >=df_units.demand[i] for i in N)

##constraint 8 minimum load factor 
mdl.add_indicator_constraints([mdl.indicator_constraint(x[j,0,k,r],u[j] 
>= lf*Q) for j in N for k in K for r in R if j != 0])

mdl.parameters.timelimit = 15
solution = mdl.solve(log_output=True)

print(solution)

我希望每条路线都会被另一辆车访问,但是同一辆车会执行多条路线。而且,现在已经为拜访的节点计算了累积负载,我希望在路线上为车辆设置此负载,以便可以执行最后一个约束(最小负载系数)。

1 个答案:

答案 0 :(得分:0)

我了解K指数代表车辆,R指数代表路线。我运行了您的代码并得到了以下任务:

y_11_9=1
y_12_4=1
y_13_7=1
y_14_10=1
y_15_10=1

似乎表明许多车辆共享同一条路线。 sum(y中的r的y [k,r] <= 1)约束不禁止这样做, 因为它禁止一辆车沿着多条路线行驶。 您是否希望将分配给一条路线的车辆数量限制为1,因为这是约束6的对称约束? 如果我弄错了,请发送您获得的解决方案和您要添加的约束。

如果我添加对称约束,即通过以下方式将分配车辆的路线限制为1(同一条路线上没有两辆车辆):

mdl.add_constraints(mdl.sum(y[k, r] for r in R) <= 1 for k in K)
mdl.add_constraints(mdl.sum(y[k, r] for k in K) <= 1 for r in R)

我得到了一个成本相同的解决方案,并且只需分配三个车辆路线:

y_11_3=1
y_12_7=1
y_15_9=1

不过,我想最好的解决方案是增加使用车辆的成本因素,并将其引入最终目标。这也可能减少问题的对称性。

菲利普。