Contents

function rastriginComparison(rfSelection)
close all
%  Copyright (c) 2010, The MathWorks, Inc.
%  All rights reserved.

Rastrigin's Function Comparison

set option to 1 = standard rastrigin, 2 = scaled, 3 = scaled + shifted

if nargin == 0
    rfSelection = 1:3;
end

for case2run = rfSelection

Problem Definition

x0 = [2 1];     % starting point
minX = [0 0];   % global min
dom = [-6 6];
switch case2run
    case 1 % standard rastrigin's function
        rf = @(x)rastriginsfcn(x);
        rasLabel = 'Rastrigin''s Function';
    case 2 % scaled rastrigin's function
        rf = @(x)rastriginsfcn(x/10);
        rasLabel = 'Scaled Rastrigin''s Function';
        x0 = x0*10;
        dom = dom*10;
    case 3 % shifted rastrigin's function
        rf = @(x)rastriginsfcn((x-10)/10);
        rasLabel = 'Scaled and Shfited Rastrigin''s Function';
        x0 = x0*10 + 10;
        minX = minX*10 + 10;
        dom = dom*10 + 10;
end
figure,ezcontour(@(x,y) rf([x,y]),dom)
hold on, plot(minX(1),minX(2),'r*','MarkerSize',20)
plot(x0(1),x0(2),'k*','MarkerSize',20), hold off
axis equal tight
legend('FuncValue','Min','StartPt','Location','NorthWest')
drawnow

fminunc

tic
[xu fu flu ou] = fminunc(rf,x0)
tu = toc;
Warning: Gradient must be provided for trust-region algorithm;
  using line-search algorithm instead. 

Local minimum found.

Optimization completed because the size of the gradient is less than
the default value of the function tolerance.




xu =

    1.9899    0.9950


fu =

    4.9748


flu =

     1


ou = 

       iterations: 3
        funcCount: 18
         stepsize: 1
    firstorderopt: 1.1981e-007
        algorithm: 'medium-scale: Quasi-Newton line search'
          message: [1x438 char]

Warning: Gradient must be provided for trust-region algorithm;
  using line-search algorithm instead. 

Local minimum found.

Optimization completed because the size of the gradient is less than
the default value of the function tolerance.




xu =

   19.8991    9.9496


fu =

    4.9748


flu =

     1


ou = 

       iterations: 2
        funcCount: 12
         stepsize: 1
    firstorderopt: 9.1657e-007
        algorithm: 'medium-scale: Quasi-Newton line search'
          message: [1x438 char]

Warning: Gradient must be provided for trust-region algorithm;
  using line-search algorithm instead. 

Local minimum found.

Optimization completed because the size of the gradient is less than
the default value of the function tolerance.




xu =

   29.8991   19.9496


fu =

    4.9748


flu =

     1


ou = 

       iterations: 2
        funcCount: 12
         stepsize: 1
    firstorderopt: 9.0905e-007
        algorithm: 'medium-scale: Quasi-Newton line search'
          message: [1x438 char]

fminsearch

tic
[xs fs fls os] = fminsearch(rf,x0)
ts = toc;
xs =

    1.9899    0.9949


fs =

    4.9748


fls =

     1


os = 

    iterations: 23
     funcCount: 45
     algorithm: 'Nelder-Mead simplex direct search'
       message: [1x196 char]

xs =

   19.8991    9.9496


fs =

    4.9748


fls =

     1


os = 

    iterations: 31
     funcCount: 61
     algorithm: 'Nelder-Mead simplex direct search'
       message: [1x196 char]

xs =

   29.8992   19.9496


fs =

    4.9748


fls =

     1


os = 

    iterations: 35
     funcCount: 67
     algorithm: 'Nelder-Mead simplex direct search'
       message: [1x196 char]

GlobalSearch

problem = createOptimProblem('fmincon','objective',rf,'x0',x0,'lb',dom(1),'ub',dom(2));
gs = GlobalSearch;
tic
[xg fg flg og] = run(gs,problem)
tg = toc;
GlobalSearch stopped because it analyzed all the trial points.

All 7 local solver runs converged with a positive local solver exit flag.

xg =

  1.0e-005 *

   -0.2980   -0.1431


fg =

  2.1681e-009


flg =

     1


og = 

                funcCount: 2149
         localSolverTotal: 7
       localSolverSuccess: 7
    localSolverIncomplete: 0
    localSolverNoSolution: 0
                  message: [1x137 char]

GlobalSearch stopped because it analyzed all the trial points.

All 15 local solver runs converged with a positive local solver exit flag.

xg =

  1.0e-003 *

         0   -0.4051


fg =

  3.2562e-007


flg =

     1


og = 

                funcCount: 2291
         localSolverTotal: 15
       localSolverSuccess: 15
    localSolverIncomplete: 0
    localSolverNoSolution: 0
                  message: [1x138 char]

GlobalSearch stopped because it analyzed all the trial points.

All 11 local solver runs converged with a positive local solver exit flag.

xg =

   10.0005   10.0000


fg =

  4.1236e-007


flg =

     1


og = 

                funcCount: 2226
         localSolverTotal: 11
       localSolverSuccess: 11
    localSolverIncomplete: 0
    localSolverNoSolution: 0
                  message: [1x138 char]

MultiStart

