| Genetic Algorithm and Direct Search Toolbox™ | ![]() |
| On this page… |
|---|
Running simulannealbnd and threshacceptbnd with the Default Options Setting Options for simulannealbnd and threshacceptbnd at the Command Line |
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
@objfun — A function handle to the M-file that computes the objective function. Writing M-Files for Functions You Want to Optimize explains how to write this M-file.
x0 — The initial guess of the optimal argument to the objective function.
The output arguments are
x — The final point.
fval — The value of the objective function at x.
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 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:
exitflag — Flag indicating the reason the algorithm terminated
output — Structure containing information about the performance of the algorithm
See the simulannealbnd and threshacceptbnd reference pages for more information about these arguments.
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) 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.8226Reset 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.

Note If you do not need to reproduce your results, it is better not to set the states of rand and randn, so that you get the benefit of the randomness in these algorithms. |
![]() | Using the Simulated Annealing and Threshold Acceptance Algorithms | Parallel Computing with Simulated Annealing and Threshold Acceptance Algorithms | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |