rastriginsfcn Minimization with Genetic Algorithm

Copyright (c) 2010, The MathWorks, Inc.
All rights reserved.

Contents

Objective Function

We wish find the minimum of the rastriginsfcn function

clear all, close all, clc
rastriginsfcnSurf

Use Genetic Algorithm to Find the Global Minimum

For illustration purposes, force GA to start in top right corner of contour plot by hard coding intitial condition of (5,5) for all points.

X0 = [5,5];
X0 = repmat(X0,100,1);

% Add custom plot function, population size and population starting point
% to options structure
options = gaoptimset('PlotFcns',@rastriginsPlotIterates,...
                     'PopulationSize',100,...
                     'InitialPopulation',X0);

% Solve using Genetic Algorithm
[x fval flag] = ga(@rastriginsfcn, 2, [],[],[],[],[],[],[],options);
hold on;

% Plot final point in red
plot(x(1),x(2),'MarkerSize',25,'Marker','*','Color',[1 0 0]);
hold off;
Optimization terminated: average change in the fitness value less than options.TolFun.