主要内容

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

恢复 ga

默认情况下,每次运行 ga 时都会创建一个新的初始种群。但是,使用前一次运行的最终种群作为新一次运行的初始种群可能会获得更好的结果。为此,您必须通过使用以下语法调用 ga 来保存上次运行的最终种群

[x,fval,exitflag,output,final_pop] = ga(@fitnessfcn,nvars);

最后一个输出参量是最终的种群。要使用 ga 作为初始种群来运行 final_pop,请输入

options = optimoptions('ga','InitialPop',final_pop);
[x,fval,exitflag,output,final_pop2] = ... 
		ga(@fitnessfcn,nvars,[],[],[],[],[],[],[],options);

然后,您可以使用第二次运行的最终种群 final_pop2 作为第三次运行的初始种群。

例如,最小化 Ackley 函数,这是运行此示例时可用的两个变量的函数。

rng(100) % For reproducibiliity
[x,fval,exitflag,output,final_pop] = ga(@ackleyfcn,2);
ga stopped because the average change in the fitness value is less than options.FunctionTolerance.

考察最佳函数值。

disp(fval)
    3.5527

尝试通过从最终种群中运行 ga 来获得更好的解。

options = optimoptions('ga','InitialPopulationMatrix',final_pop);
[x,fval2,exitflag2,output2,final_pop2] = ... 
		ga(@ackleyfcn,2,[],[],[],[],[],[],[],options);
ga stopped because the average change in the fitness value is less than options.FunctionTolerance.
disp(fval2)
    2.9886

适应度函数值明显提高。

再次尝试改进解。

options.InitialPopulationMatrix = final_pop2;
[x,fval3,exitflag3,output3,final_pop3] = ... 
		ga(@ackleyfcn,2,[],[],[],[],[],[],[],options);
ga stopped because the average change in the fitness value is less than options.FunctionTolerance.
disp(fval3)
    2.9846

这次的改进并不明显。

版权所有 2020–2022 The MathWorks, Inc.

另请参阅

主题