【TSP】混合粒子群求解TSP问题【Matlab 288期】

一、简介

1 算法
1.1 原理
在这里插入图片描述
在这里插入图片描述
1.2 性能比较
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
1.3 步骤
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

二、源代码

function varargout = PSO(varargin)
% PSO M-file for PSO.fig
%      PSO, by itself, creates a new PSO or raises the existing
%      singleton*.
%
%      H = PSO returns the handle to a new PSO or the handle to
%      the existing singleton*.
%
%      PSO('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in PSO.M with the given input arguments.
%
%      PSO('Property','Value',...) creates a new PSO or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before PSO_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to PSO_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help PSO

% Last Modified by GUIDE v2.5 12-Jun-2009 22:11:08

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @PSO_OpeningFcn, ...
                   'gui_OutputFcn',  @PSO_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{
    
    1})
    gui_State.gui_Callback = str2func(varargin{
    
    1});
end

if nargout
    [varargout{
    
    1:nargout}] = gui_mainfcn(gui_State, varargin{
    
    :});
else
    gui_mainfcn(gui_State, varargin{
    
    :});
end
% End initialization code - DO NOT EDIT


% --- Executes just before PSO is made visible.
function PSO_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to PSO (see VARARGIN)

% Choose default command line output for PSO
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes PSO wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = PSO_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{
    
    1} = handles.output;


