24小时热门版块排行榜    

查看: 945  |  回复: 5
当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖

s20090281

银虫 (小有名气)

[求助] 矩阵的问题

我做遗传算法,看的别人的程序运行是正确的,可是自己同样的就出现了错误。
CODE:
selcet=evo_popu(selected,:)
      pc=0.6
     s=rand(1,popsize-1)%随机产生与交叉概率相比较的概率
     y1=find(s<0.6)             %参与交叉位的个体     
     y2=find(s>=0.6)            %不参与交叉位的个体
     len1=length(y1)           
          if len1>2&mod(len1,2)==1 %如果用来进行交叉的染色体的条数为奇数,将其调整为偶数
        y2(length(y2)+1)=y1(len1)
                 y1(len1)=[]
    end
      hidecode=rand(length(y1)/2,codel)%随机生成掩码
    hiderand=rand(length(y1)/2,codel)%随机生成掩码
    if length(y1)>=2
        for p=1:length(y1)/2
            for q=1:codel
                if hidecode(p,q)<=pc%非均匀交叉
                    cross(y1(2*p-1),q)=hiderand(p,q)*select(y1(2*p-1),q)+(1-hiderand(p,q))*select(y1(2*p),q)
                   cross(y1(2*p),q)=hiderand(p,q)*select(y1(2*p),q)+(1- hiderand(p,q))*select(y1(2*p-1),q)
                else %不交叉位
                    cross(y1(2*p-1),q)=select(y1(2*p-1),q)
                    cross(y1(2*p),q)=select(y1(2*p),q)
                end
            end
        end
    end
    cross(y2,:)=select(y2,:)

就是这段程序,就是select被赋值了,但是在下一行时select(y1(2*p-1),q)却显示的是空矩阵,请问是什么原因呢?

[ Last edited by xiegangmai on 2011-7-5 at 09:38 ]
回复此楼

» 猜你喜欢

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

s20090281

银虫 (小有名气)

引用回帖:
Originally posted by 信彼南山 at 2011-07-05 15:25:07:
大哥,你这程序太强大了,变量全部未定义,居然还能运行?

