24小时热门版块排行榜    

查看: 802  |  回复: 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,
这就是上面的程序,你能否帮我看看,十分感谢
6楼2011-07-05 20:40:46
已阅   回复此楼   关注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,
这就是上面的程序,你能否帮我看看,十分感谢
5楼2011-07-05 20:39:34
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
信息提示
请填处理意见