X = ga(FITNESSFCN,NVARS,A,b,Aeq,beq,lb,ub,NONLCON,options) minimizes
with the default optimization parameters replaced by values in OPTIONS.
OPTIONS can be created with the OPTIMOPTIONS function. See OPTIMOPTIONS
for details. For a list of options accepted by ga refer to the
documentation.
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>
intcon: <Index vector for integer variables>
options: <Options created with optimoptions('ga',...)>
rngstate: <State of the random number generator>
在做Unit电阻分配的问题时,进行使用。
结构稍微有些变动,option中的设置都是算法终止的条件。
options = gaoptimset('PopulationSize',20,'Generations',350 ...
,'StallGenLimit',Inf,'TolFun',1e-10);
%算法计算
[ord,fval,exitflag,output,population,scores]=ga(fun,len,[],[],[],[],ones(len,1),GroupNum*ones(len,1),[],[1:len],options)
ones(len,1),GroupNum*ones(len,1)——变量范围是由1到分组的数量
[1:len]——数据取整数,不能取小数
再举个例子
二次规划:已知
function y = lincontest6(x)
p1=0.5;p2=6.0;y = p1*x(1)^2 + x(2)^2 -x(1)*x(2) -2*x(1) - p2*x(2);
主函数
A = [1 1; -1 2; 2 1]; b = [2; 2; 3]; lb = zeros(2,1);
% Use mutation function which can handle constraints
options = optimoptions('ga','MutationFcn',@mutationadaptfeasible);
[x,fval,exitflag] = ga(@lincontest6,2,A,b,[],[],lb,[],[],options)
%仅为参考的用法
%fitnessfcn = @GADS; % 适应度函数句柄
%nvars = 2; % 个体的变量数目
%参数设置
%options = gaoptimset('PopulationSize',100,'EliteCount',10,'CrossoverFraction',0.75,'Generations',500,'StallGenLimit',500,'TolFun',1e-100,'PlotFcns',{@gaplotbestf,@gaplotbestindiv});
%[x_best,fval] =ga(fitnessfcn,nvars,[],[],[],[],[],[],[],options); % 调用ga函数