% --- Executes on button press in run.
function run_Callback(hObject, eventdata, handles)
% hObject    handle to run (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
TSP_type = get(findobj('tag','tsp'),'Value');
    switch TSP_type
        case 1
            data=load('burma14.txt');
        case 2
            data=load('ulysses22.txt');
        case 3
            data=load('bayg29.txt');
        case 4
            data=load('Oliver30.txt');
        case 5
            data=load('eil51.txt');
        case 6
            data=load('st70.txt');
        case 7
            data=load('pr76.txt');
        case 8
            data=load('gr96.txt');
        case 9
            data=load('ch130.txt');
        case 10
            data=load('ch150.txt');
        case 11
            data=load('pr226.txt');    
    end
a=data(:,2);
b=data(:,3);
C=[a b];                %城市坐标矩阵
n=size(C,1);            %城市数目
D=zeros(n,n);           %城市距离矩阵
%L_best=ones(Nmax,1);
for i=1:n
    for j=1:n
        if i~=j
            D(i,j)=((C(i,1)-C(j,1))^2+(C(i,2)-C(j,2))^2)^0.5;                    
        end
        D(j,i)=D(i,j); 
     end
end
Nmax=str2double(get(findobj('tag','N_max'),'string'));
m=str2double(get(findobj('tag','m'),'string'));

algo_type = get(findobj('tag','algo'),'Value');

switch algo_type
        case 1
%% 初始化所有粒子
for i=1:m
    x(i,:)=randperm(n);  %粒子位置
end
F=fitness(x,C,D);         %计算种群适应度 
%xuhao=xulie(F)           %最小适应度种群序号
a1=F(1);
a2=1;
for i=1:m
    if a1>=F(i)
        a1=F(i);
        a2=i;
    end
end
xuhao=a2;
Tour_pbest=x;            %当前个体最优
Tour_gbest=x(xuhao,:) ;  %当前全局最优路径
Pb=inf*ones(1,m);        %个体最优记录
Gb=F(a2);         %群体最优记录
xnew1=x;
N=1;
while N<=Nmax
    %计算适应度 
    F=fitness(x,C,D);
    for i=1:m
        if F(i)<Pb(i)
            Pb(i)=F(i);      %将当前值赋给新的最佳值
            Tour_pbest(i,:)=x(i,:);%将当前路径赋给个体最优路径
        end
        if F(i)<Gb
            Gb=F(i);
            Tour_gbest=x(i,:);
        end
    end
%  nummin=xulie(Pb)           %最小适应度种群序号
    a1=Pb(1);
    a2=1;
    for i=1:m
        if a1>=Pb(i)
            a1=Pb(i);
            a2=i;
        end
    end
    nummin=a2;
    Gb(N)=Pb(nummin);          %当前群体最优长度
    for i=1:m
      %% 与个体最优进行交叉
      c1=round(rand*(n-2))+1;  %[1,n-1]范围内随机产生一个交叉位
      c2=round(rand*(n-2))+1;
      while c1==c2
          c1=round(rand*(n-2))+1;  %[1,n-1]范围内随机产生一个交叉位
          c2=round(rand*(n-2))+1;
      end   
      chb1=min(c1,c2);
      chb2=max(c1,c2);
      cros=Tour_pbest(i,chb1:chb2); %交叉区域矩阵
      ncros=size(cros,2);       %交叉区域元素个数
      %删除与交叉区域相同元素
      for j=1:ncros
          for k=1:n
              if xnew1(i,k)==cros(j)
                 xnew1(i,k)=0;
                  for t=1:n-k
                      temp=xnew1(i,k+t-1);
                      xnew1(i,k+t-1)=xnew1(i,k+t);
                      xnew1(i,k+t)=temp;
                  end                 
              end
          end
      end
      xnew=xnew1;
      %插入交叉区域
      for j=1:ncros
          xnew1(i,n-ncros+j)=cros(j);
      end
      %判断产生新路径长度是否变短
      dist=0;
      for j=1:n-1
          dist=dist+D(xnew1(i,j),xnew1(i,j+1));
      end
      dist=dist+D(xnew1(i,1),xnew1(i,n));
      if F(i)>dist
          x(i,:)=xnew1(i,:);
      end
      %% 与全体最优进行交叉
      c1=round(rand*(n-2))+1;  %[1,n-1]范围内随机产生一个交叉位
      c2=round(rand*(n-2))+1;
      while c1==c2
          c1=round(rand*(n-2))+1;  %[1,n-1]范围内随机产生一个交叉位
          c2=round(rand*(n-2))+1;
      end   
      chb1=min(c1,c2);
      chb2=max(c1,c2);
      cros=Tour_gbest(chb1:chb2); %交叉区域矩阵
      ncros=size(cros,2);       %交叉区域元素个数
      %删除与交叉区域相同元素
      for j=1:ncros
          for k=1:n
              if xnew1(i,k)==cros(j)
                 xnew1(i,k)=0;
                  for t=1:n-k
                      temp=xnew1(i,k+t-1);
                      xnew1(i,k+t-1)=xnew1(i,k+t);
                      xnew1(i,k+t)=temp;
                  end                 
              end
          end
      end
      xnew=xnew1;
      %插入交叉区域
      for j=1:ncros
          xnew1(i,n-ncros+j)=cros(j);
      end
      %判断产生新路径长度是否变短
      dist=0;
      for j=1:n-1
          dist=dist+D(xnew1(i,j),xnew1(i,j+1));
      end
      dist=dist+D(xnew1(i,1),xnew1(i,n));
      if F(i)>dist
          x(i,:)=xnew1(i,:);
      end
      %% 进行变异操作
      c1=round(rand*(n-1))+1;   %[1,n]范围内随机产生一个变异位
      c2=round(rand*(n-1))+1;
      temp=xnew1(i,c1);
      xnew1(i,c1)=xnew1(i,c2);
      xnew1(i,c2)=temp;
       %判断产生新路径长度是否变短
      dist=0;
      for j=1:n-1
          dist=dist+D(xnew1(i,j),xnew1(i,j+1));
      end
      dist=dist+D(xnew1(i,1),xnew1(i,n));
      %dist=dist(xnew1(i,:),D);
      if F(i)>dist
          x(i,:)=xnew1(i,:);
      end
    end
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %  F=(x,C,D)         %计算种群适应度 
    %xuhao=xulie(F)           %最小适应度种群序号
    a1=F(1);
    a2=1;
    for i=1:m
       if a1>=F(i)
            a1=F(i);
            a2=i;
        end
    end
    xuhao=a2;
    L_best(N)=min(F);
    Tour_gbest=x(xuhao,:);     %当前全局最优路径
    N=N+1;
    axes(handles.city)         %城市路径状态
    scatter(C(:,1),C(:,2));
    hold on
    plot([C(Tour_gbest(1),1),C(Tour_gbest(n),1)],[C(Tour_gbest(1),2),C(Tour_gbest(n),2)],'ms-','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor','g')
    for ii=2:n
    plot([C(Tour_gbest(ii-1),1),C(Tour_gbest(ii),1)],[C(Tour_gbest(ii-1),2),C(Tour_gbest(ii),2)],'ms-','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor','g')
    end
    hold off
    axes(handles.shoulian)         %收敛曲线
    plot(L_best);
    set(findobj('tag','N'),'string',num2str(N-1));%当前迭代次数
    set(findobj('tag','tour'),'string',num2str(Tour_gbest));%当前最优路径
    set(findobj('tag','L'),'string',num2str(min(L_best)));%当前最优路径长度       %%%这里的L_best是当前最优路径???
    
end

三、运行结果

在这里插入图片描述

四、备注

完整代码或者代写添加QQ912100926
往期回顾>>>>>>
【路径规划】粒子群优化算法之三维无人机路径规划【Matlab 012期】
【路径规划】遗传算法之多物流中心的开放式车辆路径规划【Matlab 013期】
【路径规划】粒子群算法之机器人栅格路径规划【Matlab 014期】
【路径规划】蚁群算法之求解最短路径【Matlab 015期】
【路径规划】免疫算法之物流中心选址【Matlab 016期】
【路径规划】人工蜂群之无人机三维路径规划【Matlab 017期】
【路径规划】遗传算法之基于栅格地图机器人路径规划【Matlab 018期】
【路径规划】蚁群算法之多无人机攻击调度【Matlab 019期】
【路径规划】遗传算法之基于栅格地图的机器人最优路径规划【Matlab 020期】
【路径规划】遗传算法之考虑分配次序的多无人机协同目标分配建模【Matlab 021期】
【路径规划】蚁群算法之多中心vrp问题【Matlab 022期】
【路径规划】蚁群算法之求解带时间窗的多中心VRP【Matlab 023期】
【路径规划】遗传算法之多中心VRP求解【Matlab 024期】
【路径规划】模拟退火之求解VRP问题【Matlab 025期】
【路径规划】A星之栅格路径规划【Matlab 026期】
【路径规划】基于一种带交叉因子的双向寻优粒子群栅格地图路径规划【Matlab 027期】
【路径规划】【TSP】蚁群算法之求解TSP问题含GUI【Matlab 028期】
【路径规划】蚁群算法之栅格地图路径规划【Matlab 029期】
【路径规划】遗传算法之旅行商 TSP 【Matlab 030期】
【路径规划】模拟退火算法之旅行商 TSP 问题【Matlab 031期】
【路径规划】蚁群算法之智能车路径规划【Matlab 032期】
【路径规划】华为杯:基于matlab 无人机优化运用于抢险救灾【Matlab 033期】
【路径规划】matlab之最小费用最大流算问题【Matlab 034期】
【路径规划】A算法之解决三维路径规划问题【Matlab 035期】
【路径规划】人工蜂群算法之路径规划【Matlab036期】
【路径规划】人工蜂群算法之路径规划【Matlab 037期】
【路径规划】蚁群算法之求解多旅行商MTSP问题【Matlab 038期】
【路径规划】蚁群算法之无人机路径规划【Matlab 039期】
【路径规划】遗传算法之求解多VRP问题【Matlab 040期】
【VRP】遗传算法之带时间窗的车辆路径问题【Matlab 041期】
【路径规划】蚁群算法之三维路径规划【Matlab 042期】
【路径规划】粒子群优化蚁群之求解最短路径【Matlab 043期】
【TSP问题】差分进化之求解TSP问题【Matlab 044期】
【路径规划】RRT之三维路径规划【Matlab 144期】
【路径规划】人工势场算法之无人机编队路径规划【 Matlab 145期】
【VRP问题】节约算法之求解TWVRP问题【Matlab 146期】
【VRP问题】节约算法之求解CVRP问题【Matalb 147期】
【VRP问题】禁忌搜索算法之求解VRP问题【Matalb 148期】
【VRP问题】模拟退火算法之求解CVRP问题【Matlab 149期】
【VRP问题】模拟退火求解带时间窗之TWVRP问题【Matlab 150期】
【VRP问题】人工鱼群算法之求解带时间窗VRP问题【Matlab 151期】
【VRP问题】遗传算法之求解带容量VRP问题【Matlab 152期】
【路径规划】狼群算法算法之三维路径规划【Matlab 153期】
【路径规划】人工势场算法之无人机三维路径规划【Matlab 154期】
【路径规划】改进差分算法之三维多无人机协同航迹规划【Matlab 155期】
【路径规划】人工蜂群算法之多无人机三维路径规划【Matlab 156期】
【路径规划】麻雀搜索算法之无人机三维路径规划【Matlab 157期】
【路径规划】蚁群算法之三维路径规划【Matlab 158期】
【路径规划】免疫算法之最短路径规划【Matlab 159期】
【旅行商问题】免疫算法之求解旅行商问题【Matlab 160期】
【路径规划】遗传算法的公交排班系统分析【Matlab 161期】
【TSP】粒子群算法Hopfield之TSP求解【Matlab 162期】
【路径规划】A和改进A*的路径规划【Matlab 163期】
【TSP】改进的禁忌搜索算法之求解旅行商问题【Matlab 170期】
【TSP】改进的蚁群算法之求解旅行商问题【Matlab 171期】
【路径规划】模拟退火算法之求解火灾巡逻最短路径【Matlab 193期】
【三维路径规划】蚁群算法寻优潜水器的三维路径【Matlab 194期】
【三维路径规划】matlab 蚁群算法UAV巡检路径【Matlab 195期】
【三维路径规划】无人机的三维动态仿真【Matlab 196期】
【三维路径规划】无人机三维空间的航迹规划【Matlab 228期】
【路径规划】分布式目标检测和跟踪的多无人机【Matlab 229期】
【路径规划】粒子群算法求解无人机最短路径【Matlab 277期】
【无人机】多无人协同任务分配程序平台【Matlab278期】
【路径规划】多无人机协同任务规划【Matlab 279期】
【路径规划】任意架次植保无人机作业路径规划【Matlab 280期】
【路径规划】粒子群遗传求解多无人机三维路径规划【Matlab 281期】
【VRP问题】粒子群求解VRPTW模型【Matlab 282期】
【路径规划】改进蚁群算法的路径规划【Matlab 283期】
【VRP】改进的模拟退火和遗传算法求解VRP问题【Matlab 284期】
【VRP问题】灰狼算法求解VRPTW问题【Matlab 285期】
【VRP问题】遗传算法和模拟退火求解带时间窗的自行车调度问题【Matlab 286期】
【路径规划】改进的人工势场法机器人动静态避障【Matlab 287期】

猜你喜欢

转载自blog.csdn.net/m0_54742769/article/details/114335647