遗传算法(Genetic Algorithm)

遗传算法(Genetic Algorithm)
1 原理
遗传算法(Genetic Algorithm)是一类借鉴生物界的进化规律(适者生存,优胜劣汰遗传机制)演化而来的随机化搜索方法。它是由美国的J.Holland教授1975年首先提出,其主要特点是直接对结构对象进行操作,不存在求导和函数连续性的限定;具有内在的隐并行性和更好的全局寻优能力;采用概率化的寻优方法,能自动获取和指导优化的搜索空间,自适应地调整搜索方向,不需要确定的规则。遗传算法的这些性质,已被人们广泛地应用于组合优化、机器学习、信号处理、自适应控制和人工生命等领域。它是现代有关智能计算中的关键技术之一。
遗传算法是模拟达尔文的遗传选择和自然淘汰的生物进化过程的计算模型。它的思想源于生物遗传学和适者生存的自然规律,是具有“生存+检测”的迭代过程的搜索算法。遗传算法以一种群体中的所有个体为对象,并利用随机化技术指导对一个被编码的参数空间进行高效搜索。其中,选择、交叉和变异构成了遗传算法的遗传操作;参数编码、初始群体的设定、适应度函数的设计、遗传操作设计、控制参数设定五个要素组成了遗传算法的核心内容。 作为一种新的全局优化搜索算法,遗传算法以其简单通用、鲁棒性强、适于并行处理以及高效、实用等显著特点,在各个领域得到了广泛应用,取得了良好效果,并逐渐成为重要的智能算法之一。

遗传算法的基本步骤 :
我们习惯上把Holland1975年提出的GA称为传统的GA。它的主要步骤如下:
编码:GA在进行搜索之前先将解空间的解数据表示成遗传空间的基因型串结构数据,这些串结构数据的不同组合便构成了不同的点。
初始群体的生成:随机产生N个初始串结构数据,每个串结构数据称为一个个体, N个个体构成了一个群体。GA以这N个串结构数据作为初始点开始迭代。
适应性值评估检测:适应性函数表明个体或解的优劣性。不同的问题,适应性函数的定义方式也不同。
选择:选择的目的是为了从当前群体中选出优良的个体,使它们有机会作为父代为下一代繁殖子孙。遗传算法通过选择过程体现这一思想,进行选择的原则是适应性强的个体为下一代贡献一个或多个后代的概率大。选择实现了达尔文的适者生存原则。
交换:交换操作是遗传算法中最主要的遗传操作。通过交换操作可以得到新一代个体,新个体组合了其父辈个体的特性。交换体现了信息交换的思想。
变异:变异首先在群体中随机选择一个个体,对于选中的个体以一定的概率随机地改变串结构数据中某个串的值。同生物界一样,GA中变异发生的概率很低,通常取值在0.001~0.01之间。变异为新个体的产生提供了机会。

GA的计算过程为:
选择编码方式
产生初始群体
计算初始群体的适应性值
如果不满足条件 {
   选择
   交换
   变异
   计算新一代群体的适应性值
}

遗传算法的特点:
遗传算法作为一种快捷、简便、容错性强的算法,在各类结构对象的优化过程中显示出明显的优势。与传统的搜索方法相比,遗传算法具有如下特点:
搜索过程不直接作用在变量上,而是在参数集进行了编码的个体。此编码操作, 使得遗传算法可直接对结构对象(集合、序列、矩阵、树、图、链和表)进行操作。
搜索过程是从一组解迭代到另一组解,采用同时处理群体中多个个体的方法,降 低了陷入局部最优解的可能性,并易于并行化。
采用概率的变迁规则来指导搜索方向,而不采用确定性搜索规则。
对搜索空间没有任何特殊要求(如连通性、凸性等),只利用适应性信息,不需要 导数等其它辅助信息,适应范围更广。

2.实例
求函数-100*(x(1)^2-x(2))^2-(1-x(1))^2的最小值,两个变量的取值范围是from [-2.048;-2.048] to [2.048;2.048].
1)使用ga工具箱
X = ga(@(x) -100*(x(1)^2-x(2))^2-(1-x(1))^2,2,[],[],[],[],[-2.048;-2.048],[2.048;2.048])
2)未使用ga工具箱

《遗传算法(Genetic Algorithm)》
%
//
Generic Algorithm for function f(x1,x2) optimum

《遗传算法(Genetic Algorithm)》

clear all;
《遗传算法(Genetic Algorithm)》close all;
《遗传算法(Genetic Algorithm)》
《遗传算法(Genetic Algorithm)》

%
//
Parameters

《遗传算法(Genetic Algorithm)》

Size
=
80
;
《遗传算法(Genetic Algorithm)》G

=
100
;
《遗传算法(Genetic Algorithm)》CodeL

