Using the Simulated Annealing and Threshold Acceptance Algorithms from the Command Line

Running simulannealbnd and threshacceptbnd with the Default Options

To run either the simulated annealing or threshold acceptance algorithms with the default options, call the corresponding function with the syntax

[x,fval] = simulannealbnd(@objfun,x0)
[x,fval] = threshacceptbnd(@objfun,x0)

The input arguments to simulannealbnd and threshacceptbnd are

The output arguments are

For a description of additional input and output arguments, see the reference pages for simulannealbnd and threshacceptbnd.

You can run the example described in Example: Minimizing De Jong's Fifth Function from the command line with the simulated annealing algorithm by entering

[x,fval] = simulannealbnd(@dejong5fcn, [0 0]) 

This returns

x =
   -31.9564  -15.9755
fval =
     5.9288 

Additional Output Arguments

To get more information about the performance of the algorithm, you can call simulannealbnd or threshacceptbnd with the syntax

[x,fval,exitflag,output] = simulannealbnd(@objfun,x0) 

Besides x and fval, this function returns the following additional output arguments:

See the simulannealbnd and threshacceptbnd reference pages for more information about these arguments.

Setting Options for simulannealbnd and threshacceptbnd at the Command Line

You can specify options by passing an options structure as an input argument to either simulannealbnd and threshacceptbnd using the syntax

[x,fval] = simulannealbnd(@objfun,x0,[],[],options) 

This syntax does not specify any lower or upper bound constraints.

You create the options structure using the function saoptimset:

options = saoptimset('simulannealbnd') 

This returns the structure options with the default values for its fields:

options = 
          AnnealingFcn: @annealingfast
        TemperatureFcn: @temperatureexp
         AcceptanceFcn: @acceptancesa
                TolFun: 1.0000e-006
        StallIterLimit: '500*numberofvariables'
           MaxFunEvals: '3000*numberofvariables'
             TimeLimit: Inf
               MaxIter: Inf
        ObjectiveLimit: -Inf
               Display: 'final'
       DisplayInterval: 10
             HybridFcn: []
        HybridInterval: 'end'
              PlotFcns: []
          PlotInterval: 1
            OutputFcns: []
    InitialTemperature: 100
      ReannealInterval: 100
              DataType: 'double'

These are the default values for simulannealbnd. To see the default values for threshacceptbnd, run

options = saoptimset('threshacceptbnd') 

The value of each option is stored in a field of the options structure, such as options.ReannealInterval. You can display any of these values by entering options followed by the name of the field. For example, to display the interval for reannealing used for the simulated annealing algorithm, enter

options.ReannealInterval
ans =
   100

To create an options structure with a field value that is different from the default—for example, to set ReannealInterval to 300 instead of its default value 100—enter

options = saoptimset('ReannealInterval', 300) 

This creates the options structure with all values set to their defaults, except for ReannealInterval, which is set to 300.

If you now enter

simulannealbnd(@objfun,x0,[],[],options) 

simulannealbnd runs the simulated annealing algorithm with a reannealing interval of 300.

If you subsequently decide to change another field in the options structure, such as setting PlotFcns to @saplotbestf, which plots the best objective function value at each iteration, call saoptimset with the syntax

options = saoptimset(options,'PlotFcns',@saplotbestf) 

This preserves the current values of all fields of options except for PlotFcns, which is changed to @saplotbestf. Note that if you omit the input argument options, saoptimset resets ReannealInterval to its default value 100.

You can also set both ReannealInterval and PlotFcns with the single command

options = saoptimset('ReannealInterval',300, ...
                     'PlotFcns',@saplotbestf) 

Reproducing Your Results

Because the simulated annealing and threshold acceptance algorithms are stochastic—that is, they each makes random choices—you get slightly different results each time you run them. The algorithms use the MATLAB® uniform and normal random number generators, rand and randn, when generating subsequent points and also when determining whether or not to accept new points. Each time the algorithms call rand and randn, their states are changed so that the next time they are called, they return different random numbers.

If you need to reproduce your results exactly, call simulannealbnd or threshacceptbnd with an output argument that contains the current states of rand and randn and then reset the states to these values before running the function again. For example, to reproduce the output of simulanneal applied to De Jong's fifth function, call simulannealbnd with the syntax

[x,fval,exitflag,output] = simulannealbnd(@dejong5fcn,[0 0]); 

Suppose the results are

x =
  -32.0401  -16.1223
fval =
    5.9288

The states of rand and randn are stored in two fields of output.

output = 
     iterations: 2041
      funccount: 2058
        message: [1x80 char]
      randstate: [625x1 uint32]
     randnstate: [2x1 double]
    problemtype: 'unconstrained'
    temperature: [2x1 double]
      totaltime: 1.8226

Reset the states by entering

rand('twister', output.randstate);
randn('state', output.randnstate); 

If you now run simulannealbnd a second time, you get the same results.

You can reproduce your run in the Optimization Tool by checking the box Use random states from previous run in the Run solver and view results section.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS