MilesCranmer commited on
Commit
b18ab5a
1 Parent(s): b15eaa4

Add functions to set constants

Browse files
Files changed (2) hide show
  1. eureqa.jl +39 -0
  2. paralleleureqa.jl +4 -3
eureqa.jl CHANGED
@@ -471,3 +471,42 @@ function run(
471
  end
472
  return pop
473
  end
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
471
  end
472
  return pop
473
  end
474
+
475
+ # Get all the constants from a tree
476
+ function getConstants(tree::Node)::Array{Float32, 1}
477
+ if tree.degree == 0
478
+ if tree.constant
479
+ return [tree.val]
480
+ else
481
+ return Float32[]
482
+ end
483
+ elseif tree.degree == 1
484
+ return getConstants(tree.l)
485
+ else
486
+ both = [getConstants(tree.l), getConstants(tree.r)]
487
+ return [constant for subtree in both for constant in subtree]
488
+ end
489
+ end
490
+
491
+ # Set all the constants inside a tree
492
+ function setConstants(tree::Node, constants::Array{Float32, 1})
493
+ if tree.degree == 0
494
+ if tree.constant
495
+ tree.val = constants[1]
496
+ end
497
+ elseif tree.degree == 1
498
+ setConstants(tree.l, constants)
499
+ else
500
+ numberLeft = countConstants(tree.l)
501
+ setConstants(tree.l, constants)
502
+ setConstants(tree.r, constants[numberLeft+1:end])
503
+ end
504
+ end
505
+
506
+ # Does nothing currently
507
+ function optimizeConstants(member::PopMember)::PopMember
508
+ x0 = getConstants(member.tree)
509
+ return member
510
+ end
511
+
512
+
paralleleureqa.jl CHANGED
@@ -5,10 +5,11 @@ const nthreads = Threads.nthreads()
5
  # List of the best members seen all time
6
  mutable struct HallOfFame
7
  members::Array{PopMember, 1}
8
- exists::Array{Bool, 1}
 
9
 
10
  # Arranged by complexity - store one at each.
11
- HallOfFame() = new([PopMember(Node(1f0), 1f9) for i=1:actualMaxsize], [false for i=1:actualMaxsize])
12
  end
13
 
14
 
@@ -56,6 +57,7 @@ function fullRun(niterations::Integer;
56
  betterThanAllSmaller = (numberSmallerAndBetter == 0)
57
  if betterThanAllSmaller
58
  debug(verbosity, "$size \t $(member.score-parsimony*size) \t $(stringTree(member.tree))")
 
59
  push!(dominating, member)
60
  end
61
  end
@@ -81,6 +83,5 @@ function fullRun(niterations::Integer;
81
  end
82
  end
83
  end
84
-
85
  end
86
  end
 
5
  # List of the best members seen all time
6
  mutable struct HallOfFame
7
  members::Array{PopMember, 1}
8
+ exists::Array{Bool, 1} #Whether it has been set
9
+ optimized::Array{Bool, 1} #Whether the constants have been optimized
10
 
11
  # Arranged by complexity - store one at each.
12
+ HallOfFame() = new([PopMember(Node(1f0), 1f9) for i=1:actualMaxsize], [false for i=1:actualMaxsize], [false for i=1:actualMaxsize])
13
  end
14
 
15
 
 
57
  betterThanAllSmaller = (numberSmallerAndBetter == 0)
58
  if betterThanAllSmaller
59
  debug(verbosity, "$size \t $(member.score-parsimony*size) \t $(stringTree(member.tree))")
60
+ member = optimizeConstants(member)
61
  push!(dominating, member)
62
  end
63
  end
 
83
  end
84
  end
85
  end
 
86
  end
87
  end