失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 【TSP】基于matlab GUI遗传算法求解旅行商问题【含Matlab源码 1333期】

【TSP】基于matlab GUI遗传算法求解旅行商问题【含Matlab源码 1333期】

时间:2019-11-10 18:50:42

相关推荐

【TSP】基于matlab GUI遗传算法求解旅行商问题【含Matlab源码 1333期】

一、TSP简介

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

TSP的数学模型

二、遗传算法简介

1 引言

2 遗传算法理论

2.1 遗传算法的生物学基础

2.2 遗传算法的理论基础

2.3 遗传算法的基本概念

2.4 标准的遗传算法

2.5 遗传算法的特点

2.6 遗传算法的改进方向

3 遗传算法流程

4 关键参数说明

三、部分源代码

function varargout = tsp_ga_gui(varargin)% TSP_GA_GUI MATLAB code for tsp_ga_gui.fig%% Last Modified by GUIDE v2.5 25-Feb- 15:15:58% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct('gui_Name', mfilename, ...'gui_Singleton', gui_Singleton, ...'gui_OpeningFcn', @tsp_ga_gui_OpeningFcn, ...'gui_OutputFcn', @tsp_ga_gui_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 tsp_ga_gui is made visible.function tsp_ga_gui_OpeningFcn(hObject, eventdata, handles, varargin)global hndsglobal r nn dsm asz Gglobal startf% Choose default command line output for tsp_ga_guihandles.output = hObject;% Update handles structureguidata(hObject, handles);% UIWAIT makes tsp_ga_gui wait for user response (see UIRESUME)% uiwait(handles.figure1);hnds=handles;startf=false; % start flagasz=10; % area size asx x asznn=str2num(get(handles.nn,'string')); % number of citiesps=str2num(get(handles.ps,'string')); % population sizer=asz*rand(2,nn); % randomly distribute cities% r(1,:) -x coordinaties of cities% r(2,:) -y coordinaties of citiesdsm=zeros(nn,nn); % matrix of distanciesfor n1=1:nn-1r1=r(:,n1);for n2=n1+1:nnr2=r(:,n2);dr=r1-r2;dr2=dr'*dr;drl=sqrt(dr2);dsm(n1,n2)=drl;dsm(n2,n1)=drl;endend% start from random closed pathes:G=zeros(ps,nn); % genes, G(i,:) - gene of i-path, G(i,:) is row-vector with cities number in the pathfor psc=1:psG(psc,:)=randperm(nn);endupdate_plots;% --- Outputs from this function are returned to the command line.function varargout = tsp_ga_gui_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 randomize.function randomize_Callback(hObject, eventdata, handles)global r nn dsm asznn=str2num(get(handles.nn,'string')); % number of citiesr=asz*rand(2,nn); % randomly distribute cities% r(1,:) -x coordinaties of cities% r(2,:) -y coordinaties of citiesdsm=zeros(nn,nn); % matrix of distanciesfor n1=1:nn-1r1=r(:,n1);for n2=n1+1:nnr2=r(:,n2);dr=r1-r2;dr2=dr'*dr;drl=sqrt(dr2);dsm(n1,n2)=drl;dsm(n2,n1)=drl;endendupdate_plots;% --- Executes on button press in circle.function circle_Callback(hObject, eventdata, handles)global r nn dsm asznn=str2num(get(handles.nn,'string')); % number of citiesr=zeros(2,nn);% circleal1=linspace(0,2*pi,nn+1);al=al1(1:end-1);r(1,:)=0.5*asz+0.45*asz*cos(al);r(2,:)=0.5*asz+0.45*asz*sin(al);% r(1,:) -x coordinaties of cities% r(2,:) -y coordinaties of citiesdsm=zeros(nn,nn); % matrix of distanciesfor n1=1:nn-1r1=r(:,n1);for n2=n1+1:nnr2=r(:,n2);dr=r1-r2;dr2=dr'*dr;drl=sqrt(dr2);dsm(n1,n2)=drl;dsm(n2,n1)=drl;endendupdate_plots;function nn_Callback(hObject, eventdata, handles)update_plots_nn_ps;% --- Executes during object creation, after setting all properties.function nn_CreateFcn(hObject, eventdata, handles)% hObject handle to nn (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');endfunction ps_Callback(hObject, eventdata, handles)update_plots_nn_ps;% --- Executes during object creation, after setting all properties.function ps_CreateFcn(hObject, eventdata, handles)% hObject handle to ps (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');endfunction ng_Callback(hObject, eventdata, handles)% hObject handle to ng (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 ng as text% str2double(get(hObject,'String')) returns contents of ng as a double% --- Executes during object creation, after setting all properties.function ng_CreateFcn(hObject, eventdata, handles)% hObject handle to ng (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

四、运行结果

五、matlab版本及参考文献

1 matlab版本

a

2 参考文献

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

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

如果觉得《【TSP】基于matlab GUI遗传算法求解旅行商问题【含Matlab源码 1333期】》对你有帮助,请点赞、收藏,并留下你的观点哦!

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