-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectors.py
More file actions
executable file
·177 lines (137 loc) · 5.87 KB
/
Copy pathSelectors.py
File metadata and controls
executable file
·177 lines (137 loc) · 5.87 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
"""
:mod:`Selectors` -- selection methods module
==============================================================
This module have the *selection methods*, like roulette wheel, tournament, ranking, etc.
"""
import random
import Consts
import operator
def GRankSelector(population, **args):
""" The Rank Selector - This selector will pick the best individual of
the population every time.
"""
count = 0
if args["popID"] != GRankSelector.cachePopID:
if population.sortType == Consts.sortType["scaled"]:
best_fitness = population.bestFitness().fitness
for index in xrange(1, len(population.internalPop)):
if population[index].fitness == best_fitness:
count += 1
else:
best_raw = population.bestRaw().score
for index in xrange(1, len(population.internalPop)):
if population[index].score == best_raw:
count += 1
GRankSelector.cachePopID = args["popID"]
GRankSelector.cacheCount = count
else: count = GRankSelector.cacheCount
return population[random.randint(0, count)]
GRankSelector.cachePopID = None
GRankSelector.cacheCount = None
def GUniformSelector(population, **args):
""" The Uniform Selector """
return population[random.randint(0, len(population)-1)]
def GTournamentSelector(population, **args):
""" The Tournament Selector
It accepts the *tournamentPool* population parameter.
.. note::
the Tournament Selector uses the Roulette Wheel to
pick individuals for the pool
.. versionchanged:: 0.6
Changed the parameter `poolSize` to the `tournamentPool`, now the selector
gets the pool size from the population.
"""
choosen = None
should_minimize = population.minimax == Consts.minimaxType["minimize"]
minimax_operator = min if should_minimize else max
poolSize = population.getParam("tournamentPool", Consts.CDefTournamentPoolSize)
tournament_pool = [GRouletteWheel(population, **args) for i in xrange(poolSize) ]
if population.sortType == Consts.sortType["scaled"]:
choosen = minimax_operator(tournament_pool, key=lambda ind: ind.fitness)
else:
choosen = minimax_operator(tournament_pool, key=lambda ind: ind.score)
return choosen
def GTournamentSelectorAlternative(population, **args):
""" The alternative Tournament Selector
This Tournament Selector don't uses the Roulette Wheel
It accepts the *tournamentPool* population parameter.
.. versionadded: 0.6
Added the GTournamentAlternative function.
"""
pool_size = population.getParam("tournamentPool", Consts.CDefTournamentPoolSize)
len_pop = len(population)
should_minimize = population.minimax == Consts.minimaxType["minimize"]
minimax_operator = min if should_minimize else max
tournament_pool = [population[random.randint(0, len_pop-1)] for i in xrange(pool_size)]
if population.sortType == Consts.sortType["scaled"]:
choosen = minimax_operator(tournament_pool, key=lambda ind: ind.fitness)
else:
choosen = minimax_operator(tournament_pool, key=lambda ind: ind.score)
return choosen
def GRouletteWheel(population, **args):
""" The Roulette Wheel selector """
psum = None
if args["popID"] != GRouletteWheel.cachePopID:
GRouletteWheel.cachePopID = args["popID"]
psum = GRouletteWheel_PrepareWheel(population)
GRouletteWheel.cacheWheel = psum
else:
psum = GRouletteWheel.cacheWheel
cutoff = random.random()
lower = 0
upper = len(population) - 1
while(upper >= lower):
i = lower + ((upper-lower)/2)
if psum[i] > cutoff: upper = i-1
else: lower = i+1
lower = min(len(population)-1, lower)
lower = max(0, lower)
return population.bestFitness(lower)
GRouletteWheel.cachePopID = None
GRouletteWheel.cacheWheel = None
def GRouletteWheel_PrepareWheel(population):
""" A preparation for Roulette Wheel selection """
len_pop = len(population)
psum = [i for i in xrange(len_pop)]
population.statistics()
if population.sortType == Consts.sortType["scaled"]:
pop_fitMax = population.stats["fitMax"]
pop_fitMin = population.stats["fitMin"]
if pop_fitMax == pop_fitMin:
for index in xrange(len_pop):
psum[index] = (index+1) / float(len_pop)
elif (pop_fitMax > 0 and pop_fitMin >= 0) or (pop_fitMax <= 0 and pop_fitMin < 0):
population.sort()
if population.minimax == Consts.minimaxType["maximize"]:
psum[0] = population[0].fitness
for i in xrange(1, len_pop):
psum[i] = population[i].fitness + psum[i-1]
for i in xrange(len_pop):
psum[i] /= float(psum[len_pop - 1])
else:
psum[0] = -population[0].fitness + pop_fitMax + pop_fitMin
for i in xrange(1, len_pop):
psum[i] = -population[i].fitness + pop_fitMax + pop_fitMin + psum[i-1]
for i in xrange(len_pop):
psum[i] /= float(psum[len_pop - 1])
else:
pop_rawMax = population.stats["rawMax"]
pop_rawMin = population.stats["rawMin"]
if pop_rawMax == pop_rawMin:
for index in xrange(len_pop):
psum[index] = (index+1) / float(len_pop)
elif (pop_rawMax > 0 and pop_rawMin >= 0) or (pop_rawMax <= 0 and pop_rawMin < 0):
population.sort()
if population.minimax == Consts.minimaxType["maximize"]:
psum[0] = population[0].score
for i in xrange(1, len_pop):
psum[i] = population[i].score + psum[i-1]
for i in xrange(len_pop):
psum[i] /= float(psum[len_pop-1])
else:
psum[0] = - population[0].score + pop_rawMax + pop_rawMin
for i in xrange(1, len_pop):
psum[i] = - population[i].score + pop_rawMax + pop_rawMin + psum[i-1]
for i in xrange(len_pop):
psum[i] /= float(psum[len_pop-1])
return psum