Spaces:
Running
Running
AutonLabTruth
commited on
Commit
•
f50d9d6
1
Parent(s):
a1e832b
Refactored Till PopMember
Browse files- julia/PopMember.jl +10 -0
- julia/complexityChecks.jl +42 -0
- julia/simplification.jl +106 -0
- julia/sr.jl +3 -158
julia/PopMember.jl
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Define a member of population by equation, score, and age
|
2 |
+
mutable struct PopMember
|
3 |
+
tree::Node
|
4 |
+
score::Float32
|
5 |
+
birth::Integer
|
6 |
+
|
7 |
+
PopMember(t::Node) = new(t, scoreFunc(t), getTime())
|
8 |
+
PopMember(t::Node, score::Float32) = new(t, score, getTime())
|
9 |
+
|
10 |
+
end
|
julia/complexityChecks.jl
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Check if any binary operator are overly complex
|
2 |
+
function flagBinOperatorComplexity(tree::Node, op::Int)::Bool
|
3 |
+
if tree.degree == 0
|
4 |
+
return false
|
5 |
+
elseif tree.degree == 1
|
6 |
+
return flagBinOperatorComplexity(tree.l, op)
|
7 |
+
else
|
8 |
+
if tree.op == op
|
9 |
+
overly_complex = (
|
10 |
+
((bin_constraints[op][1] > -1) &&
|
11 |
+
(countNodes(tree.l) > bin_constraints[op][1]))
|
12 |
+
||
|
13 |
+
((bin_constraints[op][2] > -1) &&
|
14 |
+
(countNodes(tree.r) > bin_constraints[op][2]))
|
15 |
+
)
|
16 |
+
if overly_complex
|
17 |
+
return true
|
18 |
+
end
|
19 |
+
end
|
20 |
+
return (flagBinOperatorComplexity(tree.l, op) || flagBinOperatorComplexity(tree.r, op))
|
21 |
+
end
|
22 |
+
end
|
23 |
+
|
24 |
+
# Check if any unary operators are overly complex
|
25 |
+
function flagUnaOperatorComplexity(tree::Node, op::Int)::Bool
|
26 |
+
if tree.degree == 0
|
27 |
+
return false
|
28 |
+
elseif tree.degree == 1
|
29 |
+
if tree.op == op
|
30 |
+
overly_complex = (
|
31 |
+
(una_constraints[op] > -1) &&
|
32 |
+
(countNodes(tree.l) > una_constraints[op])
|
33 |
+
)
|
34 |
+
if overly_complex
|
35 |
+
return true
|
36 |
+
end
|
37 |
+
end
|
38 |
+
return flagUnaOperatorComplexity(tree.l, op)
|
39 |
+
else
|
40 |
+
return (flagUnaOperatorComplexity(tree.l, op) || flagUnaOperatorComplexity(tree.r, op))
|
41 |
+
end
|
42 |
+
end
|
julia/simplification.jl
ADDED
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Simplify tree
|
2 |
+
function combineOperators(tree::Node)::Node
|
3 |
+
# NOTE: (const (+*-) const) already accounted for. Call simplifyTree before.
|
4 |
+
# ((const + var) + const) => (const + var)
|
5 |
+
# ((const * var) * const) => (const * var)
|
6 |
+
# ((const - var) - const) => (const - var)
|
7 |
+
# (want to add anything commutative!)
|
8 |
+
# TODO - need to combine plus/sub if they are both there.
|
9 |
+
if tree.degree == 0
|
10 |
+
return tree
|
11 |
+
elseif tree.degree == 1
|
12 |
+
tree.l = combineOperators(tree.l)
|
13 |
+
elseif tree.degree == 2
|
14 |
+
tree.l = combineOperators(tree.l)
|
15 |
+
tree.r = combineOperators(tree.r)
|
16 |
+
end
|
17 |
+
|
18 |
+
top_level_constant = tree.degree == 2 && (tree.l.constant || tree.r.constant)
|
19 |
+
if tree.degree == 2 && (binops[tree.op] === mult || binops[tree.op] === plus) && top_level_constant
|
20 |
+
op = tree.op
|
21 |
+
# Put the constant in r. Need to assume var in left for simplification assumption.
|
22 |
+
if tree.l.constant
|
23 |
+
tmp = tree.r
|
24 |
+
tree.r = tree.l
|
25 |
+
tree.l = tmp
|
26 |
+
end
|
27 |
+
topconstant = tree.r.val
|
28 |
+
# Simplify down first
|
29 |
+
below = tree.l
|
30 |
+
if below.degree == 2 && below.op == op
|
31 |
+
if below.l.constant
|
32 |
+
tree = below
|
33 |
+
tree.l.val = binops[op](tree.l.val, topconstant)
|
34 |
+
elseif below.r.constant
|
35 |
+
tree = below
|
36 |
+
tree.r.val = binops[op](tree.r.val, topconstant)
|
37 |
+
end
|
38 |
+
end
|
39 |
+
end
|
40 |
+
|
41 |
+
if tree.degree == 2 && binops[tree.op] === sub && top_level_constant
|
42 |
+
# Currently just simplifies subtraction. (can't assume both plus and sub are operators)
|
43 |
+
# Not commutative, so use different op.
|
44 |
+
if tree.l.constant
|
45 |
+
if tree.r.degree == 2 && binops[tree.r.op] === sub
|
46 |
+
if tree.r.l.constant
|
47 |
+
#(const - (const - var)) => (var - const)
|
48 |
+
l = tree.l
|
49 |
+
r = tree.r
|
50 |
+
simplified_const = -(l.val - r.l.val) #neg(sub(l.val, r.l.val))
|
51 |
+
tree.l = tree.r.r
|
52 |
+
tree.r = l
|
53 |
+
tree.r.val = simplified_const
|
54 |
+
elseif tree.r.r.constant
|
55 |
+
#(const - (var - const)) => (const - var)
|
56 |
+
l = tree.l
|
57 |
+
r = tree.r
|
58 |
+
simplified_const = l.val + r.r.val #plus(l.val, r.r.val)
|
59 |
+
tree.r = tree.r.l
|
60 |
+
tree.l.val = simplified_const
|
61 |
+
end
|
62 |
+
end
|
63 |
+
else #tree.r.constant is true
|
64 |
+
if tree.l.degree == 2 && binops[tree.l.op] === sub
|
65 |
+
if tree.l.l.constant
|
66 |
+
#((const - var) - const) => (const - var)
|
67 |
+
l = tree.l
|
68 |
+
r = tree.r
|
69 |
+
simplified_const = l.l.val - r.val#sub(l.l.val, r.val)
|
70 |
+
tree.r = tree.l.r
|
71 |
+
tree.l = r
|
72 |
+
tree.l.val = simplified_const
|
73 |
+
elseif tree.l.r.constant
|
74 |
+
#((var - const) - const) => (var - const)
|
75 |
+
l = tree.l
|
76 |
+
r = tree.r
|
77 |
+
simplified_const = r.val + l.r.val #plus(r.val, l.r.val)
|
78 |
+
tree.l = tree.l.l
|
79 |
+
tree.r.val = simplified_const
|
80 |
+
end
|
81 |
+
end
|
82 |
+
end
|
83 |
+
end
|
84 |
+
return tree
|
85 |
+
end
|
86 |
+
|
87 |
+
# Simplify tree
|
88 |
+
function simplifyTree(tree::Node)::Node
|
89 |
+
if tree.degree == 1
|
90 |
+
tree.l = simplifyTree(tree.l)
|
91 |
+
if tree.l.degree == 0 && tree.l.constant
|
92 |
+
return Node(unaops[tree.op](tree.l.val))
|
93 |
+
end
|
94 |
+
elseif tree.degree == 2
|
95 |
+
tree.l = simplifyTree(tree.l)
|
96 |
+
tree.r = simplifyTree(tree.r)
|
97 |
+
constantsBelow = (
|
98 |
+
tree.l.degree == 0 && tree.l.constant &&
|
99 |
+
tree.r.degree == 0 && tree.r.constant
|
100 |
+
)
|
101 |
+
if constantsBelow
|
102 |
+
return Node(binops[tree.op](tree.l.val, tree.r.val))
|
103 |
+
end
|
104 |
+
end
|
105 |
+
return tree
|
106 |
+
end
|
julia/sr.jl
CHANGED
@@ -23,167 +23,12 @@ include("eval.jl")
|
|
23 |
|
24 |
include("randomMutations.jl")
|
25 |
|
|
|
26 |
|
27 |
-
|
28 |
-
function combineOperators(tree::Node)::Node
|
29 |
-
# NOTE: (const (+*-) const) already accounted for. Call simplifyTree before.
|
30 |
-
# ((const + var) + const) => (const + var)
|
31 |
-
# ((const * var) * const) => (const * var)
|
32 |
-
# ((const - var) - const) => (const - var)
|
33 |
-
# (want to add anything commutative!)
|
34 |
-
# TODO - need to combine plus/sub if they are both there.
|
35 |
-
if tree.degree == 0
|
36 |
-
return tree
|
37 |
-
elseif tree.degree == 1
|
38 |
-
tree.l = combineOperators(tree.l)
|
39 |
-
elseif tree.degree == 2
|
40 |
-
tree.l = combineOperators(tree.l)
|
41 |
-
tree.r = combineOperators(tree.r)
|
42 |
-
end
|
43 |
|
44 |
-
|
45 |
-
if tree.degree == 2 && (binops[tree.op] === mult || binops[tree.op] === plus) && top_level_constant
|
46 |
-
op = tree.op
|
47 |
-
# Put the constant in r. Need to assume var in left for simplification assumption.
|
48 |
-
if tree.l.constant
|
49 |
-
tmp = tree.r
|
50 |
-
tree.r = tree.l
|
51 |
-
tree.l = tmp
|
52 |
-
end
|
53 |
-
topconstant = tree.r.val
|
54 |
-
# Simplify down first
|
55 |
-
below = tree.l
|
56 |
-
if below.degree == 2 && below.op == op
|
57 |
-
if below.l.constant
|
58 |
-
tree = below
|
59 |
-
tree.l.val = binops[op](tree.l.val, topconstant)
|
60 |
-
elseif below.r.constant
|
61 |
-
tree = below
|
62 |
-
tree.r.val = binops[op](tree.r.val, topconstant)
|
63 |
-
end
|
64 |
-
end
|
65 |
-
end
|
66 |
|
67 |
-
if tree.degree == 2 && binops[tree.op] === sub && top_level_constant
|
68 |
-
# Currently just simplifies subtraction. (can't assume both plus and sub are operators)
|
69 |
-
# Not commutative, so use different op.
|
70 |
-
if tree.l.constant
|
71 |
-
if tree.r.degree == 2 && binops[tree.r.op] === sub
|
72 |
-
if tree.r.l.constant
|
73 |
-
#(const - (const - var)) => (var - const)
|
74 |
-
l = tree.l
|
75 |
-
r = tree.r
|
76 |
-
simplified_const = -(l.val - r.l.val) #neg(sub(l.val, r.l.val))
|
77 |
-
tree.l = tree.r.r
|
78 |
-
tree.r = l
|
79 |
-
tree.r.val = simplified_const
|
80 |
-
elseif tree.r.r.constant
|
81 |
-
#(const - (var - const)) => (const - var)
|
82 |
-
l = tree.l
|
83 |
-
r = tree.r
|
84 |
-
simplified_const = l.val + r.r.val #plus(l.val, r.r.val)
|
85 |
-
tree.r = tree.r.l
|
86 |
-
tree.l.val = simplified_const
|
87 |
-
end
|
88 |
-
end
|
89 |
-
else #tree.r.constant is true
|
90 |
-
if tree.l.degree == 2 && binops[tree.l.op] === sub
|
91 |
-
if tree.l.l.constant
|
92 |
-
#((const - var) - const) => (const - var)
|
93 |
-
l = tree.l
|
94 |
-
r = tree.r
|
95 |
-
simplified_const = l.l.val - r.val#sub(l.l.val, r.val)
|
96 |
-
tree.r = tree.l.r
|
97 |
-
tree.l = r
|
98 |
-
tree.l.val = simplified_const
|
99 |
-
elseif tree.l.r.constant
|
100 |
-
#((var - const) - const) => (var - const)
|
101 |
-
l = tree.l
|
102 |
-
r = tree.r
|
103 |
-
simplified_const = r.val + l.r.val #plus(r.val, l.r.val)
|
104 |
-
tree.l = tree.l.l
|
105 |
-
tree.r.val = simplified_const
|
106 |
-
end
|
107 |
-
end
|
108 |
-
end
|
109 |
-
end
|
110 |
-
return tree
|
111 |
-
end
|
112 |
-
|
113 |
-
# Simplify tree
|
114 |
-
function simplifyTree(tree::Node)::Node
|
115 |
-
if tree.degree == 1
|
116 |
-
tree.l = simplifyTree(tree.l)
|
117 |
-
if tree.l.degree == 0 && tree.l.constant
|
118 |
-
return Node(unaops[tree.op](tree.l.val))
|
119 |
-
end
|
120 |
-
elseif tree.degree == 2
|
121 |
-
tree.l = simplifyTree(tree.l)
|
122 |
-
tree.r = simplifyTree(tree.r)
|
123 |
-
constantsBelow = (
|
124 |
-
tree.l.degree == 0 && tree.l.constant &&
|
125 |
-
tree.r.degree == 0 && tree.r.constant
|
126 |
-
)
|
127 |
-
if constantsBelow
|
128 |
-
return Node(binops[tree.op](tree.l.val, tree.r.val))
|
129 |
-
end
|
130 |
-
end
|
131 |
-
return tree
|
132 |
-
end
|
133 |
-
|
134 |
-
# Define a member of population by equation, score, and age
|
135 |
-
mutable struct PopMember
|
136 |
-
tree::Node
|
137 |
-
score::Float32
|
138 |
-
birth::Integer
|
139 |
-
|
140 |
-
PopMember(t::Node) = new(t, scoreFunc(t), getTime())
|
141 |
-
PopMember(t::Node, score::Float32) = new(t, score, getTime())
|
142 |
-
|
143 |
-
end
|
144 |
-
|
145 |
-
# Check if any binary operator are overly complex
|
146 |
-
function flagBinOperatorComplexity(tree::Node, op::Int)::Bool
|
147 |
-
if tree.degree == 0
|
148 |
-
return false
|
149 |
-
elseif tree.degree == 1
|
150 |
-
return flagBinOperatorComplexity(tree.l, op)
|
151 |
-
else
|
152 |
-
if tree.op == op
|
153 |
-
overly_complex = (
|
154 |
-
((bin_constraints[op][1] > -1) &&
|
155 |
-
(countNodes(tree.l) > bin_constraints[op][1]))
|
156 |
-
||
|
157 |
-
((bin_constraints[op][2] > -1) &&
|
158 |
-
(countNodes(tree.r) > bin_constraints[op][2]))
|
159 |
-
)
|
160 |
-
if overly_complex
|
161 |
-
return true
|
162 |
-
end
|
163 |
-
end
|
164 |
-
return (flagBinOperatorComplexity(tree.l, op) || flagBinOperatorComplexity(tree.r, op))
|
165 |
-
end
|
166 |
-
end
|
167 |
-
|
168 |
-
# Check if any unary operators are overly complex
|
169 |
-
function flagUnaOperatorComplexity(tree::Node, op::Int)::Bool
|
170 |
-
if tree.degree == 0
|
171 |
-
return false
|
172 |
-
elseif tree.degree == 1
|
173 |
-
if tree.op == op
|
174 |
-
overly_complex = (
|
175 |
-
(una_constraints[op] > -1) &&
|
176 |
-
(countNodes(tree.l) > una_constraints[op])
|
177 |
-
)
|
178 |
-
if overly_complex
|
179 |
-
return true
|
180 |
-
end
|
181 |
-
end
|
182 |
-
return flagUnaOperatorComplexity(tree.l, op)
|
183 |
-
else
|
184 |
-
return (flagUnaOperatorComplexity(tree.l, op) || flagUnaOperatorComplexity(tree.r, op))
|
185 |
-
end
|
186 |
-
end
|
187 |
|
188 |
# Go through one simulated annealing mutation cycle
|
189 |
# exp(-delta/T) defines probability of accepting a change
|
|
|
23 |
|
24 |
include("randomMutations.jl")
|
25 |
|
26 |
+
include("simplification.jl")
|
27 |
|
28 |
+
include("PopMember.jl")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
+
include("complexityChecks.jl")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
# Go through one simulated annealing mutation cycle
|
34 |
# exp(-delta/T) defines probability of accepting a change
|