菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
18
0

遗传算法

原创
05/13 14:22
阅读数 24317

前言:

      放假前偷偷跑到杭州玩了几天,虽然没有预料中那么美丽,不过还是很开心,感谢老严的热情款待。回到南京后,距离完成任务就剩3天时间,而且师兄逼的很紧,于是夜以继日,实现了对比算法《Multimodal Sparse Representation-Based Classification for Lung Needle Biopsy Images》(Google学术搜索)。

      遗传算法虽然如雷贯耳,但是一直未有接触,此次从零开始,看算法、看论文,对了,还要招待从云南回来的基友。时间虽然很紧,但是最后还是圆满完成任务,得以回家过年,感觉很充实。

      网上有很多GA的参考文章,我就不画蛇添足了,但大多数文章只是给出的很简单函数的例子,浅尝辄止,目标函数太过简单,感觉很不过瘾。于是乎我将我实现的本篇文章的matlab代码记录于此,写的很乱,轻喷。

 

      文章的大意是通过肺细胞的形状、颜色和纹理,利用SRC学习构造3个子字典(代码中是4个模态),然后分别对测试样本做预测,最后通过mSRC进行分类。

主函数:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Copyright (c) 2014 七年之后. All rights reserved.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Name: main.m
%省略数据集预处理

for loop = 1:20 %循环20次,得到20个结果取平均值
    
    n=train_num*class_num; %beta字符串长度,即训练样本个数
    
    beta1=round(rand(K,n)); %随机01字符串,K为种群大小
    beta2=round(rand(K,n)); 
    beta3=round(rand(K,n)); 
    beta4=round(rand(K,n)); 
    
    %产生初始群体
    Dict=tr_dat(:,:,:);
    for g=1:G %G为迭代次数
        g
        kappa=[0.001];
        lambda=0.1;
        
        %更新beta
        newbeta1=beta1;
        newbeta2=beta2;
        newbeta3=beta3;
        newbeta4=beta4;
        
        newtrls1=[];
        newtrls2=[];
        newtrls3=[];
        newtrls4=[];
        
        %%%%%   更新子字典   %%%%%%
        Dict1=[];
        Dict2=[];
        Dict3=[];
        Dict4=[];
        
        fit=[]; %适应值
        
        for i=1:K %K个种群
            %Dict为样本集,分别构造4个模态下的子字典
            for j=1:n %n个样本
                if(newbeta1(i,j)==1)
                    Dict1=[Dict1,Dict(:,j,1)];
                    newtrls1=[newtrls1,trls(j)]; %训练样本标记
                end
                if(newbeta2(i,j)==1)
                    Dict2=[Dict2,Dict(:,j,2)];
                    newtrls2=[newtrls2,trls(j)];
                end
                if(newbeta3(i,j)==1)
                    Dict3=[Dict3,Dict(:,j,3)];
                    newtrls3=[newtrls3,trls(j)];
                end
                if(newbeta4(i,j)==1)
                    Dict4=[Dict4,Dict(:,j,4)];
                    newtrls4=[newtrls4,trls(j)];
                end
            end
            
            %%%%%% 稀疏编码,计算fitness %%%%%
            count1=0;
            count2=0;     
            
            for j=1:n %n训练样本个数
                %稀疏编码分类,此处省略,id分别为预测值
                if(id1==trls(j)||id2==trls(j)||id3==trls(j)||id4==trls(j))
                    count1=count1+1;
                end
                if(id1==id2||id1==id3||id1==id4||id2==id3||id2==id4||id3==id4)
                    count2=count2+1;
                end
            end
            fitvalue=1/n*count1+1/n*lambda*(4-count2);
            fit=[fit,fitvalue];
        end
        
        if(g~=G) %最后一次跳出循环
            newbeta1=[];
            newbeta2=[];
            newbeta3=[];
            newbeta4=[];
            
            %newbeta增加一列,然后按照fitness排序
            newbeta1=[beta1(:,:),fit'];
            newbeta2=[beta2(:,:),fit'];
            newbeta3=[beta3(:,:),fit'];
            newbeta4=[beta4(:,:),fit'];
            
            newbeta1=sortrows(newbeta1,n+1);
            newbeta2=sortrows(newbeta2,n+1);
            newbeta3=sortrows(newbeta3,n+1);
            newbeta4=sortrows(newbeta4,n+1);
            
            %去掉fitness
            newbeta1(:,n+1)=[];
            newbeta2(:,n+1)=[];
            newbeta3(:,n+1)=[];
            newbeta4(:,n+1)=[];
            
            %去掉fitness小的种群
            newbeta1(K/2+1:K,:)=[];
            newbeta2(K/2+1:K,:)=[];
            newbeta3(K/2+1:K,:)=[];
            newbeta4(K/2+1:K,:)=[];
            
            %交叉变异
            [newbeta1]=crossover(newbeta1,pc);%交叉
            [newbeta1]=mutation(newbeta1,pm);%变异
            [newbeta2]=crossover(newbeta2,pc);%交叉
            [newbeta2]=mutation(newbeta2,pm);%变异
            [newbeta3]=crossover(newbeta3,pc);%交叉
            [newbeta3]=mutation(newbeta3,pm);%变异
            [newbeta4]=crossover(newbeta4,pc);%交叉
            [newbeta4]=mutation(newbeta4,pm);%变异
            
            %随机增加K/2个染色体
            add_beta1=round(rand(K/2,n));
            add_beta2=round(rand(K/2,n));
            add_beta3=round(rand(K/2,n));
            add_beta4=round(rand(K/2,n));
            
            newbeta1=[newbeta1;add_beta1];
            newbeta2=[newbeta2;add_beta2];
            newbeta3=[newbeta3;add_beta3];
            newbeta4=[newbeta4;add_beta4];
        end
    end
    
    
    %选一个最大的beta构造最终子字典
    maxindex=find(fit==max(fit)); %最大fitness下标
    lastbeta1=newbeta1(maxindex,:);
    lastbeta2=newbeta2(maxindex,:);
    lastbeta3=newbeta3(maxindex,:);
    lastbeta4=newbeta4(maxindex,:);
    
    %%%%%% 最终子字典 %%%%%%
    Dict1=[];
    Dict2=[];
    Dict3=[];
    Dict4=[];
    
    %样本标记
    Drls1=[];
    Drls2=[];
    Drls3=[];
    Drls4=[];
    for j=1:n %样本       
        if(lastbeta1(j)==1)
            Dict1=[Dict1,Dict(:,j,1)];
            Drls1=[Drls1,floor(j/train_num)+1];
        end
        if(lastbeta2(j)==1)
            Dict2=[Dict2,Dict(:,j,2)];
            Drls2=[Drls2,floor(j/train_num)+1];
        end
        if(lastbeta3(j)==1)
            Dict3=[Dict3,Dict(:,j,3)];
            Drls3=[Drls3,floor(j/train_num)+1];
        end
        if(lastbeta4(j)==1)
            Dict4=[Dict4,Dict(:,j,4)];
            Drls4=[Drls4,floor(j/train_num)+1];
        end
    end
        
    count=0; %统计分类正确个数
    % mSRC分类,此处省略
    right_num=[right_num,count/length(ttls)]
end

交叉函数:

%Name: crossover.m  %交叉
function [newpop]=crossover(pop,pc) %pc是交叉概率
[rows,cols]=size(pop);
newpop=pop(:,:);
for i=1:2:rows-1
    if(rand<pc)
        cpoint=round(rand*cols);
        newpop(i,:)=[pop(i,1:cpoint),pop(i+1,cpoint+1:cols)];
        newpop(i+1,:)=[pop(i+1,1:cpoint),pop(i,cpoint+1:cols)];    
    end
end

变异函数:

%Name: mutation.m  %变异
function [newpop]=mutation(pop,pm) %pm是变异概率
[rows,cols]=size(pop);
newpop=pop(:,:);
for i=1:rows  
    if(rand<pm)
        mpoint=round(rand*cols);
        if mpoint<=0
            mpoint=1;
        end
        newpop(i)=pop(i);
        if any(newpop(i,mpoint))==0
            newpop(i,mpoint)=1;
        else
            newpop(i,mpoint)=0;
        end
    end
end

 

  

 

  

 

  

 

发表评论

0/200
18 点赞
0 评论
收藏
为你推荐 换一批