遗传算法

前言:

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

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

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

 

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

主函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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

交叉函数:

1
2
3
4
5
6
7
8
9
10
11
%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

变异函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
%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

 

  

 

  

 

  

遗传算法

上一篇:Flex简介


下一篇:如何自定义Grunt任务