【TSP问题】基于混沌粒子群算法求解旅行商问题matlab源码

1 算法介绍

1.1 TSP介绍

“旅行商问题”(Traveling Salesman Problem,TSP)可简单描述为:一位销售商从n个城市中的某一城市出发,不重复地走完其余n-1个城市并回到原出发点,在所有可能路径中求出路径长度最短的一条。

旅行商的路线可以看作是对n城市所设计的一个环形,或者是对一列n个城市的排列。由于对n个城市所有可能的遍历数目可达(n-1)!个,因此解决这个问题需要O(n!)的计算时间。而由美国密执根大学的Holland教授发展起来的遗传算法,是一种求解问题的高效并行全局搜索方法,能够解决复杂的全局优化问题,解决TSP问题也成为遗传算法界的一个目标。

1.2 混沌粒子群算法

 

 

 

2 部分代码

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-2020 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);
​
                 xnew1(i,k)=0;
                  for t=1:n-k
     
% Hints: get(hObject,'String') returns contents of m as text
%        str2double(get(hObject,'String')) returns contents of m as a double
​
​
% --- Executes during object creation, after setting all properties.
function m_CreateFcn(hObject, eventdata, handles)
% hObject    handle to m (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
​
% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
​
​
​
function N_max_Callback(hObject, eventdata, handles)
% hObject    handle to N_max (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
​
% Hints: get(hObject,'String') returns contents of N_max as text
%        str2double(get(hObject,'String')) returns contents of N_max as a double
​
​
% --- Executes during object creation, after setting all properties.
function N_max_CreateFcn(hObject, eventdata, handles)
% hObject    handle to N_max (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
​
% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
​
​
% --- Executes on selection change in algo.
function algo_Callback(hObject, eventdata, handles)
% hObject    handle to algo (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
​
% Hints: contents = get(hObject,'String') returns algo contents as cell array
%        contents{get(hObject,'Value')} returns selected item from algo
​
​
% --- Executes during object creation, after setting all properties.
function algo_CreateFcn(hObject, eventdata, handles)
% hObject    handle to algo (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
​
% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
​
​
% --- Executes on selection change in algo.
function tsp_Callback(hObject, eventdata, handles)
% hObject    handle to algo (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
​
% Hints: contents = get(hObject,'String') returns algo contents as cell array
%        contents{get(hObject,'Value')} returns selected item from algo
​
​
% --- Executes during object creation, after setting all properties.
function tsp_CreateFcn(hObject, eventdata, handles)
% hObject    handle to algo (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
​
% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
​
​
​
​
​
function N_Callback(hObject, eventdata, handles)
% hObject    handle to N (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
​
% Hints: get(hObject,'String') returns contents of N as text
%        str2double(get(hObject,'String')) returns contents of N as a double
​
​
% --- Executes during object creation, after setting all properties.
function N_CreateFcn(hObject, eventdata, handles)
% hObject    handle to N (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
​
% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
​
​
​
​
​
function Nbest_Callback(hObject, eventdata, handles)
% hObject    handle to Nbest (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
​
% Hints: get(hObject,'String') returns contents of Nbest as text
%        str2double(get(hObject,'String')) returns contents of Nbest as a double
​
​
% --- Executes during object creation, after setting all properties.
function Nbest_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Nbest (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
​
% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
​
​
​
function tour_Callback(hObject, eventdata, handles)
% hObject    handle to tour (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
​
% Hints: get(hObject,'String') returns contents of tour as text
%        str2double(get(hObject,'String')) returns contents of tour as a double
​
​
% --- Executes during object creation, after setting all properties.
function tour_CreateFcn(hObject, eventdata, handles)
% hObject    handle to tour (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
​
% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
​
​
​
function L_Callback(hObject, eventdata, handles)
% hObject    handle to L (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
​
% Hints: get(hObject,'String') returns contents of L as text
%        str2double(get(hObject,'String')) returns contents of L as a double
​
​
% --- Executes during object creation, after setting all properties.
function L_CreateFcn(hObject, eventdata, handles)
% hObject    handle to L (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
​
% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
​
​
​
​
% --- Executes on selection change in tsp.
function ts_Callback(hObject, eventdata, handles)
% hObject    handle to tsp (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
​
% Hints: contents = get(hObject,'String') returns tsp contents as cell array
%        contents{get(hObject,'Value')} returns selected item from tsp
​
​
% --- Executes during object creation, after setting all properties.
function ts_CreateFcn(hObject, eventdata, handles)
% hObject    handle to tsp (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
​
% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
​
​
​

3 仿真结果

4 参考文献

[1]沈继红, 王侃. 求解旅行商问题的混合粒子群优化算法[J]. 智能系统学报, 2012(02):84-92.

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
粒子群算法是一种常用的优化算法,可以用于求解旅行商问题。在MATLAB中,可以使用粒子群算法工具箱进行旅行商问题求解。这个工具箱提供了一些函数和算法,可以帮助用户实现粒子群算法。例如,可以使用"psotsp"函数来求解旅行商问题。该函数需要输入旅行商问题的距离矩阵,然后返回求解的最优路径和最短距离。 参考文献中提到了一种改进的粒子群算法用于求解旅行商问题。这篇文章介绍了该算法的具体实现细节,并给出了实验结果和分析。这可以作为一个参考,帮助理解粒子群算法旅行商问题上的应用。 此外,参考文献中的课件内容也涵盖了MATLAB在数学建模和优化求解方面的应用。其中介绍了使用MATLAB进行数学建模的方法,并给出了优化求解的示例。这些内容可以帮助进一步理解如何使用MATLAB进行旅行商问题求解。 总结来说,粒子群算法可以在MATLAB中用来求解旅行商问题,可以使用"psotsp"函数来实现。参考文献中提供了一种改进的粒子群算法用于求解旅行商问题的具体实现。参考文献中的课件内容也包含了MATLAB在数学建模和优化求解方面的应用,可以提供更多的帮助和参考。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [【PSO TSP】基于matlab粒子群算法Hopfield求解旅行商问题【含Matlab源码 224期】](https://blog.csdn.net/TIQCmatlab/article/details/113693450)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [Matlab基础应用学习笔记.md](https://download.csdn.net/download/weixin_52057528/88284511)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Matlab科研辅导帮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值