Spaces:
Sleeping
Sleeping
Commit
·
921343f
1
Parent(s):
21ee78d
Moved things around. Seems like it still works as well
Browse files- julia/Node.jl +31 -0
- julia/halloffame.jl +8 -0
- julia/sr.jl +4 -63
- julia/utils.jl +21 -0
julia/Node.jl
CHANGED
|
@@ -139,4 +139,35 @@ function countConstants(tree::Node)::Integer
|
|
| 139 |
else
|
| 140 |
return 0 + countConstants(tree.l) + countConstants(tree.r)
|
| 141 |
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
end
|
|
|
|
| 139 |
else
|
| 140 |
return 0 + countConstants(tree.l) + countConstants(tree.r)
|
| 141 |
end
|
| 142 |
+
end
|
| 143 |
+
|
| 144 |
+
# Get all the constants from a tree
|
| 145 |
+
function getConstants(tree::Node)::Array{Float32, 1}
|
| 146 |
+
if tree.degree == 0
|
| 147 |
+
if tree.constant
|
| 148 |
+
return [tree.val]
|
| 149 |
+
else
|
| 150 |
+
return Float32[]
|
| 151 |
+
end
|
| 152 |
+
elseif tree.degree == 1
|
| 153 |
+
return getConstants(tree.l)
|
| 154 |
+
else
|
| 155 |
+
both = [getConstants(tree.l), getConstants(tree.r)]
|
| 156 |
+
return [constant for subtree in both for constant in subtree]
|
| 157 |
+
end
|
| 158 |
+
end
|
| 159 |
+
|
| 160 |
+
# Set all the constants inside a tree
|
| 161 |
+
function setConstants(tree::Node, constants::Array{Float32, 1})
|
| 162 |
+
if tree.degree == 0
|
| 163 |
+
if tree.constant
|
| 164 |
+
tree.val = constants[1]
|
| 165 |
+
end
|
| 166 |
+
elseif tree.degree == 1
|
| 167 |
+
setConstants(tree.l, constants)
|
| 168 |
+
else
|
| 169 |
+
numberLeft = countConstants(tree.l)
|
| 170 |
+
setConstants(tree.l, constants)
|
| 171 |
+
setConstants(tree.r, constants[numberLeft+1:end])
|
| 172 |
+
end
|
| 173 |
end
|
julia/halloffame.jl
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# List of the best members seen all time
|
| 2 |
+
mutable struct HallOfFame
|
| 3 |
+
members::Array{PopMember, 1}
|
| 4 |
+
exists::Array{Bool, 1} #Whether it has been set
|
| 5 |
+
|
| 6 |
+
# Arranged by complexity - store one at each.
|
| 7 |
+
HallOfFame() = new([PopMember(Node(1f0), 1f9) for i=1:actualMaxsize], [false for i=1:actualMaxsize])
|
| 8 |
+
end
|
julia/sr.jl
CHANGED
|
@@ -27,6 +27,10 @@ include("simplification.jl")
|
|
| 27 |
|
| 28 |
include("PopMember.jl")
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
include("complexityChecks.jl")
|
| 31 |
|
| 32 |
|
|
@@ -235,37 +239,6 @@ function run(
|
|
| 235 |
return pop
|
| 236 |
end
|
| 237 |
|
| 238 |
-
# Get all the constants from a tree
|
| 239 |
-
function getConstants(tree::Node)::Array{Float32, 1}
|
| 240 |
-
if tree.degree == 0
|
| 241 |
-
if tree.constant
|
| 242 |
-
return [tree.val]
|
| 243 |
-
else
|
| 244 |
-
return Float32[]
|
| 245 |
-
end
|
| 246 |
-
elseif tree.degree == 1
|
| 247 |
-
return getConstants(tree.l)
|
| 248 |
-
else
|
| 249 |
-
both = [getConstants(tree.l), getConstants(tree.r)]
|
| 250 |
-
return [constant for subtree in both for constant in subtree]
|
| 251 |
-
end
|
| 252 |
-
end
|
| 253 |
-
|
| 254 |
-
# Set all the constants inside a tree
|
| 255 |
-
function setConstants(tree::Node, constants::Array{Float32, 1})
|
| 256 |
-
if tree.degree == 0
|
| 257 |
-
if tree.constant
|
| 258 |
-
tree.val = constants[1]
|
| 259 |
-
end
|
| 260 |
-
elseif tree.degree == 1
|
| 261 |
-
setConstants(tree.l, constants)
|
| 262 |
-
else
|
| 263 |
-
numberLeft = countConstants(tree.l)
|
| 264 |
-
setConstants(tree.l, constants)
|
| 265 |
-
setConstants(tree.r, constants[numberLeft+1:end])
|
| 266 |
-
end
|
| 267 |
-
end
|
| 268 |
-
|
| 269 |
|
| 270 |
# Proxy function for optimization
|
| 271 |
function optFunc(x::Array{Float32, 1}, tree::Node)::Float32
|
|
@@ -316,38 +289,6 @@ function optimizeConstants(member::PopMember)::PopMember
|
|
| 316 |
end
|
| 317 |
|
| 318 |
|
| 319 |
-
# List of the best members seen all time
|
| 320 |
-
mutable struct HallOfFame
|
| 321 |
-
members::Array{PopMember, 1}
|
| 322 |
-
exists::Array{Bool, 1} #Whether it has been set
|
| 323 |
-
|
| 324 |
-
# Arranged by complexity - store one at each.
|
| 325 |
-
HallOfFame() = new([PopMember(Node(1f0), 1f9) for i=1:actualMaxsize], [false for i=1:actualMaxsize])
|
| 326 |
-
end
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
# Check for errors before they happen
|
| 330 |
-
function testConfiguration()
|
| 331 |
-
test_input = LinRange(-100f0, 100f0, 99)
|
| 332 |
-
|
| 333 |
-
try
|
| 334 |
-
for left in test_input
|
| 335 |
-
for right in test_input
|
| 336 |
-
for binop in binops
|
| 337 |
-
test_output = binop.(left, right)
|
| 338 |
-
end
|
| 339 |
-
end
|
| 340 |
-
for unaop in unaops
|
| 341 |
-
test_output = unaop.(left)
|
| 342 |
-
end
|
| 343 |
-
end
|
| 344 |
-
catch error
|
| 345 |
-
@printf("\n\nYour configuration is invalid - one of your operators is not well-defined over the real line.\n\n\n")
|
| 346 |
-
throw(error)
|
| 347 |
-
end
|
| 348 |
-
end
|
| 349 |
-
|
| 350 |
-
|
| 351 |
function fullRun(niterations::Integer;
|
| 352 |
npop::Integer=300,
|
| 353 |
ncyclesperiteration::Integer=3000,
|
|
|
|
| 27 |
|
| 28 |
include("PopMember.jl")
|
| 29 |
|
| 30 |
+
|
| 31 |
+
include("halloffame.jl")
|
| 32 |
+
|
| 33 |
+
|
| 34 |
include("complexityChecks.jl")
|
| 35 |
|
| 36 |
|
|
|
|
| 239 |
return pop
|
| 240 |
end
|
| 241 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
|
| 243 |
# Proxy function for optimization
|
| 244 |
function optFunc(x::Array{Float32, 1}, tree::Node)::Float32
|
|
|
|
| 289 |
end
|
| 290 |
|
| 291 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 292 |
function fullRun(niterations::Integer;
|
| 293 |
npop::Integer=300,
|
| 294 |
ncyclesperiteration::Integer=3000,
|
julia/utils.jl
CHANGED
|
@@ -8,4 +8,25 @@ end
|
|
| 8 |
|
| 9 |
function getTime()::Integer
|
| 10 |
return round(Integer, 1e3*(time()-1.6e9))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
end
|
|
|
|
| 8 |
|
| 9 |
function getTime()::Integer
|
| 10 |
return round(Integer, 1e3*(time()-1.6e9))
|
| 11 |
+
end
|
| 12 |
+
|
| 13 |
+
# Check for errors before they happen
|
| 14 |
+
function testConfiguration()
|
| 15 |
+
test_input = LinRange(-100f0, 100f0, 99)
|
| 16 |
+
|
| 17 |
+
try
|
| 18 |
+
for left in test_input
|
| 19 |
+
for right in test_input
|
| 20 |
+
for binop in binops
|
| 21 |
+
test_output = binop.(left, right)
|
| 22 |
+
end
|
| 23 |
+
end
|
| 24 |
+
for unaop in unaops
|
| 25 |
+
test_output = unaop.(left)
|
| 26 |
+
end
|
| 27 |
+
end
|
| 28 |
+
catch error
|
| 29 |
+
@printf("\n\nYour configuration is invalid - one of your operators is not well-defined over the real line.\n\n\n")
|
| 30 |
+
throw(error)
|
| 31 |
+
end
|
| 32 |
end
|