MATLAB自带的遗传算法工具箱是GADS,输入help ga即可看到ga的用法,ga函数最完整的格式是[X,FVAL,EXITFLAG,OUTPUT,POPULATION,SCORES]=GA(FITNESSFCN,NVARS,A,b,Aeq,beq,lb,ub,NONLCON,options),
如计算函数y=100*(x1^2 – x2)^2 + (1-x2)^2的最大值,x1和x2都在范围[-2.048,2.048]内:
>> [x,val,flag,output,population,scores]=ga(@(x)-100*(x(1)^2-x(2))^2-(1-x(1))^2, 2, [],[],[],[],[-2.048, -2.048], [2.048, 2.048])
Optimization terminated: average change in the fitness value less than options.TolFun.
x =
2.0480 -2.0466
val =
-3.8960e+03
flag =
1
output =
problemtype: 'boundconstraints'
rngstate: [1x1 struct]
generations: 51
funccount: 1040
message: [1x86 char]
maxconstraint: 0
population =
2.0480 -2.0466
2.0480 -2.0466
2.0480 -2.0466
2.0480 -2.0466
2.0480 -2.0466
2.0480 -2.0466
2.0480 -2.0466
2.0480 -2.0434
2.0480 -2.0466
2.0480 -2.0466
2.0480 -2.0466
2.0480 -2.0466
2.0480 -2.0466
2.0480 -2.0466
2.0480 -2.0466
2.0480 -2.0466
2.0480 -2.0427
2.0441 -2.0463
2.0480 -2.0466
2.0475 -2.0427
scores =
1.0e+03 *
-3.8960 -3.8960 -3.8960 -3.8960 -3.8960 -3.8960 -3.8960 -3.8918 -3.8959 -3.8960 -3.8960 -3.8960 -3.8959 -3.8960 -3.8959 -3.8960 -3.8911 -3.8757 -3.8959 -3.8883
另外,输入ga可以直接在图形界面上进行操作。