# ga

Find the constrained minimum of a real function.

## Syntax

x = ga(@func,nVars)

x = ga(@func,nVars,A,b)

x = ga(@func,nVars,A,b,Aeq,beq)

x = ga(@func,nVars,A,b,Aeq,beq,lb,ub)

x = ga(@func,nVars,A,b,Aeq,beq,lb,ub,nonlcon)

x = ga(@func,nVars,A,b,Aeq,beq,lb,ub,nonlcon,options)

[x,fval,info,output] = ga(...)

## Inputs

func
The function to minimize.
nVars
The number of variables in the domain.
A
A matrix used to compute A*x for inequality constraints.
Use [ ] if unneeded.
b
The upper bound of the inequality constraints A*x<=b.
Use [ ] if unneeded.
Aeq
A matrix used to compute Aeq*x for equality constraints.
Use [ ] if unneeded.
beq
The upper bound of the equality constraints Aeq*x=beq.
Use [ ] if unneeded.
lb
The design variable lower bounds.
Use [ ] if unbounded. Support for this option is limited. See Comments.
ub
The design variable upper bounds.
Use [ ] if unbounded. Support for this option is limited. See Comments.
nonlcon
The non-linear constraints function.
The function signature is as follows: function [c, ceq] = ConFunc(x) where c and ceq contain inequality and equality constraints, respectively. The inequality constraints are assumed to have upper bounds of 0.
Use [ ] if unused.
options
A struct containing option settings.
See gaoptimset for details.

## Outputs

x
The location of the function minimum.
fval
The minimum of the function.
info
The convergence status flag.
info = 3
Converged with a constraint violation within tolCon.
info = 1
Function value converged to within tolFun or tolKKT.
info = 0
Reached maximum number of iterations or function calls, or the algorithm aborted because it was not converging.
output
A struct containing generation details. The members are as follows:
generations
The number of generations.
xgen
The candidate solution for each generation.
fvalgen
The objective function value for each iteration.
congen
The constraint values for each iteration. The columns will contain the constraint function values in the following order: linear inequality contraints, linear equality constraints, nonlinear inequality contraints, nonlinear equality constraints.

## Example

Minimize the function ObjFunc, subject to a linear inequality constraint.

``````function obj = ObjFunc(x)
obj = 2*(x(1)-3)^2 - 5*(x(1)-3)*(x(2)-2) + 4*(x(2)-2)^2 + 6;
end

n = 2;
A = [-1, -4];
b = [-27];
lb = [-10, -10];
ub = [10, 10];
[x,fval] = ga(@ObjFunc,2,A,b,[],[],lb,ub)``````
``````x = [Matrix] 1 x 2
6.95801  5.00452
fval = 13.9806344``````

## Comments

Options are specified with gaoptimset.

The initial design variable values can be specified with a vector using the InitialPopulation option.

The design variable ranges can be specified as a 2xN matrix using the PopInitRange option.

The gaoptimset options and defaults are as follows:
• Generations: 100 * number of variables, capped at 1000
• MaxFail: 20000
• Population Size: 100
• PopInitRange: [-10,10] units for each variable
• InitialPopulation: random over PopInitRange
• TolCon: 0.5%
• Seed: 0
• Display: 'off'

The unbounded limits for lb and ub are not fully supported and are set to -1000 and 1000. Use of these limits is discouraged due to the size of the search area.

To pass additional parameters to a function argument, use an anonymous function.