Spaces:
Sleeping
Sleeping
AutonLabTruth
commited on
Commit
•
32df36d
1
Parent(s):
a9184d1
Refactored till Node
Browse files- julia/Node.jl +142 -0
- julia/sr.jl +2 -155
- julia/utils.jl +11 -0
julia/Node.jl
ADDED
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Define a serialization format for the symbolic equations:
|
2 |
+
mutable struct Node
|
3 |
+
#Holds operators, variables, constants in a tree
|
4 |
+
degree::Integer #0 for constant/variable, 1 for cos/sin, 2 for +/* etc.
|
5 |
+
val::Union{Float32, Integer} #Either const value, or enumerates variable
|
6 |
+
constant::Bool #false if variable
|
7 |
+
op::Integer #enumerates operator (separately for degree=1,2)
|
8 |
+
l::Union{Node, Nothing}
|
9 |
+
r::Union{Node, Nothing}
|
10 |
+
|
11 |
+
Node(val::Float32) = new(0, val, true, 1, nothing, nothing)
|
12 |
+
Node(val::Integer) = new(0, val, false, 1, nothing, nothing)
|
13 |
+
Node(op::Integer, l::Node) = new(1, 0.0f0, false, op, l, nothing)
|
14 |
+
Node(op::Integer, l::Union{Float32, Integer}) = new(1, 0.0f0, false, op, Node(l), nothing)
|
15 |
+
Node(op::Integer, l::Node, r::Node) = new(2, 0.0f0, false, op, l, r)
|
16 |
+
|
17 |
+
#Allow to pass the leaf value without additional node call:
|
18 |
+
Node(op::Integer, l::Union{Float32, Integer}, r::Node) = new(2, 0.0f0, false, op, Node(l), r)
|
19 |
+
Node(op::Integer, l::Node, r::Union{Float32, Integer}) = new(2, 0.0f0, false, op, l, Node(r))
|
20 |
+
Node(op::Integer, l::Union{Float32, Integer}, r::Union{Float32, Integer}) = new(2, 0.0f0, false, op, Node(l), Node(r))
|
21 |
+
end
|
22 |
+
|
23 |
+
# Copy an equation (faster than deepcopy)
|
24 |
+
function copyNode(tree::Node)::Node
|
25 |
+
if tree.degree == 0
|
26 |
+
return Node(tree.val)
|
27 |
+
elseif tree.degree == 1
|
28 |
+
return Node(tree.op, copyNode(tree.l))
|
29 |
+
else
|
30 |
+
return Node(tree.op, copyNode(tree.l), copyNode(tree.r))
|
31 |
+
end
|
32 |
+
end
|
33 |
+
|
34 |
+
# Count the operators, constants, variables in an equation
|
35 |
+
function countNodes(tree::Node)::Integer
|
36 |
+
if tree.degree == 0
|
37 |
+
return 1
|
38 |
+
elseif tree.degree == 1
|
39 |
+
return 1 + countNodes(tree.l)
|
40 |
+
else
|
41 |
+
return 1 + countNodes(tree.l) + countNodes(tree.r)
|
42 |
+
end
|
43 |
+
end
|
44 |
+
|
45 |
+
# Count the max depth of a tree
|
46 |
+
function countDepth(tree::Node)::Integer
|
47 |
+
if tree.degree == 0
|
48 |
+
return 1
|
49 |
+
elseif tree.degree == 1
|
50 |
+
return 1 + countDepth(tree.l)
|
51 |
+
else
|
52 |
+
return 1 + max(countDepth(tree.l), countDepth(tree.r))
|
53 |
+
end
|
54 |
+
end
|
55 |
+
|
56 |
+
# Convert an equation to a string
|
57 |
+
function stringTree(tree::Node)::String
|
58 |
+
if tree.degree == 0
|
59 |
+
if tree.constant
|
60 |
+
return string(tree.val)
|
61 |
+
else
|
62 |
+
if useVarMap
|
63 |
+
return varMap[tree.val]
|
64 |
+
else
|
65 |
+
return "x$(tree.val - 1)"
|
66 |
+
end
|
67 |
+
end
|
68 |
+
elseif tree.degree == 1
|
69 |
+
return "$(unaops[tree.op])($(stringTree(tree.l)))"
|
70 |
+
else
|
71 |
+
return "$(binops[tree.op])($(stringTree(tree.l)), $(stringTree(tree.r)))"
|
72 |
+
end
|
73 |
+
end
|
74 |
+
|
75 |
+
# Print an equation
|
76 |
+
function printTree(tree::Node)
|
77 |
+
println(stringTree(tree))
|
78 |
+
end
|
79 |
+
|
80 |
+
# Return a random node from the tree
|
81 |
+
function randomNode(tree::Node)::Node
|
82 |
+
if tree.degree == 0
|
83 |
+
return tree
|
84 |
+
end
|
85 |
+
a = countNodes(tree)
|
86 |
+
b = 0
|
87 |
+
c = 0
|
88 |
+
if tree.degree >= 1
|
89 |
+
b = countNodes(tree.l)
|
90 |
+
end
|
91 |
+
if tree.degree == 2
|
92 |
+
c = countNodes(tree.r)
|
93 |
+
end
|
94 |
+
|
95 |
+
i = rand(1:1+b+c)
|
96 |
+
if i <= b
|
97 |
+
return randomNode(tree.l)
|
98 |
+
elseif i == b + 1
|
99 |
+
return tree
|
100 |
+
end
|
101 |
+
|
102 |
+
return randomNode(tree.r)
|
103 |
+
end
|
104 |
+
|
105 |
+
# Count the number of unary operators in the equation
|
106 |
+
function countUnaryOperators(tree::Node)::Integer
|
107 |
+
if tree.degree == 0
|
108 |
+
return 0
|
109 |
+
elseif tree.degree == 1
|
110 |
+
return 1 + countUnaryOperators(tree.l)
|
111 |
+
else
|
112 |
+
return 0 + countUnaryOperators(tree.l) + countUnaryOperators(tree.r)
|
113 |
+
end
|
114 |
+
end
|
115 |
+
|
116 |
+
# Count the number of binary operators in the equation
|
117 |
+
function countBinaryOperators(tree::Node)::Integer
|
118 |
+
if tree.degree == 0
|
119 |
+
return 0
|
120 |
+
elseif tree.degree == 1
|
121 |
+
return 0 + countBinaryOperators(tree.l)
|
122 |
+
else
|
123 |
+
return 1 + countBinaryOperators(tree.l) + countBinaryOperators(tree.r)
|
124 |
+
end
|
125 |
+
end
|
126 |
+
|
127 |
+
# Count the number of operators in the equation
|
128 |
+
function countOperators(tree::Node)::Integer
|
129 |
+
return countUnaryOperators(tree) + countBinaryOperators(tree)
|
130 |
+
end
|
131 |
+
|
132 |
+
|
133 |
+
# Count the number of constants in an equation
|
134 |
+
function countConstants(tree::Node)::Integer
|
135 |
+
if tree.degree == 0
|
136 |
+
return convert(Integer, tree.constant)
|
137 |
+
elseif tree.degree == 1
|
138 |
+
return 0 + countConstants(tree.l)
|
139 |
+
else
|
140 |
+
return 0 + countConstants(tree.l) + countConstants(tree.r)
|
141 |
+
end
|
142 |
+
end
|
julia/sr.jl
CHANGED
@@ -15,151 +15,9 @@ else
|
|
15 |
const baselineMSE = MSE(y, convert(Array{Float32, 1}, ones(len) .* avgy))
|
16 |
end
|
17 |
|
|
|
18 |
|
19 |
-
|
20 |
-
x
|
21 |
-
end
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
function debug(verbosity, string...)
|
26 |
-
verbosity > 0 ? println(string...) : nothing
|
27 |
-
end
|
28 |
-
|
29 |
-
function getTime()::Integer
|
30 |
-
return round(Integer, 1e3*(time()-1.6e9))
|
31 |
-
end
|
32 |
-
|
33 |
-
# Define a serialization format for the symbolic equations:
|
34 |
-
mutable struct Node
|
35 |
-
#Holds operators, variables, constants in a tree
|
36 |
-
degree::Integer #0 for constant/variable, 1 for cos/sin, 2 for +/* etc.
|
37 |
-
val::Union{Float32, Integer} #Either const value, or enumerates variable
|
38 |
-
constant::Bool #false if variable
|
39 |
-
op::Integer #enumerates operator (separately for degree=1,2)
|
40 |
-
l::Union{Node, Nothing}
|
41 |
-
r::Union{Node, Nothing}
|
42 |
-
|
43 |
-
Node(val::Float32) = new(0, val, true, 1, nothing, nothing)
|
44 |
-
Node(val::Integer) = new(0, val, false, 1, nothing, nothing)
|
45 |
-
Node(op::Integer, l::Node) = new(1, 0.0f0, false, op, l, nothing)
|
46 |
-
Node(op::Integer, l::Union{Float32, Integer}) = new(1, 0.0f0, false, op, Node(l), nothing)
|
47 |
-
Node(op::Integer, l::Node, r::Node) = new(2, 0.0f0, false, op, l, r)
|
48 |
-
|
49 |
-
#Allow to pass the leaf value without additional node call:
|
50 |
-
Node(op::Integer, l::Union{Float32, Integer}, r::Node) = new(2, 0.0f0, false, op, Node(l), r)
|
51 |
-
Node(op::Integer, l::Node, r::Union{Float32, Integer}) = new(2, 0.0f0, false, op, l, Node(r))
|
52 |
-
Node(op::Integer, l::Union{Float32, Integer}, r::Union{Float32, Integer}) = new(2, 0.0f0, false, op, Node(l), Node(r))
|
53 |
-
end
|
54 |
-
|
55 |
-
# Copy an equation (faster than deepcopy)
|
56 |
-
function copyNode(tree::Node)::Node
|
57 |
-
if tree.degree == 0
|
58 |
-
return Node(tree.val)
|
59 |
-
elseif tree.degree == 1
|
60 |
-
return Node(tree.op, copyNode(tree.l))
|
61 |
-
else
|
62 |
-
return Node(tree.op, copyNode(tree.l), copyNode(tree.r))
|
63 |
-
end
|
64 |
-
end
|
65 |
-
|
66 |
-
# Count the operators, constants, variables in an equation
|
67 |
-
function countNodes(tree::Node)::Integer
|
68 |
-
if tree.degree == 0
|
69 |
-
return 1
|
70 |
-
elseif tree.degree == 1
|
71 |
-
return 1 + countNodes(tree.l)
|
72 |
-
else
|
73 |
-
return 1 + countNodes(tree.l) + countNodes(tree.r)
|
74 |
-
end
|
75 |
-
end
|
76 |
-
|
77 |
-
# Count the max depth of a tree
|
78 |
-
function countDepth(tree::Node)::Integer
|
79 |
-
if tree.degree == 0
|
80 |
-
return 1
|
81 |
-
elseif tree.degree == 1
|
82 |
-
return 1 + countDepth(tree.l)
|
83 |
-
else
|
84 |
-
return 1 + max(countDepth(tree.l), countDepth(tree.r))
|
85 |
-
end
|
86 |
-
end
|
87 |
-
|
88 |
-
# Convert an equation to a string
|
89 |
-
function stringTree(tree::Node)::String
|
90 |
-
if tree.degree == 0
|
91 |
-
if tree.constant
|
92 |
-
return string(tree.val)
|
93 |
-
else
|
94 |
-
if useVarMap
|
95 |
-
return varMap[tree.val]
|
96 |
-
else
|
97 |
-
return "x$(tree.val - 1)"
|
98 |
-
end
|
99 |
-
end
|
100 |
-
elseif tree.degree == 1
|
101 |
-
return "$(unaops[tree.op])($(stringTree(tree.l)))"
|
102 |
-
else
|
103 |
-
return "$(binops[tree.op])($(stringTree(tree.l)), $(stringTree(tree.r)))"
|
104 |
-
end
|
105 |
-
end
|
106 |
-
|
107 |
-
# Print an equation
|
108 |
-
function printTree(tree::Node)
|
109 |
-
println(stringTree(tree))
|
110 |
-
end
|
111 |
-
|
112 |
-
# Return a random node from the tree
|
113 |
-
function randomNode(tree::Node)::Node
|
114 |
-
if tree.degree == 0
|
115 |
-
return tree
|
116 |
-
end
|
117 |
-
a = countNodes(tree)
|
118 |
-
b = 0
|
119 |
-
c = 0
|
120 |
-
if tree.degree >= 1
|
121 |
-
b = countNodes(tree.l)
|
122 |
-
end
|
123 |
-
if tree.degree == 2
|
124 |
-
c = countNodes(tree.r)
|
125 |
-
end
|
126 |
-
|
127 |
-
i = rand(1:1+b+c)
|
128 |
-
if i <= b
|
129 |
-
return randomNode(tree.l)
|
130 |
-
elseif i == b + 1
|
131 |
-
return tree
|
132 |
-
end
|
133 |
-
|
134 |
-
return randomNode(tree.r)
|
135 |
-
end
|
136 |
-
|
137 |
-
# Count the number of unary operators in the equation
|
138 |
-
function countUnaryOperators(tree::Node)::Integer
|
139 |
-
if tree.degree == 0
|
140 |
-
return 0
|
141 |
-
elseif tree.degree == 1
|
142 |
-
return 1 + countUnaryOperators(tree.l)
|
143 |
-
else
|
144 |
-
return 0 + countUnaryOperators(tree.l) + countUnaryOperators(tree.r)
|
145 |
-
end
|
146 |
-
end
|
147 |
-
|
148 |
-
# Count the number of binary operators in the equation
|
149 |
-
function countBinaryOperators(tree::Node)::Integer
|
150 |
-
if tree.degree == 0
|
151 |
-
return 0
|
152 |
-
elseif tree.degree == 1
|
153 |
-
return 0 + countBinaryOperators(tree.l)
|
154 |
-
else
|
155 |
-
return 1 + countBinaryOperators(tree.l) + countBinaryOperators(tree.r)
|
156 |
-
end
|
157 |
-
end
|
158 |
-
|
159 |
-
# Count the number of operators in the equation
|
160 |
-
function countOperators(tree::Node)::Integer
|
161 |
-
return countUnaryOperators(tree) + countBinaryOperators(tree)
|
162 |
-
end
|
163 |
|
164 |
# Randomly convert an operator into another one (binary->binary;
|
165 |
# unary->unary)
|
@@ -179,17 +37,6 @@ function mutateOperator(tree::Node)::Node
|
|
179 |
return tree
|
180 |
end
|
181 |
|
182 |
-
# Count the number of constants in an equation
|
183 |
-
function countConstants(tree::Node)::Integer
|
184 |
-
if tree.degree == 0
|
185 |
-
return convert(Integer, tree.constant)
|
186 |
-
elseif tree.degree == 1
|
187 |
-
return 0 + countConstants(tree.l)
|
188 |
-
else
|
189 |
-
return 0 + countConstants(tree.l) + countConstants(tree.r)
|
190 |
-
end
|
191 |
-
end
|
192 |
-
|
193 |
# Randomly perturb a constant
|
194 |
function mutateConstant(
|
195 |
tree::Node, T::Float32,
|
|
|
15 |
const baselineMSE = MSE(y, convert(Array{Float32, 1}, ones(len) .* avgy))
|
16 |
end
|
17 |
|
18 |
+
include("utils.jl")
|
19 |
|
20 |
+
include("Node.jl")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
# Randomly convert an operator into another one (binary->binary;
|
23 |
# unary->unary)
|
|
|
37 |
return tree
|
38 |
end
|
39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
# Randomly perturb a constant
|
41 |
function mutateConstant(
|
42 |
tree::Node, T::Float32,
|
julia/utils.jl
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
function id(x::Float32)::Float32
|
2 |
+
x
|
3 |
+
end
|
4 |
+
|
5 |
+
function debug(verbosity, string...)
|
6 |
+
verbosity > 0 ? println(string...) : nothing
|
7 |
+
end
|
8 |
+
|
9 |
+
function getTime()::Integer
|
10 |
+
return round(Integer, 1e3*(time()-1.6e9))
|
11 |
+
end
|