-
Notifications
You must be signed in to change notification settings - Fork 4
/
Roulette.py
179 lines (152 loc) · 4.39 KB
/
Roulette.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
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
178
179
'''
ways of roulette wheel selection
'''
import random
import numpy as np
import time
import matplotlib.pyplot as plt
def roulette_basic(fitness, times):
'''
input: a list of fitness, resampling times
output: selected index
O(N^2)
'''
sumFit = sum(fitness)
resam_index = []
for i in range(times):
rand = random.uniform(0, sumFit)
accum = 0.0
for ind, val in enumerate(fitness):
accum += val
if accum >= rand:
resam_index.append(ind)
break
return resam_index
def roulette_basic_2(fitness, times):
'''
input: a list of fitness, resampling times
output: selected index
O(N^2)
'''
accm_fitness = []
accm_for_sum = []
for val in fitness:
accm_for_sum.append(val)
accm_fitness.append(sum(accm_for_sum))
sumFit = sum(fitness)
resam_index = []
for i in range(times):
rand = random.uniform(0, sumFit)
for ind, val in enumerate(accm_fitness):
if val >= rand:
resam_index.append(ind)
break
return resam_index
# ==========================
# time test
N = [1000,1000*2,1000*3,1000*4,1000*5,1000*6,1000*7,1000*8]
algos = [roulette_basic, roulette_basic_2]
times = [[], []]
for n in N:
fitness = np.random.random((n,))
for ind,algo in enumerate(algos):
start = time.time()
algo(fitness,n)
end = time.time()
times[ind].append(end-start)
lineStyle = ['b-o', 'g--p', 'r:*']
algoName = ['roulette_basic', 'roulette_basic_2']
for i in range(len(times)):
plt.plot(N, times[i], lineStyle[i], label=algoName[i])
plt.legend(loc=2)
plt.title('plot of average running time')
plt.xlabel('N')
plt.ylabel('Average Running Time')
plt.grid('on')
plt.show()
'''
# from __future__ import division
import random
from bisect import bisect_left
import numpy as np
import timeit
import matplotlib.pyplot as plt
"""
Basic roulette wheel selection: O(N)
"""
def basic(fitness):
# Input: a list of N fitness values (list or tuple)
# Output: selected index
sumFits = sum(fitness)
# generate a random number
rndPoint = random.uniform(0, sumFits)
# calculate the index: O(N)
accumulator = 0.0
for ind, val in enumerate(fitness):
accumulator += val
if accumulator >= rndPoint:
return ind
"""
Bisecting search roulette wheel selection: O(N + logN)
"""
def bisectSearch(fitness):
# Input: a list of N fitness values (list or tuple)
# Output: selected index
sumFits = sum(fitness)
# generate a random number
rndPoint = random.uniform(0, sumFits)
# calculate the accumulator: O(N)
accumulator = []
accsum = 0.0
for fit in fitness:
accsum += fit
accumulator.append(accsum)
return bisect_left(accumulator, rndPoint) # O(logN)
"""
Stochastic Acceptance: O(1) if given the N and maxFit before
"""
def stochasticAccept(fitness):
# Input: a list of N fitness values (list or tuple)
# Output: selected index
# calculate N and max fitness value
N = len(fitness)
maxFit = max(fitness)
# select: O(1)
while True:
# randomly select an individual with uniform probability
ind = int(N * random.random())
# with probability wi/wmax to accept the selection
if random.random() <= fitness[ind] / maxFit:
return ind
"""
main function
"""
def main():
# init number of fitness values
N = [10, 10**2, 10**3, 10**4, 10**5]
# calculate average total run time for each algorithm
times = [[], [], []]
algos = [basic, bisectSearch, stochasticAccept]
for n in N:
fitness = np.random.random((n,))
for ind, algo in enumerate(algos):
sample_times = []
for _ in range(100):
start = timeit.default_timer()
algo(fitness)
sample_times.append(timeit.default_timer() - start)
times[ind].append(np.array(sample_times).mean())
# plot the result
lineStyle = ['b-o', 'g--p', 'r:*']
algoName = ['basic', 'bisectSearch', 'stochasticAccept']
for i in range(len(times)):
plt.loglog(N, times[i], lineStyle[i], label=algoName[i])
plt.legend(loc=2)
plt.title('log-log plot of average running time')
plt.xlabel('N')
plt.ylabel('Average Running Time')
plt.grid('on')
plt.show()
if __name__ == "__main__":
main()
'''