main content

hybrid scheme in the genetic algorithm -凯发k8网页登录

this example shows how to use a hybrid scheme to optimize a function using the genetic algorithm and another optimization method. ga can quickly reach a neighborhood of a local minimum, but it can require many function evaluations to achieve convergence. to speed the solution process, first run ga for a small number of generations to approach an optimum point. then use the solution from ga as 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)=100(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 the dejong2fcn file.

type dejong2fcn.m
function scores = dejong2fcn(pop)
�jong2fcn 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.

genetic algorithm solution

first, use ga alone 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 run ga using the options.

rng default
[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, xlabel generation, ylabel fitness value contains 2 objects of type line. one or more of the lines displays its values using only markers these objects represent best fitness, mean fitness. axes object 2 with title stopping criteria, xlabel % of criteria met contains an object of type bar.

x = 1×2
    0.3454    0.1444
fval = 0.4913

using the default stopping criteria, ga does not provide a very accurate solution. you can change the stopping criteria to try to find a more accurate solution, but ga requires many function evaluations to approach the global optimum x* = (1,1).

instead, perform a more efficient local search that starts where ga stops by using the hybrid function option in ga.

adding a hybrid function

a hybrid function begins from the point where ga stops. hybrid function choices are fminsearch, patternsearch, or fminunc. because this optimization example is smooth and unconstrained, use fminunc as the hybrid function. provide fminunc with plot options as an additional argument when specifying the hybrid function.

fminuncoptions = optimoptions(@fminunc,'plotfcn',{'optimplotfval','optimplotx'});
options = optimoptions(options,'hybridfcn',{@fminunc, fminuncoptions});

run ga again with fminunc as 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, xlabel iteration, ylabel function value contains a line object which displays its values using only markers. axes object 2 with title current point, xlabel number of variables: 2, ylabel 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, xlabel generation, ylabel fitness value contains 2 objects of type line. one or more of the lines displays its values using only markers these objects represent best fitness, mean fitness. axes object 2 with title stopping criteria, xlabel % of criteria met contains an object of type bar.

x = 1×2
    1.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

the ga plot shows the best and mean values of the population in every generation. the plot title identifies the best value found by ga when it stops. the hybrid function fminunc starts from the best point found by ga. the fminunc plot shows the solution x and fval, which result from using ga and fminunc together. in this case, using a hybrid function improves the accuracy and efficiency of the solution. the output.hybridflag field shows that fminunc stops with exit flag 1, indicating that x is a true local minimum.

related topics

网站地图