Julu.m
function lucheng=juli(zuobiao)
length=max(size(zuobiao));
s=0;
for i=2:length
s=s+sqrt(sum((zuobiao(:,i)-zuobiao(:,i-1)).^2));
end
if length~=2
s=s+sqrt(sum((zuobiao(:,1)-zuobiao(:,length)).^2));
end
lucheng=s;
newpath.m
function zuobiao=newpath(zuobiao,length)
%随机交换两个点的坐标..
a=ceil(rand(1,2)*length);
qian=a(1);
hou=a(2);
temp=zuobiao(:,qian);
zuobiao(:,qian)=zuobiao(:,hou);
zuobiao(:,hou)=temp;
main.m
zuobiao=[0.37 0.75 0.45 0.76 0.71 0.07 0.42 0.59 0.32 0.6
0.3 0.67 0.62 0.67 0.20 ...
0.35 0.27 0.94 0.82 0.37 0.61 0.42 0.6 0.39 0.53 0.4
0.63 0.5 0.98 0.68;
0.91 0.87 0.85 0.75 0.72 0.74 0.71 0.69 0.64 0.64 0.59
0.59 0.55 0.55 0.5...
0.45 0.43 0.42 0.38 0.27 0.26 0.25 0.23 0.19 0.19 0.13
0.08 0.04 0.02 0.85]
length=max(size(zuobiao));
%求初始距离..
zhixu=randperm(length)
%随机生成一个路线经过点的顺
序
temp=zuobiao(1,:);
newzuobiao(1,:)=temp(zhixu);
temp=zuobiao(2,:);
newzuobiao(2,:)=temp(zhixu);
newzuobiao
zuobiao=newzuobiao;
huatu=[zuobiao,zuobiao(:,1)];
plot(huatu(1,:),huatu(2,:)),hold on
plot(huatu(1,:),huatu(2,:),'ro'),hold off
pause(1);
f=juli(newzuobiao)
%参数定义区--------------------------------------
%初始温度为 10000
tmax=100;
tmin=0.001;
%温度下降速率
down=0.95;
%退火算法的函数..
t=tmax;
while t>tmin
m=1;
for n=1:300
newzuobiao=newpath(zuobiao,length);
newf=juli(newzuobiao);
if newf=70
m=1;
n=n+1;
end
end
end
huatu=[zuobiao,zuobiao(:,1)];
plot(huatu(1,:),huatu(2,:)),hold on
plot(huatu(1,:),huatu(2,:),'ro'),hold off
pause(0.000000001)
t=t*down
f
end