📄 tournamentselection.m
字号:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% File: tournamentSelection.m
%
% Description: Perform tournament selection without replacement and return
% the matingpool (index of winners of the tournament)
%
% @param tournSize is the tournament size (note that population size should
% be a multiplicant of the tournament size)
%
% @return winners is an array with the index of the individuals that won
% the tournaments.
%
% Author: Kumara Sastry
%
% Date: March 2007
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function winners = tournamentSelection(tournSize, fitness)
n = length(fitness);
winners = [];
% Make sure that the population size is divisible by the tournament size
if(mod(n,tournSize) > 0)
error('Population size has to be divisible by the tournament size\n');
end
% Repeat the process "tournSize" times
for i = 1:tournSize
%Create a random set of competitors
shuffleOrder = randperm(n);
competitors = reshape(shuffleOrder, tournSize, n/tournSize)';
%The winner is the competitor with best fitness
[winFit, winID] = max(fitness(competitors),[],2);
idMap = (0:tournSize-1)*n/tournSize;
idMap1 = idMap(winID) + (1:size(competitors,1));
winners = [winners; competitors(idMap1)];
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -