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

Function to return random node AND parent

Browse files
Files changed (1) hide show
  1. eureqa.jl +25 -0
eureqa.jl CHANGED
@@ -361,6 +361,31 @@ function randomConstantNode()::Node
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
 
361
  return newnode
362
  end
363
 
364
+ # Return a random node from the tree with parent
365
+ function randomNodeAndParent(tree::Node, parent::Union{Node, Nothing})::Tuple{Node, Union{Node, Nothing}}
366
+ if tree.degree == 0
367
+ return tree, parent
368
+ end
369
+ a = countNodes(tree)
370
+ b = 0
371
+ c = 0
372
+ if tree.degree >= 1
373
+ b = countNodes(tree.l)
374
+ end
375
+ if tree.degree == 2
376
+ c = countNodes(tree.r)
377
+ end
378
+
379
+ i = rand(1:1+b+c)
380
+ if i <= b
381
+ return randomNodeAndParent(tree.l, tree)
382
+ elseif i == b + 1
383
+ return tree, parent
384
+ end
385
+
386
+ return randomNodeAndParent(tree.r, tree)
387
+ end
388
+
389
  # Select a random node, and replace it an the subtree
390
  # with a variable or constant
391
  function deleteRandomOp(tree::Node)::Node