您好,登錄后才能下訂單哦!
二元函數(shù)為y=x1^2+x2^2,x∈[-5,5]
NIND=121; %初始種群的個(gè)數(shù)(Number of individuals) NVAR=2; %一個(gè)染色體(個(gè)體)有多少基因 PRECI=20; %變量的二進(jìn)制位數(shù)(Precision of variables) MAXGEN=200; %最大遺傳代數(shù)(Maximum number of generations) GGAP=0.8; %代溝(Generation gap),以一定概率選擇父代遺傳到下一代 trace=zeros(MAXGEN,2); %尋優(yōu)結(jié)果的初始值 Chrom=crtbp(NIND,PRECI*NVAR); %初始種群 %區(qū)域描述器(Build field descriptor) %確定每個(gè)變量的二進(jìn)制位數(shù),取值范圍,及取值范圍是否包括邊界等。 FieldD=[rep([PRECI],[1,NVAR]);rep([-5;5],[1,NVAR]);rep([1;0;1;1],[1,NVAR])]; Objv=objfun(bs2rv(Chrom,FieldD)) gen=1; %代計(jì)數(shù)器 while gen<=MAXGEN Fitv=ranking(Objv); %分配適應(yīng)度值(Assign fitness values) SelCh=select('sus',Chrom,Fitv,GGAP); %選擇 SelCh=recombin('xovsp',SelCh,1); %重組 SelCh=mut(SelCh); %變異 ObjVSel=objfun(bs2rv(SelCh,FieldD));%子代個(gè)體的十進(jìn)制轉(zhuǎn)換 %重插入子代的新種群 [Chrom,Objv]=reins(Chrom,SelCh,1,1,Objv,ObjVSel); trace(gen,1)=min(Objv); %遺傳算法性能跟蹤 trace(gen,2)=sum(Objv)/length(Objv); gen=gen+1; %代計(jì)數(shù)器增加 end plot(trace(:,1)); hold on plot(trace(:,2),'.') grid legend('最優(yōu)解的變化','解的平均值的變化')
根據(jù)上面的求解模型,可以寫出模型的.M文件如下,即適應(yīng)度函數(shù)
% 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
注釋:
種群表示和初始化函數(shù) bs2rv:
二進(jìn)制串到實(shí)值的轉(zhuǎn)換
Phen=bs2rv(Chrom,FieldD) FieldD=[len, lb, ub, code, scale, lbin, ubin]
code(i)=1為標(biāo)準(zhǔn)的二進(jìn)制編碼,code(i)=0為格雷編碼
scale(i)=0為算術(shù)刻度,scale(i)=1為對(duì)數(shù)刻度
函數(shù) crtbp:
創(chuàng)建初始種群
[Chrom,Lind,BaseV]=crtbp(Nind,Lind)
[Chrom,Lind,BaseV]=crtbp(Nind,BaseV)
[Chrom,Lind,BaseV]=crtbp(Nind,Lind,BaseV)
Nind指定種群中個(gè)體的數(shù)量,Lind指定個(gè)體的長度
函數(shù) crtrp:
創(chuàng)建實(shí)值原始種群
Chrom=crtrp(Nind,FieldDR)
適應(yīng)度計(jì)算函數(shù) ranking:
基于排序的適應(yīng)度分配(此函數(shù)是從最小化方向?qū)€(gè)體進(jìn)行排序的)
FitV=ranking(ObjV)
FitV=ranking(ObjV, RFun)
FitV=ranking(ObjV, RFun, SUBPOP)
Rfun(1)線性排序標(biāo)量在[1 2]間為,非線性排序在[1 length(ObjV)-2]
Rfun(2)指定排序方法,0為線性排序,1為非線性排序
SUBPOP指明ObjV中子種群的數(shù)量,默認(rèn)為1
選擇高級(jí)函數(shù) select:
從種群中選擇個(gè)體
SelCh=select(SEL_F, Chrom, FitnV)
SelCh=select(SEL_F, Chrom, FitnV, GGAP)
SelCh=select(SEL_F, Chrom, FitnV, GGAP, SUBPOP)
SEL_F是一字符串,為一低級(jí)選擇函數(shù)名,如rws或sus
GGAP指出了代溝,默認(rèn)為1;也可大于1,允許子代數(shù)多于父代的數(shù)量
rws: 輪盤賭選擇
NewChrIx=rws(FitnV, Nsel) 使用輪盤賭選擇從一個(gè)種群中選擇Nsel個(gè)個(gè)體
NewChrIx 是為育種選擇的個(gè)體的索引值
sus:
隨機(jī)遍歷抽樣
NewChrIx=sus(FitnV, Nsel)
交叉高級(jí)函數(shù) recombin:
重組個(gè)體
NewChrom=recombin(REC_F, Chrom)
NewChrom=recombin(REC_F, Chrom, RecOpt)
NewChrom=recombin(REC_F, Chrom, RecOpt, SUBPOP)
REC_F是包含低級(jí)重組函數(shù)名的字符串,例如recdis,recint,reclin,xovdp, xovdprs, xovmp, xovsh, xovshrs, xovsp, xovsprs
recdis:
離散重組
NewChrom=recdis(OldChorm)
recint:
中間重組
NewChrom=recint(OldChorm)
reclin:
線性重組
NewChrom=reclin(OldChorm)
xovdp:
兩點(diǎn)交叉
NewChrom=xovdp(OldChrom, XOVR)
XOVR為交叉概率, 默認(rèn)為0.7
Xovdprs:
減少代理的兩點(diǎn)交叉
NewChrom=xovdprs(OldChrom, XOVR)
Xovmp:
多點(diǎn)交叉
NewChrom=xovmp(OldChrom, XOVR, Npt, Rs)
Npt指明交叉點(diǎn)數(shù), 0 洗牌交叉;1 單點(diǎn)交叉;2 兩點(diǎn)交叉;
默認(rèn)為0
Rs指明使用減少代理, 0 不減少代理;1 減少代理;
默認(rèn)為0
Xovsh:
洗牌交叉
NewChrom=xovsh(OldChrom, XOVR)
Xovshrs:
減少代理的洗牌交叉
NewChrom=xovshrs(OldChrom, XOVR)
Xovsp:
單點(diǎn)交叉
NewChrom=xovsp(OldChrom, XOVR)
Xovsprs:
減少代理的單點(diǎn)交叉
NewChrom=xovsprs(OldChrom, XOVR)
變異高級(jí)函數(shù) mutate:
個(gè)體的變異
NewChorm=mutate(MUT_F, OldChorm, FieldDR) NewChorm=mutate(MUT_F, OldChorm, FieldDR, MutOpt) NewChorm=mutate(MUT_F, OldChorm, FieldDR, MutOpt, SUBPOP) MUT_F為包含低級(jí)變異函數(shù)的字符串,例如mut, mutbga, recmut
mut:
離散變異算子
NewChrom=mut(OldChorm, Pm) NewChrom=mut(OldChorm, Pm, BaseV)
Pm為變異概率,默認(rèn)為Pm=0.7/Lind
mutbga:
實(shí)值種群的變異(遺傳算法育種器的變異算子) NewChrom=mutbga(OldChorm, FieldDR)
NewChrom=mubga(OldChorm, FieidDR, MutOpt)
MutOpt(1)是在[ 0 1]間的重組概率的標(biāo)量,默認(rèn)為1
MutOpt(2)是在[0 1]間的壓縮重組范圍的標(biāo)量,默認(rèn)為1(不壓縮)
recmut:
具有突變特征的線性重組
NewChrom=recmut(OldChorm, FieldDR)
NewChrom=recmut(OldChorm, FieidDR, MutOpt)
重插入函數(shù) 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為基于適應(yīng)度的選擇,默認(rèn)為0
InsOpt(2)指明在[0 1]間每個(gè)子種群中重插入的子代個(gè)體在整個(gè)子種群的中個(gè)體的比率,默認(rèn)為1
ObjVch包含Chorm中個(gè)體的目標(biāo)值,對(duì)基于適應(yīng)度的重插入是必需的
ObjVSel包含Selch中個(gè)體的目標(biāo)值,如子代數(shù)量大于重插入種群的子代數(shù)量是必需的
其他函數(shù)矩陣復(fù)試函數(shù) rep:
MatOut=rep(MatIn, REPN)
REPN為復(fù)制次數(shù)
以上這篇使用遺傳算法求二元函數(shù)的最小值就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。