-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTree3.py
237 lines (190 loc) · 7.64 KB
/
Tree3.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
"""
Node class to represent an equation tree for use in Symbolic regression.
Author: Rich Korzelius, Caroline Thompson
"""
import random
from Node import Node
from copy import deepcopy
class Tree3():
def __init__(self, root):
self.root = root
self.fitness = None
def print_expression(self):
temp = deepcopy(self)
return temp.print_helper(temp.root)
def print_helper(self, node):
operations = ['*', '/', '+', '-', '^']
if node:
if type(node) is int:
return str(node)
elif node.element not in operations:
return str(node.element)
else:
node.element = '(' + self.print_helper(node.left)+' '+node.element+' '+self.print_helper(node.right)+')'
if type(node) is int:
return str(node)
else:
return node.element
def evaluate(self, x1, x2, x3):
temp = deepcopy(self)
return temp.evaluate_helper(temp.root, x1, x2, x3)
def evaluate_helper(self, node, x1, x2, x3):
operations = ['*', '/', '+', '-', '^']
if type(node) is int:
return node
elif node.element not in operations:
if node.element == 'x1':
return x1
elif node.element == 'x2':
return x2
elif node.element == 'x3':
return x3
else:
return node.element
else:
if node.left != None and node.right != None:
if node.element == '*':
try:
node.element = self.evaluate_helper(node.left, x1, x2, x3) * self.evaluate_helper(node.right, x1, x2, x3)
return node.element
except AttributeError:
return node.element
except OverflowError:
return 1
elif node.element == '+':
try:
node.element = self.evaluate_helper(node.left, x1, x2, x3) + self.evaluate_helper(node.right, x1, x2, x3)
return node.element
except AttributeError:
return node.element
except OverflowError:
return 1
elif node.element == '-':
try:
node.element = self.evaluate_helper(node.left, x1, x2, x3) - self.evaluate_helper(node.right, x1, x2, x3)
return node.element
except AttributeError:
return node.element
except OverflowError:
return 1
elif node.element == '^':
try:
p = int(abs(self.evaluate_helper(node.right, x1, x2, x3)))
return self.evaluate_helper(node.left, x1, x2, x3) ** p
except OverflowError or AttributeError:
return 1
else:
try:
left = self.evaluate_helper(node.left, x1, x2, x3)
right = self.evaluate_helper(node.right, x1, x2, x3)
if right == 0:
return 1
else:
return left/right
except AttributeError:
return node.element
except OverflowError:
return 1
else:
return node.element
def find_fitness(self, x1_data, x2_data, x3_data, y_data):
error = 0
for i in range(len(x1_data)):
x1 = x1_data[i]
x2 = x2_data[i]
x3 = x3_data[i]
error += abs(y_data[i] - float(self.evaluate(x1, x2, x3)))
self.fitness = error
def mutate(self, node):
operations = ['*', '/', '+', '-', '^']
terminals = ['x1','x2','x3','x1','x2','x3','x1','x2','x3', -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]
n = random.random()
if node:
#print 'node valid'
#if n < mutation_rate:
#print 'mutation started'
if node.element not in terminals:
#print 'Mutating operation'
old = node.element
new = node.element
while (new == old):
new = random.choice(operations)
if new == '^' and (node.right == 'x1' or node.right == 'x2' or node.right == 'x3' or node.right < 0):
while node.right == 'x'or node.right == 'x2' or node.right == 'x3' or node.right < 0:
node.right = random.choice(terminals)
node.element = new
else:
#print 'Mutating terminal'
old = node.element
new = node.element
while (new == old):
new = random.choice(terminals)
node.element = new
def crossover(self, other):
a = random.randint(1,2)
b = random.randint(1,2)
if a == 1:
temp_self = self.root.left
else:
temp_self = self.root.right
if b == 1:
temp_other = other.root.left
else:
temp_other = other.root.right
if a == 1:
self.root.left = temp_other
else:
self.root.right = temp_other
if b == 1:
other.root.left = temp_self
else:
other.root.right = temp_self
def choose_random_node(self, node, current_depth, max_depth):
node_choose_rate = 0.5
n = random.random()
if node:
if type(node) is int:
return
if n < node_choose_rate:
#print 'wow'
#print type(node)
return node
elif current_depth == max_depth:
return node
else:
PARSE_DIRECTION = 0.5
direction = random.random()
if direction < PARSE_DIRECTION:
self.choose_random_node(node.left, current_depth+1, max_depth)
else:
self.choose_random_node(node.right, current_depth + 1, max_depth)
def generate_random_tree(self, node, depth, maxDepth):
operators = ['*', '/', '+', '-','^']
terminals = ['x1','x2','x3','x1','x2','x3','x1','x2','x3', -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]
if depth == maxDepth:
l = random.randint(0, 19)
r = random.randint(0, 19)
node.left = Node(terminals[l])
if 0 <= r <= 8 and node.element == '^':
r = random.randint(9, 19)
node.right = Node(terminals[r])
elif r == 15 and node.element == '/':
while r == 15:
r = random.randint(0,19)
node.right = Node(terminals[r])
else:
node.right = Node(terminals[r])
else:
#if node.element not in terminals:
l = random.randint(0, 4)
r = random.randint(0, 4)
if terminals[l] == '^' and node.element == '^':
while l == operators.index('^'):
l = random.randint(0,4)
if terminals[r] == '^' and node.element == '^':
while r == operators.index('^'):
r = random.randint(0, 4)
node.left = Node(operators[l])
self.generate_random_tree(node.left, depth + 1, maxDepth)
node.right = Node(operators[r])
self.generate_random_tree(node.right, depth + 1, maxDepth)