-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiProcess.py
More file actions
386 lines (332 loc) · 12.4 KB
/
multiProcess.py
File metadata and controls
386 lines (332 loc) · 12.4 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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import random
import numpy
import math
import pickle
import time
from multiprocessing import Pool
import os
import glob
start_time = time.time()
class Wallet:
def __init__(self, fees_rate, money, btc):
self.score = 0
self.fees_rate = fees_rate
self.money = money
self.btc = btc
self.last_action = "SELL"
def update_score(self, price):
self.score = self.money
self.score = self.score + (self.btc * price)
return self.score
def make_action(self, action, price, i):
if action == "BUY":
# print(action)
self.money = self.money - (self.fees_rate / 100) * self.money
self.btc = self.btc + self.money / price
self.money = self.money - self.money
self.last_action = "BUY"
elif action == "SELL":
# print(action)
self.money = self.money - (self.fees_rate / 100) * self.money
self.money = self.money + self.btc * price
self.btc = self.btc - self.btc
self.last_action = "SELL"
elif action == "HOLD":
# print(action)
self.btc = self.btc
self.money = self.money
self.last_action = "HOLD"
else:
print("\t\t\tERROR BAD ACTION!!!!")
# print("My btc: " + str(self.btc))
# print("My money: " + str(self.money))
# print(" ")
def update_wallet_i(wallet, price):
res = wallet.update_score(price)
return res
class Population:
def __init__(self, nb_population, layers, fees_rate, money, btc):
self.layers = layers
self.list_individual = []
self.list_wallet = []
self.initial_fees_rate = fees_rate
self.initial_money = money
self.initial_btc = btc
for i in range(nb_population):
self.list_individual.append(init_NN(self.layers))
self.list_wallet.append(
Wallet(self.initial_fees_rate, self.initial_money, self.initial_btc)
)
def update_all_scores(self, price):
for wallet in self.list_wallet:
wallet.update_score(price)
def print_scores(self):
i = 0
for wallet in self.list_wallet:
print("Wallet " + str(i) + " = " + str(wallet.score))
i = i + 1
def print_avg_score(self, epoch):
avg = 0
for wallet in self.list_wallet:
avg = avg + wallet.score
print(
"Average score for this generation is :" + str(avg / len(self.list_wallet))
)
with open('avg.csv','a') as fd:
fd.write(str(epoch) + "," + str(avg / len(self.list_wallet)) + '\n')
def reset_all_scores(self, money):
for wallet in self.list_wallet:
wallet.score = 0
wallet.money = money
wallet.btc = 0
def select_best_individual(self, limit):
if limit % 2 != 0:
limit = limit + 1
tmp_ind = self.list_individual.copy()
tmp_wallet = self.list_wallet.copy()
best_individuals = []
for i in range(limit):
max_score = 0
index = 0
for j in range(len(tmp_wallet)):
if max_score < tmp_wallet[j].score:
index = j
max_score = tmp_wallet[index].score
best_individuals.append(tmp_ind[index])
tmp_ind.pop(index)
tmp_wallet.pop(index)
return best_individuals
def create_new_from_old_gen(self, best_individuals, len_best):
new_individuals = []
for i in range(0, len_best, 2):
father = best_individuals[i]
mother = best_individuals[i + 1]
W1, W2 = crossover(self.layers, father, mother)
new_individuals.append(W1)
new_individuals.append(W2)
return new_individuals
def create_next_generation(self, best_individuals):
len_best = len(best_individuals)
len_old = len(self.list_individual) - len_best
self.list_individual.clear()
self.list_wallet.clear()
self.list_individual.append(best_individuals[0]) # Keep best of best
self.list_individual = self.list_individual + self.create_new_from_old_gen(
best_individuals, len_best
)
for i in range(len_old - 1): # fill with new random ones
self.list_individual.append(init_NN(self.layers))
for i in range(len_best + len_old): # reset wallets
self.list_wallet.append(
Wallet(self.initial_fees_rate, self.initial_money, self.initial_btc)
)
best_individuals.clear()
def save_individuals(self):
for i in range(len(self.list_individual)):
l = str(self.layers)
filename = "saves/l_" + l
filename = filename + "_nb_" + str(i) + ".dat"
filename = filename.replace(", ", "_")
filename = filename.replace("[", "")
filename = filename.replace("]", "")
f = open(filename,'wb')
pickle.dump(self.list_individual[i], f)
f.close()
def load_individuals(self):
for i in range(len(self.list_individual)):
l = str(self.layers)
filename = "saves/l_" + l
filename = filename + "_nb_" + str(i) + ".dat"
filename = filename.replace(", ", "_")
filename = filename.replace("[", "")
filename = filename.replace("]", "")
f = open(filename,'rb')
example_dict = pickle.load(f)
self.list_individual[i] = example_dict
f.close()
def mutate_all(self, freq, rate):
for i in range(1, len(self.list_individual)): # mutate best
mutate( self.layers, self.list_individual[i], freq, rate)
def edit_wallet(self, btc, money, last_action, i):
self.list_wallet[i].btc = btc
self.list_wallet[i].money = money
self.list_wallet[i].last_action = last_action
def predict(population, layers, price, X, i):
if population.list_wallet[i].last_action == "SELL":
X.append(-1)
elif population.list_wallet[i].last_action == "BUY":
X.append(0.7)
elif population.list_wallet[i].last_action == "HOLD":
X.append(0.4)
else:
X.append("This should make me crash")
predictions = get_all_predictions(layers, population.list_individual[i], X)
action = get_next_action(predictions)
population.list_wallet[i].make_action(action, price, i)
return action
def get_all_predictions(layers, W, Xinput):
Xinput.append(1) # Bias
X = []
for l in range(len(layers)):
if l == 0:
X.append([])
pos = 0
for i in range(len(Xinput)):
X[l].append([])
X[l][i] = Xinput[pos]
pos = pos + 1
else:
X.append([])
for i in range(layers[l] + 1):
X[l].append([])
if i == 0:
X[l][i] = 1
X[l][i] = 0
for l in range(1, len(layers)):
for j in range(1, layers[l] + 1):
res = 0.0
for i in range(layers[l - 1] + 1):
res = res + W[l][j][i] * X[l - 1][i]
X[l][j] = math.tanh(res)
return X[len(layers) - 1]
def init_NN(layers):
W = []
for l in range(1, len(layers)):
if l == 1:
W.append([])
W.append([])
for j in range(1, layers[l] + 1):
if j == 1:
W[l].append([])
W[l].append([])
for i in range(layers[l - 1] + 1):
W[l][j].append([])
W[l][j][i] = random.uniform(-1, 1)
return W
def get_next_action(predictions):
predictions.pop(0)
action = predictions.index(max(predictions))
if action == 0:
return "BUY"
if action == 1:
return "HOLD"
if action == 2:
return "SELL"
def crossover_w(father_w, mother_w):
return random.uniform(father_w + 0.5, mother_w - 0.5)
def crossover(layers, father, mother):
rng = random.uniform(0, 100)
rate = random.uniform(0, 100)
W1 = []
W2 = []
for l in range(1, len(layers)):
if l == 1:
W1.append([])
W2.append([])
W1.append([])
W2.append([])
for j in range(1, layers[l] + 1):
if j == 1:
W1[l].append([])
W2[l].append([])
W1[l].append([])
W2[l].append([])
for i in range(layers[l - 1] + 1):
W1[l][j].append([])
W2[l][j].append([])
if rng >= rate:
W1[l][j][i] = father[l][j][i]
W2[l][j][i] = mother[l][j][i]
else:
W1[l][j][i] = mother[l][j][i]
W2[l][j][i] = father[l][j][i]
return W1, W2
def mutate(layers, W, freq, rate):
for l in range(1, len(layers)):
for j in range(1, layers[l] + 1):
for i in range(layers[l - 1] + 1):
rng = random.uniform(0, 100) / 100
if (rng <= freq):
sign = random.uniform(-1, 1)
if sign >= 0:
W[l][j][i] = W[l][j][i] + (W[l][j][i] * rate)
else:
W[l][j][i] = W[l][j][i] - (W[l][j][i] * rate)
def get_X(line, nb_neur_first_layer):
X = line.split(",")
price = float(X[-1])
X = X[1:nb_neur_first_layer] # rm timestamp
X = [float(i) for i in X]
return X, price
def get_all_line_csv(filename):
f = open(filename, "r")
lines = f.readlines()[1:]
f.close()
return lines
def predict_individual(population, i, filename):
for line in get_all_line_csv(filename):
X, price = get_X(line, population.layers[0])
predict(population, population.layers, price, X, i)
#print(population.list_wallet[i].money)
return update_wallet_i(population.list_wallet[i], price)
def predict_individual_log(population, i, filename):
for line in get_all_line_csv(filename):
X, price = get_X(line, population.layers[0])
action = predict(population, population.layers, price, X, i)
with open("saves/log_actions_" + str(i) + ".csv",'a') as fd:
fd.write(str(price) + "," + str(action) + '\n')
print("Wallet " + str(i) + " " + str(population.list_wallet[i].score))
if __name__ == "__main__":
#filename = "coinbaseUSD_1min_clean.csv"
#filename = "coinbaseUSD_1M.csv"
filename = "output.csv"
train_mode = True
layers = [4, 5, 3]
epochs = 5000
starting_balance = 1
keep_best = 15
nb_population = 20
btc = 0
fees_rate = 0.25
mutate_rate = 0.45
mutation_mutiplier = 0.30
fileList = glob.glob('saves/log_actions_*.csv')
layers[0] = layers[0] + 1 # bias DO NOT EDIT
for filePath in fileList:
os.remove(filePath)
population = Population(nb_population, layers, fees_rate, starting_balance, btc)
if train_mode:
for epoch in range(epochs):
population.reset_all_scores(starting_balance)
p = Pool()
params = []
for i in range(len(population.list_individual)):
params.append((population, i, filename))
result = p.starmap(predict_individual, params)
p.close()
p.join()
for i in range(len(population.list_individual)):
population.list_wallet[i].score = result[i]
population.print_scores()
population.print_avg_score(epoch)
population.save_individuals()
best_individuals = population.select_best_individual(keep_best)
if epoch == epochs / 2 or epoch == epochs / 4:
print("Changing Mutation rates")
mutate_rate = mutate_rate / 2
mutation_mutiplier = mutation_mutiplier / 2
if epoch < epochs - 1:
population.create_next_generation(best_individuals)
population.mutate_all(mutate_rate, mutation_mutiplier) # 10% chance of mutate neuron de 3%
print("--- " + str(time.time() - start_time) + " seconds --- epoch: " + str(epoch))
else:
population.load_individuals()
# log actions of the best indivudual
p = Pool()
params = []
for i in range(len(population.list_individual)):
params.append((population, i, filename))
result = p.starmap(predict_individual_log, params)
p.close()
p.join()
#population.print_scores()