遗传算法(GA)

遗传算法

背景介绍

  GA(Genetic Algorithm):是模拟达尔文生物进化论的自然选择遗传学机理的生物进化过程的计算模型,不需要确定的规则就能自动获取和指导优化的搜索空间,自适应地调整搜索方向,是一种通过模拟自然进化过程搜索最优解的方法。
初代种群产生之后,按照适者生存优胜劣汰的原理,逐代(generation)演化产生出越来越好的近似解,在每一代,根据问题域中个体的适应度(fitness)大小选择个体,并借助于自然遗传学的遗传算子进行组合交叉(crossover)变异(mutation),产生出代表新的解集的种群。

核心思想

  1. 随机产生一些初始种群

  2. 根据适应度对种群采用某种方式进行自然选择

  3. 对选择剩余的种群进行遗传操作,产生新的种群

  4. 回到步骤2,直到满足某个终止条件

  5. 此时剩余的是适应度较好的种群,比较可得该算法的最优解

算法流程

GA

代码实战

代码中所用测试函数可以查看相关文档,测试函数(Test Function)

GA_main.m

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
clear;clc;close all;
%自变量取值范围
range_x=[ones(1,1),-ones(1,1)]*500;
%维度
n=size(range_x,1);
%种群数量
gn=400;
%进入下一代的数量
m=50;
%迭代次数
times=200;
%随机产生一些种群
group=zeros(n,gn);
for k=1:n
group(k,:)=(rand(1,gn))*(range_x(k,2)-range_x(k,1))+range_x(k,1);
end
%设置当前最优解
best_value=zeros(1,times);
tic;
for k=1:times
y=f(group);
%全部变成正值
if min(y)<0
tem=y-min(y)*1.0001;
else
tem=y+0.1;
end
%值越小适应越好
tem=1./tem;
child=zeros(n,gn);
%挑选m个种群进入下一代
for i=1:m
%轮盘赌选择,适应大的选择概率大
temp=zeros(1,gn-i+1);
for j=1:gn-i+1
temp(j)=sum(tem(1:j));
end
temp=temp/temp(gn-i+1);
%保留最合适的物种
choose=find(temp>rand(1),1);
child(:,i)=group(:,choose);
group=[group(:,1:choose-1),group(:,choose+1:end)];
tem=[tem(1:choose-1),tem(choose+1:end)];
end
%染色体交换,保留的物种产生后代时发生基因重组
for i=1:floor((gn-m)/2)
exchange=randperm(m,2);
a=rand(n,1);
child(:,i*2-1+m)=a.*child(:,exchange(1))+(1-a).*child(:,exchange(2));
child(:,i*2+m)=(1-a).*child(:,exchange(1))+a.*child(:,exchange(2));
end
if mod(gn-m,2)==1
exchange=randperm(m,2);
child(:,gn)=(child(:,exchange(1))+child(:,exchange(2)))/2;
end
%基因重组的过程中可能发生染色体变异
if rand(1)<0.1
exchange=randperm(gn-m,1);
a=rand(1);
for j=1:n
child(j,exchange+m)=a.*child(j,exchange+m)+(1-a).*(rand(1)*(range(j,2)-range(j,1))+range(j,1));
end
end
%重组之后后代变成当前种群
group=child;
best_value(k)=min(f(group));
if k>5&&abs(best_value(k)-best_value(k-5))<1e-5
break;
end
end
time=toc;
disp(['用时:',num2str(time),'秒'])
[mini,index]=min(f(group));
disp(['fmin=',num2str(mini)]);
for k=1:n
disp(['x',num2str(k),'=',num2str(group(k,index))]);
end
if n==1
hold on;
plot(group(index),mini,'ro');
plot_x=range_x(1):(range_x(2)-range_x(1))/1000:range_x(2);
plot_y=f(plot_x);
plot(plot_x,plot_y);
text((range_x(1)+range_x(2))/2,max(plot_y)+0.1*(max(plot_y)-min(plot_y)),['用时:',num2str(time),'秒']);
hold off;
end
if n==2
%所求最小值的函数
func=@(x1,x2)x1.*sin(sqrt(abs(x1)))+x2.*sin(sqrt(abs(x2)));
plot_x=range_x(1,1):(range_x(1,2)-range_x(1,1))/1000:range_x(1,2);
plot_y=range_x(2,1):(range_x(2,2)-range_x(2,1))/1000:range_x(2,2);
[plot_x,plot_y] =meshgrid(plot_x,plot_y);
plot_z=func(plot_x,plot_y);
surf(plot_x,plot_y,plot_z);
xlabel('x1');
ylabel('x2');
zlabel('y');
hold on;
plot3(group(1,index),group(2,index),mini,'ko')
text((range_x(1,1)+range_x(1,2))/2,(range_x(2,1)+range_x(2,2))/2,max(max(plot_z))+0.5*(max(max(plot_z))-min(min(plot_z))),['用时:',num2str(time),'秒']);
hold off;
end

f.m

1
2
3
4
5
6
function res=f(x)
func=@(x)(x).*sin(sqrt(abs(x)));
res=zeros(1,size(x,2));
for i=1:size(x,1)
res=res+func(x(i,:));
end



实验结果

GA
$$f(x)=x \cdot \sin(\sqrt{\lvert x \rvert}) \ , \ x \in [-500,500]$$

$$理论值:f(x)_{min}=f(-420.96874592006)=-418.982887272434$$

$$所求值:f(x)_{min}=f(-420.975929624477)=-418.982880761435$$

性能比较

  • 优点:
    • 从群体出发,具有并行性
    • 可用于求解复杂的非线性优化问题
    • 使用概率机制进行迭代,具有随机性
    • 具有可扩展性,容易与其他算法结合
  • 缺点:
    • 受到参数影响较大
    • 可能产生早熟收敛问题
    • 对问题编码表示较为困难
    • 算法对初始种群的选择有一定的依赖性
    • 搜索速度比较慢,要得到较精确的解需要较多的训练时间
-------------本文结束感谢您的阅读-------------
0%