Accelerating the pace of engineering and science

# Documentation

## Include a Hybrid Function

A hybrid function is an optimization function that runs after the genetic algorithm terminates in order to improve the value of the fitness function. The hybrid function uses the final point from the genetic algorithm as its initial point. You can specify a hybrid function in Hybrid function options.

This example uses Optimization Toolbox™ function fminunc, an unconstrained minimization function. The example first runs the genetic algorithm to find a point close to the optimal point and then uses that point as the initial point for fminunc.

The example finds the minimum of Rosenbrock's function, which is defined by

$f\left({x}_{1},{x}_{2}\right)=100{\left({x}_{2}-{x}_{1}^{2}\right)}^{2}+{\left(1-{x}_{1}\right)}^{2}.$

The following figure shows a plot of Rosenbrock's function.

Global Optimization Toolbox software contains the dejong2fcn.m file, which computes Rosenbrock's function. To see a worked example of a hybrid function, enter

`hybriddemo`

at the MATLAB® prompt.

To explore the example, first enter optimtool('ga') to open the Optimization app to the ga solver. Enter the following settings:

• Set Fitness function to @dejong2fcn.

• Set Number of variables to 2.

• Optionally, to get the same pseudorandom numbers as this example, switch to the command line and enter:

`rng(1,'twister')`

Before adding a hybrid function, click Start to run the genetic algorithm by itself. The genetic algorithm displays the following results in the Run solver and view results pane:

The final point is somewhat close to the true minimum at (1, 1). You can improve this result by setting Hybrid function to fminunc (in the Hybrid function options).

fminunc uses the final point of the genetic algorithm as its initial point. It returns a more accurate result, as shown in the Run solver and view results pane.

Specify nondefault options for the hybrid function by creating options at the command line. Use optimset for fminsearch, psoptimset for patternsearch, or optimoptions for fmincon or fminunc. For example:

`hybridopts = optimoptions('fminunc','Display','iter','Algorithm','quasi-newton');`

In the Optimization app enter the name of your options structure in the Options box under Hybrid function:

At the command line, the syntax is as follows:

`options = gaoptimset('HybridFcn',{@fminunc,hybridopts});`

hybridopts must exist before you set options.