Spaces:
Sleeping
Sleeping
MilesCranmer
commited on
Commit
•
d1327f1
1
Parent(s):
08a98f6
Rewrite delete function to join cutoff subtree with parent
Browse files
eureqa.jl
CHANGED
@@ -389,15 +389,47 @@ end
|
|
389 |
# Select a random node, and replace it an the subtree
|
390 |
# with a variable or constant
|
391 |
function deleteRandomOp(tree::Node)::Node
|
392 |
-
node =
|
393 |
-
|
394 |
-
|
395 |
-
node.
|
396 |
-
|
397 |
-
|
398 |
-
|
399 |
-
|
400 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
401 |
return tree
|
402 |
end
|
403 |
|
|
|
389 |
# Select a random node, and replace it an the subtree
|
390 |
# with a variable or constant
|
391 |
function deleteRandomOp(tree::Node)::Node
|
392 |
+
node, parent = randomNodeAndParent(tree, nothing)
|
393 |
+
isroot = (parent == nothing)
|
394 |
+
|
395 |
+
if node.degree == 0
|
396 |
+
# Replace with new constant
|
397 |
+
newnode = randomConstantNode()
|
398 |
+
node.l = newnode.l
|
399 |
+
node.r = newnode.r
|
400 |
+
node.op = newnode.op
|
401 |
+
node.degree = newnode.degree
|
402 |
+
node.val = newnode.val
|
403 |
+
node.constant = newnode.constant
|
404 |
+
elseif node.degree == 1
|
405 |
+
# Join one of the children with the parent
|
406 |
+
if isroot
|
407 |
+
return node.l
|
408 |
+
elseif parent.l == node
|
409 |
+
parent.l = node.l
|
410 |
+
else
|
411 |
+
parent.r = node.l
|
412 |
+
end
|
413 |
+
else
|
414 |
+
# Join one of the children with the parent
|
415 |
+
if rand() < 0.5
|
416 |
+
if isroot
|
417 |
+
return node.l
|
418 |
+
elseif parent.l == node
|
419 |
+
parent.l = node.l
|
420 |
+
else
|
421 |
+
parent.r = node.l
|
422 |
+
end
|
423 |
+
else
|
424 |
+
if isroot
|
425 |
+
return node.r
|
426 |
+
elseif parent.l == node
|
427 |
+
parent.l = node.r
|
428 |
+
else
|
429 |
+
parent.r = node.r
|
430 |
+
end
|
431 |
+
end
|
432 |
+
end
|
433 |
return tree
|
434 |
end
|
435 |
|