MATLAB 模拟退火SA

模拟退火首先从某个初始候选解开始,当温度大于0时执行循环。

在循环中,通过随机扰动产生一个新的解,然后求得新解和原解之间的能量差,如果差小于0,则采用新解作为当前解。

如果差大于0,则采用一个当前温度与能量差成比例的概率来选择是否接受新解。温度越低,接受的概率越小,差值越大,同样接受概率越小。

是否接受的概率用此公式计算:p=exp(-ΔE/T)。这里ΔE为新解与原解的差,T为当前的温度。

由于温度随迭代次数逐渐降低,因此获得一个较差的解的概率较小。

典型的模拟退火算法还使用了蒙特卡洛循环,在温度降低之前,通过多次迭代来找到当前温度下比较好的解。

这里使用模拟退火解旅行商问题,因为这个问题本身是一个NP难问题,所以也就求不到最优解,不过应该可以求得一个比较好的解,然后再手工优化。

具体到程序中,这里的随机扰动就是随机置换两个城市的位置,能量就是旅行商路线的总长度。

算法结果如下:

初始旅行商路线:

最终求得的旅行商路线:

每次迭代求得的旅行距离:

matlab代码如下:

main.m

 1 clear all;close all;clc
 2 
 3 n=20;                   %城市个数
 4 temperature=100*n;      %初始温度
 5 iter=100;               %内部蒙特卡洛循环迭代次数
 6 
 7 %随机初始化城市坐标
 8 city=struct([]);
 9 for i=1:n
10     city(i).x=floor(1+100*rand()); 
11     city(i).y=floor(1+100*rand());
12 end
13 
14 l=1;                            %统计迭代次数
15 len(l)=computer_tour(city,n);   %每次迭代后的路线长度  
16 netplot(city,n);                %初始旅行路线
17 
18 while temperature>0.001     %停止迭代温度
19     
20     for i=1:iter     %多次迭代扰动,一种蒙特卡洛方法,温度降低之前多次实验
21         len1=computer_tour(city,n);         %计算原路线总距离
22         tmp_city=perturb_tour(city,n);      %产生随机扰动
23         len2=computer_tour(tmp_city,n);     %计算新路线总距离
24         
25         delta_e=len2-len1;  %新老距离的差值,相当于能量
26         if delta_e<0        %新路线好于旧路线,用新路线代替旧路线
27             city=tmp_city;
28         else                        %温度越低,越不太可能接受新解;新老距离差值越大,越不太可能接受新解
29             if exp(-delta_e/temperature)>rand() %以概率选择是否接受新解
30                 city=tmp_city;      %可能得到较差的解
31             end
32         end        
33     end
34     l=l+1;
35     len(l)=computer_tour(city,n);   %计算新路线距离
36     temperature=temperature*0.99;   %温度不断下降
37   
38 end  
39 figure;
40 netplot(city,n);    %最终旅行路线
41 
42 figure;
43 plot(len)

computer_tour.m

1 function len=computer_tour(city,n)   %计算路线总长度,每个城市只计算和下家城市之间的距离。
2     len=0;
3     for i=1:n-1
4         len=len+sqrt((city(i).x-city(i+1).x)^2+(city(i).y-city(i+1).y)^2);        
5     end
6     len=len+sqrt((city(n).x-city(1).x)^2+(city(n).y-city(1).y)^2);
7 end

perturb_tour.m

 1 function city=perturb_tour(city,n)  
 2     
 3     %随机置换两个不同的城市的坐标
 4     %产生随机扰动
 5     p1=floor(1+n*rand());
 6     p2=floor(1+n*rand());
 7 
 8     while p1==p2
 9         p1=floor(1+n*rand());
10         p2=floor(1+n*rand());    
11     end
12     
13     tmp=city(p1);
14     city(p1)=city(p2);
15     city(p2)=tmp;
16 
17 end

netplot.m

 1 function netplot(city,n)        %连线各城市,将路线画出来
 2     hold on;
 3     for i=1:n-1
 4         plot(city(i).x,city(i).y,'r*');  
 5         line([city(i).x city(i+1).x],[city(i).y city(i+1).y]);  %只连线当前城市和下家城市     
 6     end
 7 
 8     plot(city(n).x,city(n).y,'r*');  
 9     line([city(n).x city(1).x],[city(n).y city(1).y]);     %最后一家城市连线第一家城市
10     hold off;
11 end
原文地址:https://www.cnblogs.com/ybqjymy/p/13645395.html