Spaces:
Sleeping
Sleeping
MilesCranmer
commited on
Commit
•
b408888
1
Parent(s):
627c408
Add insertion op
Browse files
eureqa.jl
CHANGED
@@ -293,17 +293,80 @@ function appendRandomOp(tree::Node)::Node
|
|
293 |
return tree
|
294 |
end
|
295 |
|
296 |
-
#
|
297 |
-
|
298 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
299 |
node = randomNode(tree)
|
300 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
301 |
if rand() > 0.5
|
302 |
val = Float32(randn())
|
303 |
else
|
304 |
val = rand(1:nvar)
|
305 |
end
|
306 |
newnode = Node(val)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
307 |
node.l = newnode.l
|
308 |
node.r = newnode.r
|
309 |
node.op = newnode.op
|
@@ -357,7 +420,12 @@ function iterate(
|
|
357 |
elseif mutationChoice < cweights[2]
|
358 |
tree = mutateOperator(tree)
|
359 |
elseif mutationChoice < cweights[3] && n < maxsize
|
360 |
-
|
|
|
|
|
|
|
|
|
|
|
361 |
elseif mutationChoice < cweights[4]
|
362 |
tree = deleteRandomOp(tree)
|
363 |
elseif mutationChoice < cweights[5]
|
|
|
293 |
return tree
|
294 |
end
|
295 |
|
296 |
+
# Add random node to the top of a tree
|
297 |
+
function popRandomOp(tree::Node)::Node
|
298 |
+
node = tree
|
299 |
+
choice = rand()
|
300 |
+
makeNewBinOp = choice < nbin/nops
|
301 |
+
left = tree
|
302 |
+
|
303 |
+
if makeNewBinOp
|
304 |
+
right = randomConstantNode()
|
305 |
+
newnode = Node(
|
306 |
+
binops[rand(1:length(binops))],
|
307 |
+
left,
|
308 |
+
right
|
309 |
+
)
|
310 |
+
else
|
311 |
+
newnode = Node(
|
312 |
+
unaops[rand(1:length(unaops))],
|
313 |
+
left
|
314 |
+
)
|
315 |
+
end
|
316 |
+
node.l = newnode.l
|
317 |
+
node.r = newnode.r
|
318 |
+
node.op = newnode.op
|
319 |
+
node.degree = newnode.degree
|
320 |
+
node.val = newnode.val
|
321 |
+
node.constant = newnode.constant
|
322 |
+
return node
|
323 |
+
end
|
324 |
+
|
325 |
+
# Insert random node
|
326 |
+
function insertRandomOp(tree::Node)::Node
|
327 |
node = randomNode(tree)
|
328 |
+
choice = rand()
|
329 |
+
makeNewBinOp = choice < nbin/nops
|
330 |
+
left = copyNode(node)
|
331 |
+
|
332 |
+
if makeNewBinOp
|
333 |
+
right = randomConstantNode()
|
334 |
+
newnode = Node(
|
335 |
+
binops[rand(1:length(binops))],
|
336 |
+
left,
|
337 |
+
right
|
338 |
+
)
|
339 |
+
else
|
340 |
+
newnode = Node(
|
341 |
+
unaops[rand(1:length(unaops))],
|
342 |
+
left
|
343 |
+
)
|
344 |
+
end
|
345 |
+
node.l = newnode.l
|
346 |
+
node.r = newnode.r
|
347 |
+
node.op = newnode.op
|
348 |
+
node.degree = newnode.degree
|
349 |
+
node.val = newnode.val
|
350 |
+
node.constant = newnode.constant
|
351 |
+
return tree
|
352 |
+
end
|
353 |
+
|
354 |
+
function randomConstantNode()::Node
|
355 |
if rand() > 0.5
|
356 |
val = Float32(randn())
|
357 |
else
|
358 |
val = rand(1:nvar)
|
359 |
end
|
360 |
newnode = Node(val)
|
361 |
+
return newnode
|
362 |
+
end
|
363 |
+
|
364 |
+
# Select a random node, and replace it an the subtree
|
365 |
+
# with a variable or constant
|
366 |
+
function deleteRandomOp(tree::Node)::Node
|
367 |
+
node = randomNode(tree)
|
368 |
+
# Can "delete" variable or constant too
|
369 |
+
newnode = randomConstantNode()
|
370 |
node.l = newnode.l
|
371 |
node.r = newnode.r
|
372 |
node.op = newnode.op
|
|
|
420 |
elseif mutationChoice < cweights[2]
|
421 |
tree = mutateOperator(tree)
|
422 |
elseif mutationChoice < cweights[3] && n < maxsize
|
423 |
+
toInsert = rand() < 0.1
|
424 |
+
if toInsert
|
425 |
+
tree = insertRandomOp(tree)
|
426 |
+
else
|
427 |
+
tree = appendRandomOp(tree)
|
428 |
+
end
|
429 |
elseif mutationChoice < cweights[4]
|
430 |
tree = deleteRandomOp(tree)
|
431 |
elseif mutationChoice < cweights[5]
|