## Documentation Center |

Suppose you want to minimize the simple fitness function of
two variables *x*_{1} and *x*_{2},

subject to the following nonlinear inequality constraints and bounds

Begin by creating the fitness and constraint functions. First,
create a file named `simple_fitness.m` as follows:

function y = simple_fitness(x) y = 100*(x(1)^2 - x(2))^2 + (1 - x(1))^2;

(`simple_fitness.m` ships
with Global Optimization Toolbox software.)

The genetic algorithm function, `ga`, assumes
the fitness function will take one input `x`, where `x` has
as many elements as the number of variables in the problem. The fitness
function computes the value of the function and returns that scalar
value in its one return argument, `y`.

Then create a file, `simple_constraint.m`,
containing the constraints

function [c, ceq] = simple_constraint(x) c = [1.5 + x(1)*x(2) + x(1) - x(2);... -x(1)*x(2) + 10]; ceq = [];

(`simple_constraint.m` ships
with Global Optimization Toolbox software.)

The `ga` function assumes the constraint
function will take one input `x`, where `x` has
as many elements as the number of variables in the problem. The constraint
function computes the values of all the inequality and equality constraints
and returns two vectors, `c` and `ceq`,
respectively.

To minimize the fitness function, you need to pass a function
handle to the fitness function as the first argument to the `ga` function,
as well as specifying the number of variables as the second argument.
Lower and upper bounds are provided as `LB` and `UB` respectively.
In addition, you also need to pass a function handle to the nonlinear
constraint function.

ObjectiveFunction = @simple_fitness; nvars = 2; % Number of variables LB = [0 0]; % Lower bound UB = [1 13]; % Upper bound ConstraintFunction = @simple_constraint; rng(1,'twister') % for reproducibility [x,fval] = ga(ObjectiveFunction,nvars,... [],[],[],[],LB,UB,ConstraintFunction) Optimization terminated: average change in the fitness value less than options.TolFun and constraint violation is less than options.TolCon. x = 0.8122 12.3122 fval = 1.3578e+004

The genetic algorithm solver handles linear constraints and
bounds differently from nonlinear constraints. All the linear constraints
and bounds are satisfied throughout the optimization. However, `ga` may
not satisfy all the nonlinear constraints at every generation. If `ga` converges
to a solution, the nonlinear constraints will be satisfied at that
solution.

`ga` uses the mutation and crossover functions
to produce new individuals at every generation. `ga` satisfies
linear and bound constraints by using mutation and crossover functions
that only generate feasible points. For example, in the previous call
to `ga`, the mutation function `mutationguassian` does
not necessarily obey the bound constraints. So when there are bound
or linear constraints, the default `ga` mutation
function is `mutationadaptfeasible`. If you provide
a custom mutation function, this custom function must only generate
points that are feasible with respect to the linear and bound constraints.
All the included crossover functions generate points that satisfy
the linear constraints and bounds except the `crossoverheuristic` function.

To see the progress of the optimization, use the `gaoptimset` function
to create an options structure that selects two plot functions. The
first plot function is `gaplotbestf`, which plots
the best and mean score of the population at every generation. The
second plot function is `gaplotmaxconstr`, which
plots the maximum constraint violation of nonlinear constraints at
every generation. You can also visualize the progress of the algorithm
by displaying information to the command window using the `'Display'` option.

options = gaoptimset('PlotFcns',{@gaplotbestf,@gaplotmaxconstr},'Display','iter');

Rerun the `ga` solver.

[x,fval] = ga(ObjectiveFunction,nvars,[],[],[],[],... LB,UB,ConstraintFunction,options) Best max Stall Generation f-count f(x) constraint Generations 1 2670 13768.4 0 0 2 5286 13581.3 2.049e-08 0 3 7898 13579.5 0 0 4 12660 13578.3 1.062e-09 0 5 18922 13616.4 0 0 6 28972 13578.5 0 0 Optimization terminated: average change in the fitness value less than options.TolFun and constraint violation is less than options.TolCon. x = 0.8122 12.3123 fval = 1.3579e+04

You can provide a start point for the minimization to the `ga` function
by specifying the `InitialPopulation` option. The `ga` function
will use the first individual from `InitialPopulation` as
a start point for a constrained minimization.

X0 = [0.5 0.5]; % Start point (row vector) options = gaoptimset(options,'InitialPopulation',X0);

Now, rerun the `ga` solver.

[x,fval] = ga(ObjectiveFunction,nvars,[],[],[],[],... LB,UB,ConstraintFunction,options) Best max Stall Generation f-count f(x) constraint Generations 1 2674 13578.4 0 0 2 5290 13578.2 0 0 3 7902 13578.2 5.702e-11 0 Optimization terminated: average change in the fitness value less than options.TolFun and constraint violation is less than options.TolCon. x = 0.8122 12.3122 fval = 1.3578e+04

Was this topic helpful?