-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselection.py
53 lines (36 loc) · 1.33 KB
/
selection.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import random
from population import Population, Tour
from typing import List
def select_tournament(population: Population, tournament_size) -> (Tour, Tour):
selected = []
while len(selected) < 2:
tournament = Population()
for _ in range(tournament_size):
random_index = random.randint(0, len(population)-1)
tournament.append_tour(population.get_tour(random_index))
selected_tour = tournament.get_fittest()
# def is_equal(t1, t2):
# for i in range(Graph.num_nodes):
# if t1.path[i] != t2.path[i]:
# return False
# return True
# if len(selected) == 1 and is_equal(selected[0], selected_tour):
# continue
selected.append(selected_tour)
return selected
def select_roulette_sampling(population: Population, num_samples=2, s: int = 1.5) -> List[Tour]:
p = population.get_rank_probability(s)
N = len(population)
# a: cumulative probability
a = [p[0]] + [0] * (N-1)
for i in range(1, N):
a[i] = a[i-1] + p[i]
r = random.uniform(0, 1/num_samples)
i = 0
mating_pool = []
while len(mating_pool) < num_samples:
while a[i] < r:
i += 1
mating_pool.append(population.get_tour(i))
r += 1 / num_samples
return mating_pool