24小时热门版块排行榜    

Znn3bq.jpeg
查看: 1005  |  回复: 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 的主题更新
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 一志愿浙大生物325分求调剂 +7 zysheng 2026-04-12 7/350 2026-04-12 20:15 by bljnqdcc
[考研] 一志愿双非085400电子信息344 求调剂,对材料和化学方向也感兴趣 +11 无情的小羊 2026-04-09 12/600 2026-04-12 17:41 by 686666
[考研] 305求调剂 +6 77Qi 2026-04-07 6/300 2026-04-12 02:30 by 秋豆菜芽
[考研] 331求调剂 +5 王国帅 2026-04-11 5/250 2026-04-11 22:56 by 溪涧流水
[考研] 085400 328分 求调剂 +10 喂你一个大橙子 2026-04-09 14/700 2026-04-11 19:53 by lqspecial
[考研] 11408。358求调剂 +3 TMYzds 2026-04-07 3/150 2026-04-11 17:10 by 氮气气气
[考研] 0854调剂 +8 950824he@ 2026-04-09 8/400 2026-04-11 10:11 by zhq0425
[考研] 一志愿985机械学硕380求调剂 +5 关关雎鸠10 2026-04-11 5/250 2026-04-11 10:10 by 知念。A
[考研] 309求调剂 +14 wdhw 2026-04-10 15/750 2026-04-10 21:06 by zhouxiaoyu
[考研] 22408 366分,本科211,一志愿西工大 +4 Rubt 2026-04-09 4/200 2026-04-10 19:51 by chemisry
[考研] 一志愿0703化学招61最终排名62化学求调剂 +24 招61排名62 2026-04-07 28/1400 2026-04-10 16:15 by yx54321
[考研] 347材料专硕求调剂 +19 zj8215216 2026-04-06 19/950 2026-04-10 09:36 by 690616278
[考研] 284求调剂 +7 让我上岸吧阿西 2026-04-09 7/350 2026-04-09 18:59 by haironglove
[考研] 一志愿0807 数一英一 313 有没有二轮调剂 +11 emokidd 2026-04-08 12/600 2026-04-09 09:24 by wyf236
[考研] 化工学硕 285求调剂 +26 Wisjxn 2026-04-07 26/1300 2026-04-08 14:42 by screening
[考研] 313求调剂 +3 十六拾陆 2026-04-07 3/150 2026-04-07 23:20 by lbsjt
[考研] 307求调剂 +3 Youth@@ 2026-04-07 3/150 2026-04-07 22:00 by hemengdong
[考研] 22408 一志愿双一流人工智能300分 四六级,数据分析国奖 +4 zzfeng123 2026-04-06 6/300 2026-04-07 21:02 by zzfeng123
[考研] 071000生物学,一志愿深圳大学296分,求调剂 +12 TIckLw 2026-04-06 13/650 2026-04-07 20:34 by lijunpoly
[考研] 327考研调剂推荐 +6 呜呜呜呜呢 2026-04-06 6/300 2026-04-06 21:39 by 啵啵啵0119
信息提示
请填处理意见