Main Content

Effects of Genetic Algorithm Options

This example shows the effects of some options for the genetic algorithm functionGA。您可以使用最佳选择功能。

设置一个问题GA

GA使用遗传算法搜索最少的函数。对于此示例,请使用GAto minimize the fitness functionShufcn, a real-valued function of two variables.

PlotShufcn超过范围= [-2 2; -2 2]通过打电话plotObjective

plotObjective(@shufcn,[-2 2; -2 2]);

图包含一个轴对象。轴对象包含2个类型表面的对象,轮廓。

使用GAsolver, provide at least two input arguments: a fitness function and the number of variables in the problem. The first two output arguments returned byGAX, the best point found, andFval,最佳点功能值。第三个输出参数,出口,指示原因GA停了下来。GAcan also return a fourth argument,输出, which contains information about the performance of the solver.

fitnessfunction = @shufcn;numberOfvariables = 2;

跑过GA求解器。

rng默认% For reproducibility[X,FVAL,EXITFLAG,输出] = GA(fitnessfunction,numberOfvariables);
Optimization terminated: average change in the fitness value 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('找到的最佳功能值是:%g \ n', Fval);
找到的最佳功能值是:-186.199

如果您在没有这个示例的情况下运行此示例rng defaultcommand, your results can differ, becauseGA是一种随机算法。

遗传算法如何工作

The genetic algorithm works on a population using a set of operators that are applied to the population. A population is a set of points in the design space. The initial population is generated randomly by default. The algorithm computes the next generation of the population using the fitness of the individuals in the current generation. For details, see遗传算法如何工作

Add Visualization

To visualize the solver performance while it is running, set a'PlotFcn'option using最佳选择。In this case, select two plot functions in a cell array. SetGAplotbestf,这绘制了每一代人口的最佳和平均得分。也设置GAplotstopping,其中绘制了满足停止标准的百分比。

选择= optimoptions(@ga,'PlotFcn',{@gaplotbestf,@gaplotstopping});

跑过GA求解器,包括选择argument.

[X,FVAL,EXITFLAG,输出] =...ga(fitnessfunction,numberOfvariables,[],[],[],[],[],[],[],[],[],opts);
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.

图遗传算法包含2个轴对象。Axes object 1 with title Best: -186.71 Mean: -123.753 contains 2 objects of type line. These objects represent Best fitness, Mean fitness. Axes object 2 with title Stopping Criteria contains an object of type bar.

指定Population Options

人口选择可能对求解器的性能产生很大的影响。每次迭代的速度取决于人口规模:较大的人口导致迭代较慢。相反,较大的人口导致GAexploring more thoroughly, so can lead to a better solution. Similarly, a wider initial range can lead to more thorough exploration, but can require a larger population to explore the wider range with a similar thoroughness.

指定Population Size

GAcreates a default initial population by using a uniform random number generator. The default population size used byGAis 50 when the number of decision variables is less than 5, and 200 otherwise. The default size might not work well for some problems; for example, a smaller population size can be sufficient for smaller problems. Since the current problem has only two variables, specify a population size of 10. Set the value of the option人口化to 10 in the existing options,选择

选择。人口化= 10;

指定Initial Population Range

生成初始总体的默认方法使用统一的随机数发生器。对于没有整数限制的问题,GAcreates an initial population where all the points are in the range –10 to 10. For example, you can generate a population of size three in the default range using this command:

Population = [-10,-10] + 20*rand(3,2);

You can set the initial range by changing the初始人群option. The range must be a matrix with two rows. If the range has only one column, that is, it is 2-by-1, then the range of every variable is the given range. For example, if you set the range to[-1; 1],然后两个变量的初始范围为–1至1。要为每个变量指定不同的初始范围,则必须将范围指定为带有两个行和的矩阵numberOfVariablescolumns. For example, if you set the range to[-1 0;1 2], then the first variable has the range –1 to 1, and the second variable has the range 0 to 2 (each column corresponds to a variable).

Modify the value of the option初始人群in the existing options,选择

opts.initialPopulationRange = [-1 0;1 2];

跑过GA求解器。

[x,fval,Exitflag,output] = ga(fitnessfunction,numberOfvariables,[],[],[],[],,,,...[],[],[],[],opts);
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.

图遗传算法包含2个轴对象。Axes object 1 with title Best: -179.987 Mean: -78.6061 contains 2 objects of type line. These objects represent Best fitness, Mean fitness. Axes object 2 with title Stopping Criteria contains an object of type bar.

fprintf('The number of generations is: %d\n',output.generations);
世代的数量为:67
fprintf('The number of function evaluations is: %d\n',output.funccount);
The number of function evaluations is: 614
fprintf('找到的最佳功能值是:%g \ n', Fval);
The best function value found is: -179.987

Reproduce Results

By default,GA从使用MATLAB®随机数生成器创建的随机初始总体开始。求解器使用GA也使用这些相同随机数生成器的运算符。每次生成随机数时,随机数生成器的状态都会更改。因此,即使您不更改任何选项,再次运行求解器时也可以获得不同的结果。

跑过solver twice to show this phenomenon.

跑过GA求解器。

[X,FVAL,EXITFLAG,输出] = GA(fitnessfunction,numberOfvariables);
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
fprintf('找到的最佳功能值是:%g \ n', Fval);
The best function value found is: -186.484

GAagain.

[X,FVAL,EXITFLAG,输出] = GA(fitnessfunction,numberOfvariables);
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
fprintf('找到的最佳功能值是:%g \ n', Fval);
找到的最佳功能值是:-185.867

GA给两分,因为th不同的结果e state of the random number generator changes from one run to another.

If you want to reproduce your results before you runGA, you can save the state of the random number stream.

thestate = rng;

GA

[X,FVAL,EXITFLAG,输出] = GA(fitnessfunction,numberOfvariables);
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
fprintf('找到的最佳功能值是:%g \ n', Fval);
The best function value found is: -186.467

Reset the stream and rerunGA。结果与先前的运行相同。

rng(thestate); [x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables);
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
fprintf('找到的最佳功能值是:%g \ n', Fval);
The best function value found is: -186.467

如果您运行GA在指定重现结果之前,只要您拥有output结构体。

strm = randstream.getGlobalStream;strm.State = output.rngstate.state;

RerunGA。同样,结果是相同的。

[X,FVAL,EXITFLAG,输出] = GA(fitnessfunction,numberOfvariables);
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
fprintf('找到的最佳功能值是:%g \ n', Fval);
The best function value found is: -186.467

修改停止标准

GA使用四个不同的标准来确定何时阻止求解器。GA当它达到最大世代数时停止;默认情况下,此数字是变量数量的100倍。GAalso detects if the best fitness value does not change for some time given in seconds (stall time limit), or for some number of generations (maximum stall generations). Another criteria is the maximum time limit in seconds. Modify the stopping criteria to increase the maximum number of generations to 300 and the maximum stall generations to 100.

选择= optimoptions(opts,“最大化”,300,'MaxStallGenerations', 100);

Rerun theGA求解器。

[x,fval,Exitflag,output] = ga(fitnessfunction,numberOfvariables,[],[],[],[],,,,...[],[],[],[],opts);
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.

图遗传算法包含2个轴对象。轴对象1具有最佳标题:-186.729平均值:-186.202包含2个类型行的对象。这些物体代表最佳健身,平均健身。轴对象2带有标题停止标准的对象包含类型栏的对象。

fprintf('The number of generations is: %d\n',output.generations);
世代数为:299
fprintf('The number of function evaluations is: %d\n',output.funccount);
功能评估的数量为:2702
fprintf('找到的最佳功能值是:%g \ n', Fval);
The best function value found is: -186.729

指定GA操作员

GA从人口中的一组随机点开始,然后使用操作员生产下一代人口。不同的操作员是缩放,选择,交叉和突变。该工具箱提供了几个功能,可以为每个操作员指定。指定fitscalingprop为了Fitnessscalingfcnand选择图为了SelectionFcn

选择= optimoptions(@ga,'SelectionFcn',@selectionTournament,...'Fitnessscalingfcn',@fitscalingprop);

RerunGA

[x,fval,Exitflag,output] = ga(fitnessfunction,numberOfvariables,[],[],[],[],,,,...[],[],[],[],opts);
Optimization terminated: average change in the fitness value 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('找到的最佳功能值是:%g \ n', Fval);
找到的最佳功能值是:-186.417

最佳功能值可以根据指定的操作员改善或恶化。尝试不同的操作员通常是确定哪种操作员最适合您的问题的最佳方法。

也可以看看

Related Topics