select_roulette.m
来自「人工免疫算法基于遗传MATLAB代码很有用哦」· M 代码 · 共 35 行
M
35 行
function[newPop] = select_Roulette(oldPop)
% roulette is the traditional selection function with the probability of
% surviving equal to the fitness of i / sum of the fitness of all individuals
%
% function[newPop] = roulette(oldPop,options)
% newPop - the new population selected from the oldPop
% oldPop - the current population
% options - options [gen]
% Binary and Real-Valued Simulation Evolution for Matlab
% Copyright (C) 1996 C.R. Houck, J.A. Joines, M.G. Kay
%
% Get the parameters of the population
numVars = size(oldPop,2); % 列
numSols = size(oldPop,1); % 行
% Generate the relative probabilities of selection
totalFit = sum(oldPop(:,numVars));
prob=oldPop(:,numVars) / totalFit;
prob=cumsum(prob);
rNums=sort(rand(numSols,1)); % Generate random numbers
% Select individuals from the oldPop to the new
fitIn=1;newIn=1;
while newIn<=numSols
if(rNums(newIn)<prob(fitIn))
newPop(newIn,:) = oldPop(fitIn,:);
newIn = newIn+1;
else
fitIn = fitIn + 1;
end
end
end
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?