[Path planning] Open vehicle path planning for multiple logistics centers based on genetic algorithm [Matlab 013]

1. Background

Have multiple logistics centers.
When a vehicle departs from a certain logistics center, after completing the delivery task, it does not need to return to the logistics center where it originally started, but to the nearest logistics center, that is, the open vehicle path. Of course, according to the distance between the last customer of the vehicle service and all the logistics centers, if it is the shortest distance from the original logistics center, it can also return to the original logistics center.
Optimization goal: The sum of fixed vehicle usage cost (200 yuan/vehicle), vehicle travel time cost (0.3 yuan/minute), vehicle waiting time cost (0.3 yuan/minute), and vehicle service time (0.3 yuan/minute) is the smallest.
Constraints: (1) Do not exceed the vehicle capacity limit (vehicle capacity is 1,000 units), (2) The service must be performed within the customer's time window, (3) The vehicle can arrive at the customer in advance, resulting in waiting time. (4) Set the departure time of the vehicle from the logistics center to time 0, that is, the 0th minute.
Second, the source code

function [] =opengamainforfzh20200706()
close all
clear all
tic%开始计时。
%%%% 数据读取
[data1,data2,data3,data4,data5,data6,data7]=textread('depot2.txt','%n%n%n%n%n%n%n','delimiter', ',','headerlines', 1);% 中心
datazx=[data1 data2 data3 data4 data5 data6 data7]; % 中心数据读取
%%%%%%%%%
[data1,data2,data3,data4,data5,data6,data7]=textread('RC201.txt','%n%n%n%n%n%n%n','delimiter', ',','headerlines', 1);
data=[data1 data2 data3 data4 data5 data6 data7];
wjchangdu=size(data,1);%%调用文件的长度
%把个客户的服务时间修改为5分钟
% for gg=2:wjchangdu
%     data(gg,7)=5;
% end
% data;
%%%%%%%%%%%%%
Gmax=1000;% 迭代次数
Size=200;% 规模
Pm=0.03;% 变异概率
Pc=0.85;% 交叉概率
maxQ=1000;% 载货量
sudu=60/60;% 车辆速度 km/min
fy=0.3;% 单位费用
cb=200;% 固定成本
khNum=length(data(:,1));% 客户数量
zxNum=length(datazx(:,1));% 中心数量
cheNum=khNum;% 车辆最多数量 =客户数量
CodeL=khNum*2;% 编码个数=先后顺序+中心分配  分配采用整数代码 1是属于1 2 属于2
juli=zeros(khNum+1,khNum+1);% 距离矩阵计算
for i=1:khNum % 客户之间的距离
    for j=i+1:khNum
        juli(i,j)=(sum((data(i,2:3)-data(j,2:3)).^2))^0.5;% 距离
        juli(j,i)=juli(i,j);% 对称
    end
end
 
for i=1:zxNum
    for j=1:khNum
        julizx(i,j)=(sum((datazx(i,2:3)-data(j,2:3)).^2))^0.5;% 到中心距离
        
    end
end
 
for i=1 :Size
    Pop(i,:)= [randperm(CodeL/2) ceil(rand(1,CodeL/2)*zxNum)];% 初始化 前面是顺序 后面是隶属关系
    
end
 
for kg=1:Gmax % 开始循环
    disp(['正在进行遗传算法第' num2str(kg) '次优化'])
    
    for i=1:Size % 对每个个体进行操作
        % 计算目标函数
        fi=fical(Pop(i,:),CodeL,data,juli,maxQ,sudu,fy,cb,julizx ,zxNum);  %   调用目标
        fit(1,i)=1/fi;% 适应度=目标函数倒数
    end
    
    [Val,Ind]=max(fit);% 找出最大数值及其位置
    gBestf=Val;% 全局最优
    gBestS=Pop(Ind,:);% 全局最优个体
    
    % 选择
    ga_chose(Pop,fit,Size);
    
    % 交叉操作
    Pop=ga_cross(Pop,Size,CodeL,Pc);
    
    %   变异操作
    Pop=ga_mutation(Pop,Size,CodeL,Pm) ;
    
    Bestfi(kg)=gBestf;% 最优目标函数保存 以便画图用
    
    Pop(Size,:)=gBestS;% 精英保留策略
    
end
 
