-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPolyGA.py
330 lines (262 loc) · 9.58 KB
/
PolyGA.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import threading
import queue
from copy import deepcopy
from random import shuffle, randint
from math import ceil
import numpy as np
from matplotlib import pyplot as plt
from PolyExpression import PolyExpression
from time import time
import os
POPULATION_SIZE = 500
MUTATION_RATE = 40
NUMBER_OF_GENERATIONS = 6000
SELECTION_RATE = 25
MAX_COEFFICIENT = 100
MAX_EXPONENT = 5
MIN_LENGTH = 1
MAX_LENGTH = 6
MAX_MUTATION_SIZE = 100
NUMBER_OF_CHI_THREADS = 64
NUMBER_OF_REPRODUCTION_THREADS = 4
NUMBER_OF_MUTATION_THREADS = 16
DATASET_PATH = "SCPUnion_mu_vs_z.txt"
x = []
y = []
s = []
# save file stuff
time_str = str(time())
os.mkdir(time_str)
file_f = open(time_str + "/output.txt", "w")
number_of_gens = []
best_chi_squared = []
# thread stuff
exitFlag = False
chiSquaredQueue = queue.Queue()
chiSquaredLock = threading.Lock()
chiSquaredThreads = []
reproductionQueue = queue.Queue()
reproductionLock = threading.Lock()
reproductionThreads = []
processed = []
new_generation = []
# thread classes
class ChiSquaredThread(threading.Thread):
def __init__(self, threadID, q):
threading.Thread.__init__(self)
self.q = q
self.threadID = threadID
def run(self):
print("Starting chi thread " + str(self.threadID) + "...")
process_genes_chi(self.q)
print("Finished chi thread " + str(self.threadID))
class ReproductionThread(threading.Thread):
def __init__(self, threadID, q):
threading.Thread.__init__(self)
self.q = q
self.threadID = threadID
def run(self):
print("Starting reproduction thread " + str(self.threadID) + "...")
process_reproduction(self.q)
print("Finished reproduction thread " + str(self.threadID))
# thread functions
def process_genes_chi(q):
while not exitFlag:
chiSquaredLock.acquire()
if not chiSquaredQueue.empty():
data = q.get()
chiSquaredLock.release()
data.set_chi_squared(fitness_function(data))
processed.append(deepcopy(data))
else:
chiSquaredLock.release()
def process_reproduction(q):
while not exitFlag:
reproductionLock.acquire()
if not reproductionQueue.empty():
parent_a = q.get()
parent_b = q.get()
reproductionLock.release()
child_a, child_b = crossover(parent_a, parent_b)
new_generation.append(deepcopy(child_a))
new_generation.append(deepcopy(child_b))
new_generation.append(deepcopy(parent_a))
new_generation.append(deepcopy(parent_b))
else:
reproductionLock.release()
# ga functions
def read_dataset():
f = open(DATASET_PATH)
i = 0
for line in f:
if i < 4:
# skip lines before data starts
i += 1
else:
line = line.strip()
line_split = line.split("\t")
x.append(float(line_split[1]))
y.append(float(line_split[2]))
s.append(float(line_split[3]))
def plot_scatter_graph(func: PolyExpression, index):
plt.plot(x, y, 'o')
plt.errorbar(x, y, yerr=s, fmt=' ')
func_x = np.linspace(0, max(x), 100)
func_y = []
for i in func_x:
func_y.append(func.evaluate(i))
z = np.linspace(min(x), max(x), 100)
mu = np.log((z**2.17145) * ((-z ** 2.82) + z + np.exp(z))) + 42.83 - 5. * np.log10(0.7)
plt.plot(z, mu, c='g')
plt.plot(func_x, func_y, c='r')
plt.xlim(0, max(x))
plt.xlabel("Redshift")
plt.ylabel("Distance modulus")
plt.title("Genetic Algorithm Rank:" + str(index))
plt.savefig(time_str + "/" + str(index) + ".png")
plt.show()
def plot_generation_graph():
plt.plot(number_of_gens, best_chi_squared, label='best', color='blue')
plt.xlabel("Generation Number")
plt.ylabel("$\\chi^2$ value")
plt.xticks(np.arange(1, NUMBER_OF_GENERATIONS + 1, step=500))
# plt.ylim([0, 10000])
plt.legend()
plt.savefig(time_str + "/generations_chi.png")
plt.show()
def fitness_function(func: PolyExpression):
chi_2 = 0
for index in range(0, len(x)):
chi_2 += pow(((y[index] - func.evaluate(x[index])) / s[index]), 2)
return chi_2
def create_expression():
return PolyExpression(max_length=MAX_LENGTH, min_length=MIN_LENGTH, max_coefficient=MAX_COEFFICIENT,
max_exponent=MAX_EXPONENT, mutation_rate=MUTATION_RATE, max_mutation_size=MAX_MUTATION_SIZE)
def create_population():
population = []
for i in range(POPULATION_SIZE):
population.append(deepcopy(create_expression()))
return population
def evaluate_population(population):
chiSquaredLock.acquire()
for i in range(POPULATION_SIZE):
chiSquaredQueue.put(deepcopy(population[i]))
chiSquaredLock.release()
while not chiSquaredQueue.empty():
pass
return deepcopy(processed)
def prune_population(population):
while len(population) - 1 >= POPULATION_SIZE:
population.remove(population[len(population) - 1])
return population
def crossover(parent_a: PolyExpression, parent_b: PolyExpression):
len_a = len(parent_a.terms)
len_b = len(parent_b.terms)
terms_a = []
terms_b = []
if len_a == len_b:
crossover_point = randint(0, ceil(len_a / 2))
for i in range(len_a):
if i <= crossover_point:
terms_a.append(parent_a.terms[i])
terms_b.append(parent_b.terms[i])
else:
terms_a.append(parent_b.terms[i])
terms_b.append(parent_a.terms[i])
else:
if len_a < len_b:
for i in range(len_b):
if i < len_a:
terms_a.append(parent_a.terms[i])
terms_b.append(parent_b.terms[i])
else:
terms_a.append(parent_b.terms[i])
else:
for i in range(len_a):
if i < len_b:
terms_a.append(parent_a.terms[i])
terms_b.append(parent_b.terms[i])
else:
terms_b.append(parent_a.terms[i])
child_1 = deepcopy(create_expression())
child_1.set_terms(terms_a)
child_2 = deepcopy(create_expression())
child_2.set_terms(terms_b)
return child_1, child_2
def simulate_generation(population, generation_number):
global new_generation
global processed
start_time = time()
processed = []
new_generation = []
processed_population = deepcopy(evaluate_population(population))
processed_population.sort(key=lambda l: l.get_chi_squared(), reverse=False)
processed_population = deepcopy(prune_population(processed_population))
selection_amount = ceil((len(processed_population) / 100) * SELECTION_RATE)
parents = processed_population[:selection_amount]
for i in range(selection_amount):
index = randint(selection_amount, len(processed_population) - 1)
parents.append(processed_population[index])
processed_population.remove(processed_population[index])
shuffle(parents) # shuffle to increase diversity in reproduction
reproductionLock.acquire()
for p in parents:
reproductionQueue.put(deepcopy(p))
reproductionLock.release()
while not reproductionQueue.empty():
pass
next_generation = []
for i in new_generation:
next_generation.append(deepcopy(i))
# todo can be combined into the above loop
for p in next_generation:
mutation = deepcopy(p)
mutated = mutation.mutate()
if mutated:
next_generation.append(deepcopy(mutation))
end_time = time()
time_taken = end_time - start_time
population_summary(processed_population, generation_number, time_taken)
return next_generation
def population_summary(population, generation_number, time_taken):
if len(population) < 10:
r = len(population)
else:
r = 10
best_chi_squared.append(population[0].get_chi_squared())
print("----------------------------")
file_f.write("----------------------------" + "\n")
print("GENERATION: " + str(generation_number))
file_f.write("GENERATION: " + str(generation_number) + "\n")
print("Best Chi Squared: " + str(population[0].get_chi_squared()))
file_f.write("Best Chi Squared: " + str(population[0].get_chi_squared()) + "\n")
print("Worst Chi Squared: " + str(population[len(population) - 1].get_chi_squared()))
file_f.write("Worst Chi Squared: " + str(population[len(population) - 1].get_chi_squared()) + "\n")
print("Time Taken: " + str(time_taken))
file_f.write("Time Taken: " + str(time_taken) + "\n")
print("Best 10 of generation:")
file_f.write("Best 10 of generation:" + "\n")
for i in range(1, (r + 1)):
print(str(i) + ": " + population[i].get_equation('x') + " | " + str(population[i].get_chi_squared()))
file_f.write(str(i) + ": " + population[i].get_equation('x') + " | " + str(population[i].get_chi_squared()) +
"\n")
def start():
global file_f
for i in range(NUMBER_OF_CHI_THREADS):
thread = ChiSquaredThread(i, chiSquaredQueue)
thread.start()
chiSquaredThreads.append(thread)
for i in range(NUMBER_OF_REPRODUCTION_THREADS):
thread = ReproductionThread(i, reproductionQueue)
thread.start()
reproductionThreads.append(thread)
read_dataset()
population = deepcopy(create_population())
for i in range(1, NUMBER_OF_GENERATIONS + 1):
population = deepcopy(simulate_generation(population, i))
number_of_gens.append(i)
for i in range(1, 11):
plot_scatter_graph(population[i], i)
plot_generation_graph()
file_f.close()
start()