CSDN博客

img yuanqingfei

Matlab 7 GA

发表于2004/7/3 12:34:00  2694人阅读

分类: 〖Matlab〗

在 Matlab 7.0 中提供了 Genetic Algorithm and Direct Search 工具箱,
现在简单介绍下。转载时请保持作者信息完整。

1、首先编写fitness函数,函数内容见后面的引用。

function score = my_func1(pop)
%f(x1,x2)=21.5+x1*sin(4*pi*x1)+x2*sin(20*pi*x2)
%-3.0<=x1<=12.1; 4.1<=x2<=5.8
score = -21.5 - pop(1)*sin(4*pi*pop(1)) - pop(2)*sin(20*pi*pop(2));

从ga函数说明可知
% X = GA(FITNESSFCN,NVARS) finds the minimum of FITNESSFCN using
% GA. NVARS is the dimension (number of design variables) of the
% FITNESSFCN. FITNESSFCN accepts a vector X of size 1-by-NAVRS,
% and returns a scalar evaluated at X.
%     
% X = GA(FITNESSFCN,NAVRS,OPTIONS) finds the minimum for
% FITNESSFCN with the default optimization parameters replaced by values
% in the structure OPTIONS. OPTIONS can be created with the GAOPTIMSET
% function.
ga 是寻找最小值的,所以将原函数取负。


从本站翻出来一个老贴。
[parse]lihuanc[/parse]
关于优化方面的:
f(x1,x2)=21.5+x1*sin(4*pi*x1)+x2*sin(20*pi*x2)
-3.0<=x1<=12.1 4.1<=x2<=5.8

[parse]zonly[/parse]
用遗传算法算了下,得到一个最优值为38.8503
x1*=11.6255 x2*=5.7250
但似乎仍不是全局最优解

[parse]easyworm[/parse]
呵呵,这个问题的局部极小较多,我用NLPSolver1.0(到偶的网站www.easyworm.com下载)算了一下:
初始值取X1=11.600000, X2=4.500000
最优目标值为37.650301
最优点处X1*=11.625545, X2*=4.525056
lingo 8.0的计算结果为37.45030
最优点处X1*=11.62554, X2*=4.325057


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

(缩略图,点击图片链接看原图)


0 0

相关博文

我的热门文章

img
取 消
img