Skip to Main Content Skip to Search
Home |   Select Country  Choose Country  |  Contact Us  |  Cart Store 
Create Account | Log In
Products & Services Solutions Academia Support User Community Company
spacer spacer spacer spacer spacer spacer

 

Genetic Algorithm and Direct Search Toolbox 2.4.2

Optimization of Stochastic Objective Function

This is a demonstration of how to find a minimum of a stochastic objective function using PATTERNSEARCH function in the Genetic Algorithm and Direct Search Toolbox™. We also demonstrate why the Optimization Toolbox™ functions are not suitable for this kind of problems. A simple 2-dimensional optimization problem is selected for this demo to help visualize the objective function

Contents

Initialization

X0 = [2.5 -2.5];   % Starting point.
LB = [-5 -5];      % Lower bound
UB = [5 5];        % Upper bound
range = [LB(1) UB(1); LB(2) UB(2)];
Objfcn = @smoothFcn; % Handle to the objective function.
% Plot the smooth objective function
clf;showSmoothFcn(Objfcn,range); hold on;
title('Smooth objective function')
plot3(X0(1),X0(2),Objfcn(X0)+30,'om','MarkerSize',12, ...
    'MarkerFaceColor','r'); hold off;
set(gca,'CameraPosition',[-31.0391  -85.2792 -281.4265]);
set(gca,'CameraTarget',[0 0 -50])
set(gca,'CameraViewAngle',6.7937)
fig = gcf;

Run FMINCON on a Smooth Objective Function

The objective function is smooth (twice continuously differentiable). We will solve the optimization problem using FMINCON function from the Optimization Toolbox. FMINCON finds a constrained minimum of a function of several variables. This function has a unique minimum at the point x* = (-5.0,-5) where it has a function value f(x*) = -250.

% Set options to display iterative results.
options = optimset('Algorithm','active-set','Display','iter', ...
    'OutputFcn',@fminuncOutps);
[Xop,Fop] = fmincon(Objfcn,X0,[],[],[],[],LB,UB,[],options)
figure(fig);
hold on;
% Plot the final point
plot3(Xop(1),Xop(2),Fop,'dm','MarkerSize',12,'MarkerFaceColor','m');
hold off;
                                Max     Line search  Directional  First-orde
r
 Iter F-count        f(x)   constraint   steplength   derivative   optimalit
y Procedure
    0      3      -10.625         -2.5
    1      6         -250            0            1        -23.4         82.
1

Local minimum found that satisfies the constraints.

Optimization completed because the objective function is non-decreasing in
feasible directions, to within the default value of the function tolerance,
and constraints were satisfied to within the default value of the constraint
 tolerance.



Active inequalities (to within options.TolCon = 1e-006):
  lower      upper     ineqlin   ineqnonlin
    1
    2

Xop =

    -5    -5


Fop =

  -250

Stochastic Objective Function

The objective function we use now is the same as the previous example but with some random noise added to it. This is done by adding a random component to the function value.

% Reset the state of random number generator
reset(RandStream.getDefaultStream);
peaknoise = 4.5;
Objfcn = @(x) smoothFcn(x,peaknoise); % Handle to the objective function.
% Plot the objective function (non-smooth)
fig = figure;
showSmoothFcn(Objfcn,range);
title('Stochastic objective function')
set(gca,'CameraPosition',[-31.0391  -85.2792 -281.4265]);
set(gca,'CameraTarget',[0 0 -50])
set(gca,'CameraViewAngle',6.7937)

Run FMINCON on a Stochastic Objective Function

The objective function is stochastic and not smooth. FMINCON is a general constrained optimization solver which finds a local minima using first derivative of the objective function. If derivative of the objective function is not provided, FMINCON uses finite difference to approximate first derivative of the objective function. In this example, the objective function have some random noise in it. The derivatives hence could be highly unreliable. FMINCON can potentially stop at a point which is not a minimum. This may happen because the optimal conditions seems to be satisfied at the final point because of noise or it could not make any progress.

options = optimset('Algorithm','active-set','Display','iter');
[Xop,Fop] = fmincon(Objfcn,X0,[],[],[],[],LB,UB,[],options)
figure(fig);
hold on;
plot3(X0(1),X0(2),Objfcn(X0)+30,'om','MarkerSize',12,'MarkerFaceColor','r');
plot3(Xop(1),Xop(2),Fop,'dm','MarkerSize',12,'MarkerFaceColor','m');
                                Max     Line search  Directional  First-orde
