模拟退火算法的TSP问题求解
模拟退火算法来源于固体退火原理,将固体加温至充分高,再让其徐徐冷却,加温时,固体内部粒子随温升变为无序状,内能增大,而徐徐冷却时粒子渐趋有序,在每个温度都达到平衡态,最后在常温时达到基态,内能减为最小。模拟退火算法(Simulated Annealing,SA)最早的思想是由N. Metropolis[1]等人于1953年提出。1983 年,S. Kirkpatrick 等成功地将退火思想引入到组合优化领域。它是基于Monte-Carlo迭代求解策略的一种随机寻优算法,其出发点是基于物理中固体物质的退火过程与一般组合优化问题之间的相似性。模拟退火算法从某一较高初温出发,伴随温度参数的不断下降,结合概率突跳特性在解空间中随机寻找目标函数的全局最优解,即在局部最优解能概率性地跳出并最终趋于全局最优。模拟退火算法是一种通用的优化算法,理论上算法具有概率的全局优化性能,目前已在工程中得到了广泛应用,诸如VLSI、生产调度、控制工程、机器学习、神经网络、信号处理等领域。模拟退火算法是通过赋予搜索过程一种时变且最终趋于零的概率突跳性,从而可有效避免陷入局部极小并最终趋于全局最优的串行结构的优化算法。
**本程序由4个.m文件组成,其中,主函数为main.m。computer.m用来计算当前旅行商经过所有城市的总距离;perturb.m用来随即交换任意两座城市的位置,netplot.m画出旅行商经过的最优路线。
function len=computer(city,n)
global x;
global y;
len=0;
for i=1:n-1
len=len+sqrt((x(city(i))-x(city(i+1)))^2+(y(city(i))-y(city(i+1)))^2);
end
len=len+sqrt((x(city(n))-x(city(1)))^2+(y(city(n))-y(city(1)))^2);
end
function city=perturb(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(p1);
city(p1)=city(p2);
city(p2)=tmp;
end
function netplot(city,n)
global x
global y
hold on
for i=1:n-1
plot(x(city(i)),y(city(i)),'r*');
line([x(city(i)) x(city(i+1))],[y(city(i)) y(city(i+1))]);
end
plot(x(city(i)),y(city(i)),'r*');
line([x(city(n)) x(city(1))],[y(city(n)) y(city(1))]);
hold off;
end
clear all;close all;clc
global x;
global y;
temperature = 1000;
n = 50;
iter = 100;
a = importdata('D:\MATLAB\R2015b\bin\TSP\tsp123.txt');
a = a';
city = a(1,:);
x = a(2,:);
y = a(3,:);
l = 1;
len(l) = computer(city, n);
%netplot(city, n);
while temperature > 0.001
for i = 1 : iter
len1 = computer(city, n);
tmp_city = perturb(city, n);
len2 = computer(tmp_city, n);
delta_e = len2 - len1;
if delta_e < 0
city = tmp_city;
else
if exp(-delta_e / temperature) > rand()
city = tmp_city;
end
end
end
l = l + 1;
len(l) = computer(city, n);
temperature = temperature * 0.99;
end
min(len)
figure;
netplot(city, n);
%算法收敛图
figure;
a = 1:length(len);
plot(a,len);
grid on
最短路长度为:ans =3.8554e+04;
n=50:
模拟退火其实也是一种贪心算法,但是它的搜索过程引入了随机因素。模拟退火算法以一定的概率来接受一个比当前解要差的解,因此有可能会跳出这个局部的最优解,达到全局的最优解。