=
10
;
《遗传算法(Genetic Algorithm)》
《遗传算法(Genetic Algorithm)》umax

=
2.048
;
《遗传算法(Genetic Algorithm)》umin

=-
2.048
;
《遗传算法(Genetic Algorithm)》
《遗传算法(Genetic Algorithm)》E

=
round(rand(Size,
2
*
CodeL));    
%
//
Initial Code 产生初始群体

《遗传算法(Genetic Algorithm)》


《遗传算法(Genetic Algorithm)》

%
//
Main Program

《遗传算法(Genetic Algorithm)》

for
 k
=
1
:
1
:G
《遗传算法(Genetic Algorithm)》    time(k)

=
k;
《遗传算法(Genetic Algorithm)》
《遗传算法(Genetic Algorithm)》    

%
//
选择 %
//
计算目标函数    

《遗传算法(Genetic Algorithm)》

    
for
 s
=
1
:
1
:Size 
%
//
对每一行

《遗传算法(Genetic Algorithm)》

        m
=
E(s,:);
《遗传算法(Genetic Algorithm)》        y1

=
0
;y2
=
0
;
《遗传算法(Genetic Algorithm)》
《遗传算法(Genetic Algorithm)》        

%
//
Uncoding

《遗传算法(Genetic Algorithm)》

        m1
=
m(
1
:
1
:CodeL);
《遗传算法(Genetic Algorithm)》        

for
 i
=
1
:
1
:CodeL
《遗传算法(Genetic Algorithm)》            y1

=
y1
+
m1(i)
*
2
^
(i

1
);
《遗传算法(Genetic Algorithm)》        end
《遗传算法(Genetic Algorithm)》        x1

=
(umax

umin)
*
y1
/
1023
+
umin; 
%
//
计算参数1

《遗传算法(Genetic Algorithm)》


《遗传算法(Genetic Algorithm)》        m2

=
m(CodeL
+
1
:
1
:
2
*
CodeL);
《遗传算法(Genetic Algorithm)》        

for
 i
=
1
:
1
:CodeL
《遗传算法(Genetic Algorithm)》            y2

=
y2
+
m2(i)
*
2
^
(i

1
);
《遗传算法(Genetic Algorithm)》        end
《遗传算法(Genetic Algorithm)》        x2

=
(umax

umin)
*
y2
/
1023
+
umin; 
%
//
计算参数2

《遗传算法(Genetic Algorithm)》


《遗传算法(Genetic Algorithm)》        F(s)

=
100
*
(x1
^
2

x2)
^
2
+
(
1

x1)
^
2

%
//
计算目标函数 ,F是向量

《遗传算法(Genetic Algorithm)》

    end
《遗传算法(Genetic Algorithm)》
《遗传算法(Genetic Algorithm)》    Ji

=
1
.
/
F;
《遗传算法(Genetic Algorithm)》    

%
//
****** Step 1 : Evaluate BestJ ******

《遗传算法(Genetic Algorithm)》

    BestJ(k)
=
min(Ji); 
%
//
找到F中最大的一项,保存到向量BestJ

《遗传算法(Genetic Algorithm)》


《遗传算法(Genetic Algorithm)》    fi

=
F;                          
%
//
Fitness Function

《遗传算法(Genetic Algorithm)》

    [Oderfi,Indexfi]
=
sort(fi);     
%
//
Arranging fi small to bigger

《遗传算法(Genetic Algorithm)》

    Bestfi
=
Oderfi(Size);           
%
//
Let Bestfi=max(fi)

《遗传算法(Genetic Algorithm)》

    BestS
=
E(Indexfi(Size),:);      
%
//
Let BestS=E(m), m is the Indexfi belong to max(fi)

《遗传算法(Genetic Algorithm)》

    bfi(k)
=
Bestfi;
《遗传算法(Genetic Algorithm)》
《遗传算法(Genetic Algorithm)》    

%
//
****** Step 2 : Select and Reproduct Operation******选择F较大的fi项

《遗传算法(Genetic Algorithm)》

    fi_sum
=
sum(fi);
《遗传算法(Genetic Algorithm)》    fi_Size

=
(Oderfi
/
fi_sum)
*
Size;
《遗传算法(Genetic Algorithm)》
《遗传算法(Genetic Algorithm)》    fi_S

=
floor(fi_Size);       
%
//
Selecting Bigger fi value ,fi_S为80项的向量,每一项为0或1,1表示该项被选择

《遗传算法(Genetic Algorithm)》


《遗传算法(Genetic Algorithm)》    kk

=
1
;
《遗传算法(Genetic Algorithm)》    

for
 i
=
1
:
1
:Size
《遗传算法(Genetic Algorithm)》        

for
 j
=
1
:
1
:fi_S(i)        
%
//
Select and Reproduce

