变异和交叉
设定变异量
遗传算法使用 MutationFcn
选项应用突变。默认变异选项 @mutationgaussian
将从高斯分布中选择的随机数或变异添加到父代向量的每个条目中。通常,突变量与分布的标准差成比例,并且在每一代都会减少。您可以通过元胞数组中包含的 Scale
和 Shrink
输入来控制算法在每一代应用于父代的平均突变量:
options = optimoptions('ga',... 'MutationFcn',{@mutationgaussian Scale Shrink});
Scale
和 Shrink
是标量,每个都有默认值 1
。
Scale
控制第一代变异的标准差。该值是Scale
乘以初始种群的范围,您可以通过InitialPopulationRange
选项指定。Shrink
控制平均变异减少的速度。标准差呈线性减小,因此其最终值在第一代等于其初始值的 1 –Shrink
倍。例如,如果Shrink
的默认值为 1,则变异在最后一步减少到 0。
您可以通过选择绘图函数 @gaplotdistance
和 @gaplotrange
,然后在诸如 最小化拉斯特里金函数 中描述的问题上运行遗传算法来查看变异的效果。下图是设置随机数生成器后的绘图。
rng default % For reproducibility options = optimoptions('ga','PlotFcn',{@gaplotdistance,@gaplotrange},... 'MaxStallGenerations',200); % to get a long run [x,fval] = ga(@rastriginsfcn,2,[],[],[],[],[],[],[],options);
ga stopped because it exceeded options.MaxGenerations.
上图显示的是每代中点之间的平均距离。随着突变量的减少,个体之间的平均距离也随之减少,在最后一代大约为 0。下图显示每代的一条垂直线,显示从最小到最大适应度值的范围,以及平均适应度值。随着变异数量的减少,范围也会减小。这些图表明,减少突变量会降低后代的多样性。
为了进行比较,下图显示了将 Shrink
设置为 0.5
时出现的相同图。
options = optimoptions('ga',options,... 'MutationFcn',{@mutationgaussian,1,.5}); [x,fval] = ga(@rastriginsfcn,2,[],[],[],[],[],[],[],options);
ga stopped because it exceeded options.MaxGenerations.
这一次,到最后一代,突变的平均数量减少了 1/2。因此,个体之间的平均距离减少的幅度比以前更小。
设置交叉分数
CrossoverFraction
选项指定了除精英子代之外的每个种群中由交叉子代构成的比例。交叉分数为 1
意味着除精英个体之外的所有子代都是交叉子代,而交叉分数为 0
意味着所有子代都是变异子代。下面的例子表明,这两种极端情况都不是优化函数的有效策略。
该示例使用适应度函数,其在某一点的值是该点处坐标绝对值的总和。即:
您可以通过将适应度函数设置为,将此函数定义为匿名函数
fun = @(x) sum(abs(x));
使用 0.8
的默认值作为 CrossoverFraction
选项来运行示例。
nvar = 10; options = optimoptions('ga',... 'InitialPopulationRange',[-1;1],... 'PlotFcn',{@gaplotbestf,@gaplotdistance}); rng(14,'twister') % For reproducibility [x,fval] = ga(fun,nvar,[],[],[],[],[],[],[],options)
ga stopped because the average change in the fitness value is less than options.FunctionTolerance.
x = 1×10
-0.0020 -0.0134 -0.0067 -0.0028 -0.0241 -0.0118 0.0021 0.0113 -0.0021 -0.0036
fval = 0.0799
无变异的交叉
要查看遗传算法在没有变异时的表现,请将 CrossoverFraction
选项设置为 1.0
并重新运行求解器。
options.CrossoverFraction = 1; [x,fval] = ga(fun,nvar,[],[],[],[],[],[],[],options)
ga stopped because the average change in the fitness value is less than options.FunctionTolerance.
x = 1×10
-0.0241 -0.0043 -0.0146 0.0117 -0.0118 -0.0432 0.0129 0.0160 0.0227 0.0032
fval = 0.1645
在这种情况下,算法从初始种群中的个体中选择基因并重新组合它们。由于没有变异,该算法无法创造任何新基因。该算法在第 8 代使用这些基因生成最佳个体,其中最佳适应度图变为水平。此后,它会创建最佳个体的新副本,然后将这些副本选入代。到第 17 代时,种群中所有个体都相同,即最优个体。当这种情况发生时,个体之间的平均距离为 0。由于该算法在第 8 代之后无法提高最佳适应度值,因此它会在 50 代之后停止,因为停止代数设置为 50。
无交叉变异
要查看遗传算法在没有交叉时的表现,请将 CrossoverFraction
选项设置为 0
。
options.CrossoverFraction = 0; [x,fval] = ga(fun,nvar,[],[],[],[],[],[],[],options)
ga stopped because the average change in the fitness value is less than options.FunctionTolerance.
x = 1×10
-0.5820 -0.0124 0.4912 0.0405 0.0017 0.0052 -0.9718 0.1179 -0.0602 -0.1452
fval = 2.4281
在这种情况下,算法所应用的随机变化永远不会提高第一代最佳个体的适应度值。虽然它改进了其他个体的个体基因,但正如您在上图中通过适应度函数平均值的下降所看到的那样,这些改进的基因永远不会与最佳个体的基因相结合,因为没有交叉。结果,最佳适应度图是水平的,并且算法在第 50 代停止。
比较不同交叉分数的结果
运行此示例时包含的示例 deterministicstudy.m
将对将遗传算法应用于拉斯特里金函数的结果进行比较,其中 CrossoverFraction
选项设置为 0
、0.2
、0.4
、0.6
、0.8
和 1
。该示例运行了 10 代。在每一代中,该示例针对 CrossoverFraction
选项的每个值绘制了所有前几代中最佳适应度值的平均值和标准差。
运行示例。
deterministicstudy
ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance.
下图显示了 10 代中每个交叉分数的最佳适应度值的平均值和标准差。上图以颜色编码显示了每代中的最佳适应度值。
对于这个适应度函数,将交叉分数设置为 0.8
可获得最佳结果。然而,对于另一个适应度函数,不同的交叉分数设置可能会产生最佳结果。