24小时热门版块排行榜    

北京石油化工学院2026年研究生招生接收调剂公告
查看: 846  |  回复: 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的回帖
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 求调剂 +4 QiMing7 2026-03-25 5/250 2026-03-29 21:10 by 唐沐儿
[考研] 311求调剂 +6 冬十三 2026-03-24 6/300 2026-03-29 20:45 by 无际的草原
[考研] 291求调剂 +7 Y-cap 2026-03-29 8/400 2026-03-29 19:53 by klasasda
[考研] 085600 材料与化工 329分求调剂 +14 Mr. Z 2026-03-25 14/700 2026-03-29 17:27 by 学员8dgXkO
[考研] 一志愿双一流机械285分求调剂 +4 幸运的三木 2026-03-29 5/250 2026-03-29 14:49 by Miko19
[考研] 291求调剂 +5 Y-cap 2026-03-29 6/300 2026-03-29 13:18 by mumin1990
[考研] 356求调剂 +4 gysy?s?a 2026-03-28 4/200 2026-03-29 10:32 by 唐沐儿
[考研] 调剂求院校招收 +6 鹤鲸鸽 2026-03-28 6/300 2026-03-29 08:15 by fmesaito
[考研] 289求调剂 +13 新时代材料 2026-03-27 13/650 2026-03-29 01:16 by 544594351
[考研] 一志愿中南大学化学0703总分337求调剂 +5 niko- 2026-03-27 5/250 2026-03-28 14:25 by 唐沐儿
[考研] 085602 307分 求调剂 +7 不知道叫什么! 2026-03-26 7/350 2026-03-28 09:57 by 神马都不懂
[考研] 材料求调剂一志愿哈工大324 +7 闫旭东 2026-03-28 9/450 2026-03-28 08:51 by Xu de nuo
[考研] 086502化学工程342求调剂 +6 阿姨复古不过 2026-03-27 6/300 2026-03-28 07:06 by wangy0907
[考研] 086000调剂 +3 7901117076 2026-03-26 3/150 2026-03-27 21:34 by Jianing_Mi
[考研] 348求调剂 +4 小懒虫不懒了 2026-03-27 5/250 2026-03-27 12:47 by 果果妈咪
[考研] 085601 材料工程 313分 求调剂 +5 Ong3 2026-03-27 5/250 2026-03-27 12:24 by goldfish51
[考研] 314求调剂 +3 溪云珂 2026-03-26 3/150 2026-03-27 11:20 by sanrepian
[考研] 359求调剂 +4 王了个楠 2026-03-25 4/200 2026-03-27 08:43 by 不吃魚的貓
[考研] 081200-11408-276学硕求调剂 +4 崔wj 2026-03-26 4/200 2026-03-27 08:04 by chemisry
[考研] 机械学硕总分317求调剂!!!! +4 Acaciad 2026-03-25 4/200 2026-03-25 19:59 by hanserlol
信息提示
请填处理意见