主要内容

使用遗传算法编码和最小化健身函数

This example shows how to create and minimize a fitness function for the genetic algorithm solvergausing three techniques:

  • 基本的

  • Including additional parameters

  • Vectorized for speed

基本的Fitness Function

这basic fitness function is Rosenbrock's function, a common test function for optimizers. The function is a sum of squares:

f ( x ) = 1 0 0 ( x 1 2 - x 2 ) 2 + ( 1 - x 1 ) 2

该函数在该点的最小值为零[1,1]。Because the Rosenbrock function is quite steep, plot the logarithm of one plus the function.

fsurf(@(x,y)log(1 + 100*(x.^2 - y).^2 + (1 - x).^2),[0,2]) title('log(1 + 100*(x(1)^2 - x(2))^2 + (1 - x(1))^2)') view(-13,78) holdh1 = plot3(1,1,0.1,'r*',“标记”,12); legend(h1,'Minimum','地点','best');抓住离开

Fitness Function Code

simple_fitnessfunction file implements Rosenbrock's function.

typesimple_fitness
function y = simple_fitness(x) %SIMPLE_FITNESS fitness function for GA % Copyright 2004 The MathWorks, Inc. y = 100 * (x(1)^2 - x(2)) ^2 + (1 - x(1))^2;

健身功能必须接受一个输入xwherex是一个行矢量,其元素与问题中的变量数量一样多。健身函数计算函数的值,并在其一个返回参数中返回标量值y

Minimize Usingga

使用ga, pass a function handle to the fitness function as well as the number of variables in the problem. To havegaexamine the relevant region, include bounds-3 <= x(i)<= 3。Pass the bounds as the fifth and sixth arguments after数字变量。Forgasyntax details, seega

ga是随机算法。对于可重复性,请设置随机数流。

RNGdefault%可再现性fitnessfunction = @simple_fitness;numberOfvariables = 2;lb = [-3,-3];ub = [3,3];[x,fval] = ga(fitnessfunction,numberOfvariables,[],[],[],[],[],lb,ub)
Optimization terminated: maximum number of generations exceeded.
x =1×21.5083 2.2781
fval = 0.2594

x求解器返回是最终人口中的最佳点ga。这fvalis the value of the functionsimple_fitnessevaluated at the pointxga没有找到特别好的解决方案。有关改进解决方案的方法,请参阅遗传算法选择的影响

Fitness Function with Additional Parameters

Sometimes your fitness function has extra parameters that act as constants during the optimization. For example, a generalized Rosenbrock's function can have extra parameters representing the constants 100 and 1:

f ( x , a , b ) = a ( x 1 2 - x 2 ) 2 + ( b - x 1 ) 2

ab是在优化过程中充当常数的适应性函数的参数(它们不是最小化的一部分)。这parameterized_fitness.m文件实现此参数化健身功能。

typeparameterized_fitness
函数y = parameterized_fitness(x,p1,p2)%parameterized_fitness for ga%版权所有2004 Mathworks,Inc。y = p1 *(x(1) ^2 -x(2 -x(2)) ^2 +(p2 -x(p2 -x)1))^2;

Minimize Using Additional Parameters

Use an anonymous function to capture the values of the additional arguments, namely, the constantsab。Create a function handleFitnessFunctionto an anonymous function that takes one inputx, 和callsparameterized_fitnesswithx,a, 和b。这anonymous function contains the values of a and b that exist when the function handle is created.

a = 100; b = 1;% define constant valuesfitnessfunction = @(x)parameterized_fitness(x,a,b);[x,fval] = ga(fitnessfunction,numberOfvariables,[],[],[],[],[],lb,ub)
Optimization terminated: maximum number of generations exceeded.
x =1×21.3198 1.7434
fval = 0.1025

传递额外的参数

矢量化适应性函数

为了提高速度,矢量化your fitness function. A vectorized fitness function computes the fitness of a collection of points at once, which generally saves time over evaluating these points individually. To write a vectorized fitness function, have your function accept a matrix, where each matrix row represents one point, and have the fitness function return a column vector of fitness function values.

To change theparameterized_fitnessfunction file to a vectorized form:

  • Change each variablex(i)tox(:,i),意思是变量的列向量对应于x(i)

  • Change each vector multiplication*to。*和each exponentiation^to。^表明操作是元素的。此代码中没有矢量乘法,因此只需更改指数即可。

typevectorized_fitness
函数y = vectorized_fitness(x,p1,p2)%vectorized_fitness fitness for ga%版权所有2004-2010 The MathWorks,Inc。y = p1 *(x(x(:,1)。^2 -x(:,2))。^2 +(p2 -x(:,1))。^2;

此矢量化版本的健身功能采用矩阵xwith an arbitrary number of points, meaning and arbitrary number of rows, and returns a column vectorywith the same number of rows asx

告诉健身的解决者function is vectorized in the'UseVectorized'选项。

options = optimoptions(@ga,'UseVectorized',true);

将选项作为最后一个参数ga

VFitnessFunction = @(x) vectorized_fitness(x,100,1); [x,fval] = ga(VFitnessFunction,numberOfVariables,[],[],[],[],lb,ub,[],options)
Optimization terminated: maximum number of generations exceeded.
x =1×21.6219 2.6334
FVAL = 0.3876

速度有什么区别?时间在有或没有矢量化的情况下进行优化。

tic [x,fval] = ga(vfitnessfunction,numberOfvariables,[],[],[],[],[],lb,ub,[],options);
Optimization terminated: maximum number of generations exceeded.
v = toc;tic [x,fval] = ga(fitnessfunction,numberOfvariables,[],[],[],[],[],lb,ub);
Optimization terminated: maximum number of generations exceeded.
nv = toc;fprintf('使用矢量化需要%f秒。没有矢量化需要%f秒。\ n',V,NV)
Using vectorization took 0.153337 seconds. No vectorization took 0.212880 seconds.

在这种情况下,矢量化的改进不是很好,因为计算健身函数所需的时间很少。但是,对于更多耗时的健身功能,矢量化可能会有所帮助。看Vectorize the Fitness Function

相关话题