ms = MultiStart;
tic
problem = createOptimProblem('fminunc','objective',rf,'x0',x0,'lb',dom(1),'ub',dom(2));
[xm fm flm om] = run(ms,problem,100)
tm = toc;
Warning: Gradient must be provided for trust-region algorithm;
  using line-search algorithm instead. 

MultiStart completed the runs from all start points.

All 100 local solver runs converged with a positive local solver exit flag.

xm =

    0.9950   -0.9950


fm =

    1.9899


flm =

     1


om = 

                funcCount: 8013
         localSolverTotal: 100
       localSolverSuccess: 100
    localSolverIncomplete: 0
    localSolverNoSolution: 0
                  message: [1x129 char]

Warning: Gradient must be provided for trust-region algorithm;
  using line-search algorithm instead. 

MultiStart completed the runs from all start points.

All 100 local solver runs converged with a positive local solver exit flag.

xm =

    0.0000   -9.9496


fm =

    0.9950


flm =

     1


om = 

                funcCount: 6699
         localSolverTotal: 100
       localSolverSuccess: 100
    localSolverIncomplete: 0
    localSolverNoSolution: 0
                  message: [1x129 char]

Warning: Gradient must be provided for trust-region algorithm;
  using line-search algorithm instead. 

MultiStart completed the runs from all start points.

All 100 local solver runs converged with a positive local solver exit flag.

xm =

   10.0000   10.0000


fm =

  2.5224e-012


flm =

     1


om = 

                funcCount: 6909
         localSolverTotal: 100
       localSolverSuccess: 100
    localSolverIncomplete: 0
    localSolverNoSolution: 0
                  message: [1x129 char]

PatternSearch

tic
[xp fp flp op] = patternsearch(rf,x0,[],[],[],[],dom(1),dom(2));
tp = toc;
Optimization terminated: mesh size less than options.TolMesh.
Optimization terminated: mesh size less than options.TolMesh.
Optimization terminated: mesh size less than options.TolMesh.

Genetic Algorithm

initpop = 10*randn(20,2) + repmat([10 30],20,1);
opts = gaoptimset('InitialPopulation',initpop);
tic
[xga fga flga oga] = ga(rf,2,[],[],[],[],dom(1),dom(2))%,[],opts)
tga = toc;
Optimization terminated: average change in the fitness value less than options.TolFun.

xga =

  1.0e-004 *

   -0.2011    0.0576


fga =

  8.6851e-008


flga =

     1


oga = 

      problemtype: 'boundconstraints'
         rngstate: [1x1 struct]
      generations: 51
        funccount: 1040
          message: [1x86 char]
    maxconstraint: 0

Optimization terminated: average change in the fitness value less than options.TolFun.

xga =

  1.0e-004 *

    0.3257    0.0067


fga =

  2.1059e-009


flga =

     1


oga = 

      problemtype: 'boundconstraints'
         rngstate: [1x1 struct]
      generations: 51
        funccount: 1040
          message: [1x86 char]
    maxconstraint: 0

Optimization terminated: average change in the fitness value less than options.TolFun.

xga =

    0.0504    0.0504


fga =

    1.9899


flga =

     1


oga = 

      problemtype: 'boundconstraints'
         rngstate: [1x1 struct]
      generations: 51
        funccount: 1040
          message: [1x86 char]
    maxconstraint: 0

Simulated Annealing

tic
[xsa fsa flsa osa] =simulannealbnd(rf,x0,dom(1),dom(2))
tsa = toc;
Optimization terminated: change in best function value less than options.TolFun.

xsa =

    0.9950    0.0004


fsa =

    0.9950


flsa =

     1


osa = 

     iterations: 1221
      funccount: 1230
        message: [1x80 char]
       rngstate: [1x1 struct]
    problemtype: 'boundconstraints'
    temperature: [2x1 double]
      totaltime: 0.5000

Optimization terminated: change in best function value less than options.TolFun.

xsa =

   -0.0013   -9.9506


fsa =

    0.9950


flsa =

     1


osa = 

     iterations: 4146
      funccount: 4189
        message: [1x80 char]
       rngstate: [1x1 struct]
    problemtype: 'boundconstraints'
    temperature: [2x1 double]
      totaltime: 1.7813

Optimization terminated: change in best function value less than options.TolFun.

xsa =

   19.9446   10.4336


fsa =

    1.3657


flsa =

     1


osa = 

     iterations: 2040
      funccount: 2061
        message: [1x80 char]
       rngstate: [1x1 struct]
    problemtype: 'boundconstraints'
    temperature: [2x1 double]
      totaltime: 1.0156

Show Tabular Results

rowhead = {'Solution-X1','Solution-X2','Objective','# Func Evals','Time'}';
colhead = {'fminunc','fminsearch','GlobalSearch','MultiStart',...
           'Pattern Search','Genetic Algorithm','Simulated Annealing'};
atab = {xu(1), xs(1), xg(1), xm(1), xp(1), xga(1), xsa(1);
        xu(2), xs(2), xg(2), xm(2), xp(2), xga(2), xsa(2);
        fu, fs, fg, fm, fp, fga, fsa;
        ou.funcCount, os.funcCount, og.funcCount, om.funcCount, op.funccount, oga.funccount, osa.funccount;
        tu, ts, tg, tm, tp, tga, tsa};
atab = [rowhead, atab];
atab = [{'Results',colhead{1:end}}; atab];
statdisptable(atab,'Comparison of Multiple Solver Runs',rasLabel)
set(gcf,'Position',[73 1010 926 138]);
end