Resume ga

Resuming ga From the Final Population

The following example shows how export a problem so that when you import it and click Start, the genetic algorithm resumes from the final population saved with the exported problem. To run the example, enter the following in the Optimization app:

  1. Set Fitness function to @ackleyfcn, which computes Ackley's function, a test function provided with the software.

  2. Set Number of variables to 10.

  3. Select Best fitness in the Plot functions pane.

  4. Click Start.

This displays the following plot, or similar.

Suppose you want to experiment by running the genetic algorithm with other options settings, and then later restart this run from its final population with its current options settings. You can do this using the following steps:

  1. Click Export to Workspace.

  2. In the dialog box that appears,

    • Select Export problem and options to a MATLAB structure named.

    • Enter a name for the problem and options, such as ackley_uniform, in the text field.

    • Select Include information needed to resume this run.

    The dialog box should now appear as in the following figure.

  3. Click OK.

This exports the problem and options to a structure in the MATLAB® workspace. You can view the structure in the MATLAB Command Window by entering

ackley_uniform

ackley_uniform = 

    fitnessfcn: @ackleyfcn
         nvars: 10
         Aineq: []
         bineq: []
           Aeq: []
           beq: []
            lb: []
            ub: []
       nonlcon: []
        intcon: []
      rngstate: []
        solver: 'ga'
       options: [1x1 struct]

After running the genetic algorithm with different options settings or even a different fitness function, you can restore the problem as follows:

  1. Select Import Problem from the File menu. This opens the dialog box shown in the following figure.

  2. Select ackley_uniform.

  3. Click Import.

This sets the Initial population and Initial scores fields in the Population panel to the final population of the run before you exported the problem. All other options are restored to their setting during that run. When you click Start, the genetic algorithm resumes from the saved final population. The following figure shows the best fitness plots from the original run and the restarted run.

    Note   If, after running the genetic algorithm with the imported problem, you want to restore the genetic algorithm's default behavior of generating a random initial population, delete the population in the Initial population field.

The version of Ackley's function in the toolbox differs from the published version of Ackley's function in Ackley [1]. The toolbox version has another exponential applied, leading to flatter regions, so a more difficult optimization problem.

References

[1] Ackley, D. H. A connectionist machine for genetic hillclimbing. Kluwer Academic Publishers, Boston, 1987.

Resuming ga From a Previous Run

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 = gaoptimset('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.

In Optimization app, you can choose to export a problem in a way that lets you resume the run. Simply check the box Include information needed to resume this run when exporting the problem.

This saves the final population, which becomes the initial population when imported.

If you want to run a problem that was saved with the final population, but would rather not use the initial population, simply delete or otherwise change the initial population in the Options > Population pane.

Was this topic helpful?