gusucode.com > matlab编程遗传算法计算匹配电路源码程序 > code1/code/MATLAB源代码/matlab_match/sus.m

    % SUS.M          (Stochastic Universal Sampling)
%
% This function performs selection with STOCHASTIC UNIVERSAL SAMPLING.
%
% Syntax:  NewChrIx = sus(FitnV, Nsel)
%
% Input parameters:
%    FitnV     - Column vector containing the fitness values of the
%                individuals in the population.
%    Nsel      - number of individuals to be selected
%
% Output parameters:
%    NewChrIx  - column vector containing the indexes of the selected
%                individuals relative to the original population, shuffled.
%                The new population, ready for mating, can be obtained
%                by calculating OldChrom(NewChrIx,:).
%
% Author:     Hartmut Pohlheim (Carlos Fonseca)
% History:    12.12.93     file created
%             22.02.94     clean up, comments
%             22.01.03     tested under MATLAB v6 by Alex Shenfield

function NewChrIx = sus(FitnV,Nsel);

% Identify the population size (Nind)
   [Nind,ans] = size(FitnV);

% Perform stochastic universal sampling
   cumfit = cumsum(FitnV);          %因为FitnV为一个列向量,所以返回一个列向量cumfit,该向量的第n行是FitnV第1行到第n行的所有元素的累加和
   trials = cumfit(Nind) / Nsel * (rand + (0:Nsel-1)');     %trials为Nsel行的列向量,(0:Nsel-1)'为一个n行的列向量,数值从0到Nsel-1依次加1,cumfit(Nind)为FitnV中所有元素的总和,Nsel * (rand + (0:Nsel-1)')表示(0:Nsel-1)'中所有元素加上同一个随机数再乘以cumfit(Nind)/Nsel
   Mf = cumfit(:, ones(1, Nsel));                %将cumfit复制Nsel列
   Mt = trials(:, ones(1, Nind))';               %将trials复制Nsel列,再转置
   [NewChrIx, ans] = find(Mt < Mf & [ zeros(1, Nsel); Mf(1:Nind-1, :) ] <= Mt);

% Shuffle new population
   [ans, shuf] = sort(rand(Nsel, 1));
   NewChrIx = NewChrIx(shuf);


% End of function