MilesCranmer commited on
Commit
d1327f1
1 Parent(s): 08a98f6

Rewrite delete function to join cutoff subtree with parent

Browse files
Files changed (1) hide show
  1. eureqa.jl +41 -9
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 = randomNode(tree)
393
- # Can "delete" variable or constant too
394
- newnode = randomConstantNode()
395
- node.l = newnode.l
396
- node.r = newnode.r
397
- node.op = newnode.op
398
- node.degree = newnode.degree
399
- node.val = newnode.val
400
- node.constant = newnode.constant
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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