我开发了一种车辆路线选择(VRP)模型,该模型可以通过为正确的车辆分配正确的交付位置来优化路线规划。我模型的输入是input.csv,可以将其作为如下所示的数据框加载,其中每个位置都分配有唯一的ID,GPS坐标和交货需求。起点是配送中心。
ID Location_name Location_Lat Location_Lon Date Demand
0 DC 51.4457678 -0.45613 3/4/2016 0
1 Customer_1 51.799089 -0.29716 3/4/2016 472
2 Customer_2 52.497911 -1.903832 3/4/2016 1488
3 Customer_3 53.74589 -2.379778 3/4/2016 5652
4 Customer_4 52.332395 -1.346753 3/4/2016 2348
5 Customer_5 52.276323 -1.579845 3/4/2016 12
6 Customer_6 52.254674 -0.265726 3/4/2016 2940
7 Customer_7 51.480381 0.332106 3/4/2016 2533
8 Customer_8 51.466844 -0.452952 3/4/2016 1142
9 Customer_9 51.714144 -0.462949 3/4/2016 115
10 Customer_10 50.918412 -0.14951 3/4/2016 325
11 Customer_11 52.042318 0.311799 3/4/2016 886
12 Customer_12 52.084242 0.457063 3/4/2016 412
13 Customer_13 52.126166 0.602327 3/4/2016 58
14 Customer_14 52.16809 0.747591 3/4/2016 161
15 Customer_15 52.210014 0.892855 3/4/2016 29
然后我运行我的模型,我将得到一个输出(一个变量作为列表的列表): [[0,8,3,6,11,13,0],[0,12,9,5,0],[0,4,7,10,15,1,14,2,0]]。这意味着我必须使用3辆车:
Routes for vehicle 0: [0,8,3,6,11,13,0] #always start and back to DC
Routes for vehicle 1: [0,12,9,5,0]
Routes for vehicle 2: [0,4,7,10,15,1,14,2,0]
现在我想添加一个新的输出函数,该函数允许模型以如下表格式重新组织上述输出结果:(请注意前三行)
ID Location_name Location_Lat Location_Lon Date demand Vehicle_ID Pos_in_routes
0 DC 51.4457678 -0.45613 3/4/2016 0 0 0
0 DC 51.4457678 -0.45613 3/4/2016 0 1 0
0 DC 51.4457678 -0.45613 3/4/2016 0 2 0
1 Customer_1 51.799089 -0.29716 3/4/2016 472 2 5
2 Customer_2 52.497911 -1.903832 3/4/2016 1488 2 7
3 Customer_3 53.74589 -2.379778 3/4/2016 5652 0 2
4 Customer_4 52.332395 -1.346753 3/4/2016 2348 2 1
5 Customer_5 52.276323 -1.579845 3/4/2016 12 1 3
6 Customer_6 52.254674 -0.265726 3/4/2016 2940 0 3
7 Customer_7 51.480381 0.332106 3/4/2016 2533 2 2
8 Customer_8 51.466844 -0.452952 3/4/2016 1142 0 1
9 Customer_9 51.714144 -0.462949 3/4/2016 115 1 2
10 Customer_10 50.918412 -0.14951 3/4/2016 325 2 3
11 Customer_11 52.042318 0.311799 3/4/2016 886 0 4
12 Customer_12 52.084242 0.457063 3/4/2016 412 1 1
13 Customer_13 52.126166 0.602327 3/4/2016 58 0 5
14 Customer_14 52.16809 0.747591 3/4/2016 161 2 6
15 Customer_15 52.210014 0.892855 3/4/2016 29 2 4
0 DC 51.4457678 -0.45613 3/4/2016 0 0 6
0 DC 51.4457678 -0.45613 3/4/2016 0 1 4
0 DC 51.4457678 -0.45613 3/4/2016 0 2 8
答案 0 :(得分:1)
也许有一种更优雅的方法,但是目前可以。
构造新列:
df['Vehicle_ID'] = 0
df['Pos_in_route'] = 0
创建一个空的数据框:df1 = pd.DataFrame(columns = df.columns)
假设您的车辆分配输出为:
veh = [[0,8,3,6,11,13,0],[0,12,9,5,0],[0,4,7,10,15,1,14,2,0]]
要创建Vehicle_ID
,您想将行按其索引映射到车辆ID。以下循环将查看每个车辆分配列表,并创建除最后一个条目(在终端站结束)以外的所有条目。
for i in range(len(veh)):
veh_id_dict = dict.fromkeys(veh[i][:-1],i)
veh_pos_dict = dict(zip(veh[i][:-1],range(len(veh[i])-1)))
df.loc[veh[i][:-1],'Vehicle_ID'] = df.loc[veh[i][:-1]].ID.map(veh_id_dict)
df.loc[veh[i][:-1],'Pos_in_route'] = df.loc[veh[i][:-1]].ID.map(veh_pos_dict)
df1 = df1.append(df.loc[veh[i][:-1]])
df1.sort_values(['ID','Vehicle_ID'],inplace =True)
然后我们可以构造最后几行:
for i in range(len(veh)):
df.loc[veh[i][-1],'Vehicle_ID'] = i
df.loc[veh[i][-1],'Pos_in_route'] = len(veh[i]-1)
df1 = df1.append(df.loc[veh[i][-1]])