基于python语言,实现经典遗传算法(GA)对多车场车辆路径规划问题(MDCVRP)进行求解。
车俩路径规划问题按照车场数量可分为单一车场路径规划问题和多车场路径规划问题。针对单一车场条件下仅考虑车辆容量限制的路径规划问题前边帖子中已实现GA算法求解,本文采用GA算法对多车场条件下仅考虑车辆容量限制的路径规划问题进行求解。为了保持代码的延续性以及求解思路的一致性,这里对上述GA算法代码进行如下主要修正实现MDCVRP问题的求解。
以csv文件储存数据,其中demand.csv文件记录需求节点数据,共包含需求节点id,需求节点横坐标,需求节点纵坐标,需求量;depot.csv文件记录车场节点数据,共包含车场id,车场横坐标,车场纵坐标,车队数量。需要注意的是:需求节点id应为整数,车场节点id任意,但不可与需求节点id不可重复。 可参考github主页相关文件。
(1)数据结构
定义Sol()类,Node()类,Model()类,其属性如下表:
属性 | 描述 |
---|---|
node_id_list | 需求节点id有序排列集合,对应TSP的解 |
obj | 优化目标值 |
fit | 解的适应度 |
routes | 车辆路径集合,对应MDCVRP的解 |
属性 | 描述 |
---|---|
id | 物理节点id,需唯一 |
x_coord | 物理节点x坐标 |
y_coord | 物理节点y坐标 |
demand | 物理节点需求 |
depot_capacity | 车辆基地车队规模 |
属性 | 描述 |
---|---|
best_sol | 全局最优解,值类型为Sol() |
demand_dict | 需求节点集合(字典),值类型为Node() |
depot_dict | 车场节点集合(字典),值类型为Node() |
demand_id_list | 需求节点id集合 |
sol_list | 种群,值类型为Sol() |
distance_matrix | 节点距离矩阵 |
opt_type | 优化目标类型,0:最小车辆数,1:最小行驶距离 |
vehicle_cap | 车辆容量 |
pc | 交叉概率 |
pm | 突变概率 |
n_select | 优良个体选择数量 |
popsize | 种群规模 |
(2)文件读取
def readCsvFile(demand_file,depot_file,model):
with open(demand_file,'r') as f:
demand_reader=csv.DictReader(f)
for row in demand_reader:
node = Node()
node.id = int(row['id'])
node.x_coord = float(row['x_coord'])
node.y_coord = float(row['y_coord'])
node.demand = float(row['demand'])
model.demand_dict[node.id] = node
model.demand_id_list.append(node.id)
with open(depot_file,'r') as f:
depot_reader=csv.DictReader(f)
for row in depot_reader:
node = Node()
node.id = row['id']
node.x_coord=float(row['x_coord'])
node.y_coord=float(row['y_coord'])
node.depot_capacity=float(row['capacity'])
model.depot_dict[node.id] = node
(3)计算距离矩阵
def calDistance(model):
for i in range(len(model.demand_id_list)):
from_node_id = model.demand_id_list[i]
for j in range(i+1,len(model.demand_id_list)):
to_node_id=model.demand_id_list[j]
dist=math.sqrt( (model.demand_dict[from_node_id].x_coord-model.demand_dict[to_node_id].x_coord)**2
+(model.demand_dict[from_node_id].y_coord-model.demand_dict[to_node_id].y_coord)**2)
model.distance_matrix[from_node_id,to_node_id]=dist
model.distance_matrix[to_node_id,from_node_id]=dist
for _,depot in model.depot_dict.items():
dist = math.sqrt((model.demand_dict[from_node_id].x_coord - depot.x_coord) ** 2
+ (model.demand_dict[from_node_id].y_coord -depot.y_coord)**2)
model.distance_matrix[from_node_id, depot.id] = dist
model.distance_matrix[depot.id, from_node_id] = dist
(4)初始解生成
def generateInitialSol(model):
demand_id_list=copy.deepcopy(model.demand_id_list)
for i in range(model.popsize):
seed=int(random.randint(0,10))
random.seed(seed)
random.shuffle(demand_id_list)
sol=Sol()
sol.node_id_list=copy.deepcopy(demand_id_list)
model.sol_list.append(sol)
(5)适应度计算
适应度计算依赖" splitRoutes “函数对TSP可行解分割得到车辆行驶路线和所需车辆数,在得到各车辆形式路线后在满足车场车队规模条件下分配最近车场,” calDistance "函数计算行驶距离。
def selectDepot(route,depot_dict,model):
min_in_out_distance=float('inf')
index=None
for _,depot in depot_dict.items():
if depot.depot_capacity>0:
in_out_distance=model.distance_matrix[depot.id,route[0]]+model.distance_matrix[route[-1],depot.id]
if in_out_distance<min_in_out_distance:
index=depot.id
min_in_out_distance=in_out_distance
if index is None:
print("there is no vehicle to dispatch")
route.insert(0,index)
route.append(index)
depot_dict[index].depot_capacity=depot_dict[index].depot_capacity-1
return route,depot_dict
def splitRoutes(node_id_list,model):
num_vehicle = 0
vehicle_routes = []
route = []
remained_cap = model.vehicle_cap
depot_dict=copy.deepcopy(model.depot_dict)
for node_id in node_id_list:
if remained_cap - model.demand_dict[node_id].demand >= 0:
route.append(node_id)
remained_cap = remained_cap - model.demand_dict[node_id].demand
else:
route,depot_dict=selectDepot(route,depot_dict,model)
vehicle_routes.append(route)
route = [node_id]
num_vehicle = num_vehicle + 1
remained_cap =model.vehicle_cap - model.demand_dict[node_id].demand
route, depot_dict = selectDepot(route, depot_dict, model)
vehicle_routes.append(route)
return num_vehicle,vehicle_routes
def calRouteDistance(route,model):
distance=0
for i in range(len(route)-1):
from_node=route[i]
to_node=route[i+1]
distance +=model.distance_matrix[from_node,to_node]
return distance
def calFit(model):
#calculate fit value:fit=Objmax-obj
max_obj=-float('inf')
best_sol=Sol()#record the local best solution
best_sol.obj=float('inf')
for sol in model.sol_list:
node_id_list=sol.node_id_list
num_vehicle, vehicle_routes = splitRoutes(node_id_list, model)
if model.opt_type==0:
sol.obj=num_vehicle
sol.routes=vehicle_routes
if sol.obj>max_obj:
max_obj=sol.obj
if sol.obj<best_sol.obj:
best_sol=copy.deepcopy(sol)
else:
distance=0
for route in vehicle_routes:
distance+=calRouteDistance(route,model)
sol.obj=distance
sol.routes=vehicle_routes
if sol.obj>max_obj:
max_obj=sol.obj
if sol.obj < best_sol.obj:
best_sol = copy.deepcopy(sol)
#calculate fit value
for sol in model.sol_list:
sol.fit=max_obj-sol.obj
#update the global best solution
if best_sol.obj<model.best_sol.obj:
model.best_sol=best_sol
(6)优良个体选择
def selectSol(model):
sol_list=copy.deepcopy(model.sol_list)
model.sol_list=[]
for i in range(model.n_select):
f1_index=random.randint(0,len(sol_list)-1)
f2_index=random.randint(0,len(sol_list)-1)
f1_fit=sol_list[f1_index].fit
f2_fit=sol_list[f2_index].fit
if f1_fit<f2_fit:
model.sol_list.append(sol_list[f2_index])
else:
model.sol_list.append(sol_list[f1_index])
(7)交叉
def crossSol(model):
sol_list=copy.deepcopy(model.sol_list)
model.sol_list=[]
while True:
f1_index = random.randint(0, len(sol_list) - 1)
f2_index = random.randint(0, len(sol_list) - 1)
if f1_index!=f2_index:
f1 = copy.deepcopy(sol_list[f1_index])
f2 = copy.deepcopy(sol_list[f2_index])
if random.random() <= model.pc:
cro1_index=int(random.randint(0,len(model.demand_id_list)-1))
cro2_index=int(random.randint(cro1_index,len(model.demand_id_list)-1))
new_c1_f = []
new_c1_m=f1.node_id_list[cro1_index:cro2_index+1]
new_c1_b = []
new_c2_f = []
new_c2_m=f2.node_id_list[cro1_index:cro2_index+1]
new_c2_b = []
for index in range(len(model.demand_id_list)):
if len(new_c1_f)<cro1_index:
if f2.node_id_list[index] not in new_c1_m:
new_c1_f.append(f2.node_id_list[index])
else:
if f2.node_id_list[index] not in new_c1_m:
new_c1_b.append(f2.node_id_list[index])
for index in range(len(model.demand_id_list)):
if len(new_c2_f)<cro1_index:
if f1.node_id_list[index] not in new_c2_m:
new_c2_f.append(f1.node_id_list[index])
else:
if f1.node_id_list[index] not in new_c2_m:
new_c2_b.append(f1.node_id_list[index])
new_c1=copy.deepcopy(new_c1_f)
new_c1.extend(new_c1_m)
new_c1.extend(new_c1_b)
f1.nodes_seq=new_c1
new_c2=copy.deepcopy(new_c2_f)
new_c2.extend(new_c2_m)
new_c2.extend(new_c2_b)
f2.nodes_seq=new_c2
model.sol_list.append(copy.deepcopy(f1))
model.sol_list.append(copy.deepcopy(f2))
else:
model.sol_list.append(copy.deepcopy(f1))
model.sol_list.append(copy.deepcopy(f2))
if len(model.sol_list)>model.popsize:
break
(8)突变
def muSol(model):
sol_list=copy.deepcopy(model.sol_list)
model.sol_list=[]
while True:
f1_index = int(random.randint(0, len(sol_list) - 1))
f1 = copy.deepcopy(sol_list[f1_index])
m1_index=random.randint(0,len(model.demand_id_list)-1)
m2_index=random.randint(0,len(model.demand_id_list)-1)
if m1_index!=m2_index:
if random.random() <= model.pm:
node1=f1.node_id_list[m1_index]
f1.node_id_list[m1_index]=f1.node_id_list[m2_index]
f1.node_id_list[m2_index]=node1
model.sol_list.append(copy.deepcopy(f1))
else:
model.sol_list.append(copy.deepcopy(f1))
if len(model.sol_list)>model.popsize:
break
(9)绘制收敛曲线
def plotObj(obj_list):
plt.rcParams['font.sans-serif'] = ['SimHei'] #show chinese
plt.rcParams['axes.unicode_minus'] = False # Show minus sign
plt.plot(np.arange(1,len(obj_list)+1),obj_list)
plt.xlabel('Iterations')
plt.ylabel('Obj Value')
plt.grid()
plt.xlim(1,len(obj_list)+1)
plt.show()
(10)绘制车辆路线
def plotRoutes(model):
for route in model.best_sol.routes:
x_coord=[model.depot_dict[route[0]].x_coord]
y_coord=[model.depot_dict[route[0]].y_coord]
for node_id in route[1:-1]:
x_coord.append(model.demand_dict[node_id].x_coord)
y_coord.append(model.demand_dict[node_id].y_coord)
x_coord.append(model.depot_dict[route[-1]].x_coord)
y_coord.append(model.depot_dict[route[-1]].y_coord)
plt.grid()
if route[0]=='d1':
plt.plot(x_coord,y_coord,marker='o',color='black',linewidth=0.5,markersize=5)
elif route[0]=='d2':
plt.plot(x_coord,y_coord,marker='o',color='orange',linewidth=0.5,markersize=5)
else:
plt.plot(x_coord,y_coord,marker='o',color='b',linewidth=0.5,markersize=5)
plt.xlabel('x_coord')
plt.ylabel('y_coord')
plt.show()
(11)输出结果
def outPut(model):
work=xlsxwriter.Workbook('result.xlsx')
worksheet=work.add_worksheet()
worksheet.write(0,0,'opt_type')
worksheet.write(1,0,'obj')
if model.opt_type==0:
worksheet.write(0,1,'number of vehicles')
else:
worksheet.write(0, 1, 'drive distance of vehicles')
worksheet.write(1,1,model.best_sol.obj)
for row,route in enumerate(model.best_sol.routes):
worksheet.write(row+2,0,'v'+str(row+1))
r=[str(i)for i in route]
worksheet.write(row+2,1, '-'.join(r))
work.close()
(12)主函数
def run(demand_file,depot_file,epochs,pc,pm,popsize,n_select,v_cap,opt_type):
"""
:param demand_file: demand file path
:param depot_file: depot file path
:param epochs:Iterations
:param pc:Crossover probability
:param pm:Mutation probability
:param popsize:Population size
:param n_select:Number of excellent individuals selected
:param v_cap:Vehicle capacity
:param opt_type:Optimization type:0:Minimize the number of vehicles,1:Minimize travel distance
:return:
"""
model=Model()
model.vehicle_cap=v_cap
model.opt_type=opt_type
model.pc=pc
model.pm=pm
model.popsize=popsize
model.n_select=n_select
readCsvFile(demand_file,depot_file,model)
calDistance(model)
generateInitialSol(model)
history_best_obj = []
best_sol=Sol()
best_sol.obj=float('inf')
model.best_sol=best_sol
for ep in range(epochs):
calFit(model)
selectSol(model)
crossSol(model)
muSol(model)
history_best_obj.append(model.best_sol.obj)
print("%s/%s, best obj: %s" % (ep,epochs,model.best_sol.obj))
plotObj(history_best_obj)
plotRoutes(model)
outPut(model)
代码和数据文件可获取:
https://download.csdn.net/download/python_n/37365542