Products & Services Solutions Academia Support User Community Company

Learn more about Genetic Algorithm and Direct Search Toolbox   

Using Simulated Annealing from the Command Line

Running simulannealbnd With the Default Options

To run the simulated annealing algorithm with the default options, call simulannealbnd with the syntax

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

The input arguments to simulannealbnd are

The output arguments are

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

You can run the example described in Example — Minimizing De Jong's Fifth Function from the command line 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 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 reference pages for more information about these arguments.

Setting Options for simulannealbnd at the Command Line

You can specify options by passing an options structure as an input argument to simulannealbnd 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 saoptimset function:

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'

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 algorithm is stochastic—that is, it makes random choices—you get slightly different results each time you run it. The algorithm uses the default MATLAB pseudorandom number stream. For more information about random number streams, see RandStream. Each time the algorithm calls the stream, its state changes. So the next time the algorithm calls the stream, it returns a different random number.

If you need to reproduce your results exactly, call simulannealbnd with the output argument. The output structure contains the current random number generator state in the output.rngstate field. Reset the state before running the function again.

For example, to reproduce the output of simulannealbnd 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 =
   31.9361  -31.9457

fval =
    4.9505

The state of the random number generator, rngstate, is stored in output:

output = 
     iterations: 1754
      funccount: 1769
        message: 'Optimization terminated:...
    change in best function value less than options.TolFun.'
       rngstate: [1x1 struct]
    problemtype: 'unconstrained'
    temperature: [2x1 double]
      totaltime: 1.1094

Reset the stream by entering

stream = RandStream.getDefaultStream
stream.State = output.rngstate.state;

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.

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

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