-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparent_selection_methods.py
More file actions
26 lines (19 loc) · 898 Bytes
/
Copy pathparent_selection_methods.py
File metadata and controls
26 lines (19 loc) · 898 Bytes
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
import numpy as np
def tournament_selection(population, fit_pop, k):
"""
This function preforms a tournament selection of the population, the inputs
are the population, the fitness of the population and the tournamnet size.
The function return the winner of the tournamanet.
"""
# pick random index of population and set current winner to this indes
max_idx = len(fit_pop)
parent_idx = np.random.randint(0, max_idx)
# preform k direct comparissons to random members of population, if one has
# a higher fitness than the current member, update the current winner index.
for i in range(k):
rnd_idx = np.random.randint(0, max_idx)
if fit_pop[rnd_idx] > fit_pop[parent_idx]:
parent_idx = rnd_idx
# return the parent according to the winning index
parent = population[parent_idx][:]
return parent