24小时热门版块排行榜    

查看: 991  |  回复: 0

c.sharp

新虫 (初入文坛)

[求助] 求各位牛人给看一下程序(见附件)。matlab遗传算法实现函数优化,好像陷入了局部最优

求各位牛人给看一下程序(见附件)。matlab遗传算法实现函数优化,好像陷入了局部最优
原始数据.png

程序如下:
%function result=sga(n,a,b,pc,pm,e)
clear;
%n—群体规模;a—搜索上限;b—搜索下限; %pc—交叉概率;pm—变异概率;e—计算精度;
%clear
E=[12.216,12.678,13.979,14.085,13.428,7.317,9.092,6.64;
    20.36,21.13,19.97,15.65,14.92,8.13,7.88,6.64;
    6.108,6.339,7.988,12.52,7.46,4.878,4.728,6.64];

IP1=[3355;4500;2385];
IP2=[10,4,3,14,34,120,481,691];
ID=[4,2,2,4,5,7,9,10,0];
ID1=[1,1,1,  1,1,1    ,1,1,1    ,2,2,2    ,2,2,2    ,3,3,3    ,3,3,3    ,3,3,3    ,0];



jieguo=[];
n=10000;    %n—群体规模;
a=111010100100100111101010010010011110101001001001;    %a—搜索上限;
b=911010100100100111101010010010011110101001001001;    %b—搜索下限;
pc=0.7;  %pc—交叉概率;
pm=0.02; %pm—变异概率;
e=1;  %e—计算精度;
l=1;
while (b-a)/e>2^(l)
    l=l+1;
end
newpopu=[];
popusize=n;
chromlength=l;
j=1;
popu1=[];
Sc=1;
%随机产生n行m列的初始群体
while j<=100                                %设置程序中止条件
    j=j+1;
    j
    if Sc==1
        popu=round(rand(popusize,chromlength));
    end
    py=chromlength;
    for i=1:py                             %进行二进制转换成十进制的解码操作
        popu1(:,i)=2.^(py-1).*popu(:,i);
        py=py-1;
    end
    popu2=sum(popu1,2);
    x=a+popu2*(b-a)/(2^l-1);
    % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
   
    yvalue=2*x.^2.*cos(3*x)+x.*sin(5*x)+8; %计算群体中每个个体的适应度
    Ils=[];
    ls=0;
    for i=1:size(ID1,2)-1
        x=x-ls*10^sum(ID1(i:end));
        pf=sum(ID1(i+1:end));
        Ils(:,i)=floor(x/(10^pf));
        ls=Ils(:,i);
    end
    Ils1=[];
    for i=1:size(Ils,1)
        Ils1(:,1)=Ils(i,1:3);
        Ils1(:,2)=Ils(i,4:6);
        Ils1(:,3)=Ils(i,7:9);
        Ils1(:,4)=Ils(i,10:12);
        Ils1(:,5)=Ils(i,13:15);
        Ils1(:,6)=Ils(i,16:18);
        Ils1(:,7)=Ils(i,19:21);
        Ils1(:,8)=Ils(i,22:24);
        Ism=sum(Ils1,1);
        Ils2=round((Ils1./[Ism;Ism;Ism]).*[IP2;IP2;IP2]);
        
%         
        if max(sum(Ils2,1)>IP2)==1
            yvalue(i,1)=0;
            continue
        end
        if min(sum(Ils2.*E,2)>=IP1)==0
            yvalue(i,1)=0;
            continue
        end
        
        yvalue(i,1)=sum(sum(Ils2.*E));
        %            if yvalue(i,1)>0
        %                1
        %            end
    end
   
   
    yvalue=yvalue;
   
    if max(yvalue)==0
        Sc=1;
        continue
    else
        Sc=0;
    end
   
   
    %         yvalue(i,1)=js1*js2/(js13+js23);
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % II=II+flipud(II);%依标准差衡量
    % yvalue(i,1)=1/sum(std(II,0,2));
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % II=II+flipud(II);
    % yvalue(i,1)=1/sum(abs(max(II)-min(II)));
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   
    max(yvalue)
   
   
    for i=1:popusize                       %执行复制操作
        if yvalue(i)<0
            yvalue(i)=0;
        end
    end
    fitscore=yvalue/sum(yvalue);           %个体被选中的概率
    fitscore=cumsum(fitscore);             % 群体中个体的累积概率
    wh=sort(rand(popusize,1));             % 从小到大排列
    wheel=1;
    fitone=1;
    while wheel<=popusize                  %执行转盘式选择操作
        if wh(wheel)<fitscore(fitone)
            newpopu(wheel,=popu(fitone,;
            wheel=wheel+1;
            % fitone
        else
            fitone=fitone+1;
        end
    end
    popu=newpopu;
    for i=1:2:popusize-1                   %执行交叉操作
        if rand<pc
            cpoint=round(rand*chromlength);
            newpopu(i,=[popu(i,1:cpoint) popu(i+1,cpoint+1:chromlength)];
            newpopu(i+1,=[popu(i+1,1:cpoint) popu(i,cpoint+1:chromlength)];
        else
            newpopu(i,=popu(i,;
            newpopu(i+1,=popu(i+1,;
        end
    end
    popu=newpopu;
    for i=1:popusize                       %执行变异操作
        if rand<pm
            mpoint=round(rand*chromlength);
            if mpoint<=0;
                mpoint=1;
            end
            newpopu(i,=popu(i,;
            if newpopu(i,mpoint)==0
                newpopu(i,mpoint)=1;
            else
                newpopu(i,mpoint)=0;
            end
        else
            newpopu(i,=popu(i,;
        end
    end
    [y(j) index]=max(yvalue);              %求出群体中适应值最大的个体及其适应值
       bestindividual=newpopu(index,;
        py=chromlength;
     for i=1:py                             %进行二进制转换成十进制的解码操作
            bestindividual(1,i)=2.^(py-1).*bestindividual(:,i);
            py=py-1;
        end
        r(j)=a+sum(bestindividual,2)*(b-a)/(2^l-1);
    popu=newpopu;
    % 重新赋值并返回
    Ij=x(index,;
    Ij(:,end+1)=max(yvalue);
    jieguo=[jieguo;Ij];
    save jieguo jieguo
    j
   
   
end
%计算最大值及其位置 返回优化结果
plot(1:length(yvalue),yvalue,'b-',1:length(yvalue),Labels,'r.');
ylabel('yvalue');
xlabel('Pattern Index');
legend('True Labels','Resultant Labels',0);
回复此楼

» 本帖附件资源列表

  • 欢迎监督和反馈:小木虫仅提供交流平台,不对该内容负责。
    本内容由用户自主发布,如果其内容涉及到知识产权问题,其责任在于用户本人,如对版权有异议,请联系邮箱:xiaomuchong@tal.com
  • 附件 1 : ga000.rar
  • 2014-12-03 12:45:42, 19.5 K

» 收录本帖的淘帖专辑推荐

source

» 猜你喜欢

学习、学习、再学习。。。。
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

智能机器人

Robot (super robot)

我们都爱小木虫

相关版块跳转 我要订阅楼主 c.sharp 的主题更新
信息提示
请填处理意见