遗传算法选项的影响
此示例展示了遗传算法函数 ga
的一些选项的效果。您可以使用 optimoptions
函数创建和更改选项。
为 ga
设置问题
ga
使用遗传算法寻找函数的最小值。对于此示例,使用 ga
来最小化适应度函数 shufcn
,它是两个变量的实值函数。
通过调用 shufcn
在范围 = [-2 2;-2 2]
上绘制 plotobjective
,运行此示例时会包含该函数。
plotobjective(@shufcn,[-2 2; -2 2]);
要使用 ga
求解器,请提供至少两个输入参量:适应度函数和问题中的变量数量。ga
返回的前两个输出参量是 x
(找到的最佳点)和 Fval
(最佳点处的函数值)。第三个输出参量 exitFlag
表示 ga
停止的原因。ga
还可以返回第四个参量 Output
,其中包含有关求解器性能的信息。
FitnessFunction = @shufcn; numberOfVariables = 2;
运行 ga
求解器。
rng default % For reproducibility [x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables);
ga stopped because the average change in the fitness value is less than options.FunctionTolerance.
fprintf('The number of generations is: %d\n', Output.generations);
The number of generations is: 124
fprintf('The number of function evaluations is: %d\n', Output.funccount);
The number of function evaluations is: 5881
fprintf('The best function value found is: %g\n', Fval);
The best function value found is: -186.199
如果您在没有 rng default
命令的情况下运行此示例,您的结果可能会有所不同,因为 ga
是一种随机算法。
遗传算法的工作原理
遗传算法使用一组适用于种群的运算符来对种群进行操作。种群是设计空间中的一组点。初始种群默认是随机生成的。该算法利用当前代个体的适应度来计算代种群。有关详细信息,请参阅遗传算法的工作原理。
添加可视化
为了在求解器运行时可视化其性能,请使用 'PlotFcn'
设置 optimoptions
选项。在这种情况下,在元胞数组中选择两个绘图函数。设置 gaplotbestf
,绘制每代种群的最佳得分和平均得分。还设置 gaplotstopping
,它绘制了满足的停止条件的百分比。
opts = optimoptions(@ga,'PlotFcn',{@gaplotbestf,@gaplotstopping});
运行 ga
求解器,包括 opts
参量。
[x,Fval,exitFlag,Output] = ...
ga(FitnessFunction,numberOfVariables,[],[],[],[],[],[],[],opts);
ga stopped because the average change in the fitness value is less than options.FunctionTolerance.
指定种群选项
种群选项会对求解器性能产生很大的影响。每次迭代的速度取决于种群规模:种群越大,迭代速度越慢。相反,种群越大,ga
探索就越彻底,从而可以找到更好的解。同样,更大的初始范围可以导致更彻底的探索,但可能需要更大的种群以类似的彻底性探索更大的范围。
指定种群大小
ga
使用均匀随机数生成器创建默认初始种群。当决策变量的数量小于 5 时,ga
使用的默认种群规模为 50,否则为 200。对于某些问题,默认大小可能不太适用;例如,较小的种群规模对于较小的问题就足够了。由于当前问题只有两个变量,因此指定种群规模为 10。在现有选项 PopulationSize
中将选项 opts
的值设置为 10。
opts.PopulationSize = 10;
指定初始种群范围
生成初始种群的默认方法是使用均匀随机数生成器。对于没有整数约束的问题,ga
创建一个初始种群,其中所有点都在 -10 到 10 的范围内。例如,您可以使用以下命令在默认范围内生成大小为三的种群:
Population = [-10,-10] + 20*rand(3,2);
您可以通过改变 InitialPopulationRange
选项来设置初始范围。该范围必须是一个具有两行的矩阵。如果范围只有一列,即 2×1,则每个变量的范围都是给定的范围。例如,如果将范围设置为 [-1; 1]
,则两个变量的初始范围都是 -1 到 1。要为每个变量指定不同的初始范围,必须将范围指定为具有两行和 numberOfVariables
列的矩阵。例如,如果将范围设置为 [-1 0; 1 2]
,则第一个变量的范围为 -1 到 1,第二个变量的范围为 0 到 2(每列对应一个变量)。
修改现有选项 InitialPopulationRange
中选项 opts
的值。
opts.InitialPopulationRange = [-1 0; 1 2];
运行 ga
求解器。
[x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables,[],[],[], ...
[],[],[],[],opts);
ga stopped because the average change in the fitness value is less than options.FunctionTolerance.
fprintf('The number of generations is: %d\n', Output.generations);
The number of generations is: 67
fprintf('The number of function evaluations is: %d\n', Output.funccount);
The number of function evaluations is: 614
fprintf('The best function value found is: %g\n', Fval);
The best function value found is: -179.987
重现结果
默认情况下,ga
从使用 MATLAB® 随机数生成器创建的随机初始种群开始。求解器使用同样使用这些相同随机数生成器的 ga
运算符来生成下一代。每次生成随机数时,随机数生成器的状态都会发生变化。因此,即使您不更改任何选项,再次运行求解器时也会得到不同的结果。
运行求解器两次来显示这种现象。
运行 ga
求解器。
[x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables);
ga stopped because the average change in the fitness value is less than options.FunctionTolerance.
fprintf('The best function value found is: %g\n', Fval);
The best function value found is: -186.484
再次运行 ga
。
[x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables);
ga stopped because the average change in the fitness value is less than options.FunctionTolerance.
fprintf('The best function value found is: %g\n', Fval);
The best function value found is: -185.867
ga
在两次运行中给出了不同的结果,因为随机数生成器的状态在两次运行中发生了变化。
如果您想在运行 ga
之前重现结果,您可以保存随机数流的状态。
thestate = rng;
运行 ga
。
[x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables);
ga stopped because the average change in the fitness value is less than options.FunctionTolerance.
fprintf('The best function value found is: %g\n', Fval);
The best function value found is: -186.467
重置流并重新运行 ga
。结果与之前的运行相同。
rng(thestate); [x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables);
ga stopped because the average change in the fitness value is less than options.FunctionTolerance.
fprintf('The best function value found is: %g\n', Fval);
The best function value found is: -186.467
如果在指定重现结果之前运行 ga
,只要您有 output
结构体,您就可以重置随机数生成器。
strm = RandStream.getGlobalStream; strm.State = Output.rngstate.State;
重新运行 ga
。再次,结果相同。
[x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables);
ga stopped because the average change in the fitness value is less than options.FunctionTolerance.
fprintf('The best function value found is: %g\n', Fval);
The best function value found is: -186.467
修改停止标准
ga
使用四种不同的标准来确定何时停止求解器。当达到最大代数时,ga
会停止 ga
默认情况下,该数字是变量数量的 100 倍。ga 还会检测最佳适应度值是否在给定的秒数内(停顿时间限制)或某个代数内(最大停顿代数)没有改变。另一个标准是以秒为单位的最大时间限制。修改停止标准,将最大代数增加到 300,将最大停顿代数增加到 100。
opts = optimoptions(opts,'MaxGenerations',300,'MaxStallGenerations', 100);
重新运行 ga
求解器。
[x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables,[],[],[], ...
[],[],[],[],opts);
ga stopped because the average change in the fitness value is less than options.FunctionTolerance.
fprintf('The number of generations is: %d\n', Output.generations);
The number of generations is: 299
fprintf('The number of function evaluations is: %d\n', Output.funccount);
The number of function evaluations is: 2702
fprintf('The best function value found is: %g\n', Fval);
The best function value found is: -186.729
指定 ga
运算符
ga
从种群中的一组随机点开始,并使用运算符来产生下一代种群。不同的操作符包括缩放、选择、交叉和变异。该工具箱为每个操作员提供了几个指定的函数。为 FitnessScalingFcn
指定 fitscalingprop
,为 SelectionFcn
指定 selectiontournament
。
opts = optimoptions(@ga,'SelectionFcn',@selectiontournament, ... 'FitnessScalingFcn',@fitscalingprop);
重新运行 ga
。
[x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables,[],[],[], ...
[],[],[],[],opts);
ga stopped because the average change in the fitness value is less than options.FunctionTolerance.
fprintf('The number of generations is: %d\n', Output.generations);
The number of generations is: 52
fprintf('The number of function evaluations is: %d\n', Output.funccount);
The number of function evaluations is: 2497
fprintf('The best function value found is: %g\n', Fval);
The best function value found is: -186.417
最佳函数值可以根据指定的运算符而变好或变坏。尝试不同的运算符通常是确定哪组运算符最适合您的问题的最佳方法。