Main Content

Reproduce 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, the stream 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 (which is available when you run this example), call simulannealbnd with the syntax

rng(10,'twister') % For reproducibility
[x,fval,exitflag,output] = simulannealbnd(@dejong5fcn,[0 0]);
simulannealbnd stopped because the change in best function value is less than options.FunctionTolerance.

Examine the x and fval outputs.

x,fval
x = 1×2

  -16.1292  -15.8214

fval = 6.9034

The state of the random number generator, rngstate, is stored in output.rngstate. Reset the stream as follows.

stream = RandStream.getGlobalStream;
stream.State = output.rngstate.State;

Run simulannealbnd a second time, and you get the same results.

[x,fval,exitflag,output] = simulannealbnd(@dejong5fcn,[0 0]);
simulannealbnd stopped because the change in best function value is less than options.FunctionTolerance.
x,fval
x = 1×2

  -16.1292  -15.8214

fval = 6.9034

If you run simulannealbnd again without resetting the random number stream, the results change.

[x,fval,exitflag,output] = simulannealbnd(@dejong5fcn,[0 0]);
simulannealbnd stopped because the change in best function value is less than options.FunctionTolerance.
x,fval
x = 1×2

     0     0

fval = 12.6705

Note: If you do not need to reproduce your results, it is better not to set the states of RandStream, so that you get the benefit of the randomness in these algorithms.

See Also

Related Topics