TWVRP基于matlab A_star算法求解带时间窗和任务软时间窗多车场多车型车辆路径问题含Matlab源码 1561期

Posted 紫极神光

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TWVRP基于matlab A_star算法求解带时间窗和任务软时间窗多车场多车型车辆路径问题含Matlab源码 1561期相关的知识,希望对你有一定的参考价值。

一、VRP简介

1 VRP基本原理
车辆路径规划问题(Vehicle Routing Problem,VRP)是运筹学里重要的研究问题之一。VRP关注有一个供货商与K个销售点的路径规划的情况,可以简述为:对一系列发货点和收货点,组织调用一定的车辆,安排适当的行车路线,使车辆有序地通过它们,在满足指定的约束条件下(例如:货物的需求量与发货量,交发货时间,车辆容量限制,行驶里程限制,行驶时间限制等),力争实现一定的目标(如车辆空驶总里程最短,运输总费用最低,车辆按一定时间到达,使用的车辆数最小等)。
VRP的图例如下所示:

2 问题属性与常见问题
车辆路径问题的特性比较复杂,总的来说包含四个方面的属性:
(1)地址特性包括:车场数目、需求类型、作业要求。
(2)车辆特性包括:车辆数量、载重量约束、可运载品种约束、运行路线约束、工作时间约束。
(3)问题的其他特性。
(4)目标函数可能是总成本极小化,或者极小化最大作业成本,或者最大化准时作业。

3 常见问题有以下几类:
(1)旅行商问题
(2)带容量约束的车辆路线问题(CVRP)



该模型很难拓展到VRP的其他场景,并且不知道具体车辆的执行路径,因此对其模型继续改进。



(3)带时间窗的车辆路线问题
由于VRP问题的持续发展,考虑需求点对于车辆到达的时间有所要求之下,在车辆途程问题之中加入时窗的限制,便成为带时间窗车辆路径问题(VRP with Time Windows, VRPTW)。带时间窗车辆路径问题(VRPTW)是在VRP上加上了客户的被访问的时间窗约束。在VRPTW问题中,除了行驶成本之外, 成本函数还要包括由于早到某个客户而引起的等待时间和客户需要的服务时间。在VRPTW中,车辆除了要满足VRP问题的限制之外,还必须要满足需求点的时窗限制,而需求点的时窗限制可以分为两种,一种是硬时窗(Hard Time Window),硬时窗要求车辆必须要在时窗内到达,早到必须等待,而迟到则拒收;另一种是软时窗(Soft Time Window),不一定要在时窗内到达,但是在时窗之外到达必须要处罚,以处罚替代等待与拒收是软时窗与硬时窗最大的不同。


模型2(参考2017 A generalized formulation for vehicle routing problems):
该模型为2维决策变量



(4)收集和分发问题
(5)多车场车辆路线问题
参考(2005 lim,多车场车辆路径问题的遗传算法_邹彤, 1996 renaud)

由于车辆是同质的,这里的建模在变量中没有加入车辆的维度。


(6)优先约束车辆路线问题
(7)相容性约束车辆路线问题
(8)随机需求车辆路线问题

4 解决方案
(1)数学解析法
(2)人机交互法
(3)先分组再排路线法
(4)先排路线再分组法
(5)节省或插入法
(6)改善或交换法
(7)数学规划近似法
(8)启发式算法

5 VRP与VRPTW对比

二、A_star算法简介

1 A Star算法及其应用现状
进行搜索任务时提取的有助于简化搜索过程的信息被称为启发信息.启发信息经过文字提炼和公式化后转变为启发函数.启发函数可以表示自起始顶点至目标顶点间的估算距离, 也可以表示自起始顶点至目标顶点间的估算时间等.描述不同的情境、解决不同的问题所采用的启发函数各不相同.我们默认将启发函数命名为H (n) .以启发函数为策略支持的搜索方式我们称之为启发型搜索算法.在救援机器人的路径规划中, A Star算法能结合搜索任务中的环境情况, 缩小搜索范围, 提高搜索效率, 使搜索过程更具方向性、智能性, 所以A Star算法能较好地应用于机器人路径规划相关领域.

2 A Star算法流程
承接2.1节, A Star算法的启发函数是用来估算起始点到目标点的距离, 从而缩小搜索范围, 提高搜索效率.A Star算法的数学公式为:F (n) =G (n) +H (n) , 其中F (n) 是从起始点经由节点n到目标点的估计函数, G (n) 表示从起点移动到方格n的实际移动代价, H (n) 表示从方格n移动到目标点的估算移动代价.

如图2所示, 将要搜寻的区域划分成了正方形的格子, 每个格子的状态分为可通过(walkable) 和不可通过 (unwalkable) .取每个可通过方块的代价值为1, 且可以沿对角移动 (估值不考虑对角移动) .其搜索路径流程如下:

图2 A Star算法路径规划
Step1:定义名为open和closed的两个列表;open列表用于存放所有被考虑来寻找路径的方块, closed列表用于存放不会再考虑的方块;
Step2:A为起点, B为目标点, 从起点A开始, 并将起点A放入open列表中, closed列表初始化为空;
Step3:查看与A相邻的方格n (n称为A的子点, A称为n的父点) , 可通过的方格加入到open列表中, 计算它们的F, G和H值.将A从open移除加入到closed列表中;
Step4:判断open列表是否为空, 如果是, 表示搜索失败, 如果不是, 执行下一步骤;
Step5:将n从open列表移除加入到closed列表中, 判断n是否为目标顶点B, 如果是, 表示搜索成功, 算法运行结束;
Step6:如果不是, 则扩展搜索n的子顶点:
a.如果子顶点是不可通过或在close列表中, 忽略它.
b.子顶点如果不在open列表中, 则加入open列表, 并且把当前方格设置为它的父亲, 记录该方格的F, G和H值.
Step7:跳转到步骤Step4;
Step8:循环结束, 保存路径.从终点开始, 每个方格沿着父节点移动直至起点, 即是最优路径.A Star算法流程图如图3所示.

图3 A Star算法流程

三、部分源代码

clc;
clear all;
close all;


global popsize
global D
global dis_airport
global point_num
global plane_number
global all_point
global belong_to_airport
global demand
global time_window
global speed
global service_time
global capacity_plane
global danger_area_axis
global danger_radius
global threat_matrix
global penatly_coefficient_dis
global penatly_coefficient_load
global penatly_coefficient_early_time
global penatly_coefficient_late_time
global airport_scope
global plotting_sacle    %比例尺
global danger_num 

maxgan=400; %迭代次数
popsize=100;  %种群大小
%各种惩罚系数
penatly_coefficient_dis = 100;%距离惩罚系数
penatly_coefficient_load = 1000;%负载惩罚系数
penatly_coefficient_early_time =100;%最早时间惩罚系数
penatly_coefficient_late_time = 100;%最晚时间惩罚系数
airport_scope = 300; %机场辐射范围
plotting_sacle=2;    %比例尺等于1
%%读取数据
file='data.xlsx';
data_1=xlsread(file,'机场信息');
data_2=xlsread(file,'飞机信息');
data_3=xlsread(file,'目标点信息');
data_4=xlsread(file,'威胁区域');
airport_num = size(data_1,1);   %飞机场数量
coor_airport = data_1(1:airport_num,1:2);%飞机场经纬度
scope_airport = data_1(1:2,3)/1000;%飞机场辐射范围
plane_number = size(data_2,1);%飞机数量
capacity_plane = data_2(1:plane_number,2);%各个飞机容量
speed = data_2(1:plane_number,3)*3.6;%飞机速度 km/h
belong_to_airport = data_2(1:plane_number,4);%飞机所属机场
point_num = length(data_3);%点的数量
coor_point = data_3(1:point_num,1:2);%点的经纬度
demand = data_3(1:point_num,3);%点的需求
time_window = data_3(1:point_num,4:5);%时间窗
service_time = data_3(1:point_num,6);
danger_num = size(data_4,1);%危险区域数量
coor_danger_point=data_4(1:danger_num,1:2);%危险区域经纬度
danger_radius = data_4(1:danger_num,3:4)/1000;%危险区域半径  km


[n,m]=size(coor_point);
X=zeros(n+airport_num,m);   %横纵坐标矩阵
min_long=floor(min(coor_point(:,1)));
min_lat=floor(min(coor_point(:,2)));
for i =1:n
    X(i,1)=computeDistanceLatLon(coor_point(i,1),min_lat,min_long,min_lat)/1000;%单位是km
    X(i,2)=computeDistanceLatLon(min_long,coor_point(i,2),min_long,min_lat)/1000;
end
for i=1:airport_num
    X(n+i,1)=computeDistanceLatLon(coor_airport(i,1),min_lat,min_long,min_lat)/1000;%单位是米
    X(n+i,2)=computeDistanceLatLon(min_long,coor_airport(i,2),min_long,min_lat)/1000;
end
danger_area_axis=[danger_num,2];
for i = 1:danger_num
    danger_area_axis(i,1)=computeDistanceLatLon(coor_danger_point(i,1),min_lat,min_long,min_lat)/1000;%单位是km
    danger_area_axis(i,2)=computeDistanceLatLon(min_long,coor_danger_point(i,2),min_long,min_lat)/1000;
end
D=calculate_distance(X);  %距离矩阵
dis_airport = D(1:point_num,point_num+1:point_num+airport_num);
threat_matrix=cell(airport_num+point_num,airport_num+point_num);
for i = 1:airport_num+point_num
    for j = 1:airport_num+point_num
        if i == j 
            continue;
        else
            threat_matrixi,j=threat_value(i,j,X);
        end
    end
end
function [fitvalue,whether_effective] = func(pop)
%输入种群,输出每个染色体的适应度函数
global D     
global point_num
global plane_number
global all_point
global dis_airport
global belong_to_airport
global demand
global time_window
global speed
global service_time
global capacity_plane
global threat_matrix
global penatly_coefficient_dis
global penatly_coefficient_load
global penatly_coefficient_early_time
global penatly_coefficient_late_time
global airport_scope



% pop=zeros(popsize,all_point);
% for k=1:popsize
%     start_point = randperm(plane_number,1)+point_num;
%     p = [1:start_point-1,start_point+1:all_point];
%     pop(k,1)=start_point;
%     ord = randperm(all_point-1);
%     for j = 1:all_point-1
%         pop(k,j+1)=p(ord(j));
%     end
% end

mm = size(pop,1);
fitvalue = zeros(mm,1);
whether_effective=zeros(mm,1);
for i = 1:mm
    l = pop(i,:);
    result = zeros(1,plane_number);
    ind=zeros(plane_number+1,1);
    ind(plane_number+1)=all_point+1;
    ind(1:plane_number) = find(l>point_num);
    judeg_dis = false;  %判断该方案中是否有飞机超过了机场辐射范围
    judge_load = false; %判断是否超载
    for j = 1:plane_number
        over_dis = 0;    %飞机飞行距离超过飞机场辐射范围的量
        all_early_time= 0;    %飞机早到时间之和
        all_late_time = 0;     %飞机晚到时间之和
        threat=0;           %经过雷达区域距离
        load=0;        %初始化载重
        num = ind(j+1)-ind(j);
        route=l(ind(j):(ind(j+1)-1));
        chosed_plane = route(1)-point_num;  %该路线使用的飞机
        start_airport = belong_to_airport(chosed_plane);  %起飞的机场
        if num<2
            continue;
        end
        dis = 0;
        for k = 2:num
            load = load+demand(route(k));
            if k==2    
                arr_time = time_window(route(k),1);
                dis = dis_airport(route(k),start_airport);
                threat=threat+threat_matrixstart_airport+point_num,route(k)(1);
            else
                arr_time=arr_time + service_time(route(k-1))+D(route(k),route(k-1))/speed(chosed_plane);
                early_time = max(0,(time_window(route(k),1)-arr_time));   %早到的时间
                late_time = max(0,arr_time-time_window(route(k),2));      %晚到的时间
                all_early_time=all_early_time+early_time;
                all_late_time=all_late_time+late_time;
                dis = dis+D(route(k-1),route(k));
                threat=threat+threat_matrixroute(k-1),route(k)(1);
            end
            if dis_airport(route(k),start_airport)>airport_scope
                dis_airport(route(k),start_airport);
                over_dis = over_dis+dis_airport(route(k),start_airport)-airport_scope;
                judeg_dis=true;
            end
        end
        over_load = max(0,(load-capacity_plane(chosed_plane)));   %计算超载量
        if over_load >0
             judge_load=true;
        end
        penalty_load=penatly_coefficient_load*over_load;  %超载的惩成本
        penalty_dis = penatly_coefficient_dis*over_dis;
        penalty_time = penatly_coefficient_early_time*all_early_time+...
            penatly_coefficient_late_time*all_late_time;
        dis = dis+dis_airport(route(num),start_airport);     %该飞机的最后飞机距离
        threat=threat+threat_matrixroute(k),start_airport+point_num(1);
        result(j) = dis+penalty_load+penalty_dis+penalty_time+threat;
    end
    fitvalue(i)=sum(result);
    if judge_load || judeg_dis 
        whether_effective(i) = 0;
    else
        whether_effective(i)=1;
    end       
end



四、运行结果



五、matlab版本及参考文献

1 matlab版本
2014a

2 参考文献
[1] 包子阳,余继周,杨杉.智能优化算法及其MATLAB实例(第2版)[M].电子工业出版社,2016.
[2]张岩,吴水根.MATLAB优化算法源代码[M].清华大学出版社,2017.

以上是关于TWVRP基于matlab A_star算法求解带时间窗和任务软时间窗多车场多车型车辆路径问题含Matlab源码 1561期的主要内容,如果未能解决你的问题,请参考以下文章

TWVRP基于matlab遗传算法求解带时间窗的VRP问题含Matlab源码 1074期

TWVRP基于matlab鲸鱼算法求解带时间窗开放式车辆路径问题含Matlab源码 1986期

TWVRP基于matlab节约算法求解带时间窗的电动车路径规划问题含Matlab源码 1169期

TWVRP基于matlab灰狼算法求解带时间窗的路径规划问题含Matlab源码 1075期

TWVRP基于matlab遗传和模拟退火算法求解带时间窗的取送货问题含Matlab源码 1139期

TWVRP基于matlab遗传算法求解带时间窗的车辆路径问题含Matlab源码 2424期