MCM战备历程3(模拟退火算法Matlab源程序)For glory
2007-02-03 11:20:04| 分类:数学建模 | 标签:学习|字号订阅
%模拟退火算法程序
T_max=input('please input the start temprature');
T_min=input('please input the end temprature');
iter_max=input('please input the most interp steps on the fit temp');
s_max=input('please input the most steady steps ont the fit temp');
T=T_max;
load d:\address.txt;
order1=randperm(size(address,1))';%生成初始解。
plot(address(order1,1),address(order1,2),'*r-')
totaldis1=distance(address,order1);
while T>=T_min
iter_num=1;
s_num=1;
plot(T,totaldis1)
hold on
while iter_num<iter_max&s_num<s_max;
order2=exhgpath(order1);
totaldis2=distance(address,order2);
R=rand;
DeltaDis=totaldis2-totaldis1;
if DeltaDis<0;
order1=order2;
totaldis1=totaldis2;
elseif (exp((totaldis1-totaldis2)/(T))>R)
order1=order2;
totaldis1=totaldis2;
else s_num=s_num+1;
end
iter_num=iter_num+1;
end
T=T*0.99;
end
order1
totaldis1
figure(2)
plot(address(order1,1),address(order1,2),'*r-')
function y=distance(address,order)
nmb=size(address,1);
y=0;
for i=1:nmb-1
y=y+sqrt((address(order(i+1),1)-address(order(i),1))^2+(address(order(i+1) ,2)-address(order(i),2))^2);
end
y=y+sqrt((address(order(i+1),1)-address(order(1),1))^2+(address(order(i+1),2) -address(order(1),2))^2);
function y=exhgpath(order)
while 1
b=size(order,1);
r=unidrnd(b,1,2);
if r(1)-r(2)~=0
break
end
end
b=order(r(2));
order(r(2))=order(r(1));
order(r(1))=b;
(模拟退火算法Matlab源程序)
学术讨论2008-04-01 18:05:56 阅读1322 评论6 字号:大中小订阅
(模拟退火算法Matlab源程序)For glory
%模拟退火算法程序
T_max=input('please input the start temprature');
T_min=input('please input the end temprature');
iter_max=input('please input the most interp steps on the fit temp');
s_max=input('please input the most steady steps ont the fit temp');
T=T_max;
load d:\address.txt;
order1=randperm(size(address,1))';%生成初始解。
plot(address(order1,1),address(order1,2),'*r-')
totaldis1=distance(address,order1);
while T>=T_min
iter_num=1;
s_num=1;
plot(T,totaldis1)
hold on
while iter_num<iter_max&s_num<s_max;
order2=exhgpath(order1);
totaldis2=distance(address,order2);
R=rand;
DeltaDis=totaldis2-totaldis1;
if DeltaDis<0;
order1=order2;
totaldis1=totaldis2;
elseif (exp((totaldis1-totaldis2)/(T))>R)
order1=order2;
totaldis1=totaldis2;
else s_num=s_num+1;
end
iter_num=iter_num+1;
end
T=T*0.99;
end
order1
totaldis1
figure(2)
plot(address(order1,1),address(order1,2),'*r-')
function y=distance(address,order)
nmb=size(address,1);
y=0;
for i=1:nmb-1
y=y+sqrt((address(order(i+1),1)-address(order(i),1))^2+(address(order(i+1),2)-address(order(i),2))^2); end
y=y+sqrt((address(order(i+1),1)-address(order(1),1))^2+(address(order(i+1),2)-address(order(1),2))^2);
function y=exhgpath(order)
while 1
b=size(order,1);
r=unidrnd(b,1,2);
if r(1)-r(2)~=0
break
end
end
b=order(r(2));
order(r(2))=order(r(1));
order(r(1))=b;
y=order;。