【啃书】《智能优化算法及其MATLAB实例》例7.3模拟退火算法求解TSP问题

问题描述

在这里插入图片描述

仿真过程

在这里插入图片描述

matlab源码

%该脚本要命名为func3.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%计算路线总长度%%%%%%%%%%%%%%%%%%%%%%%%
function len=func3(city,n)
len=0;
for i=1:n-1
    len=len+sqrt((city(i).x-city(i+1).x)^2+(city(i).y-city(i+1).y)^2);
end
len=len+sqrt((city(n).x-city(1).x)^2+(city(n).y-city(1).y)^2);
%20201017lu注:该matlab代码成功在matlabR2019a运行
%%%%%%%%%%%%%%%%%%%%%%模拟退火算法解决TSP问题%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%初始化%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all;                      %清除所有变量
close all;                      %清图
clc;                            %清屏
% C=[6734	1453
% 2233	10
% 5530	1424
% 401	841
% 3082	1644
% 7608	4458
% 7573	3716
% 7265	1268
% 6898	1885
% 1112	2049
% 5468	2606
% 5989	2873
% 4706	2674
% 4612	2035
% 6347	2683
% 6107	669
% 7611	5184
% 7462	3590
% 7732	4723
% 5900	3561
% 4483	3369
% 6101	1110
% 5199	2182
% 1633	2809
% 4307	2322
% 675	1006
% 7555	4819
% 7541	3981
% 3177	756
% 7352	4506
% 7545	2801
% 3245	3305
% 6426	3173
% 4608	1198
% 23	2216
% 7248	3779
% 7762	4595
% 7392	2244
% 3484	2829
% 6271	2135
% 4985	140
% 1916	1569
% 7280	4899
% 7509	3239
% 10	2676
% 6807	2993
% 5185	3258
% 3023	1942];
C=[1304 2312
    3639 1315
    4177 2244
    3712 1399
    3488 1535
    3326 1556
    3238 1229
    4196 1004
    4312 790
    4386 570
    3007 1970
    2562 1756
    2788 1491
    2381 1676
    1332 695
    3715 1678
    3918 2179
    4061 2370
    3780 2212
    3676 2578
    4029 2838
    4263 2931
    3429 1908
    3507 2376
    3394 2643
    3439 3201
    2935 3240
    3140 3550
    2545 2357
    2778 2826
    2370 2975];

n=size(C,1);                     %TSP问题的规模,即城市数目
T=100*n;                         %初始温度
L=100;                           %马可夫链长度
K=0.99;                          %衰减参数
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%城市坐标结构体%%%%%%%%%%%%%%%%%%%%%%%%%%
city=struct([]);
for i=1:n
    city(i).x=C(i,1);
    city(i).y=C(i,2);
end
l=1;                             %统计迭代次数
len(l)=func3(city,n);            %每次迭代后的路线长度
figure(1); 
while T>0.001                    %停止迭代温度
    %%%%%%%%%%%%%%%%多次迭代扰动,温度降低之前多次实验%%%%%%%%%%%%%%%
    for i=1:L            
        %%%%%%%%%%%%%%%%%%%计算原路线总距离%%%%%%%%%%%%%%%%%%%%%%%%%
        len1=func3(city,n);         
        %%%%%%%%%%%%%%%%%%%%%%%%%产生随机扰动%%%%%%%%%%%%%%%%%%%%%%%
        %%%%%%%%%%%%%%%%随机置换两个不同的城市的坐标%%%%%%%%%%%%%%%%%
        p1=floor(1+n*rand());
        p2=floor(1+n*rand());
        while p1==p2
            p1=floor(1+n*rand());
            p2=floor(1+n*rand());
        end
        tmp_city=city;
        tmp=tmp_city(p1);
        tmp_city(p1)=tmp_city(p2);
        tmp_city(p2)=tmp;
        %%%%%%%%%%%%%%%%%%%%%%%%计算新路线总距离%%%%%%%%%%%%%%%%%%%%
        len2=func3(tmp_city,n);     
        %%%%%%%%%%%%%%%%%%新老距离的差值,相当于能量%%%%%%%%%%%%%%%%%
        delta_e=len2-len1;
        %%%%%%%%%%%%新路线好于旧路线,用新路线代替旧路线%%%%%%%%%%%%%%  
        if delta_e<0        
            city=tmp_city;
        else
            %%%%%%%%%%%%%%%%%%以概率选择是否接受新解%%%%%%%%%%%%%%%%%
            if exp(-delta_e/T)>rand()
                city=tmp_city;      
            end
        end
    end
    l=l+1;
    %%%%%%%%%%%%%%%%%%%%%%%%%计算新路线距离%%%%%%%%%%%%%%%%%%%%%%%%%%
    len(l)=func3(city,n); 
    %%%%%%%%%%%%%%%%%%%%%%%%%%%温度不断下降%%%%%%%%%%%%%%%%%%%%%%%%%%
    T=T*K;   
    for i=1:n-1
        plot([city(i).x,city(i+1).x],[city(i).y,city(i+1).y],'bo-');
        hold on;
    end
    plot([city(n).x,city(1).x],[city(n).y,city(1).y],'ro-');
    title(['优化最短距离:',num2str(len(l))]);
    hold off;
    pause(0.005);
end
figure(2);
plot(len)
xlabel('迭代次数')
ylabel('目标函数值')
title('适应度进化曲线')

在这里插入图片描述
在这里插入图片描述

中 智能优化算法及其MATLAB实例(第二版)[包子阳,余继周][电子工业出版社][2018年01月][9787121330308]

经过调试,随书所有代码均可以在matlabR2019a上成功运行https://mianbaoduo.com/o/bread/YZyVlp9v

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
func3是一个通过对积分进行嵌套积分得到的函数。在给定的代码中,首先定义了一个名为func2的函数,这个函数是对func1在0到x之间进行积分得到的结果。然后,又定义了一个名为func3的函数,这个函数是对func2在0到x之间进行积分得到的结果。最后,使用fplot函数func1、func2和func3在区间[-10, 10]上绘制出来。根据代码中的注释,绘制的图形应该是保持坐标轴比一致的。如果你想查看完整的代码并在MATLAB R2019a上运行,可以访问https://mianbaoduo.com/o/bread/YZyVlp9v。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [matlab匿名函数实现含参变量的对分段函数的不定积分及绘图](https://blog.csdn.net/weixin_43699700/article/details/105212739)[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_2"}}] [.reference_item style="max-width: 50%"] - *3* [【啃书】《智能优化算法及其MATLAB实例7.3模拟退火算法求解TSP问题](https://blog.csdn.net/weixin_44331401/article/details/109175759)[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_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值