主要内容

本页采用了机器翻译。点击此处可查看最新英文版本。

重现结果

由于模拟退火算法是随机的(即它会做出随机选择),因此每次运行时都会得到略有不同的结果。该算法使用默认的 MATLAB® 伪随机数流。有关随机数流的更多信息,请参阅 RandStream。每次算法调用流时,其状态都会发生变化。因此,下次算法调用流时,流将返回不同的随机数。

如果您需要准确地重现结果,请使用 simulannealbnd 参量调用 outputoutput 结构体包含 output.rngstate 字段中的当前随机数生成器状态。再次运行该函数之前重置状态。

例如,要重现应用于 De Jong 的第五个函数(运行此示例时可用)的 simulannealbnd 的输出,请使用以下语法调用 simulannealbnd

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.

检查 xfval 输出。

x,fval
x = 1×2

  -16.1292  -15.8214

fval = 
6.9034

随机数生成器的状态 rngstate 存储在 output.rngstate 中。按如下方式重置流。

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

再次运行 simulannealbnd,您会得到相同的结果。

[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

如果您再次运行 simulannealbnd 而不重置随机数流,结果会发生变化。

[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

注意:如果您不需要重现您的结果,最好不要设置 RandStream 的状态,这样您就可以从这些算法中的随机性中受益。

另请参阅

主题