[mb,AP,zsj,zjl,waitingtime,qi]=fical(gBestS,CodeL,data,juli,maxQ,sudu,fy,cb,julizx ,zxNum); % 最优计算
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure('color',[1 1 1])% 收敛曲线
plot(1./Bestfi)
xlabel('进化代数')
ylabel('目标函数')
 
figure('color',[1 1 1])% 收敛曲线
hold on
[val,ind]=sort(AP(:,2));% 安排的序号
plot(AP(ind,3));% 到达时间曲线绘制
plot(data(1:end,5),'r--')% 时间窗开始
plot(data(1:end,6),'r:')% 时间窗结束
xlabel('客户序号')
ylabel('时间/min')
legend('开始服务时间','最早要求时间','最晚要求时间')
 
yanse={
    
    'r','b','m'};
for i1=1:max(AP(:,5))% 每一个中心
    ind1=find(AP(:,5)==i1);% 当前中心
    
    for i=1:max(AP(ind1,1))% 每一个安排车辆的路径
        figure('color',[1 1 1])
        hold on
        % 配送中心
        for  j=1:zxNum
            scatter(datazx(j,2),datazx(j,3),'b*')% 每个中心坐标
            text(datazx(j,2),datazx(j,3),[ '配送中心' num2str(datazx(j,1))])% 序号标注
        end
        
        for j=1:khNum
            scatter(data(j,2),data(j,3),'bo')% 每个客户坐标
            text(data(j,2),data(j,3),num2str(data(j,1)))% 序号标注
        end
        ind=find(AP(ind1,1)==i);% 当前车辆
        lj=[AP(ind1(ind),2)];%
        ind3=find(qi(:,1)==i1 & qi(:,2)==i);
        
        %             plot([datazx(i1,2);data(lj,2)],[datazx(i1,3);data(lj,3)],'r')% 路径绘制
        
        plot([datazx(i1,2);data(lj,2);datazx(qi(ind3,3),2)],[datazx(i1,3);data(lj,3);datazx(qi(ind3,3),3)],'r')% 路径绘制
        %         end
        xlabel('x')% 横坐标
        ylabel('y')% 纵坐标
        title(['中心' num2str(i1) '  车辆 ' num2str(i) ' 的路径为 ' num2str(lj')])% 路径标题标注
        
    end
end
 
figure('color',[1 1 1])
hold on
% 配送中心
for  j=1:zxNum
    scatter(datazx(j,2),datazx(j,3),'b*')% 每个中心坐标
    text(datazx(j,2),datazx(j,3),[ '配送中心' num2str(datazx(j,1))])% 序号标注
end
 
for j=1:khNum
    scatter(data(j,2),data(j,3),'bo')% 每个客户坐标
    text(data(j,2),data(j,3),num2str(data(j,1)))% 序号标注
end
 
for i1=1:max(AP(:,5))% 每一个中心
    ind1=find(AP(:,5)==i1);% 当前中心
    disp(['中心' num2str(i1) '路径安排: '])
    for i=1:max(AP(ind1,1))% 每一个安排车辆的路径
        ind=find(AP(ind1,1)==i);% 当前车辆
        lj=[ AP(ind1(ind),2)];% 加上起点终点
        ind3=find(qi(:,1)==i1 & qi(:,2)==i);
        
        disp(['    车辆 ' num2str(i) ' 路径安排: ' num2str([i1+khNum lj'   qi(ind3,3)+khNum]) ])
        plot([datazx(i1,2);data(lj,2);datazx(i1,2)],[datazx(i1,3);data(lj,3);datazx(qi(ind3,3),3)],'r')% 路径绘制
        
    end
end
 
xlabel('x')% 横坐标
ylabel('y')% 纵坐标
title( '车辆总路线图')  % 路径标题标注
 
toc
disp(['最优总成本为: '  num2str(1./Bestfi(end))    ', 车辆总数量为:'   num2str(length(qi(:,1)))  ', 车辆总行驶距离:'  num2str(zjl) ', 车辆总使用时间:' num2str(zsj)  ', 总等待时间 :' num2str(waitingtime)])
end

Three, running results

Insert picture description here

Four, remarks

Complete code or write code to add QQ912100926 past review
>>>>>>
[Path planning] Particle swarm optimization algorithm for three-dimensional UAV path planning [Matlab 012]

Guess you like

Origin blog.csdn.net/m0_54742769/article/details/112982627