# 基于模拟退火算法的TSP问题matlab实现

• K5_356915
了解作者
• matlab
开发工具
• 3.6KB
文件大小
• zip
文件格式
• 0
收藏次数
• VIP专享
资源类型
• 0
下载次数
• 2022-02-23 10:14
上传日期

• 模拟退火算法
• Insertion.m
392B
• SA.m
2.8KB
• PlotSolution.m
712B
• CreateModel.m
441B
• Reversion.m
382B
• demo.m
826B
• Swamp.m
419B
• PathLength.m
235B

function [sol_best,cost_best] = SA(sol_init,loss,options) %-------------------------------------------------------------------------- def = struct(... 'Verbose' ,0,... 'InitTemp' ,1,... 'StopTemp' ,1e-8,... 'MaxIt_outer' ,1000,... 'MaxIt_inner' ,20,... 'MaxIt_fails' ,10,... 'nCitySwap' ,2,... 'CoolSched' ,@(T) (0.99*T)); if nargin<3 options = def; end if nargin<2 error('not enough inputs!'); end %-------------------------------------------------------------------------- Verbose = options.Verbose; if Verbose==1 if ~isfield(options,'plotFun') error('You need to input a plot function'); end end MaxIt_outer = options.MaxIt_outer; MaxIt_inner = options.MaxIt_inner; MaxIt_fails = options.MaxIt_fails; InitTemp = options.InitTemp; StopTemp = options.StopTemp; CoolSched = options.CoolSched; nCitySwap = options.nCitySwap; plotFun = options.plotFun; %-------------------------------------------------------------------------- T = InitTemp; cost_init = loss(sol_init); sol_old = sol_init; cost_old = cost_init; sol_best = sol_init; cost_best = cost_init; outer = 0; fails = 0; while outer < MaxIt_outer && StopTemp < T && fails < MaxIt_fails inner = 0; while inner <= MaxIt_inner %sol_new = Swamp(sol_old,nCitySwap); sol_new = Reversion(sol_old); %sol_new = Insertion(sol_old); cost_new = loss(sol_new); diff = abs(cost_old - cost_new); if cost_new < cost_old sol_old = sol_new; cost_old = cost_new; %-------------------------------------------------------------- nCitySwap = round(nCitySwap*exp(-diff/(outer*T))); if nCitySwap == 0 nCitySwap = 1; end inner = inner + 1; else if rand(1) < exp(-diff/(T)) sol_old = sol_new; cost_old = cost_new; %---------------------------------------------------------- nCitySwap = round(nCitySwap*exp(-diff/(outer*T))); if nCitySwap == 0 nCitySwap = 1; end inner = inner + 1; end end end if cost_old < cost_best sol_best = sol_old; cost_best = cost_old; fails = 0; else fails = fails + 1; end if Verbose == 1 plotFun(sol_best,cost_best,outer); %plotFun(sol_old,cost_old,outer); pause(0.01) end T = CoolSched(T); outer = outer + 1; end

相关推荐
• matlab 课件
matlab 功能很强大，本课件详细的介绍了matlab在科学计算中的应用。
• Matlab教程
MatlaB经典ppt，主要讲解了MatlaB的入门知识，图形编程以及仿真。
• matlab入门
matlab的入门教程，很值得仔细看看，对提高matlab能力有帮助
• matlab编译器
matlab编译为exe文件所需要的文件； 使用时解压，将解压后文件拖入至matlab命令行即可
• matlab
matlab的基础知识进行介绍
• MatLab
“＃MatLab
• MATLAB编译器
基于MATLAB 2018b版本介绍MATLAB编译器。介绍如何利用编译器将MATLAB代码编译为独立应用程序或组件，并在没有安装MATLAB的计算机上进行部署。
• MATLAB 论文
基于MATLAB的论文撰写！可以为你指明一条写作MATLAB论文的道路！
• matlab
matlab经典课堂讲义
• matlab
matlab