forked from sumatrae/gatbx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rws.M
33 lines (30 loc) · 1.11 KB
/
rws.M
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
% RWS.m - Roulette Wheel Selection
%
% Syntax:
% NewChrIx = rws(FitnV, Nsel)
%
% This function selects a given number of individuals Nsel from a
% population. FitnV is a column vector containing the fitness
% values of the individuals in the population.
%
% The function returns another column vector containing the
% indexes of the new generation of chromosomes relative to the
% original population matrix, shuffled. The new population, ready
% for mating, can be obtained by calculating
% OldChrom(NewChrIx, :).
%
% Author: Carlos Fonseca, Updated: Andrew Chipperfield
% Date: 04/10/93, Date: 27-Jan-94
%
% Tested under MATLAB v6 by Alex Shenfield (22-Jan-03)
function NewChrIx = rws(FitnV,Nsel);
% Identify the population size (Nind)
[Nind,ans] = size(FitnV);
% Perform Stochastic Sampling with Replacement
cumfit = cumsum(FitnV);
trials = cumfit(Nind) .* rand(Nsel, 1);
Mf = cumfit(:, ones(1, Nsel));
Mt = trials(:, ones(1, Nind))';
[NewChrIx, ans] = find(Mt < Mf & ...
[ zeros(1, Nsel); Mf(1:Nind-1, :) ] <= Mt);
% end of function