Documentation Center |
Find minimum of function using genetic algorithm
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(fitnessfcn,nvars,A,b,[],[],LB,UB,nonlcon,IntCon)
x = ga(fitnessfcn,nvars,A,b,[],[],LB,UB,nonlcon,IntCon,options)
x = ga(problem)
[x,fval]
= ga(fitnessfcn,nvars,...)
[x,fval,exitflag]
= ga(fitnessfcn,nvars,...)
[x,fval,exitflag,output]
= ga(fitnessfcn,nvars,...)
[x,fval,exitflag,output,population]
= ga(fitnessfcn,nvars,...)
[x,fval,exitflag,output,population,scores]
= ga(fitnessfcn,nvars,...)
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.
x = ga(fitnessfcn,nvars,A,b) finds a local minimum x to fitnessfcn, subject to the linear inequalities A*x ≤ b. ga evaluates the matrix product A*x as if x is transposed (A*x').
x = ga(fitnessfcn,nvars,A,b,Aeq,beq) finds a local minimum x to fitnessfcn, subject to the linear equalities Aeq*x = beq as well as A*x ≤ b. (Set A=[] and b=[] if no linear inequalities exist.) ga evaluates the matrix product Aeq*x as if x is transposed (Aeq*x').
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 LB ≤ x ≤ UB. (Set Aeq=[] and beq=[] if no linear equalities exist.)
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 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.)
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. (Set nonlcon=[] if no nonlinear constraints exist.)
x = ga(fitnessfcn,nvars,A,b,[],[],LB,UB,nonlcon,IntCon) requires that the variables listed in IntCon take integer values.
Note: When there are integer constraints, ga does not accept linear or nonlinear equality constraints, only inequality constraints. |
x = ga(fitnessfcn,nvars,A,b,[],[],LB,UB,nonlcon,IntCon,options) minimizes with integer constraints and with the default optimization parameters replaced by values in the options structure.
x = ga(problem) finds the minimum for problem, where problem is a structure.
[x,fval] = ga(fitnessfcn,nvars,...) returns fval, the value of the fitness function at x.
[x,fval,exitflag] = ga(fitnessfcn,nvars,...) returns exitflag, an integer identifying the reason the algorithm terminated.
[x,fval,exitflag,output] = ga(fitnessfcn,nvars,...) returns output, a structure that contains output from each generation and other information about the performance of the algorithm.
[x,fval,exitflag,output,population] = ga(fitnessfcn,nvars,...) returns the matrix, population, whose rows are the final population.
[x,fval,exitflag,output,population,scores] = ga(fitnessfcn,nvars,...) returns scores the scores of the final population.
fitnessfcn |
Handle to the fitness function. The fitness function should accept a row vector of length nvars and return a scalar value. When the 'Vectorized' option is 'on', fitnessfcn should accept a pop-by-nvars matrix, where pop is the current population size. In this case fitnessfcn should return a vector the same length as pop containing the fitness function values. fitnessfcn should not assume any particular size for pop, since ga can pass a single member of a population even in a vectorized calculation. | ||||||||||||||||||||||||||
nvars |
Positive integer representing the number of variables in the problem. | ||||||||||||||||||||||||||
A |
Matrix for linear inequality constraints of the form A*x ≤ b. If the problem has m linear inequality constraints and nvars variables, then
ga evaluates the matrix product A*x as if x is transposed (A*x'). | ||||||||||||||||||||||||||
b |
Vector for linear inequality constraints of the form A*x ≤ b. If the problem has m linear inequality constraints and nvars variables, then
| ||||||||||||||||||||||||||
Aeq |
Matrix for linear equality constraints of the form Aeq*x = beq. If the problem has m linear equality constraints and nvars variables, then
ga evaluates the matrix product Aeq*x as if x is transposed (Aeq*x'). | ||||||||||||||||||||||||||
beq |
Vector for linear equality constraints of the form Aeq*x = beq. If the problem has m linear equality constraints and nvars variables, then
| ||||||||||||||||||||||||||
LB |
Vector of lower bounds. ga enforces that iterations stay above LB. Set LB(i) = –Inf if x(i)is unbounded below. | ||||||||||||||||||||||||||
UB |
Vector of upper bounds. ga enforces that iterations stay below UB. Set UB(i) = Inf if x(i) is unbounded above. | ||||||||||||||||||||||||||
nonlcon |
Function handle that returns two outputs: [c,ceq] = nonlcon(x) ga attempts to achieve c ≤ 0 and ceq = 0. c and ceq are row vectors when there are multiple constraints. Set unused outputs to []. You can write nonlcon as a function handle to a file, such as nonlcon = @constraintfile where constraintfile.m is a file on your MATLAB^{®} path. To learn how to use vectorized constraints, see Vectorized Constraints.
| ||||||||||||||||||||||||||
options |
Structure containing optimization options. Create options using gaoptimset, or by exporting options from the Optimization app as described in Importing and Exporting Your Work in the Optimization Toolbox™ documentation. | ||||||||||||||||||||||||||
IntCon |
Vector of positive integers taking values from 1 to nvars. Each value in IntCon represents an x component that is integer-valued.
| ||||||||||||||||||||||||||
problem |
Structure containing the following fields:
Create problem by exporting a problem from the Optimization app, as described in Importing and Exporting Your Work in the Optimization Toolbox documentation. |
x |
Best point that ga located during its iterations. | |||||||||||||||||||||||
fval |
Fitness function evaluated at x. | |||||||||||||||||||||||
exitflag |
Integer giving the reason ga stopped iterating:
When there are integer constraints, ga uses the penalty fitness value instead of the fitness value for stopping criteria. | |||||||||||||||||||||||
output |
Structure containing output from each generation and other information about algorithm performance. The output structure contains the following fields:
| |||||||||||||||||||||||
population |
Matrix whose rows contain the members of the final population. | |||||||||||||||||||||||
scores |
Column vector of the fitness values (scores for integerconstraints problems) of the final population. |
Given the following inequality constraints and lower bounds
use this code to find the minimum of the lincontest6 function, which is provided in 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.6700 1.3310 fval = -8.2218 exitflag = 1
Optimize a function where some variables must be integers:
fun = @(x) (x(1) - 0.2)^2 + ... (x(2) - 1.7)^2 + (x(3) - 5.1)^2; x = ga(fun,3,[],[],[],[],[],[],[], ... [2 3]) % variables 2 and 3 are integers Optimization terminated: average change in the penalty fitness value less than options.TolFun and constraint violation is less than options.TolCon. x = 0.2000 2.0000 5.0000
[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.