遗传算法 多元单峰函数的优化实例

loading 分享 2026-9-4 下载文档

多元单峰函数的优化实例

1, 工具:谢菲尔德遗传算法工具箱 2, 运行环境:Matlab7

3, 表达式 f(x)=sum(x(i)^2) , -512<=x(i)<=512 i 为定义问题的一个值。 4, 本例选择 个体数量 40;最大遗传代数500;变量个数即i为20,每个变量用20位表示,

代沟选择0.9

5, De Jong 函数 代码

% OBJFUN1.M (OBJective function for De Jong's FUNction 1) %

% This function implements the De Jong function 1. %

% Syntax: ObjVal = objfun1(Chrom,switch1) %

% Input parameters:

% Chrom - Matrix containing the chromosomes of the current % population. Each row corresponds to one individual's % string representation.

% if Chrom == [], then special values will be returned % switch - if Chrom == [] and

% switch == 1 (or []) return boundaries % switch == 2 return title

% switch == 3 return value of global minimum %

% Output parameters:

% ObjVal - Column vector containing the objective values of the % individuals in the current population.

% if called with Chrom == [], then ObjVal contains

% switch == 1, matrix with the boundaries of the function % switch == 2, text for the title of the graphic output % switch == 3, value of global minimum %

% Author: Hartmut Pohlheim

% History: 26.11.93 file created

% 27.11.93 text of title and switch added % 30.11.93 show Dim in figure title

% 16.12.93 switch == 3, return value of global minimum % 01.03.94 name changed in obj*

function ObjVal = objfun1(Chrom,switch1);

% Dimension of objective function Dim = 20;

% Compute population parameters [Nind,Nvar] = size(Chrom);

% Check size of Chrom and do the appropriate thing

% if Chrom is [], then define size of boundary-matrix and values if Nind == 0

% return text of title for graphic output if switch1 == 2

ObjVal = ['DE JONG function 1-' int2str(Dim)]; % return value of global minimum elseif switch1 == 3 ObjVal = 0;

% define size of boundary-matrix and values else

% lower and upper bound, identical for all n variables ObjVal = 100*[-5.12; 5.12];

ObjVal = ObjVal(1:2,ones(Dim,1)); end

% if Dim variables, compute values of function elseif Nvar == Dim

% function 1, sum of xi^2 for i = 1:Dim (Dim=30) % n = Dim, -5.12 <= xi <= 5.12

% global minimum at (xi)=(0) ; fmin=0 ObjVal = sum((Chrom .* Chrom)')';

% ObjVal = diag(Chrom * Chrom'); % both lines produce the same % otherwise error, wrong format of Chrom else

error('size of matrix Chrom is not correct for function evaluation'); end

% End of function

注意:在原工具箱所带的目标函数文件里,用switch 做为标志,和Matlab 内部函数冲突,所以该为switch1

6,工作框下输入的代码 NIND=40;

MAXGEN=500; NVAR=20; PRECI=20; GGAP=0.9;

trace=zeros(MAXGEN,2);

FieldD=[rep([PRECI],[1,NVAR]);rep([-512;512],[1,NVAR]);rep([1;0;1;1],[1,NVAR])]; Chrom=CRTBP(NIND,NVAR*PRECI); gen=0;

ObjV=OBJFUN1(BS2RV(Chrom,FieldD)); while gen

SelCh=select('sus',Chrom,FitnV,GGAP); SelCh=recombin('xovsp',SelCh,0.7); SelCh=mut(SelCh);

ObjVSel=objfun1(BS2RV(SelCh,FieldD));

[Chrom ObjV]=reins(Chrom,SelCh,1,1,ObjV,ObjVSel); gen=gen+1;

trace(gen,1)=min(ObjV);

trace(gen,2)=sum(ObjV)/length(ObjV); end

plot (trace(:,1));hold on; plot (trace(:,2),'-.');grid;

legend('mean fitness','solution')

注意:大家最好把所要用到的函数名称(比如 交叉 变异 选择等)和工具箱中的文件名称大小写一致,这样在运行的时候就不会出现警告提示 6, 结果图

1614121086420mean fitness solutionx 105050100150200250300350400450500


遗传算法 多元单峰函数的优化实例.doc 将本文的Word文档下载到电脑
搜索更多关于: 遗传算法 多元单峰函数的优化实例 的文档
相关推荐
相关阅读