600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > 【TSP】基于matlab混合粒子群算法求解旅行商问题【含Matlab源码 397期】

【TSP】基于matlab混合粒子群算法求解旅行商问题【含Matlab源码 397期】

时间:2021-03-28 08:20:48

相关推荐

【TSP】基于matlab混合粒子群算法求解旅行商问题【含Matlab源码 397期】

一、TSP简介

旅行商问题,即TSP问题(Traveling Salesman Problem)又译为旅行推销员问题、货郎担问题,是数学领域中著名问题之一。假设有一个旅行商人要拜访n个城市,他必须选择所要走的路径,路径的限制是每个城市只能拜访一次,而且最后要回到原来出发的城市。路径的选择目标是要求得的路径路程为所有路径之中的最小值。

TSP的数学模型

二、粒子群算法简介

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- 22:11:08% Begin initialization code - DO NOT EDITgui_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});endif nargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});elsegui_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 PSOhandles.output = hObject;% Update handles structureguidata(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 structurevarargout{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_typecase 1data=load('burma14.txt');case 2data=load('ulysses22.txt');case 3data=load('bayg29.txt');case 4data=load('Oliver30.txt');case 5data=load('eil51.txt');case 6data=load('st70.txt');case 7data=load('pr76.txt');case 8data=load('gr96.txt');case 9data=load('ch130.txt');case 10data=load('ch150.txt');case 11data=load('pr226.txt'); enda=data(:,2);b=data(:,3);C=[a b];%城市坐标矩阵n=size(C,1); %城市数目D=zeros(n,n); %城市距离矩阵%L_best=ones(Nmax,1);for i=1:nfor j=1:nif i~=jD(i,j)=((C(i,1)-C(j,1))^2+(C(i,2)-C(j,2))^2)^0.5;endD(j,i)=D(i,j); endendNmax=str2double(get(findobj('tag','N_max'),'string'));m=str2double(get(findobj('tag','m'),'string'));algo_type = get(findobj('tag','algo'),'Value');switch algo_typecase 1%% 初始化所有粒子for i=1:mx(i,:)=randperm(n); %粒子位置endF=fitness(x,C,D); %计算种群适应度 %xuhao=xulie(F) %最小适应度种群序号a1=F(1);a2=1;for i=1:mif a1>=F(i)a1=F(i);a2=i;endendxuhao=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:mif F(i)<Pb(i)Pb(i)=F(i);%将当前值赋给新的最佳值Tour_pbest(i,:)=x(i,:);%将当前路径赋给个体最优路径endif F(i)<GbGb=F(i);Tour_gbest=x(i,:);endend% nummin=xulie(Pb) %最小适应度种群序号a1=Pb(1);a2=1;for i=1:mif a1>=Pb(i)a1=Pb(i);a2=i;endendnummin=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==c2c1=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:ncrosfor k=1:nif xnew1(i,k)==cros(j)xnew1(i,k)=0;for t=1:n-ktemp=xnew1(i,k+t-1);xnew1(i,k+t-1)=xnew1(i,k+t);xnew1(i,k+t)=temp;end endendendxnew=xnew1;%插入交叉区域for j=1:ncrosxnew1(i,n-ncros+j)=cros(j);end%判断产生新路径长度是否变短dist=0;for j=1:n-1dist=dist+D(xnew1(i,j),xnew1(i,j+1));enddist=dist+D(xnew1(i,1),xnew1(i,n));if F(i)>distx(i,:)=xnew1(i,:);end%% 与全体最优进行交叉c1=round(rand*(n-2))+1; %在[1,n-1]范围内随机产生一个交叉位c2=round(rand*(n-2))+1;while c1==c2c1=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:ncrosfor k=1:nif xnew1(i,k)==cros(j)xnew1(i,k)=0;for t=1:n-ktemp=xnew1(i,k+t-1);xnew1(i,k+t-1)=xnew1(i,k+t);xnew1(i,k+t)=temp;end endendendxnew=xnew1;%插入交叉区域for j=1:ncrosxnew1(i,n-ncros+j)=cros(j);end%判断产生新路径长度是否变短dist=0;for j=1:n-1dist=dist+D(xnew1(i,j),xnew1(i,j+1));enddist=dist+D(xnew1(i,1),xnew1(i,n));if F(i)>distx(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-1dist=dist+D(xnew1(i,j),xnew1(i,j+1));enddist=dist+D(xnew1(i,1),xnew1(i,n));%dist=dist(xnew1(i,:),D);if F(i)>distx(i,:)=xnew1(i,:);endend%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% F=(x,C,D) %计算种群适应度 %xuhao=xulie(F) %最小适应度种群序号a1=F(1);a2=1;for i=1:mif a1>=F(i)a1=F(i);a2=i;endendxuhao=a2;L_best(N)=min(F);Tour_gbest=x(xuhao,:);%当前全局最优路径N=N+1;axes(handles.city) %城市路径状态scatter(C(:,1),C(:,2));hold onplot([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:nplot([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')endhold offaxes(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

四、运行结果

五、matlab版本及参考文献

1 matlab版本

a

2 参考文献

[1] 包子阳,余继周,杨杉.智能优化算法及其MATLAB实例(第2版)[M].电子工业出版社,.

[2]张岩,吴水根.MATLAB优化算法源代码[M].清华大学出版社,.

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。