⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 initializega.m

📁 对遗传算法中产生初始种群进行了详尽的描述
💻 M
字号:
function [pop] = initializega(num, bounds, evalFN,evalOps,options) %有5个输入项,1个输出项% function [pop]=initializega(populationSize, variableBounds,evalFN,%                           evalOps,options)%    initializega creates a matrix of random numbers with %    a number of rows equal to the populationSize and a number%    columns equal to the number of rows in bounds plus 1 for%    the f(x) value which is found by applying the evalFN.%    This is used by the ga to create the population if it%    is not supplied.%% pop            - the initial, evaluated, random population % populatoinSize - the size of the population, i.e. the number to create% variableBounds - a matrix which contains the bounds of each variable, i.e.%                  [var1_high var1_low; var2_high var2_low; ....]% evalFN         - the evaluation fn, usually the name of the .m file for %                  evaluation% evalOps        - any options to be passed to the eval function defaults []% options        - options to the initialize function, ie. %                  [type prec] where eps is the epsilon value %                  and the second option is 1 for float and 0 for binary, %                  prec is the precision of the variables defaults [1e-6 1]% Binary and Real-Valued Simulation Evolution for Matlab GAOT V2 % Copyright (C) 1998 C.R. Houck, J.A. Joines, M.G. Kay %% C.R. Houck, J.Joines, and M.Kay. A genetic algorithm for function% optimization: A Matlab implementation. ACM Transactions on Mathmatical% Software, Submitted 1996.%% This program is free software; you can redistribute it and/or modify% it under the terms of the GNU General Public License as published by% the Free Software Foundation; either version 1, or (at your option)% any later version.%% This program is distributed in the hope that it will be useful,% but WITHOUT ANY WARRANTY; without even the implied warranty of% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the% GNU General Public License for more details. A copy of the GNU % General Public License can be obtained from the % Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.if nargin<5  %nargin:是用来判断输入项个数的函数  options=[1e-6 1]; %如果初始化函数的输入项个数小于5,则参数结构体options=[1e-6 1]endif nargin<4 %如果初始化函数的输入项个数小于4,则传递给适应度函数的参数evalOps=[]  evalOps=[];     endif any(evalFN<48) %Not a .m file 意思是说如果evalFN不是一个M文件  if options(2)==1 %Float GA    estr=['x=pop(i,1); pop(i,xZomeLength)=', evalFN ';'];    else %Binary GA    estr=['x=b2f(pop(i,:),bounds,bits); pop(i,xZomeLength)=', evalFN ';'];   endelse %A .m file 如果是M文件    if options(2)==1 %Float GA 如果options的第二列为1,则采用实数编码        estr=['[ pop(i,:) pop(i,xZomeLength)]=' evalFN '(pop(i,:),[0 evalOps]);']; %实际上就是和适应度函数衔接了。'[ pop(i,:) pop(i,xZomeLength)]=' evalFN '(pop(i,:),[0 evalOps]);'的格式和适应度函数的定义的格式是一摸一样。    else %Binary GA 如果参数结构体的第二列为0,则采用二进制编码        estr=['x=b2f(pop(i,:),bounds,bits);[x v]=' evalFN ...            '(x,[0 evalOps]); pop(i,:)=[f2b(x,bounds,bits) v];'];      endendnumVars     = size(bounds,1); 		%求出变量的个数,size(bb,1)返回的是bb的行(第一维)的数量。size(bb,2)返回的是bb的列(第二维)的数量。[a, b]=size(A)返回A的行和列数。rng         = (bounds(:,2)-bounds(:,1))'; %The variable ranges','代表的是对矩阵进行转置。这句程序的意思是确定变量的范围if options(2)==1 %Float GA 如果浮点编码  xZomeLength = numVars+1; 		%Length of string is numVar + fit 生成的初始个体的长度等于变量的个数加上1(即含有一个适应度)  pop         = zeros(num,xZomeLength); 	%Allocate the new population分配新矩阵,num为初始种群的个数  pop(:,1:numVars)=(ones(num,1)*rng).*(rand(num,numVars))+...    (ones(num,1)*bounds(:,1)');%生成初始种群各变量的初始值else %Binary GA  bits=calcbits(bounds,options(1));  xZomeLength = sum(bits)+1; 		% Length of string is numVar + fit  pop = round(rand(num,sum(bits)+1));endfor i=1:num  eval(estr);%eval将字符串作为表达式执行,此句程序用来计算适应度值,并且完成全部的pop返回。实际上是eval(['[ pop(i,:) pop(i,xZomeLength)]=' evalFN '(pop(i,:),[0 evalOps]);'];),生成最终的pop。pop(i,:),[0 evalOps],这一部分实际上是evalFN的输入参数。end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -