forked from brunopereiracosta/finetic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GA.py
247 lines (204 loc) · 7.21 KB
/
GA.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
#DEVELOPED
from gp_edit import *
from MyFuncs import *
import random
import operator
import math
import numpy
from deap import algorithms
from deap import base
from deap import creator
from deap import tools
from deap import gp
epsilon = 1e-20
parallel = 2 #(0-None, 1-SCOOP, 2-Multi)
import pandas as pd
import time
# clock
start = time.time()
# xls = pd.ExcelFile('./SECRET ADMIRER.xlsx')
xls = pd.ExcelFile('./EURUSD.xlsx')
end = time.time()
elapsed = end - start
print(elapsed)
EURUSD = xls.parse(0)
price, years, weekdays = read_sheet(EURUSD)
def pow2(input):
return pow(input,2)
def if_then_else(input, output1, output2):
return output1 if input else output2
def maximum(input):
return max(input[0:random.randint(0, len(input))])
def minimum(input):
return min(input[0:random.randint(0, len(input))])
def get_price(input):
return input[random.randint(0, len(input))]
def window(input):
return random.randint(0, len(input))
#ind SHOULD BE POSTITIVE
#part is exactly like shift for negative numbers, but we'll keep it separate because when introducing weights it might be useful to give shift and part different weights
def shift(arr,ind):
return arr[-(ind+1)]
#ind SHOULD BE POSTITIVE
def part(arr,ind):
return arr[ind]
def protectedDiv(left, right):
try:
return left / right
except ZeroDivisionError:
return left / epsilon
def IF2(arg1,arg2,out1,out2):
if arg1>arg2:
return out1
return out2
def IF(argB,out1,out2):
if argB:
return out1
return out2
def gt(arg1,arg2):
if arg1>arg2:
return True
return False
#window and ind SHOULD BE POSTITIVE
def SMA(arr,window,ind):
if window==0: #protect agains division by 0 (choose arr.s just as a choice...)
window=arr.s
if ind==0:
temp = arr[-window:]
elif window+ind>arr.s:
temp = arr[0:-ind]
else:
temp = arr[-window-ind:-ind]
return sum(temp)/window
class array:
def __init__(self, v):
self.v = v
self.s = len(v)
def __len__(self):
return self.s
def __iter__(self): #in order for sum(array) to work
return self.v.__iter__()
def __repr__(self): #this is only so that print(array) works
return self.v.__repr__()
def protect(self,key): #make it so that key cannot excede [-N,N-1]
if key>=self.s: #protect agains invalid indices
print("WARNING: index over valid size (key={key}; size={size})".format(key=key,size=self.s))
return self.s-1
elif -key>self.s: #protect agains invalid indices
print("WARNING: index under valid size (key={key}; size={size})".format(key=key,size=self.s))
return 0
return key
def __getitem__(self, key):
if isinstance(key, slice): #if key=slice(start,stop,step), protect the start and stop
if key.stop==None or key.stop==sys.maxsize or key.stop==self.s:
stop=self.s
else:
stop=self.protect(key.stop)
out = array(self.v[slice(self.protect(key.start),stop,key.step)])
return out
return self.v[self.protect(key)]
# N=1000
# n=10
# vec=[0]*N
# for i in range(1,N):
# vec[i]=math.sin(i/5.)
# # vec[i]=random.uniform(-1,1)
# # vec[i]=-i
# arr=array(vec)
n=10
errors=array(error(price))
pset = gp.PrimitiveSetTyped("main", [array], float)
pset.addPrimitive(SMA, [array, int, int], float)
pset.addPrimitive(operator.add, [float, float], float)
pset.addPrimitive(part, [array,int], float)
pset.addPrimitive(shift, [array,int], float)
pset.addEphemeralConstant("randI", lambda: random.randint(0,n-1),int)
pset.addEphemeralConstant("randF", lambda: random.uniform(-1,1),float)
pset.addPrimitive(operator.sub, [float, float], float)
pset.addPrimitive(operator.mul, [float, float], float)
pset.addPrimitive(protectedDiv, [float, float], float)
# pset.addPrimitive(operator.pow, [float, float], float)
# pset.addPrimitive(pow2, [float], float)
# pset.addPrimitive(math.sqrt, [float], float)
# pset.addPrimitive(operator.abs, [float], float)
# pset.addPrimitive(math.cos, [float], float)
# pset.addPrimitive(math.sin, [float], float)
# pset.addPrimitive(math.tan, [float], float)
# pset.addPrimitive(math.atan, [float], float)
# pset.addPrimitive(math.log1p, [float], float)
# pset.addPrimitive(math.exp, [float], float)
# pset.addPrimitive(idem, [A], A)
# pset.addPrimitive(idem, [int], int)
pset.addPrimitive(IF2, [float,float,float, float], float)
# pset.addPrimitive(IF, [bool,float, float], float)
# pset.addPrimitive(gt, [float, float], bool)
# pset.renameArguments(ARG0="x")
# pset.renameArguments(ARG1="y")
# pset.addPrimitive(operator.xor, [bool, bool], bool)
# pset.addPrimitive(if_then_else, [bool, float, float], float)
# pset.addTerminal(3.0, float)
# pset.addTerminal(1, int)
creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
creator.create("Individual", gp.PrimitiveTree, fitness=creator.FitnessMin,
pset=pset)
def fitness_predictor(individual,arg,n):
func = toolbox.compile(expr=individual)
fit=0.
for i in range(n,len(arg)):
if ((func(arg[i-n:i])>0)==(arg[i]>0)):
fit += -1
return fit/(len(arg)-n)*100,
#
# def myfit(ind,arg):
# # Transform the tree expression in a callable function
# func = toolbox.compile(expr=ind)
# out = (func(arg)-SMA(arg,4,2))**2
# # out = (func(arg)-arg[4])**2
# for i in range(1,500):
# for j in range(1,500):
# i
# return out,
#
toolbox = base.Toolbox()
toolbox.register("expr", genGrow_edit, pset=pset, min_=1, max_=15)
toolbox.register("individual", tools.initIterate, creator.Individual,toolbox.expr)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("compile", gp.compile, pset=pset)
toolbox.register("expr_mut", genGrow_edit, min_=0, max_=5)
toolbox.register("mutate", gp.mutUniform, expr=toolbox.expr_mut, pset=pset)
toolbox.decorate("mutate", gp.staticLimit(key=operator.attrgetter("height"), max_value=17))
stats_fit = tools.Statistics(lambda ind: ind.fitness.values)
stats_size = tools.Statistics(len)
mstats = tools.MultiStatistics(fitness=stats_fit, size=stats_size)
mstats.register("avg", numpy.mean)
mstats.register("std", numpy.std)
mstats.register("min", numpy.min)
mstats.register("max", numpy.max)
if parallel==1:
from scoop import futures
toolbox.register("map", futures.map) #PARALLELIZATION
elif parallel==2:
import multiprocessing
pool = multiprocessing.Pool()
toolbox.register("map", pool.map) #PARALLELIZATION
def run(cxpb=0.5,mutpb=0.1,n=10,tour=3,termpb=0.2,pop=10,ngen=10):
toolbox.register("evaluate", fitness_predictor, arg=errors, n=n)
toolbox.register("select", tools.selTournament, tournsize=tour)
toolbox.register("mate", gp.cxOnePointLeafBiased,termpb=termpb)
toolbox.decorate("mate", gp.staticLimit(key=operator.attrgetter("height"), max_value=17))
pop = toolbox.population(n=pop)
hof = tools.HallOfFame(1)
#hof_aux.append(hof)
pop, log = algorithms.eaSimple(pop, toolbox, cxpb, mutpb, ngen, stats=mstats, halloffame=hof, verbose=True)
#print(arr,SMA(arr,4,2))
#print(hof[0])
#print(type(hof[0]))
#print(hof_aux.append(hof[0]))
return fitness_predictor(hof[0],errors,n)[0]
def average_fitness(N):
return sum([run(cxpb=0.5,mutpb=0.1,n=10,tour=3) for i in range(0,N)])/N
def main():
repeat = 3
print(average_fitness(repeat))
if __name__ == '__main__':
main()