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

📄 generation.m

📁 遗传规划的matlab工具箱
💻 M
字号:
function [pop,state]=generation(pop,params,state,data,varargin)
%GENERATION    Creates a new generation for the GPLAB algorithm.
%   GENERATION(POPULATION,PARAMETERS,STATE,DATA) returns a
%   new population of individuals created by applying the
%   genetic operators to the current population.
%
%   GENERATION(POPULATION,PARAMETERS,STATE,DATA,NEWPOPSIZE)
%   creates a new population with NEWPOPSIZE individuals, which
%   can be different from the size of the current population.
%
%   [POPULATION,STATE]=GENERATION(...) also returns the updated
%   state of the algorithm.
%
%   Input arguments:
%      POPULATION - the current population (array)
%      PARAMETERS - the algorithm running parameters (struct)
%      STATE - the algorithm current state (struct)
%      DATA - the dataset on which the algorithm runs (struct)
%      NEWPOPSIZE - the number of individuals to create (integer)
%   Output arguments:
%      POPULATION - the new population (array)
%      STATE - the algorithm current state, now updated (struct)
%
%   See also GENPOP
%
%   Copyright (C) 2003-2004 Sara Silva (sara@dei.uc.pt)
%   This file is part of the GPLAB Toolbox

if nargin==5
   newpopsize=varargin{1};
else
   newpopsize=length(pop);
end

state.generation=state.generation+1;
if ~strcmp(params.output,'silent')
	fprintf('- Creating generation %d -\n',state.generation);
end

% create a new generation:
      
% first set a temporary empty population, with newpopsize individuals...
tmppop=initpop(newpopsize); % tmppop is an empty population
currentsize=0; % number of non empty individuals in tmppop
      
% ...then apply genetic operators to pop and fill tmppop with the new individuals...
% (and eventually calculate stuff for variable operator probabilities)
parentspool=[];
while currentsize<newpopsize
   % first choose between reproduction and genetic operator:
   rrate=params.reproduction;
   if (rrate>0 & rand<rrate)
      % reproduction
      [tmppop,newsize,parentspool,state]=applyoperator(pop,params,state,data,tmppop,currentsize,0,parentspool);
      % (opnum=0 : reproduction)
   else
      % genetic operator - if there's only one don't bother to randomize
      if length(params.operatornames)==1
          opnum=1;
      else
          opnum=pickoperator(state); % pickoperator gives the index of the operator chosen
      end
      [tmppop,newsize,parentspool,state]=applyoperator(pop,params,state,data,tmppop,currentsize,opnum,parentspool);
   end
   if strcmp(params.operatorprobstype,'variable')
      bestindex=find(state.popranking==1);
      bestindex=bestindex(1); % in case there is more than one in first place
      bestfit=pop(bestindex).fitness;
      worstindex=find(state.popranking==max(state.popranking));
      worstindex=worstindex(1); % the same reason
      worstfit=pop(worstindex).fitness;
      [state,tmppop]=automaticoperatorprobs(tmppop,params,state,data,currentsize,newsize,bestfit,worstfit);
   end   
	currentsize=newsize;
end

% note that tmppop may have more individuals than needed, because the last operator applied
% may have produced more offspring than needed to fill the population. tmppop will be
% subject to applysurvival, where the worst individuals will be discarded. (even if there is
% no elitism, if tmppop has more individuals than needed, the worst will be discarded.)

% ...measure fitness on the tmppop individuals with empty fitness...
[tmppop,state]=calcpopfitness(tmppop,params,data,state);

% ...and finally apply survival to choose individuals from both pop and tmppop,
% creating a definite new population
pop=applysurvival(pop,params,tmppop);

% get fitness list and set state measures (fitness, rank, level history):
state.popfitness=[pop.fitness];
[state,pop]=updatestate(params,state,data,pop);

⌨️ 快捷键说明

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