MilesCranmer commited on
Commit
327e651
1 Parent(s): 651f56a

Create copy operation for nodes; faster than deepcopy

Browse files
Files changed (2) hide show
  1. README.md +2 -2
  2. eureqa.jl +14 -3
README.md CHANGED
@@ -147,14 +147,14 @@ pd.DataFrame, Results dataframe, giving complexity, MSE, and equations
147
 
148
  # TODO
149
 
150
- - [ ] Write our own tree copy operation; deepcopy() is the slowest operation by far.
151
  - [ ] Consider adding mutation for constant<->variable
152
  - [ ] Use NN to generate weights over all probability distribution conditional on error and existing equation, and train on some randomly-generated equations
153
  - [ ] Performance:
154
  - [ ] Use an enum for functions instead of storing them?
155
  - Current most expensive operations:
156
  - [ ] Calculating the loss function - there is duplicate calculations happening.
157
- - [ ] Declaration of the weights array every iteration
 
158
  - [x] Hyperparameter tune
159
  - [x] Create a benchmark for accuracy
160
  - [x] Add interface for either defining an operation to learn, or loading in arbitrary dataset.
 
147
 
148
  # TODO
149
 
 
150
  - [ ] Consider adding mutation for constant<->variable
151
  - [ ] Use NN to generate weights over all probability distribution conditional on error and existing equation, and train on some randomly-generated equations
152
  - [ ] Performance:
153
  - [ ] Use an enum for functions instead of storing them?
154
  - Current most expensive operations:
155
  - [ ] Calculating the loss function - there is duplicate calculations happening.
156
+ - [x] Declaration of the weights array every iteration
157
+ - [x] Write our own tree copy operation; deepcopy() is the slowest operation by far.
158
  - [x] Hyperparameter tune
159
  - [x] Create a benchmark for accuracy
160
  - [x] Add interface for either defining an operation to learn, or loading in arbitrary dataset.
eureqa.jl CHANGED
@@ -39,6 +39,17 @@ mutable struct Node
39
  Node(op, l::Union{Float32, Integer}, r::Union{Float32, Integer}) = new(2, 0.0f0, false, op, Node(l), Node(r))
40
  end
41
 
 
 
 
 
 
 
 
 
 
 
 
42
  # Evaluate a symbolic equation:
43
  function evalTree(tree::Node, x::Array{Float32, 1}=Float32[])::Float32
44
  if tree.degree == 0
@@ -327,11 +338,11 @@ function iterate(
327
  annealing::Bool=true
328
  )::Node
329
  prev = tree
330
- tree = deepcopy(tree)
331
 
332
  mutationChoice = rand()
333
  weightAdjustmentMutateConstant = min(8, countConstants(tree))/8.0
334
- cur_weights = deepcopy(mutationWeights) .* 1.0
335
  cur_weights[1] *= weightAdjustmentMutateConstant
336
  cur_weights /= sum(cur_weights)
337
  cweights = cumsum(cur_weights)
@@ -361,7 +372,7 @@ function iterate(
361
  probChange = exp(-delta/(T*alpha))
362
 
363
  if isnan(afterLoss) || probChange < rand()
364
- return deepcopy(prev)
365
  end
366
  end
367
 
 
39
  Node(op, l::Union{Float32, Integer}, r::Union{Float32, Integer}) = new(2, 0.0f0, false, op, Node(l), Node(r))
40
  end
41
 
42
+ # Copy an equation (faster than deepcopy)
43
+ function copyNode(tree::Node)::Node
44
+ if tree.degree == 0
45
+ return Node(tree.val)
46
+ elseif tree.degree == 1
47
+ return Node(tree.op, copyNode(tree.l))
48
+ else
49
+ return Node(tree.op, copyNode(tree.l), copyNode(tree.r))
50
+ end
51
+ end
52
+
53
  # Evaluate a symbolic equation:
54
  function evalTree(tree::Node, x::Array{Float32, 1}=Float32[])::Float32
55
  if tree.degree == 0
 
338
  annealing::Bool=true
339
  )::Node
340
  prev = tree
341
+ tree = copyNode(tree)
342
 
343
  mutationChoice = rand()
344
  weightAdjustmentMutateConstant = min(8, countConstants(tree))/8.0
345
+ cur_weights = copy(mutationWeights) .* 1.0
346
  cur_weights[1] *= weightAdjustmentMutateConstant
347
  cur_weights /= sum(cur_weights)
348
  cweights = cumsum(cur_weights)
 
372
  probChange = exp(-delta/(T*alpha))
373
 
374
  if isnan(afterLoss) || probChange < rand()
375
+ return copyNode(prev)
376
  end
377
  end
378