急求,模拟退火遗传算法的MATLAB程序!谢谢
非常感谢!太谢谢了,谢谢救命啊!请问还有模拟退火混合遗传算法的TSP问题的MATLAB程序吗?...
非常感谢!太谢谢了,谢谢救命啊!请问还有模拟退火混合遗传算法的TSP问题的MATLAB程序吗?
展开
1个回答
展开全部
你真幸福。我刚刚编了一个模拟退火算法,计算旅行商问题:注意:一共三个文件,第一个是主程序,下面两个是子函数。
% for d=1:50 %循环10次发现最小路径为4.115,循环50次有3次出现4.115
T_max=80; %input('please input the start temprature');
T_min=0.001; %input('please input the end temprature');
iter_max=100;%input('please input the most interp steps on the fit temp');
s_max=100; %input('please input the most steady steps ont the fit temp');
T=T_max;
load .\address.txt;
order1=randperm(size(address,1))';%生成初始解。
figure(1);
plot(address(order1,1),address(order1,2),'*r-')
title('随机产生的路径');
totaldis1=distance(address,order1);
for n=1:size(address,1)
text(address(n,1)+0.01,address(n,2),num2str(n))%标号
end
text(0.9,0.9,num2str(totaldis1))
figure(2);
while T>=T_min
iter_num=1;
s_num=1;
plot(T,totaldis1,'r.')
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
set(gca,'xscale','log');%或者使用semilogx,有相同效果
xlabel('退火温度');ylabel('总距离');
order1
totaldis1
figure(3)
plot(address(order1,1),address(order1,2),'*b-')
title('最终路径');
for n=1:size(address,1)
text(address(n,1)+0.01,address(n,2),num2str(n))%标号
end
text(0.9,0.9,num2str(totaldis1))
dstc(d)=totaldis1;
% end
——————————————————————————————
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;
------------------------------------------------------------
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);
% for d=1:50 %循环10次发现最小路径为4.115,循环50次有3次出现4.115
T_max=80; %input('please input the start temprature');
T_min=0.001; %input('please input the end temprature');
iter_max=100;%input('please input the most interp steps on the fit temp');
s_max=100; %input('please input the most steady steps ont the fit temp');
T=T_max;
load .\address.txt;
order1=randperm(size(address,1))';%生成初始解。
figure(1);
plot(address(order1,1),address(order1,2),'*r-')
title('随机产生的路径');
totaldis1=distance(address,order1);
for n=1:size(address,1)
text(address(n,1)+0.01,address(n,2),num2str(n))%标号
end
text(0.9,0.9,num2str(totaldis1))
figure(2);
while T>=T_min
iter_num=1;
s_num=1;
plot(T,totaldis1,'r.')
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
set(gca,'xscale','log');%或者使用semilogx,有相同效果
xlabel('退火温度');ylabel('总距离');
order1
totaldis1
figure(3)
plot(address(order1,1),address(order1,2),'*b-')
title('最终路径');
for n=1:size(address,1)
text(address(n,1)+0.01,address(n,2),num2str(n))%标号
end
text(0.9,0.9,num2str(totaldis1))
dstc(d)=totaldis1;
% end
——————————————————————————————
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;
------------------------------------------------------------
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);
富港检测技术(东莞)有限公司_
2024-04-02 广告
2024-04-02 广告
正弦振动多用于找出产品设计或包装设计的脆弱点。看在哪一个具体频率点响应最大(共振点);正弦振动在任一瞬间只包含一种频率的振动,而随机振动在任一瞬间包含频谱范围内的各种频率的振动。由于随机振动包含频谱内所有的频率,所以样品上的共振点会同时激发...
点击进入详情页
本回答由富港检测技术(东莞)有限公司_提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询