ga
用遗传算法寻找函数的最优解
语法规则
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)%
其中fitnessfc为函数的句柄或者为匿名函数
nvars,表示自变量个个数(例如自变量为向量X,nvars代表X中的元素个数)
A,b就是表达式A*X<=b;
Aeq:表示线性等式约束矩阵,若是没有等式约束就写为[];
Beq:表示线性等式约束的个数Beq=length(nvars);
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(…)
例子
A = [1 1; -1 2; 2 1]; b = [2; 2; 3]; lb = zeros(2,1); [x,fval,exitflag] = ga(@lincontest6,2,A,b,[],[],lb) %lb表示x的下界,up表示上界 Optimization terminated: average change in the fitness value less than options.TolFun. x = 0.7794 1.2205 fval = -8.03916 exitflag =