-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tree.py
182 lines (159 loc) · 5.94 KB
/
Tree.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
import tensorflow as tf
import random as rand
import copy
import statistics
from Utils import *
#from GP import training_x, training_y, test_x, test_y
def set_data(training_x, training_y, test_x, test_y):
global cols, ncols, labels, nlabels, tcols, tncols, tlabels, tnlabels
cols = training_x
labels = training_y
tcols = test_x
tlabels = test_y
ncols = len(cols)
nlabels = len(labels.numpy())
tncols = len(tcols)
tnlabels = len(tlabels.numpy())
#------------------------------------
biFunctions = ['+', '-', '*', '//']
uniFunctions = ['ln', 'sqrt']
maxDepth = 2
def getCol(x, t):
if isinstance(x, float): # se for literal
if not t:
return [x for i in range(len(cols[0].numpy()))]
else:
return [x for i in range(len(tcols[0].numpy()))]
else:
if not t:
return cols[x]
else:
return tcols[x]
class Tree(object):
def __init__(self):
self.size = 0
self.globalvalue = 0
self.fit = 0
self.root = None
class Node(object):
def __init__(self):
self.value = None
self.left = None
self.right = None
# checks if self is leaf
def is_leaf(self):
return self.left is None
# TODO pruning the tree
def prune(self):
pass
# creates a tree using the full method
def full(self, depth):
if depth == maxDepth:
# > 3 to remove literal chances
if rand.random() > 3:
self.value = rand.random()
else:
self.value = rand.randint(0, ncols - 1)
else:
self.value = rand.choice(biFunctions)
self.left = Node().full(depth+1)
self.right = Node().full(depth+1)
return self
# creates a tree using the grow method
def grow(self, depth):
if depth == maxDepth:
if rand.random() > 3: #garante que não ah terminais
self.value = rand.random()
else:
self.value = rand.randint(0, ncols - 1)
else:
if self.left is None:
if rand.randint(0, 1) == 0:
if rand.random() > 3: # same thing
self.value = rand.random()
else:
self.value = rand.randint(0, ncols - 1)
else:
if rand.randint(0,1) == 0:
self.value = rand.choice(uniFunctions)
self.left = Node().grow(depth + 1)
else:
self.value = rand.choice(biFunctions)
self.left = Node().full(depth + 1)
self.right = Node().full(depth + 1)
else:
if rand.randint(0, 1) == 0:
self.value = rand.choice(uniFunctions)
self.left = Node().grow(depth + 1)
else:
self.value = rand.choice(biFunctions)
self.left = Node().full(depth + 1)
self.right = Node().full(depth + 1)
return self
# gets the number of nodes on the tree
def number_of_nodes(self):
return 1 + \
(self.left.number_of_nodes() if self.left is not None else 0) + \
(self.right.number_of_nodes() if self.right is not None else 0)
# returns a node fromt he tree
def get_node(self, i):
if i == 0:
return self
ls = (self.left.number_of_nodes() if self.left is not None else 0)
if i - 1 < ls:
if self.left is not None:
return self.left.get_node(i - 1)
else:
if self.right is not None:
return self.right.get_node(i - ls - 1)
# mutates a node by growing it
def mutate(self, i):
if i == 0:
self.grow(0)
return
ls = (self.left.number_of_nodes() if self.left is not None else 0)
if i - 1 < ls:
if self.left is not None:
self.left.mutate(i - 1)
else:
if self.right is not None:
self.right.mutate(i - ls - 1)
# prints the tree
def print_tree(self):
print(self.value)
(self.left.print_tree() if self.left is not None else 0)
(self.right.print_tree() if self.right is not None else 0)
# calculates the tree
def calculate(self, result, test):
if self.is_leaf():
result = getCol(self.value, test)
else:
if self.value == '+':
result = self.left.calculate(result, test) + self.right.calculate(result, test)
elif self.value == '-':
result = self.left.calculate(result, test) - self.right.calculate(result, test)
elif self.value == '*':
result = self.left.calculate(result, test) * self.right.calculate(result, test)
elif self.value == '//':
result = divide(self.left.calculate(result, test), self.right.calculate(result, test))
elif self.value == 'ln':
result = ln(self.left.calculate(result, test))
else:
result = sqrt(self.left.calculate(result, test))
return result
# calculates the fitness of the tree
def fitness(self, result):
return tf.sqrt(tf.losses.mean_squared_error(labels,result)).numpy()
# calculates the training accuracy
def accuracy(self, results):
results = binary_round(results)
mistaken = tf.math.count_nonzero(labels - results).numpy()
return (1-(mistaken / nlabels)) * 100
#return tf.contrib.eager.metrics.Accuracy(labels, results).result()
# calculates the test accuracy
def test_accuracy(self, results):
# mete 1 e 0
results = binary_round(results)
# subtrai e compara os nao zero, numero de errados
mistaken = tf.math.count_nonzero(tlabels - results).numpy()
return (1-(mistaken / tnlabels)) * 100