1 算法介绍
1.1 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.
本文介绍了一种基于混沌粒子群算法解决旅行商问题(TSP)的方法,并通过具体实例展示了算法流程与仿真结果。该算法适用于多种规模的TSP问题,能够有效寻找到较优解。
158

被折叠的 条评论
为什么被折叠?



