| Genetic Algorithm and Direct Search Toolbox™ | ![]() |
x = ga(fitnessfcn,nvars)
x = ga(fitnessfcn,nvars,A,b)
x = ga(fitnessfcn,nvars,A,b,Aeq,beq)
x = ga(fitnessfcn,nvars,A,b,Aeq,beq,LB,UB)
x = ga(fitnessfcn,nvars,A,b,Aeq,beq,LB,UB,nonlcon)
x = ga(fitnessfcn,nvars,A,b,Aeq,beq,LB,UB,nonlcon,options)
x = ga(problem)
[x,fval] = ga(...)
[x,fval,exitflag] = ga(...)
ga implements the genetic algorithm at the command line to minimize an objective function.
x = ga(fitnessfcn,nvars) finds a local unconstrained minimum, x, to the objective function, fitnessfcn. nvars is the dimension (number of design variables) of fitnessfcn. The objective function, fitnessfcn, accepts a vector x of size 1-by-nvars, and returns a scalar evaluated at x.
Note To write a function with additional parameters to the independent variables that can be called by ga, see the section on Passing Extra Parameters in the Optimization Toolbox™ User's Guide. |
x = ga(fitnessfcn,nvars,A,b) finds a local
minimum x to fitnessfcn, subject
to the linear inequalities
. fitnessfcn accepts input x and returns
a scalar function value evaluated at x.
If the problem has m linear inequality constraints and n variables, then
A is a matrix of size m-by-n.
b is a vector of length m.
Note that the linear constraints are not satisfied when the PopulationType option is set to 'bitString' or 'custom'.
x = ga(fitnessfcn,nvars,A,b,Aeq,beq) finds
a local minimum x to fitnessfcn, subject to the linear equalities
as
well as
. (Set A=[] and b=[] if no inequalities exist.)
If the problem has r linear equality constraints and n variables, then
Aeq is a matrix of size r-by-n.
beq is a vector of length r.
Note that the linear constraints are not satisfied when the PopulationType option is set to 'bitString' or 'custom'.
x = ga(fitnessfcn,nvars,A,b,Aeq,beq,LB,UB) defines a set of lower and upper bounds on the design variables, x, so that a solution is found in the range
. Use empty matrices for LB and UB if no bounds exist. Set LB(i) = -Inf if x(i) is unbounded below; set UB(i) = Inf if x(i) is unbounded above.
x = ga(fitnessfcn,nvars,A,b,Aeq,beq,LB,UB,nonlcon) subjects the minimization to the constraints defined in nonlcon. The function nonlcon accepts x and returns the vectors C and Ceq, representing the nonlinear inequalities and equalities respectively. ga minimizes the fitnessfcn such that C(x)≤0 and Ceq(x)=0. (Set LB=[] and UB=[] if no bounds exist.)
Note that the nonlinear constraints are not satisfied when the PopulationType option is set to 'bitString' or 'custom'.
x = ga(fitnessfcn,nvars,A,b,Aeq,beq,LB,UB,nonlcon,options) minimizes with the default optimization parameters replaced by values in the structure options, which can be created using the gaoptimset function. See the gaoptimset reference page for details.
x = ga(problem) finds the minimum for problem, where problem is a structure containing the following fields:
| fitnessfcn | Fitness function |
| nvars | Number of design variables |
| Aineq | A matrix for linear inequality constraints |
| Bineq | b vector for linear inequality constraints |
| Aeq | A matrix for linear equality constraints |
| Beq | b vector for linear equality constraints |
| lb | Lower bound on x |
| ub | Upper bound on x |
| nonlcon | Nonlinear constraint function |
| randstate | Optional field to reset rand state |
| randnstate | Optional field to reset randn state |
| solver | 'ga' |
| options | Options structure created using gaoptimset |
Create the structure problem by exporting a problem from Optimization Tool, as described in Importing and Exporting Your Work in the Optimization Toolbox User's Guide.
[x,fval] = ga(...) returns fval, the value of the fitness function at x.
[x,fval,exitflag] = ga(...) returns exitflag, an integer identifying the reason the algorithm terminated. The following lists the values of exitflag and the corresponding reasons the algorithm terminated.
1 — Average cumulative change in value of the fitness function over options.StallGenLimit generations less than options.TolFun and constraint violation less than options.TolCon.
2 — Fitness limit reached and constraint violation less than options.TolCon.
3 — The value of the fitness function did not change in options.StallGenLimit generations and constraint violation less than options.TolCon.
4 — Magnitude of step smaller than machine precision and constraint violation less than options.TolCon.
0 — Maximum number of generations exceeded.
-1 — Optimization terminated by the output or plot function.
-2 — No feasible point found.
-4 — Stall time limit exceeded.
-5 — Time limit exceeded.
[x,fval,exitflag,output] = ga(...) returns output, a structure that contains output from each generation and other information about the performance of the algorithm. The output structure contains the following fields:
randstate — The state of rand, the MATLAB® random number generator, just before the algorithm started.
randnstate — The state of randn the MATLAB normal random number generator, just before the algorithm started. You can use the values of randstate and randnstate to reproduce the output of ga. See Reproducing Your Results.
generations — The number of generations computed.
funccount — The number of evaluations of the fitness function
message — The reason the algorithm terminated.
maxconstraint — Maximum constraint violation, if any.
[x,fval,exitflag,output,population] = ga(...) returns the matrix, population, whose rows are the final population.
[x,fval,exitflag,output,population,scores] = ga(...) returns scores the scores of the final population.
Note For problems that use the population type Double Vector (the default), ga does not accept functions whose inputs are of type complex. To solve problems involving complex data, write your functions so that they accept real vectors, by separating the real and imaginary parts. |
Given the following inequality constraints and lower bounds

the following code finds the minimum of the function, lincontest6, that is provided your software:
A = [1 1; -1 2; 2 1]; b = [2; 2; 3]; lb = zeros(2,1); [x,fval,exitflag] = ga(@lincontest6,... 2,A,b,[],[],lb) Optimization terminated: average change in the fitness value less than options.TolFun. x = 0.7794 1.2205 fval = -8.03916 exitflag = 1
[1] Goldberg, David E., Genetic Algorithms in Search, Optimization & Machine Learning, Addison-Wesley, 1989.
[2] A. R. Conn, N. I. M. Gould, and Ph. L. Toint. "A Globally Convergent Augmented Lagrangian Algorithm for Optimization with General Constraints and Simple Bounds", SIAM Journal on Numerical Analysis, Volume 28, Number 2, pages 545–572, 1991.
[3] A. R. Conn, N. I. M. Gould, and Ph. L. Toint. "A Globally Convergent Augmented Lagrangian Barrier Algorithm for Optimization with General Inequality Constraints and Simple Bounds", Mathematics of Computation, Volume 66, Number 217, pages 261–288, 1997.
gaoptimget, gaoptimset, patternsearch, simulannealbnd, threshacceptbnd
![]() | Functions — Alphabetical List | gamultiobj | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |