MilesCranmer commited on
Commit
7a33a67
2 Parent(s): f570496 f972a12

Merge branch 'master' of github.com:MilesCranmer/Eureqa.jl

Browse files
Files changed (1) hide show
  1. eureqa.jl +29 -4
eureqa.jl CHANGED
@@ -1,6 +1,6 @@
1
  # Define allowed operators
2
- plus(x::Float32, y::Float32) = x+y
3
- mult(x::Float32, y::Float32) = x*y;
4
 
5
  ##########################
6
  # # Allowed operators
@@ -328,6 +328,28 @@ function deleteRandomOp(tree::Node)::Node
328
  return tree
329
  end
330
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
331
  # Go through one simulated annealing mutation cycle
332
  # exp(-delta/T) defines probability of accepting a change
333
  function iterate(
@@ -342,7 +364,7 @@ function iterate(
342
 
343
  mutationChoice = rand()
344
  weight_for_constant = min(8, countConstants(tree))
345
- weights = [weight_for_constant, 1, 1, 1, 2]
346
  weights /= sum(weights)
347
  cweights = cumsum(weights)
348
  n = countNodes(tree)
@@ -355,8 +377,11 @@ function iterate(
355
  tree = appendRandomOp(tree)
356
  elseif mutationChoice < cweights[4]
357
  tree = deleteRandomOp(tree)
 
 
 
358
  else
359
- tree = tree
360
  end
361
 
362
  if annealing
 
1
  # Define allowed operators
2
+ plus(x::Float32, y::Float32)::Float32 = x+y
3
+ mult(x::Float32, y::Float32)::Float32 = x*y;
4
 
5
  ##########################
6
  # # Allowed operators
 
328
  return tree
329
  end
330
 
331
+
332
+ # Simplify tree
333
+ function simplifyTree(tree::Node)::Node
334
+ if tree.degree == 1
335
+ tree.l = simplifyTree(tree.l)
336
+ if tree.l.degree == 0 && tree.l.constant
337
+ return Node(tree.op(tree.l.val))
338
+ end
339
+ elseif tree.degree == 2
340
+ tree.r = simplifyTree(tree.r)
341
+ tree.l = simplifyTree(tree.l)
342
+ constantsBelow = (
343
+ tree.l.degree == 0 && tree.l.constant &&
344
+ tree.r.degree == 0 && tree.r.constant
345
+ )
346
+ if constantsBelow
347
+ return Node(tree.op(tree.l.val, tree.r.val))
348
+ end
349
+ end
350
+ return tree
351
+ end
352
+
353
  # Go through one simulated annealing mutation cycle
354
  # exp(-delta/T) defines probability of accepting a change
355
  function iterate(
 
364
 
365
  mutationChoice = rand()
366
  weight_for_constant = min(8, countConstants(tree))
367
+ weights = [weight_for_constant, 1, 1, 1, 1, 2] .* 1.0
368
  weights /= sum(weights)
369
  cweights = cumsum(weights)
370
  n = countNodes(tree)
 
377
  tree = appendRandomOp(tree)
378
  elseif mutationChoice < cweights[4]
379
  tree = deleteRandomOp(tree)
380
+ elseif mutationChoice < cweights[5]
381
+ tree = simplifyTree(tree) # Sometimes we simplify tree
382
+ return tree
383
  else
384
+ return tree
385
  end
386
 
387
  if annealing