Matlab 7 GA 在 Matlab 7.0 中提供了 Genetic Algorithm and Direct Search 工具箱,
2、gatool 的用法 在matlab7命令行输入 gatool,见附图。 在 PopulationSize=10000; 请注意Mutation函数的选择。 f(x1*,x2*)=-my_func1(x1*,x2*)=38.84741978236206, where x1*=11.62378; x2*=5.72501 3、ga 函数的使用 因为要使用非原始设定,所以要采用 ga 的第2种调用方式,并且Mutation要采用mutationuniform函数。 opt1 = gaoptimset; opt1.PopInitRange = [[-3.0 4.1];[12.1 5.8]]; opt1.PopulationSize = 1000; opt1.MutationFcn=@mutationuniform; [x, fval] = ga(@my_func1,2,opt1) 结果如下: >> opt1 opt1 = PopulationType: 'doubleVector' PopInitRange: [2x2 double] PopulationSize: 1000 EliteCount: 2 CrossoverFraction: 0.80000000000000 MigrationDirection: 'forward' MigrationInterval: 20 MigrationFraction: 0.20000000000000 Generations: 100 TimeLimit: Inf FitnessLimit: -Inf StallGenLimit: 50 StallTimeLimit: 20 InitialPopulation: [] InitialScores: [] PlotInterval: 1 CreationFcn: @gacreationuniform FitnessScalingFcn: @fitscalingrank SelectionFcn: @selectionstochunif CrossoverFcn: @crossoverscattered MutationFcn: @mutationuniform HybridFcn: [] Display: 'final' PlotFcns: [] OutputFcns: [] Vectorized: 'off' >> [x, fval] = ga(@my_func1,2,opt1) Optimization terminated: stall generations limit exceeded. x = 11.60992040806107 5.72602745388080 fval = -38.61619496300001 >> (缩略图,点击图片链接看原图) function y = my_func2(x) %fmax r=sqrt((x(1)-50)^2+(x(2)-50)^2)+exp(1); y=-(sin(r)/r+1); function y = my_func2a(x) %fmin r=sqrt((x(1)-50)^2+(x(2)-50)^2)+exp(1); y=(sin(r)/r+1); 原贴见 http://bbs.matwav.com/post/view?bid=7&id=45307&sty=0&tpg=2&ppg=1&age=0#45307 请注意 Mutation 选择 Gaussian 函数时, Scale 要设成 0; 结果: minimize f(x) = 0.7827663723309104; maximize f(x) = 1.1283745483594003 (缩略图,点击图片链接看原图) (责任编辑:泉水) |