Find the Minimum of a Function Using Genetic Algorithm

Find the Minimum of a Function Using Genetic Algorithm

ga

Find the minimum of a function using genetic algorithm

Syntax

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, reason] = ga(...)

Description

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.

x = ga(fitnessfcn,nvars,A,b) finds a local minimum x to fitnessfcn, subject to the linear inequalities A*x ≤b. 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.

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

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

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 gaoptimset for details.

x = ga(problem) finds the minimum for problem, a structure containing the following fields:

[x, fval] = ga(...) returns fval, the value of the fitness function at x.

[x, fval, reason] = ga(...) returns reason, a string containing the reason the algorithm stops.

[x, fval, reason, 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. This message is the same as the output argument reason.

maxconstraint — Maximum constraint violation, if any.

[x, fval, reason, output, population] = ga(...) returns the matrix, population, whose rows are the final population.

[x, fval, reason, 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.

Example

Given the following inequality constraints and lower bounds

the following code finds the minimum of the function, lincontest6, that is provided with the toolbox:

A = [1 1; -1 2; 2 1];

b = [2; 2; 3];

lb = zeros(2,1);

[x, fval, exitflag] = ga(@lincontest6,...

2,A,b,[],[],lb,[],[],options)

Optimization terminated:

average change in the fitness value less than options.TolFun.

x =

0.7421 1.2679

fval =

-8.10739

exitflag =

Optimization terminated: average change in the fitness value less than options.TolFun.

References

[1] Goldberg, David E., Genetic Algorithms in Search, Optimzation & 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 algorithm for optimization with general constraints and simple bounds", Mathematics of Computation, Volume 66, Number 217, pages 261 – 288, 1997.