Main Content

Hybrid Scheme in the Genetic Algorithm

This example shows how to use a hybrid scheme to optimize a function using the genetic algorithm and another optimization method.gacan quickly reach a neighborhood of a local minimum, but it can require many function evaluations to achieve convergence. To speed the solution process, first rungafor a small number of generations to approach an optimum point. Then use the solution fromgaas the initial point for another optimization solver to perform a faster and more efficient local search.

Rosenbrock's Function

This example uses Rosenbrock's function (also known as Dejong's second function) as the fitness function:

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

Rosenbrock's function is notorious in optimization because of the slow convergence most methods exhibit when trying to minimize this function. Rosenbrock's function has a unique minimum at the point x* = (1,1), where it has a function value f ( x * ) = 0 .

The code for Rosenbrock's function is in thedejong2fcnfile.

typedejong2fcn.m
function scores = dejong2fcn(pop) %DEJONG2FCN Compute DeJongs second function. %This function is also known as Rosenbrock's function % Copyright 2003-2004 The MathWorks, Inc. scores = zeros(size(pop,1),1); for i = 1:size(pop,1) p = pop(i,:); scores(i) = 100 * (p(1)^2 - p(2)) ^2 + (1 - p(1))^2; end

Plot Rosenbrock's function over the range –2 <= x(1) <= 2; –2 <= x(2) <=2.

plotobjective(@dejong2fcn,[-2 2;-2 2]);

Figure contains an axes object. The axes object contains 2 objects of type surface, contour.

遗传算法Solution

First, usegaalone to find the minimum of Rosenbrock's function.

FitnessFcn = @dejong2fcn; numberOfVariables = 2;

Include plot functions to monitor the optimization process.

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

Set the random number stream for reproducibility, and rungausing the options.

rngdefault[x,fval] = ga(FitnessFcn,numberOfVariables,[],[],[],[],[],[],[],options)
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.

Figure Genetic Algorithm contains 2 axes objects. Axes object 1 with title Best: 0.491302 Mean: 664119 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.

x =1×20.3454 0.1444
fval = 0.4913

Using the default stopping criteria,gadoes not provide a very accurate solution. You can change the stopping criteria to try to find a more accurate solution, butgarequires many function evaluations to approach the global optimum x* = (1,1).

Instead, perform a more efficient local search that starts wheregastops by using the hybrid function option inga.

Adding a Hybrid Function

A hybrid function begins from the point wheregastops. Hybrid function choices arefminsearch,patternsearch, orfminunc. Because this optimization example is smooth and unconstrained, usefminuncas the hybrid function. Providefminuncwith plot options as an additional argument when specifying the hybrid function.

fminuncOptions = optimoptions(@fminunc,'PlotFcn',{'optimplotfval','optimplotx'}); options = optimoptions(options,'HybridFcn',{@fminunc, fminuncOptions});

Rungaagain withfminuncas the hybrid function.

[x,fval,exitflag,output] = ga(FitnessFcn,numberOfVariables,[],[],[],[],[],[],[],options)
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.

Figure Optimization Plot Function contains 2 axes objects. Axes object 1 with title Current Function Value: 1.72147e-11 contains an object of type line. Axes object 2 with title Current Point contains an object of type bar.

Local minimum found. Optimization completed because the size of the gradient is less than the value of the optimality tolerance.

Figure Genetic Algorithm contains 2 axes objects. Axes object 1 with title Best: 0.664192 Mean: 4.61776e+06 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.

x =1×21.0000 1.0000
fval = 1.7215e-11
exitflag = 1
output =struct with fields:problemtype: 'unconstrained' rngstate: [1x1 struct] generations: 51 funccount: 2534 message: 'Optimization terminated: average change in the fitness value less than options.FunctionTolerance....' maxconstraint: [] hybridflag: 1

Thegaplot shows the best and mean values of the population in every generation. The plot title identifies the best value found bygawhen it stops. The hybrid functionfminuncstarts from the best point found byga. Thefminuncplot shows the solutionxandfval, which result from usinggaandfminunctogether. In this case, using a hybrid function improves the accuracy and efficiency of the solution. Theoutput.hybridflagfield shows thatfminuncstops with exit flag 1, indicating thatxis a true local minimum.

Related Topics