peaks Minimization with Genetic Algorithm

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

Contents

Objective Function

We wish find the minimum of the peaks function

clear all, close all, clc
peaks
 
z =  3*(1-x).^2.*exp(-(x.^2) - (y+1).^2) ... 
   - 10*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) ... 
   - 1/3*exp(-(x+1).^2 - y.^2) 
 

Nonlinear Constraint Function

Subject to a nonlinear constraint defined by a circular region of radius three around the origin

type circularConstraint
function [c,ceq] = circularConstraint(x)
% Nonlinear constraint definition

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

% Define nonlinear equality constraint (none)
ceq = [];

% Define nonlinear inequality constraint
% circular region with radius 3: x1^2 + x^2 -3^2 <= 0 
c = x(:,1).^2 + x(:,2).^2 - 9;

Define Optimization Problem

problem = createOptimProblem('fmincon',...
                             'objective',@(x) peaks(x(:,1),x(:,2)), ...
                             'nonlcon',@circularConstraint,...
                             'x0',[-1 -1],...
                             'lb',[-3 -3],...
                             'ub',[3 3],...
                             'options',optimset('OutputFcn',...
                                                @peaksPlotIterates))
problem = 

    objective: @(x)peaks(x(:,1),x(:,2))
           x0: [-1 -1]
        Aineq: []
        bineq: []
          Aeq: []
          beq: []
           lb: [-3 -3]
           ub: [3 3]
      nonlcon: @circularConstraint
       solver: 'fmincon'
      options: [1x1 struct]

Run the solver fmincon from the inital point

We can see the solution is not the global minimum

[x,f] = fmincon(problem)
x =

   -1.3473    0.2045


f =

   -3.0498

Use Genetic Algorithm to Find the Global Minimum

Solve the problem using ga.

problem.solver  = 'ga';
problem.fitnessfcn = problem.objective;
problem.nvars = 2;
problem.options = gaoptimset('PopInitRange',[-3;3],...
                             'OutputFcn',@peaksPlotIterates,...
                             'Display','iter')

[x,f] = ga(problem)
problem = 

     objective: @(x)peaks(x(:,1),x(:,2))
            x0: [-1 -1]
         Aineq: []
         bineq: []
           Aeq: []
           beq: []
            lb: [-3 -3]
            ub: [3 3]
       nonlcon: @circularConstraint
        solver: 'ga'
       options: [1x1 struct]
    fitnessfcn: @(x)peaks(x(:,1),x(:,2))
         nvars: 2


                           Best       max        Stall
Generation  f-count        f(x)     constraint  Generations
    1        1060      -6.55112            0      0
    2        2100      -6.55113            0      0
    3        3140      -6.55113            0      0
    4        4180      -6.55113            0      1
    5        5220      -6.55113            0      2
Optimization terminated: average change in the fitness value less than options.TolFun
 and constraint violation is less than options.TolCon.

x =

    0.2283   -1.6255


f =

   -6.5511