使用遗传算法求二元函数的最小值

使用遗传算法求二元函数的最小值,第1张

使用遗传算法求二元函数的最小值

二元函数为y=x1^2+x2^2,x∈[-5,5]

NIND=121;  %初始种群的个数(Number of individuals)
NVAR=2;   %一个染色体(个体)有多少基因
PRECI=20;  %变量的二进制位数(Precision of variables)
MAXGEN=200;  %最大遗传代数(Maximum number of generations)
GGAP=0.8;  %代沟(Generation gap),以一定概率选择父代遗传到下一代
trace=zeros(MAXGEN,2);   %寻优结果的初始值

Chrom=crtbp(NIND,PRECI*NVAR); %初始种群

%区域描述器(Build field descriptor)
%确定每个变量的二进制位数,取值范围,及取值范围是否包括边界等。
FieldD=[rep([PRECI],[1,NVAR]);rep([-5;5],[1,NVAR]);rep([1;0;1;1],[1,NVAR])];
Objv=objfun(bs2rv(Chrom,FieldD))
gen=1;     %代计数器
while gen<=MAXGEN
 Fitv=ranking(Objv); %分配适应度值(Assign fitness values)
 SelCh=select('sus',Chrom,Fitv,GGAP); %选择
 SelCh=recombin('xovsp',SelCh,1);  %重组
 SelCh=mut(SelCh);      %变异
 ObjVSel=objfun(bs2rv(SelCh,FieldD));%子代个体的十进制转换
 %重插入子代的新种群
 [Chrom,Objv]=reins(Chrom,SelCh,1,1,Objv,ObjVSel);
 trace(gen,1)=min(Objv);   %遗传算法性能跟踪
 trace(gen,2)=sum(Objv)/length(Objv);
  gen=gen+1;     %代计数器增加
end
plot(trace(:,1));
hold on
plot(trace(:,2),'.')
grid
legend('最优解的变化','解的平均值的变化')

根据上面的求解模型,可以写出模型的.M文件如下,即适应度函数

% OBJFUN.M  
% Syntax: ObjVal = objfun1(Chrom,rtn_type)
%
% 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
% rtn_type - if Chrom == [] and
%    rtn_type == 1 (or []) return boundaries
%    rtn_type == 2 return title
%    rtn_type == 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
%    rtn_type == 1, matrix with the boundaries of the function
%    rtn_type == 2, text for the title of the graphic output
%    rtn_type == 3, value of global minimum
% Author:  YQ_younger

function ObjVal = objfun(Chrom,rtn_type);

% Dimension of objective function
 Dim = 2; 
% 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 rtn_type == 2
   ObjVal = ['DE JONG function 1-' int2str(Dim)];
  % return value of global minimum
  elseif rtn_type == 3
   ObjVal = 0;
  % define size of boundary-matrix and values
  else 
   % lower and upper bound, identical for all n variables  
   ObjVal = 1*[-5; 5];
   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 <= xi <= 5
  % 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

注释:
种群表示和初始化函数 bs2rv:
二进制串到实值的转换
Phen=bs2rv(Chrom,FieldD) FieldD=[len, lb, ub, code, scale, lbin, ubin]
code(i)=1为标准的二进制编码,code(i)=0为格雷编码
scale(i)=0为算术刻度,scale(i)=1为对数刻度
函数 crtbp:
创建初始种群
[Chrom,Lind,baseV]=crtbp(Nind,Lind)

[Chrom,Lind,baseV]=crtbp(Nind,baseV)
[Chrom,Lind,baseV]=crtbp(Nind,Lind,baseV)

Nind指定种群中个体的数量,Lind指定个体的长度
函数 crtrp:
创建实值原始种群
Chrom=crtrp(Nind,FieldDR)

适应度计算函数 ranking:
基于排序的适应度分配(此函数是从最小化方向对个体进行排序的)
FitV=ranking(ObjV)
FitV=ranking(ObjV, RFun)
FitV=ranking(ObjV, RFun, SUBPOP)
Rfun(1)线性排序标量在[1 2]间为,非线性排序在[1 length(ObjV)-2]
Rfun(2)指定排序方法,0为线性排序,1为非线性排序
SUBPOP指明ObjV中子种群的数量,默认为1

选择高级函数 select:
从种群中选择个体
SelCh=select(SEL_F, Chrom, FitnV)
SelCh=select(SEL_F, Chrom, FitnV, GGAP)
SelCh=select(SEL_F, Chrom, FitnV, GGAP, SUBPOP)

SEL_F是一字符串,为一低级选择函数名,如rws或sus
GGAP指出了代沟,默认为1;也可大于1,允许子代数多于父代的数量
rws: 轮盘赌选择
NewChrIx=rws(FitnV, Nsel) 使用轮盘赌选择从一个种群中选择Nsel个个体
NewChrIx 是为育种选择的个体的索引值
sus:
随机遍历抽样
NewChrIx=sus(FitnV, Nsel)

交叉高级函数 recombin:
重组个体
NewChrom=recombin(REC_F, Chrom)
NewChrom=recombin(REC_F, Chrom, RecOpt)
NewChrom=recombin(REC_F, Chrom, RecOpt, SUBPOP)
REC_F是包含低级重组函数名的字符串,例如recdis,recint,reclin,xovdp, xovdprs, xovmp, xovsh, xovshrs, xovsp, xovsprs
recdis:
离散重组
NewChrom=recdis(OldChorm)
recint:
中间重组
NewChrom=recint(OldChorm)
reclin:
线性重组
NewChrom=reclin(OldChorm)
xovdp:

两点交叉

NewChrom=xovdp(OldChrom, XOVR)

XOVR为交叉概率, 默认为0.7
Xovdprs:
减少代理的两点交叉
NewChrom=xovdprs(OldChrom, XOVR)
Xovmp:

多点交叉

NewChrom=xovmp(OldChrom, XOVR, Npt, Rs)

Npt指明交叉点数, 0 洗牌交叉;1 单点交叉;2 两点交叉;
默认为0

Rs指明使用减少代理, 0 不减少代理;1 减少代理;
默认为0
Xovsh:

洗牌交叉

NewChrom=xovsh(OldChrom, XOVR)
Xovshrs:
减少代理的洗牌交叉
NewChrom=xovshrs(OldChrom, XOVR)
Xovsp:
单点交叉
NewChrom=xovsp(OldChrom, XOVR)
Xovsprs:
减少代理的单点交叉
NewChrom=xovsprs(OldChrom, XOVR)

变异高级函数 mutate:
个体的变异
NewChorm=mutate(MUT_F, OldChorm, FieldDR) NewChorm=mutate(MUT_F, OldChorm, FieldDR, MutOpt) NewChorm=mutate(MUT_F, OldChorm, FieldDR, MutOpt, SUBPOP) MUT_F为包含低级变异函数的字符串,例如mut, mutbga, recmut
mut:
离散变异算子
NewChrom=mut(OldChorm, Pm) NewChrom=mut(OldChorm, Pm, baseV)
Pm为变异概率,默认为Pm=0.7/Lind
mutbga:
实值种群的变异(遗传算法育种器的变异算子) NewChrom=mutbga(OldChorm, FieldDR)
NewChrom=mubga(OldChorm, FieidDR, MutOpt)
MutOpt(1)是在[ 0 1]间的重组概率的标量,默认为1
MutOpt(2)是在[0 1]间的压缩重组范围的标量,默认为1(不压缩)
recmut:
具有突变特征的线性重组
NewChrom=recmut(OldChorm, FieldDR)
NewChrom=recmut(OldChorm, FieidDR, MutOpt)

重插入函数 reins:
重插入子群到种群
Chorm=reins(Chorm, SelCh)
Chorm=reins(Chorm, SelCh, SUBPOP)
Chorm=reins(Chorm, SelCh, SUBPOP, InsOpt, ObjVch)
[Chorm, ObjVch]=reins(Chorm, SelCh, SUBPOP, InsOpt, ObjVch, ObjVSel)
InsOpt(1)指明用子代代替父代的选择方法,0为均匀选择,1为基于适应度的选择,默认为0
InsOpt(2)指明在[0 1]间每个子种群中重插入的子代个体在整个子种群的中个体的比率,默认为1

ObjVch包含Chorm中个体的目标值,对基于适应度的重插入是必需的
ObjVSel包含Selch中个体的目标值,如子代数量大于重插入种群的子代数量是必需的

其他函数矩阵复试函数 rep:
MatOut=rep(MatIn, REPN)
REPN为复制次数

以上这篇使用遗传算法求二元函数的最小值就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持考高分网。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/3238710.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-10-04
下一篇 2022-10-04

发表评论

登录后才能评论

评论列表(0条)

保存