点击上方蓝字关注“公众号”
![a06308e366ca3c80f03bcda54463b660.gif](https://i-blog.csdnimg.cn/blog_migrate/ae02a8a880b626cc5499c380a651eb31.gif)
MATLAB遗传算法及其实现
遗传算法以一种群体中的所有个体为对象,并利用随机化技术指导对一个被编码的参数空间进行高效搜索。
其中,选择、交叉和变异构成了遗传算法的遗传操作;参数编码、初始群体的设定、适应度函数的设计、遗传操作设计、控制参数设定等5个要素组成了遗传算法的核心内容。
![09f92f2ecb10292c1d018063454180aa.gif](https://i-blog.csdnimg.cn/blog_migrate/6e8d14d9c165acb48bddd502f35e6d41.gif)
主要步骤如下所示:
(1)编码:GA在进行搜索之前先将解空间的解数据表示成遗传空间的基因型串结构数据,这些串结构数据的不同组合便构成了不同的点。
(2)初始群体的生成:随机产生N个初始串结构数据,每个串结构数据称为一个个体,N个个体构成了—个群体。
GA以这N个串结构数据作为初始点开始迭代。
(3)适应性值评估检测:适应性函数表明个体或解的优劣性。对于不同的问题,适应性函数的定义方式也不同。
(4)选择:选择的目的是为了从当前群体个选出优良的个体,使它们有机会作为父代为下一代繁殖子孙。
遗传算法通过选择过程体现这一思想,进行选择的原则是适应性强的个体为下一代贡献一个或多个后代的概率大。
选择实现了达尔文的适者生存原则。
(5)交叉:交叉操作是遗传算法中最主要的遗传操作。
通过交叉操作可以得到新一代个体,新个体组合了其父辈个体的特性。交叉体现了信息交换的思想。
(6)变异:变异首先在群体中随机选择一个个体,对于选中的个体以一定的概率随机地改变串结构数据中某个串的值。
同生物界一样,GA中变异发生的概率很低,通常取值在0.001~0.01之间。变异为新个体的产中提供了机会。
![d717bc326a6ba1ec8124284dbaab0c1f.gif](https://i-blog.csdnimg.cn/blog_migrate/43d5c63806fccf92519b2567215fcb4e.gif)
![8949f581678cf7ee587ae50ab44b43ab.png](https://i-blog.csdnimg.cn/blog_migrate/262133b651a96dd5aa2e75af38142c7f.png)
![1fc1dbaa1d1411462b486dd691bc07e9.png](https://i-blog.csdnimg.cn/blog_migrate/c9bcc7171b739317579f858281aa3924.jpeg)
壹
遗传算法的MATLAB实现
需要如下主函数:
编码和种群生成
function [pop] = initializega(num,bounds,evalFN,evalOps,options)
% pop - the initial, evaluated, random population
% num - the size of the population, i.e. the number to create
% bounds - the number of permutations in an individual (e.g., number
% of cities in a tsp
% evalFN - the evaluation fn, usually the name of the .m file for evaluation
% evalOps- any options to be passed to the eval function defaults [ ]
% options- options to the initialize function, ie. [eps, float/binary, prec]
% where eps is the epsilon value and the second option is 1 for
% orderOps, prec is the precision of the variables defaults [1e-6 1]
01
交叉
function [c1,c2] = arithXover(p1,p2,bounds,Ops)
% Arith crossover takes two parents P1,P2 and performs an interpolation
% along the line formed by the two parents.
%
% function [c1,c2] = arithXover(p1,p2,bounds,Ops)
% p1 - the first parent ( [solution string function value] )
% p2 - t