All problems
Tournament Selection
Problem
Select an individual by tournament selection.
After random.seed(seed), draw k contender indices with random.randrange(len(fitness)), then return the contender with the largest fitness. When contenders tie, the one drawn earliest wins.
Hint
Sample contenders with replacement; only fitness decides the winner.
Test cases
case 1
in: [[1,5,3],3,0]
expected: 1
case 2
in: [[2,2,2],2,42]
expected: 2
case 3
in: [[0.1,0.9,0.5,0.7],4,7]
expected: 1
Starter codePython
def tournament_selection(fitness, k, seed):
# Your code here
pass3 test cases · run your code against them in the editor