Main Content

Resume ga

By default, ga creates a new initial population each time you run it. However, you might get better results by using the final population from a previous run as the initial population for a new run. To do so, you must have saved the final population from the previous run by calling ga with the syntax

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

The last output argument is the final population. To run ga using final_pop as the initial population, enter

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

You can then use final_pop2, the final population from the second run, as the initial population for a third run.

For example, minimize Ackley's function, a function of two variables that is available when you run this example.

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.

Examine the best function value.

disp(fval)
    3.5527

Try to get a better solution by running ga from the final population.

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

The fitness function value improves significantly.

Try once again to improve the solution.

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

This time the improvement is insignificant.

Copyright 2020–2022 The MathWorks, Inc.

Related Topics