function [out]=my_function(input)
clear all;
clc;
popsize=10;
codel=4;
G=30;
tic
s=sprintf('程序正在运行中,请稍等......');
%要优化的是y(k)=b1*y(k-1)+2*y(k-2)+a1*u(k-1)+a2*u(k-2)的b1,b2,a1,a2这四个参数,已知
disp(s);
popu=2*rand(popsize,codel)-1             %初始化种群
evo_pop=zeros(popsize-1,codel);          %要进化的种群
select=zeros(popsize-1,codel);              %选择的种群
cross=zeros(popsize-1,codel);                %交叉的种群
mutation=zeros(popsize-1,codel);          %变异的种群
Trace=zeros(G,codel);            %最优轨迹
for kg=1:G
    time(kg)=kg;                                   %进化到第几代
    for i=1:popsize                                  %开始循环
        xi=popu(i,;
        x1=xi(1);
        x2=xi(2);
        x3=xi(3);
        x4=xi(4);   
        c=0;
       b1=[-0.1;0;0.2;0.5;1;1.8;2.1;4.2;6.6;11];
       a1=[0.1;0.2;0.1;0.2;0.1;0.2;0.1;0.2;0;0.4] ;   
       for k=3:10           
        f=[b1(k)-x1*b1(k-1)-x2*b1(k-2)-x3*a1(k-1)-x4*a1(k-2)]^2; 把它定    为目标函数
            c=c+f;
            Msum(i,k)=c;
            Mfit(i,k)=1/c;  %适应度函数
        end
        fitness=Mfit(:,k)
        Bestf(i)=max(fitness)
    end
     [Oderf,index]=sort(Bestf,'descend') %排列
     Bestfitness=Oderf(1)
     bfi(kg)=Bestfitness
     best_gene=popu(index(1),
    [max_fitness,index1]=max(fitness)
    [min_fitness,index2]=min(fitness)
    vari=mean(popu(index1,);
     popu(index2,=popu(index1,
     fitness(index2,=fitness(index1,
     index(index1)=0
     index=nonzeros(index)
     evo_popu=popu(index,
     evo_fitness=fitness(index,
     evo_popsize=popsize-1
     fitness_sum=sum(evo_fitness)
     fitscore=evo_fitness/fitness_sum
     fitnesscum=cumsum(fitscore)
     r=rand(1,evo_popsize)
     selected=sum(fitnesscum*ones(1,popsize-1)      selcet=evo_popu(selected,
这就是上面的程序,你能否帮我看看,十分感谢
5楼2011-07-05 20:39:34
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
查看全部 6 个回答

信彼南山

木虫 (著名写手)

大哥,你这程序太强大了,变量全部未定义,居然还能运行?
3楼2011-07-05 15:25:07
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

s20090281

银虫 (小有名气)

引用回帖:
Originally posted by 信彼南山 at 2011-07-05 15:25:07:
大哥,你这程序太强大了,变量全部未定义,居然还能运行?

上面都定义了,我是没往上贴
4楼2011-07-05 20:17:46
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

s20090281

银虫 (小有名气)

引用回帖:
Originally posted by 信彼南山 at 2011-07-05 15:25:07:
大哥,你这程序太强大了,变量全部未定义,居然还能运行?

function [out]=my_function(input)
clear all;
clc;
popsize=10;
codel=4;
G=30;
tic
s=sprintf('程序正在运行中,请稍等......');
%要优化的是y(k)=b1*y(k-1)+2*y(k-2)+a1*u(k-1)+a2*u(k-2)的b1,b2,a1,a2这四个参数,已知
disp(s);
popu=2*rand(popsize,codel)-1             %初始化种群
evo_pop=zeros(popsize-1,codel);          %要进化的种群
select=zeros(popsize-1,codel);              %选择的种群
cross=zeros(popsize-1,codel);                %交叉的种群
mutation=zeros(popsize-1,codel);          %变异的种群
Trace=zeros(G,codel);            %最优轨迹
for kg=1:G
    time(kg)=kg;                                   %进化到第几代
    for i=1:popsize                                  %开始循环
        xi=popu(i,;
        x1=xi(1);
        x2=xi(2);
        x3=xi(3);
        x4=xi(4);   
        c=0;
       b1=[-0.1;0;0.2;0.5;1;1.8;2.1;4.2;6.6;11];
       a1=[0.1;0.2;0.1;0.2;0.1;0.2;0.1;0.2;0;0.4] ;   
       for k=3:10           
        f=[b1(k)-x1*b1(k-1)-x2*b1(k-2)-x3*a1(k-1)-x4*a1(k-2)]^2; 把它定    为目标函数
            c=c+f;
            Msum(i,k)=c;
            Mfit(i,k)=1/c;  %适应度函数
        end
        fitness=Mfit(:,k)
        Bestf(i)=max(fitness)
    end
     [Oderf,index]=sort(Bestf,'descend') %排列
     Bestfitness=Oderf(1)
     bfi(kg)=Bestfitness
     best_gene=popu(index(1),
    [max_fitness,index1]=max(fitness)
    [min_fitness,index2]=min(fitness)
    vari=mean(popu(index1,);
     popu(index2,=popu(index1,
     fitness(index2,=fitness(index1,
     index(index1)=0
     index=nonzeros(index)
     evo_popu=popu(index,
     evo_fitness=fitness(index,
     evo_popsize=popsize-1
     fitness_sum=sum(evo_fitness)
     fitscore=evo_fitness/fitness_sum
     fitnesscum=cumsum(fitscore)
     r=rand(1,evo_popsize)
     selected=sum(fitnesscum*ones(1,popsize-1)      selcet=evo_popu(selected,
这就是上面的程序,你能否帮我看看,十分感谢
6楼2011-07-05 20:40:46
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[基金申请] 投票:  有多少人是今天查系统知道结果的? +12 爱看书的可乐 2026-08-26 14/700 2026-08-26 12:49 by coolysnow
[基金申请] 科研孤儿太难了 +21 我4大白菜 2026-08-20 22/1100 2026-08-26 11:29 by nlgza
[基金申请] 国合里面能看到了 +7 一怀馨秋 2026-08-26 7/350 2026-08-26 11:23 by zhaosm1982
[基金申请] 2026年的国家社科基金项目通讯评审的新规则与新动向、新挑战 +6 process2012 2026-08-23 8/400 2026-08-26 11:03 by zjlp
[基金申请] 国合可查了 +3 paperzjh 2026-08-26 3/150 2026-08-26 10:41 by LemmonTr
[基金申请] 今天务委会开完了,明天出结果吗 +19 angus9576 2026-08-25 23/1150 2026-08-26 10:03 by zp519
[基金申请] 牛来!米来!面来! +8 beefly 2026-08-26 8/400 2026-08-26 08:37 by xuzhipiao
[基金申请] 范进中举一文的中心思想 +8 炎黄贵胄 2026-08-22 9/450 2026-08-26 07:47 by WASM
[基金申请] 在坚冰还盖着北海的时候,我看到了怒放的梅花。 (金币+10) +6 ziyangfang 2026-08-25 9/450 2026-08-25 20:26 by huagongfeihu
[基金申请] 放榜前的不淡定 20+4 snowwithsea 2026-08-19 19/950 2026-08-25 17:57 by chick875
[基金申请] 某些机构,以效率低为荣,以效率低作为存在感 +9 yuleib84 2026-08-25 10/500 2026-08-25 17:14 by alexon
[基金申请] 今日不放榜?网传国自然预计 8 月 27 日可查结果 +17 医学老男孩 2026-08-20 22/1100 2026-08-25 15:36 by 医学老男孩
[基金申请] 没有任何消息-是不是就凉了 +9 图啦图啦 2026-08-24 10/500 2026-08-25 11:59 by 南海小哥
[基金申请] 人气不行了 +11 fansofjerry 2026-08-21 11/550 2026-08-25 11:04 by 孤独的英雄6
[基金申请] 我面上完蛋了 +13 且听虎啸 2026-08-20 14/700 2026-08-25 09:10 by mrkang
[教师之家] 跳槽后在研项目怎么办? +5 简单化xn 2026-08-22 10/500 2026-08-23 12:38 by 简单化xn
[基金申请] 时间戳今天,20号变了 +5 archvillain 2026-08-20 5/250 2026-08-22 06:12 by hui_daxiao
[基金申请] 看来今天不会放榜了? +8 chengyan1220 2026-08-21 11/550 2026-08-21 17:52 by dcqxinyang
[基金申请] 应该是下周三26日公布了吧? +4 哈哈蛤? 2026-08-21 4/200 2026-08-21 10:58 by Vivilian
[基金申请] 基金啊基金 +4 longfie172 2026-08-20 4/200 2026-08-21 08:58 by mark mao
信息提示
请填处理意见