当前位置:编程学习 > Matlab >>

急求,模拟退火遗传算法的MATLAB程序!谢谢

补充:非常感谢!太谢谢了,谢谢救命啊!请问还有模拟退火混合遗传算法的TSP问题的MATLAB程序吗?
答案:你真幸福。我刚刚编了一个模拟退火算法,计算旅行商问题:注意:一共三个文件,第一个是主程序,下面两个是子函数。
% 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);

上一个:你好,我想在embedded matlab function中编写一段程序,类似于a=b+10t,a为输出,b为输入,t为时间
下一个:用matlab编写基于自适应DCT域的数字水印程序

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,