python多元函数求极小值_使用遗传算法求二元函数的最小值
二元函數(shù)為y=x1^2+x2^2,x∈[-5,5]
NIND=121; %初始種群的個數(shù)(Number of individuals)
NVAR=2; %一個染色體(個體)有多少基因
PRECI=20; %變量的二進制位數(shù)(Precision of variables)
MAXGEN=200; %最大遺傳代數(shù)(Maximum number of generations)
GGAP=0.8; %代溝(Generation gap),以一定概率選擇父代遺傳到下一代
trace=zeros(MAXGEN,2); %尋優(yōu)結果的初始值
Chrom=crtbp(NIND,PRECI*NVAR); %初始種群
%區(qū)域描述器(Build field descriptor)
%確定每個變量的二進制位數(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; %代計數(shù)器
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; %代計數(shù)器增加
end
plot(trace(:,1));
hold on
plot(trace(:,2),'.')
grid
legend('最優(yōu)解的變化','解的平均值的變化')
根據(jù)上面的求解模型,可以寫出模型的.M文件如下,即適應度函數(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:
二進制串到實值的轉換
Phen=bs2rv(Chrom,FieldD) FieldD=[len, lb, ub, code, scale, lbin, ubin]
code(i)=1為標準的二進制編碼,code(i)=0為格雷編碼
scale(i)=0為算術刻度,scale(i)=1為對數(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指定種群中個體的數(shù)量,Lind指定個體的長度
函數(shù) crtrp:
創(chuàng)建實值原始種群
Chrom=crtrp(Nind,FieldDR)
適應度計算函數(shù) ranking:
基于排序的適應度分配(此函數(shù)是從最小化方向對個體進行排序的)
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中子種群的數(shù)量,默認為1
選擇高級函數(shù) select:
從種群中選擇個體
SelCh=select(SEL_F, Chrom, FitnV)
SelCh=select(SEL_F, Chrom, FitnV, GGAP)
SelCh=select(SEL_F, Chrom, FitnV, GGAP, SUBPOP)
SEL_F是一字符串,為一低級選擇函數(shù)名,如rws或sus
GGAP指出了代溝,默認為1;也可大于1,允許子代數(shù)多于父代的數(shù)量
rws: 輪盤賭選擇
NewChrIx=rws(FitnV, Nsel) 使用輪盤賭選擇從一個種群中選擇Nsel個個體
NewChrIx 是為育種選擇的個體的索引值
sus:
隨機遍歷抽樣
NewChrIx=sus(FitnV, Nsel)
交叉高級函數(shù) recombin:
重組個體
NewChrom=recombin(REC_F, Chrom)
NewChrom=recombin(REC_F, Chrom, RecOpt)
NewChrom=recombin(REC_F, Chrom, RecOpt, SUBPOP)
REC_F是包含低級重組函數(shù)名的字符串,例如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指明交叉點數(shù), 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)
變異高級函數(shù) mutate:
個體的變異
NewChorm=mutate(MUT_F, OldChorm, FieldDR) NewChorm=mutate(MUT_F, OldChorm, FieldDR, MutOpt) NewChorm=mutate(MUT_F, OldChorm, FieldDR, MutOpt, SUBPOP) MUT_F為包含低級變異函數(shù)的字符串,例如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)
重插入函數(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為基于適應度的選擇,默認為0
InsOpt(2)指明在[0 1]間每個子種群中重插入的子代個體在整個子種群的中個體的比率,默認為1
ObjVch包含Chorm中個體的目標值,對基于適應度的重插入是必需的
ObjVSel包含Selch中個體的目標值,如子代數(shù)量大于重插入種群的子代數(shù)量是必需的
其他函數(shù)矩陣復試函數(shù) rep:
MatOut=rep(MatIn, REPN)
REPN為復制次數(shù)
以上這篇使用遺傳算法求二元函數(shù)的最小值就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
總結
以上是生活随笔為你收集整理的python多元函数求极小值_使用遗传算法求二元函数的最小值的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网站设计软件linux,ubuntu下的
- 下一篇: php 获取header_php 输出4