r
 Iter F-count        f(x)   constraint   steplength   derivative   optimalit
y Procedure
    0      3     -19.2577         -2.5
    1     12     -19.6222       -2.617       0.0156   -2.51e+008    1.59e+00
9
    2     15     -53.5746            0            1   -5.47e+008    4.63e+00
8  Hessian modified twice

Local minimum found that satisfies the constraints.

Optimization completed because the objective function is non-decreasing in
feasible directions, to within the default value of the function tolerance,
and constraints were satisfied to within the default value of the constraint
 tolerance.



Active inequalities (to within options.TolCon = 1e-006):
  lower      upper     ineqlin   ineqnonlin
    1          2

Xop =

    -5     5


Fop =

  -53.5746

Run PATTERNSEARCH

We will now use PATTERNSEARCH from the Genetic Algorithm and Direct Search Toolbox. Pattern search optimization techniques are a class of direct search methods for optimization. A pattern search algorithm does not require any derivative information of the objective function to find an optimal point.

PSoptions = psoptimset('Display','iter','OutputFcn',@psOut);
[Xps,Fps] = patternsearch(Objfcn,X0,[],[],[],[],LB,UB,PSoptions)
figure(fig);
hold on;
plot3(Xps(1),Xps(2),Fps,'dr','MarkerSize',12,'MarkerFaceColor','r');
hold off

Iter     f-count          f(x)      MeshSize     Method
    0        1       -9.82132             1
    1        3       -45.8992             2     Successful Poll
    2        3       -45.8992             1     Refine Mesh
    3        5       -45.8992           0.5     Refine Mesh
    4        8       -88.4064             1     Successful Poll
    5       10       -88.4064           0.5     Refine Mesh
    6       13       -136.777             1     Successful Poll
    7       15       -136.777           0.5     Refine Mesh
    8       17       -136.777          0.25     Refine Mesh
    9       20       -190.615           0.5     Successful Poll
   10       22       -190.615          0.25     Refine Mesh
   11       24       -190.615         0.125     Refine Mesh
   12       27       -233.545          0.25     Successful Poll
   13       29       -241.605           0.5     Successful Poll
   14       31       -241.605          0.25     Refine Mesh
   15       33        -250.17           0.5     Successful Poll
   16       35        -250.17          0.25     Refine Mesh
   17       37        -250.17         0.125     Refine Mesh
   18       39        -250.17        0.0625     Refine Mesh
   19       41        -250.17       0.03125     Refine Mesh
   20       43        -250.17       0.01563     Refine Mesh
   21       44       -254.925       0.03125     Successful Poll
   22       46       -254.925       0.01563     Refine Mesh
   23       47        -256.57       0.03125     Successful Poll
   24       49        -261.07        0.0625     Successful Poll
   25       51        -261.07       0.03125     Refine Mesh
   26       53        -261.07       0.01563     Refine Mesh
   27       55        -261.07      0.007813     Refine Mesh
   28       57        -261.07      0.003906     Refine Mesh
   29       59        -261.07      0.001953     Refine Mesh
   30       61        -261.07     0.0009766     Refine Mesh

Iter     f-count        f(x)       MeshSize      Method
   31       63        -261.07     0.0004883     Refine Mesh
   32       65        -261.07     0.0002441     Refine Mesh
   33       66       -262.065     0.0004883     Successful Poll
   34       68       -262.065     0.0002441     Refine Mesh
   35       71       -262.065     0.0001221     Refine Mesh
   36       74       -262.065    6.104e-005     Refine Mesh
   37       77       -262.065    3.052e-005     Refine Mesh
   38       80       -262.065    1.526e-005     Refine Mesh
   39       83       -262.065    7.629e-006     Refine Mesh
   40       86       -262.065    3.815e-006     Refine Mesh
   41       89       -262.065    1.907e-006     Refine Mesh
   42       92       -262.065    9.537e-007     Refine Mesh
Optimization terminated: mesh size less than options.TolMesh.

Xps =

   -4.9998   -5.0000


Fps =

 -262.0651

Pattern search algorithm is not affected by random noise in the objective functions. Pattern search requires only function value and not the derivatives, hence noise (of some uniform kind) may not affect it. However, pattern search requires more function evaluation to find the true minimum than derivative based algorithms, a cost for not using the derivatives.

Contact sales
Free technical kit
Trial software
E-mail this page

Get Pricing and
Licensing Options