[Path planning] Path planning based on a two-way optimization particle swarm grid map with cross factors [Matlab 027]

1. Introduction

Aiming at the problems of traditional particle swarm optimization, such as easy maturity, low accuracy, and slow convergence speed in the later stage, combined with the reverse learning theory, a two-way optimization particle swarm optimization algorithm (CBMPSO) based on cross factor is proposed. The algorithm makes the initial population uniformly distributed in the search area, calculates the fitness value of the particles and their counter-particles, and takes the best as the initial population; the iterative process increases the tracking of the global worst particles, and randomly turns on the two-way learning mechanism based on the cross factor. The test results of several typical functions show that the optimization ability and convergence speed of the CBMPSO algorithm have been significantly improved, and the problem of premature convergence can be effectively avoided.

Second, the source code

clc;
close all
clear
load('data4.mat')
S=(S_coo(2)-0.5)*num_shange+(S_coo(1)+0.5);%起点对应的编号
E=(E_coo(2)-0.5)*num_shange+(E_coo(1)+0.5);%终点对应的编号
 
PopSize=20;%种群大小
OldBestFitness=0;%旧的最优适应度值
gen=0;%迭代次数
maxgen =100;%最大迭代次数
 
c1=0.5;%认知系数
c2=0.7;%社会学习系数
c3=0.2;%反向因子
w=0.96;%惯性系数
%%
%初始化路径
w_min=0.5;
w_max=1;
Group=ones(num_point,PopSize);  %种群初始化
flag=1;
%% 初始化粒子群位置
for i=1:PopSize
    p_lin=randperm(num_point)';%随机生成1*400不重复的行向量
     %% 将起点编号放在首位
        index=find(p_lin==S);
        lin=p_lin(1);
        p_lin(1)=p_lin(index);
        p_lin(index)=lin;
        Group(:,i)=p_lin;
        %%将每个个体进行合理化处理
        [Group(:,i),flag]=deal_fun(Group(:,i),num_point,liantong_point,E,num_shange);
         fangxiang_Group(:,i)=fangxiang(Group(:,i),c3);%方向粒子数量
    while flag==1%如处理不成功,则初始化个体,重新处理
        %% 将起点编号放在首位
        index=find(p_lin==S);
        lin=p_lin(1);
        p_lin(1)=p_lin(index);
        p_lin(index)=lin;
        Group(:,i)=p_lin;
        fangxiang_Group(:,i)=p_lin;
        %%将每个个体进行合理化处理
        [Group(:,i),flag]=deal_fun(Group(:,i),num_point,liantong_point,E,num_shange);
         [fangxiang_Group(:,i),flag]=deal_fun(fangxiang_Group(:,i),num_point,liantong_point,E,num_shange);
    end
   
end
 
