-
Notifications
You must be signed in to change notification settings - Fork 1
/
routesurvivor_selection.py
71 lines (59 loc) · 2.31 KB
/
routesurvivor_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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
My collection of survivor selection methods
Student number:20146990
Student name:Yifan Zhu
"""
#imports
import random
def mu_plus_lambda(current_pop, current_fitness, offspring, offspring_fitness,mu,lam):
population = []
fitness = []
combined_pop = current_pop + offspring
combined_fitness = current_fitness + offspring_fitness
sorted_pop, sorted_fitness = zip(*sorted(zip(combined_pop, combined_fitness), key=lambda x: x[1]))
sorted_pop = list(sorted_pop)
sorted_fitness = list(sorted_fitness)
for i, (item1, item2) in enumerate(zip(sorted_pop, sorted_fitness)):
if item2 == 9999999999:
sorted_pop = sorted_pop[:i]
sorted_fitness = sorted_fitness[:i]
break
population = list(sorted_pop[:mu])
population.extend(sorted_pop[-lam:])
fitness = list(sorted_fitness[:mu])
fitness.extend(sorted_fitness[-lam:])
return population, fitness
def sus(current_pop, current_fitness, offspring, offspring_fitness):
offspring, offspring_fitness = zip(*sorted(zip(offspring, offspring_fitness), key=lambda x: x[1]))
offspring = list(offspring)
offspring_fitness = list(offspring_fitness)
for i, (item1, item2) in enumerate(zip(offspring, offspring_fitness)):
if item2 == 9999999999:
offspring = offspring[:i]
offspring_fitness = offspring_fitness[:i]
break
#combine the current population and offspring into a single list
k = len(current_pop)
population = current_pop + offspring
fitness = [1/f for f in current_fitness + offspring_fitness]
#calculate the total fitness of the population
total_fitness = sum(fitness)
#calculate the distance between pointers
distance = total_fitness / k
# generate a random starting point
start = random.uniform(0, distance)
cumulative_fitness = fitness[0]
#select the individuals using Stochastic Universal Sampling
selected_pop = []
selected_fitness = []
current_point = start
i = 0
while len(selected_pop) < k:
if cumulative_fitness >= start:
selected_pop.append(population[i])
selected_fitness.append(1/fitness[i])
start += distance
else:
i += 1
cumulative_fitness += fitness[i]
return selected_pop, selected_fitness