《遗传算法(Genetic Algorithm)》

            TempE(kk,:)
=
E(Indexfi(i),:);
《遗传算法(Genetic Algorithm)》            kk

=
kk
+
1
;              
%
//
kk is used to reproduce

《遗传算法(Genetic Algorithm)》

        end
《遗传算法(Genetic Algorithm)》    end 

%
//
选择完毕

《遗传算法(Genetic Algorithm)》

    fprintf(

size TempE %//d\n

,size(TempE))
《遗传算法(Genetic Algorithm)》    
《遗传算法(Genetic Algorithm)》    

%
//
************ Step 3 : Crossover Operation ************交换

《遗传算法(Genetic Algorithm)》

    pc
=
0.60
;
《遗传算法(Genetic Algorithm)》    n

=
ceil(
20
*
rand);
《遗传算法(Genetic Algorithm)》    

for
 i
=
1
:
2
:(Size

1
)
《遗传算法(Genetic Algorithm)》        temp

=
rand;
《遗传算法(Genetic Algorithm)》        

if
 pc
>
temp                  
%
//
Crossover Condition

《遗传算法(Genetic Algorithm)》

            
for
 j
=
n:
1
:
20

《遗传算法(Genetic Algorithm)》                TempE(i,j)

=
E(i
+
1
,j);
《遗传算法(Genetic Algorithm)》                TempE(i

+
1
,j)
=
E(i,j);
《遗传算法(Genetic Algorithm)》            end
《遗传算法(Genetic Algorithm)》        end
《遗传算法(Genetic Algorithm)》    end
《遗传算法(Genetic Algorithm)》    TempE(Size,:)

=
BestS;
《遗传算法(Genetic Algorithm)》    E

=
TempE;
《遗传算法(Genetic Algorithm)》    fprintf(


size E %//d\n

,size(E))
《遗传算法(Genetic Algorithm)》

%
//
    pause

《遗传算法(Genetic Algorithm)》

    
%
//
************ Step 4: Mutation Operation **************

《遗传算法(Genetic Algorithm)》

    
%
//
pm=0.001;

《遗传算法(Genetic Algorithm)》

    
%
//
pm=0.001-[1:1:Size]*(0.001)/Size; %
//
Bigger fi, smaller Pm

《遗传算法(Genetic Algorithm)》

    
%
//
pm=0.0;    %
//
No mutation

《遗传算法(Genetic Algorithm)》

    pm
=
0.1
;     
%
//
Big mutation

《遗传算法(Genetic Algorithm)》


《遗传算法(Genetic Algorithm)》    

for
 i
=
1
:
1
:Size
《遗传算法(Genetic Algorithm)》        

for
 j
=
1
:
1
:
2
*
CodeL
《遗传算法(Genetic Algorithm)》            temp

=
rand;
《遗传算法(Genetic Algorithm)》            

if
 pm
>
temp                
%
//
Mutation Condition

《遗传算法(Genetic Algorithm)》

                
if
 TempE(i,j)
==
0

《遗传算法(Genetic Algorithm)》                    TempE(i,j)

=
1
;
《遗传算法(Genetic Algorithm)》                

else

《遗传算法(Genetic Algorithm)》                    TempE(i,j)

=
0
;
《遗传算法(Genetic Algorithm)》                end
《遗传算法(Genetic Algorithm)》            end
《遗传算法(Genetic Algorithm)》        end
《遗传算法(Genetic Algorithm)》    end
《遗传算法(Genetic Algorithm)》
《遗传算法(Genetic Algorithm)》    

%
//
Guarantee TempPop(30,:) is the code belong to the best individual(max(fi))

《遗传算法(Genetic Algorithm)》

    TempE(Size,:)
=
BestS;
《遗传算法(Genetic Algorithm)》    E

=
TempE;
《遗传算法(Genetic Algorithm)》end
《遗传算法(Genetic Algorithm)》
《遗传算法(Genetic Algorithm)》Max_Value

=
Bestfi
《遗传算法(Genetic Algorithm)》BestS
《遗传算法(Genetic Algorithm)》x1
《遗传算法(Genetic Algorithm)》x2
《遗传算法(Genetic Algorithm)》figure(

1
);
《遗传算法(Genetic Algorithm)》plot(time,BestJ);
《遗传算法(Genetic Algorithm)》xlabel(


Times

);ylabel(

Best J

);
《遗传算法(Genetic Algorithm)》figure(

2
);
《遗传算法(Genetic Algorithm)》plot(time,bfi);
《遗传算法(Genetic Algorithm)》xlabel(


times

);ylabel(

Best F

);

    原文作者:遗传算法
    原文地址: https://www.cnblogs.com/cutepig/archive/2007/08/13/853928.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