%初始化粒子速度(即交换序)
Velocity =zeros(num_point,PopSize);
for i=1:PopSize
    Velocity(:,i)=round(rand(1,num_point)'*num_point/10); %round取整
end
 
%计算每个个体对应路径的距离
for i=1:PopSize
    EachPathDis(i) = PathDistance(Group(:,i)',E,num_shange);
      EachPathDis_fangxiang(i) = PathDistance(fangxiang_Group(:,i)',E,num_shange);
end
 
IndivdualBest=Group;%记录各粒子的个体极值点位置,即个体找到的最短路径
IndivdualBestFitness=EachPathDis;%记录最佳适应度值,即个体找到的最短路径的长度
if min(EachPathDis)<min(EachPathDis_fangxiang)
[GlobalBestFitness,index]=min(EachPathDis);%找出全局最优值和相应序号
else
   [GlobalBestFitness,index]=min(EachPathDis_fangxiang);%找出全局最优值和相应序号 
end
%寻优
while gen < maxgen
    w=w_max-(w_max-w_min)*gen/maxgen;%自适应权重
    %迭代次数递增
    gen = gen +1
    %更新全局极值点位置,这里指路径
    for i=1:PopSize
        if min(EachPathDis)<min(EachPathDis_fangxiang)
 
        GlobalBest(:,i) = Group(:,index);
        else
          GlobalBest(:,i) = fangxiang_Group(:,index);
        end
    end
    
    %求pij-xij ,pgj-xij交换序,并以概率c1,c2的保留交换序
    pij_xij=GenerateChangeNums(Group,IndivdualBest);  %根据个体最优解求交换序
    pij_xij=HoldByOdds(pij_xij,c1);%以概率c1保留交换序
    pgj_xij=GenerateChangeNums(Group,GlobalBest);%根据全局最优解求交换序
    pgj_xij=HoldByOdds(pgj_xij,c2);%以概率c2保留交换序
       pfj_xij=GenerateChangeNums(Group,fangxiang_Group);%根据反向求交换序
    pfj_xij=HoldByOdds(pfj_xij,c3);%以概率c3保留交换序
    %以概率w保留上一代交换序
    Velocity=HoldByOdds(Velocity,w);
        Group = PathExchange(Group,pfj_xij);%根据反向粒子位置进行交换
    Group = PathExchange(Group,Velocity); %根据交换序进行路径交换
    Group = PathExchange(Group,pij_xij);%粒子位置变换通过速度、全局性适应度和个体适应度对比来交换来实现,完成自我学习和社会学习
    Group = PathExchange(Group,pgj_xij);
    
    for i = 1:PopSize
        [Group(:,i),flag]=deal_fun(Group(:,i),num_point,liantong_point,E,num_shange);
        while flag==1
            p_lin=randperm(num_point)';
            index=find(p_lin==S);
            lin=p_lin(1);
            p_lin(1)=p_lin(index);
            p_lin(index)=lin;
            Group(:,i)=p_lin;
            [Group(:,i),flag]=deal_fun(Group(:,i),num_point,liantong_point,E,num_shange);
        end
    end
    for i = 1:PopSize    % 更新各路径总距离
        EachPathDis(i) = PathDistance(Group(:,i)',E,num_shange);
    end
    IsChange = EachPathDis<IndivdualBestFitness;%更新后的距离优于更新前的,记录序号
    IndivdualBest(:, find(IsChange)) = Group(:, find(IsChange));%更新个体最佳路径
    IndivdualBestFitness = IndivdualBestFitness.*( ~IsChange) + EachPathDis.*IsChange;%更新个体最佳路径距离
    [GlobalBestFitness, index] = min(IndivdualBestFitness);%更新全局最佳路径,记录相应的序号
    
    if GlobalBestFitness~=OldBestFitness %比较更新前和更新后的适应度值;
        OldBestFitness=GlobalBestFitness;%不相等时更新适应度值
        best_route=IndivdualBest(:,index)';
    end
    BestFitness(gen) =GlobalBestFitness;%每一代的最优适应度
end
%最优解
index1=find(best_route==E);
route_lin=best_route(1:index1);
 
%最优解
figure(3)
hold on
for i=1:num_shange
    for j=1:num_shange
        if sign(i,j)==1
            y=[i-1,i-1,i,i];
            x=[j-1,j,j,j-1];
            h=fill(x,y,'k');
            set(h,'facealpha',0.5)
        end
        s=(num2str((i-1)*num_shange+j));
        text(j-0.95,i-0.5,s,'fontsize',6)
    end
end
axis([0 num_shange 0 num_shange])%限制图的边界
plot(S_coo(2),S_coo(1), 'p','markersize', 10,'markerfacecolor','b','MarkerEdgeColor', 'm')%画起点
plot(E_coo(2),E_coo(1),'o','markersize', 10,'markerfacecolor','g','MarkerEdgeColor', 'c')%画终点
set(gca,'YDir','reverse');%图像翻转
for i=1:num_shange
    plot([0 num_shange],[i-1 i-1],'k-');
    plot([i i],[0 num_shange],'k-');%画网格线
end
for i=2:index1
    Q1=[mod(route_lin(i-1)-1,num_shange)+1-0.5,ceil(route_lin(i-1)/num_shange)-0.5];
    Q2=[mod(route_lin(i)-1,num_shange)+1-0.5,ceil(route_lin(i)/num_shange)-0.5];
    plot([Q1(1),Q2(1)],[Q1(2),Q2(2)],'r','LineWidth',3)
end
title('粒子群算法-最优路线');
 
 
%进化曲线
figure(4);
plot(BestFitness);
xlabel('迭代次数')
ylabel('适应度值')
grid on;
title('进化曲线');
disp('粒子群算法-最优路线方案:')
disp(num2str(route_lin))
disp(['起点到终点的距离:',num2str(BestFitness(end))]);
figure(5);
plot(BestFitness*100);
xlabel('迭代次数')
ylabel('适应度值')
grid on;
title('最佳个体适应度值变化趋势');

Three, running results

Insert picture description here
Insert picture description here

Four, remarks

Complete code or writing to add QQ912100926 past review
>>>>>>
[Path planning] Particle swarm optimization algorithm for three-dimensional UAV path planning [Matlab 012]
[Path planning] Genetic algorithm for open vehicles in multiple logistics centers Path planning [Matlab 013]
[Path planning] Particle swarm algorithm for robot grid path planning [Matlab 014]
[Path planning] Ant colony algorithm for solving the shortest path [Matlab 015]
[Path planning] Immune algorithm for logistics center Site selection [Matlab 016]
[Path planning] Three-dimensional path planning of drones with artificial bee colony [Matlab 017]
[Path planning] Genetic algorithm based on grid map robot path planning [Matlab 018]
[Path planning] Ant Swarm algorithm for multiple drone attack scheduling [Matlab 019]
[Path planning] Genetic algorithm for optimal path planning of robots based on grid map [Matlab 020]
[Path planning] Genetic algorithm for multiple unmanned systems considering the order of distribution Modeling of Cooperative Target Assignment by Machine 【Matlab Issue 021】
[Path Planning]
Multi-center VRP Problem of Ant Colony Algorithm [Matlab 022] [Path Planning] Ant Colony Algorithm Solving Multi-center VRP with Time Window [Matlab 023 Issue]
[ Path planning] Multi-center VRP solution based on genetic algorithm [Matlab 024]
[Path planning] Simulated annealing to solve VRP problem [Matlab 025]
[Path planning] A star grid path planning [Matlab 026]

Guess you like

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