tournamentselection.m
来自「由我收集或写出的GA源码」· M 代码 · 共 41 行
M
41 行
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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 + =
减小字号Ctrl + -
显示快捷键?