To use the genetic algorithm at the command line, call the genetic
ga with the syntax
[x,fval] = ga(@fitnessfun, nvars, options)
@fitnessfun is a handle to the
nvars is the number of independent
variables for the fitness function.
options contains options for the
genetic algorithm. If you do not pass in this argument,
its default options.
The results are given by
x — Point at which the final
value is attained
fval — Final value of the
For an example, see Finding the Minimum from the Command Line.
Using the function
ga is convenient if
you want to
Return results directly to the MATLAB® workspace
Run the genetic algorithm multiple times with different
options, by calling
ga from a file
To open the Optimization app, enter
optimtooland then choose
gafrom the Solver menu.
You can also start the tool from the MATLAB Apps tab.
To use the Optimization app, you must first enter the following information:
Fitness function — The
objective function you want to minimize. Enter the fitness function
in the form
a file that computes the fitness function. Compute Objective Functions explains how write this file.
@ sign creates a function handle to
Number of variables —
The length of the input vector to the fitness function. For the function
in Compute Objective Functions,
you would enter
You can enter constraints or a nonlinear constraint function for the problem in the Constraints pane. If the problem is unconstrained, leave these fields blank.
To run the genetic algorithm, click the Start button. The tool displays the results of the optimization in the Run solver and view results pane.
You can change the options for the genetic algorithm in the Options pane. To view the options in one of the categories listed in the pane, click the + sign next to it.
For more information,