# 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.
`b`- The upper bound of the inequality constraints A*x<=b.
`Aeq`- A matrix used to compute Aeq*x for equality constraints.
`beq`- The upper bound of the equality constraints Aeq*x=beq.
`lb`- The design variable lower bounds.
`ub`- The design variable upper bounds.
`nonlcon`- The non-linear constraints function.
`options`- A struct containing option settings.

## 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

```
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.93843 4.98382
fval = 13.8773516
```

```
function obj = ObjFunc(x,offset)
obj = 2*(x(1)-3)^2 - 5*(x(1)-3)*(x(2)-2) + 4*(x(2)-2)^2 + offset;
end
handle = @(x) ObjFunc(x,7);
[x,fval] = ga(handle,2,A,b,[],[],lb,ub)
```

```
x = [Matrix] 1 x 2
6.88378 4.99563
fval = 14.8908606
```

## 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.

- Generations: 100 * number of variables, capped at 1000
- MaxFail: 20000
- Population Size: 0, which allows the algorithm to choose.
- PopInitRange: [-10,10] units for each variable
- InitialPopulation: random over PopInitRange
- TolCon: 0.5%
- Seed: 0
- Display: 'off'

Unbounded limits design variable limits are not fully supported and are set to -1000 and 1000. Use of large limits is discouraged due to the size of the search area.

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