【路径规划】基于改进模拟退火算法求解TSP问题matlab代码

1 简介

旅行商问题(TSP)是一种经典路径优化选择问题,可以通过暴力枚举,分支定界,动态规划,爬山算法等方法解决该问题,这些方法各有利弊.基于此,笔者对模拟退火算法进行改进处理,一是对扰动过程设置随机接受概率从而跳出局部最优解陷阱,二是设置循环阈值以较少的时空消耗获得一个最优解或者极其接近最优解的满意解.笔者使用Matlab软件进行仿真,结果表明该算法较好地解决了TSP问题.

2 部分代码

%% 清除环境变量
clear;
clc;

format long;
%% 问题准备
% 产生问题模型
FileName = 'eil76.txt';
model = CreateModel(FileName);    
% 城市分布图
figure;
plot(model.x, model.y, 'ms', 'LineWidth', 2, 'MarkerEdgeColor', 'k', 'MarkerFaceColor', 'g')
legend('城市位置')
title('城市分布图', 'fontsize', 12)
xlabel('x', 'fontsize', 12)
ylabel('y', 'fontsize', 12)
grid on
% 标记城市序号
for i = 1:model.n
   text(model.x(i), model.y(i), ['   ' num2str(model.id(i))]);
end
N = 30;                                 % 种群规模
Max_iteration = 1000;           % 最大迭代次数
cnt_max = 10;

Curve_SA = zeros(1, Max_iteration);
Curve_ISA = zeros(1, Max_iteration);

for cnt = 1:cnt_max
   disp(['第', num2str(cnt), '次迭代']);
   % 初始化种群位置
   for i = 1:N
       A(i).position = randperm(model.n);
       A(i).cost = fitnessFunction([A(i).position, A(i).position(1)], model);
   end
   T0 = max([A.cost]);     % T0 = max([C.cost])-min([C.cost]);
   T = T0;
   temp = A;
   
  [SA_Best_score(cnt), SA_Best_pos(cnt, :), SA_Curve] = SA(N, Max_iteration, A, temp, T, model);
  [ISA_Best_score(cnt), ISA_Best_pos(cnt, :), ISA_Curve] = ISA(N, Max_iteration, A, temp, T, model);
    
   Curve_SA = Curve_SA+SA_Curve;
   Curve_ISA = Curve_ISA+ISA_Curve;

end

Curve_SA = Curve_SA/cnt_max;
Curve_ISA = Curve_ISA/cnt_max;


std_SA = std(SA_Best_score);
std_ISA = std(ISA_Best_score);

[best_SA, index_SA] = min(SA_Best_score);
[best_ISA, index_ISA] = min(ISA_Best_score);

worst_SA = max(SA_Best_score);
worst_ISA = max(ISA_Best_score);

mean_SA = mean(SA_Best_score);
mean_ISA = mean(ISA_Best_score);

%% 画图
figure;
t = 1:Max_iteration;
plot(t, Curve_SA, 'k^-', t, Curve_ISA, 'rd-', ...
   'linewidth', 1.5, 'MarkerSize', 8, 'MarkerIndices', 1:100:Max_iteration);
xlabel('Iteration');
ylabel('Fitness');
axis fill
grid on
box on
legend('SA', '改进SA');
% SA最优轨迹图
figure;
PlotSolution(SA_Best_pos(index_SA, :), model, best_SA);
title(['传统SA优化路径(最短距离:' num2str(best_SA) ')'])
% ISA最优轨迹图
figure;
PlotSolution(ISA_Best_pos(index_ISA, :), model, best_ISA);
title(['改进SA优化路径(最短距离:' num2str(best_ISA) ')'])
%% 显示结果
disp(['数据集:', num2str(FileName)]);
disp(['SA:最优值: ', num2str(best_SA), ',最差值:', num2str(worst_SA), ',平均值:', num2str(mean_SA), ',标准差:', num2str(std_SA)]);
disp(['改进SA:最优值: ', num2str(best_ISA), ',最差值:', num2str(worst_ISA), ',平均值:', num2str(mean_ISA), ',标准差:', num2str(std_ISA)]);

3 仿真结果

4 参考文献

[1]齐安智. "一种基于改进模拟退火算法的TSP问题的应用研究." 信息与电脑 32.3(2020):3.

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Matlab科研辅导帮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值