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 nonlinear constraints function.
The function signature is as follows:
function [c, ceq] = ConFunc(x)
, where
c and ceq contain inequality and equality contraints, respectively. The ceq output can be omitted if it is empty.
Inequality constraints are assumed to have upper bounds of 0, and equality constraints are equal to 0. For constraints of the form g(x) < b, where b ~= 0, the recommended best practice is to write the constraint as (g(x) - b) / abs(b) < 0.
Use [ ] if unused and the options argument is needed.
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.
info = 0
Reached maximum number of iterations or function calls.
info = -2
The function did not converge.
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.93843  4.98382
fval = 13.8773516
Modify the previous example to pass an extra parameter to the function using a function handle.
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.

The gaoptimset options and defaults are as follows:
  • 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.