MilesCranmer commited on
Commit
85aeb48
1 Parent(s): fad9923

Add back function for prepending op; randomly choose it

Browse files
Files changed (1) hide show
  1. julia/sr.jl +34 -1
julia/sr.jl CHANGED
@@ -419,6 +419,35 @@ function insertRandomOp(tree::Node)::Node
419
  return tree
420
  end
421
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
422
  function randomConstantNode()::Node
423
  if rand() > 0.5
424
  val = Float32(randn())
@@ -592,7 +621,11 @@ function iterate(member::PopMember, T::Float32)::PopMember
592
  elseif mutationChoice < cweights[2]
593
  tree = mutateOperator(tree)
594
  elseif mutationChoice < cweights[3] && n < maxsize && depth < maxdepth
595
- tree = appendRandomOp(tree)
 
 
 
 
596
  elseif mutationChoice < cweights[4] && n < maxsize && depth < maxdepth
597
  tree = insertRandomOp(tree)
598
  elseif mutationChoice < cweights[5]
 
419
  return tree
420
  end
421
 
422
+ # Add random node to the top of a tree
423
+ function prependRandomOp(tree::Node)::Node
424
+ node = tree
425
+ choice = rand()
426
+ makeNewBinOp = choice < nbin/nops
427
+ left = tree
428
+
429
+ if makeNewBinOp
430
+ right = randomConstantNode()
431
+ newnode = Node(
432
+ rand(1:length(binops)),
433
+ left,
434
+ right
435
+ )
436
+ else
437
+ newnode = Node(
438
+ rand(1:length(unaops)),
439
+ left
440
+ )
441
+ end
442
+ node.l = newnode.l
443
+ node.r = newnode.r
444
+ node.op = newnode.op
445
+ node.degree = newnode.degree
446
+ node.val = newnode.val
447
+ node.constant = newnode.constant
448
+ return node
449
+ end
450
+
451
  function randomConstantNode()::Node
452
  if rand() > 0.5
453
  val = Float32(randn())
 
621
  elseif mutationChoice < cweights[2]
622
  tree = mutateOperator(tree)
623
  elseif mutationChoice < cweights[3] && n < maxsize && depth < maxdepth
624
+ if rand() < 0.5
625
+ tree = appendRandomOp(tree)
626
+ else
627
+ tree = prependRandomOp(tree)
628
+ end
629
  elseif mutationChoice < cweights[4] && n < maxsize && depth < maxdepth
630
  tree = insertRandomOp(tree)
631
  elseif mutationChoice < cweights[5]