GADS--Matlab遗传算法工具箱使用总结
e-mail:978299005@qq
一、GADS简介与启动
MATLAB是矩阵实验室(Matrix Laboratory)的简称,是美国MathWorks公司出品的商业数学软件,用于算法开发、数据可视化、数据分析以及数值计算的高级技术计算语言和交互式环境,主要包括MATLAB和Simulink两大部分。
遗传算法(Genetic Algorithm)是模拟达尔文生物进化论的自然选择和遗传学机理的生物进化过程的计算模型,是一种通过模拟自然进化过程搜索最优解的方法。
在Matlab平台上主要有三个遗传算法(GA)的工具箱,分别是:GAOT,美国北卡罗来纳大学开发;GATBX,英国谢菲尔德大学开发;GADS,Matlab7以后的版本中自带的。GATBX可以包含GAOT,而GADS显然年代又近了一些。这里主要讲的是GADS。
GADS(Genetic Algorithm and Direct Search Toolbox)遗传算法与直接搜索工具箱。可以在命令行中直接使用,在M文件的程序中调用ga函数,或在GUI 界面中使用它来解决实际问题。在不同的Matlab版本中启动方法稍有区别。以笔者的Matlab 2010b为例,启动有两种方法:
1、在Matlab命令行中输入optimtool回车,在出现的对话框左上角
到Solver,选择ga- Genetic Algorithm即可。
2、Matlab界面中单击左下角Start,选择toolboxes,选择其中的
optimization再点击optimization tool即可打开对话框,然后如1中,
选择ga即可。
二、GADS的具体使用
【1】先介绍ga函数的格式。Ga函数可以在命令行中直接使用。在命令行中键入命令type ga可以打印出ga函数的代码。键入help ga,就打印出ga函数的帮助提示。以下是help ga的输出:
GA Constrained optimization using genetic algorithm.
GA attempts to solve problems of the form:
min F(X) subject to: A*X <= B, Aeq*X = Beq (linear constraints)
X C(X) <= 0, Ceq(X) = 0 (nonlinear constraints)
LB <= X <= ub
X = GA(FITNESSFCN,NVARS) finds a local unconstrained minimum X to the
FITNESSFCN using GA. NVARS is the dimension (number of design
variables) of the FITNESSFCN. 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 the function
FITNESSFCN, subject to the linear inequalities A*X <= B. Linear
constraints are not satisfied when the PopulationType option is set to
'bitString' or 'custom'. See the documentation for details.
X = GA(FITNESSFCN,NVARS,A,b,Aeq,beq) finds a local minimum X to the
function FITNESSFCN, subject to the linear equalities Aeq*X = beq as
well as A*X <= B. (Set A=[] and B=[] if no inequalities exist.) Linear
constraints are not satisfied when the PopulationType option is set to
'bitString' or 'custom'. See the documentation for details.
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
tool工具箱exist. Set lb(i) = -Inf if X(i) is unbounded below; set ub(i) = Inf if
X(i) is unbounded above. Linear constraints are not satisfied when the
PopulationType option is set to 'bitString' or 'custom'. See the
documentation for details.
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
FITNESSFCN such that C(X)<=0 and Ceq(X)=0. (Set lb=[] and/or ub=[] if
no bounds exist.) Nonlinear constraints are not satisfied when the
PopulationType option is set to 'bitString' or 'custom'. See the
documentation for details.
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. OPTIONS can be created with the GAOPTIMSET function.
See GAOPTIMSET for details.
X = GA(PROBLEM) finds the minimum for PROBLEM. PROBLEM is a structure
that has the following fields:
fitnessfcn: <Fitness function>
nvars: <Number of design variables>
Aineq: <A matrix for inequality constraints>
bineq: <b vector for inequality constraints>
Aeq: <Aeq matrix for equality constraints>
beq: <beq vector for equality constraints>
lb: <Lower bound on X>
ub: <Upper bound on X>
nonlcon: <nonlinear constraint function>
options: <Options structure created with GAOPTIMSET>
rngstate: <State of the random number generator>
[X,FVAL] = GA(FITNESSFCN, ...) returns FVAL, the value of the fitness
function FITNESSFCN at the solution X.
[X,FVAL,EXITFLAG] = GA(FITNESSFCN, ...) returns EXITFLAG which
describes the exit condition of GA. Possible values of EXITFLAG and the
corresponding exit conditions are
1 Average change in value of the fitness function over
options.StallGenLimit generations less than options.TolFun 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. This exit condition applies
only to nonlinear constraints.
5 Fitness limit reached 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(FITNESSFCN, ...) returns a
structure OUTPUT with the following information:
rngstate: <State of the random number generator before GA started>
generations: <Total generations, excluding HybridFcn iterations>
funccount: <Total function evaluations>
maxconstraint: <Maximum constraint violation>, if any
message: <GA termination message>
[X,FVAL,EXITFLAG,OUTPUT,POPULATION] = GA(FITNESSFCN, ...) returns the
final POPULATION at termination.
[X,FVAL,EXITFLAG,OUTPUT,POPULATION,SCORES] = GA(FITNESSFCN, ...) returns
the SCORES of the final POPULATION.
Example:
Unconstrained minimization of 'rastriginsfcn' fitness function of
numberOfVariables = 2
x = ga(@rastriginsfcn,2)
Display plotting functions while GA minimizes
options = gaoptimset('PlotFcns',...
{@gaplotbestf,@gaplotbestindiv,@gaplotexpectation,@gaplotstopping});
[x,fval,exitflag,output] = ga(@rastriginsfcn,2,[],[],[],[],[],[],[],options)
An example with inequality constraints and lower bounds
A = [1 1; -1 2; 2 1];    b = [2; 2; 3]; lb = zeros(2,1);
% Use mutation function which can handle constraints
options = gaoptimset('MutationFcn',@mutationadaptfeasible);
[x,fval,exitflag] = ga(@lincontest6,2,A,b,[],[],lb,[],[],options);
FITNESSFCN can also be an anonymous function:
x = ga(@(x) 3*sin(x(1))+exp(x(2)),2)
If FITNESSFCN or NONLCON are parameterized, you can use anonymous
functions to capture the problem-dependent parameters. Suppose you want
to minimize the fitness given in the function myfit, subject to the
nonlinear constraint myconstr, where these two functions are
parameterized by their second argument a1 and a2, respectively. Here
myfit and myconstr are MATLAB file functions such as
function f = myfit(x,a1)
f = exp(x(1))*(4*x(1)^2 + 2*x(2)^2 + 4*x(1)*x(2) + 2*x(2) + a1);
and
function [c,ceq] = myconstr(x,a2)
c = [1.5 + x(1)*x(2) - x(1) - x(2);
-x(1)*x(2) - a2];
% No nonlinear equality constraints:
ceq = [];
To optimize for specific values of a1 and a2, first assign the values
to these two parameters. Then create two one-argument anonymous
functions that capture the values of a1 and a2, and call myfit and
myconstr with two arguments. Finally, pass these anonymous functions to
GA:
a1 = 1; a2 = 10; % define parameters first
% Mutation function for constrained minimization
options = gaoptimset('MutationFcn',@mutationadaptfeasible);
x = ga(@(x)myfit(x,a1),2,[],[],[],[],[],[],@(x)myconstr(x,a2),options)
解释如下:
ga函数最完整的格式是[X,FVAL,EXITFLAG,OUTPUT,POPULATION,SCORES]= GA(FITNESSFCN,NVARS,A,b,Aeq,beq,lb,ub,NONLCON,options)。
输出:
X是最优值所对应的自变量值,如果有多个变量的话,X就是一个vector;
FVAL是求得的最优值;
EXITFLAG是退出标志,代表算法结束的原因。为确保收敛,规定了许多限制,比如总时间不超过30秒,总代数不超过N代,最优值连续多少代没有变化就退出,或连续多少秒没有变化就退出运行等等,这些参数都是可设的,EXITFLAG 就记录了退出的原因。取值-5~5.
OUTPUT是一个结构体。里面包含算法终止时所经历的代数generations,随机数种子的信息rngstate等。
POPULATION是算法终止的时候的种。记录的是自变量的值。
SCORES是算法终止是种的函数值,是POPULATION中的个体,经计算得出的结果。
输入:
FITNESSFCN是所要优化的函数句柄。可以写在单独的M文件里。fun_name.m,则FITNESSFCN是@ fun_name。函数的写法有明确的形式规定。
function f=fun_name(x)
f=x(1)+x(2);
就代表y=x1+x2这个函数。注意,输入的x是一个矢量,得到的返回值是标量。或者也可以不写成m文件。可以匿名的写:
ga(@(x) x(1)+x(2),…)
NVARS是自变量的个数,上例中,就是2.
A和b共同构成了对X的一个线性约束,代表A*X<=b.
Aeq和beq也是类似:Aeq*X=beq.这四个如果不存在的话写[]就可以了。
Lb和ub构成了对自变量范围的约束。如NVARS=2,x1属于[0,1],x2属于[-2,2],则lb=[0,-2],ub=[1,2]。
NONLCON是非线性约束的函数句柄。也有参数和返回值的规定。举例说明:function [c,ceq] = myconstr(x)
c = [1.5 + x(1)*x(2) - x(1) - x(2);
-x(1)*x(2)];