国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

使用遺傳算法求二元函數的最小值

2020-02-15 21:16:23
字體:
來源:轉載
供稿:網友

二元函數為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;     %代計數器增加endplot(trace(:,1));hold onplot(trace(:,2),'.')gridlegend('最優解的變化','解的平均值的變化')

根據上面的求解模型,可以寫出模型的.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_youngerfunction 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            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 铅山县| 东方市| 武功县| 墨玉县| 塔城市| 怀安县| 诸城市| 六盘水市| 印江| 通江县| 齐齐哈尔市| 三台县| 本溪| 沾益县| 兰西县| 内丘县| 龙里县| 遵化市| 玉溪市| 新河县| 龙川县| 虎林市| 易门县| 英超| 灌云县| 夹江县| 威远县| 韩城市| 读书| 康乐县| 尚义县| 曲沃县| 新巴尔虎左旗| 澄城县| 茶陵县| 宜阳县| 吉林市| 凤山市| 鲁山县| 福泉市| 大名县|