Spaces:
Sleeping
Sleeping
MilesCranmer
commited on
Merge eureqa files
Browse files- neweureqa.jl +91 -0
- paralleleureqa.jl +0 -95
neweureqa.jl
CHANGED
@@ -10,6 +10,7 @@ const nuna = size(unaops)[1]
|
|
10 |
const nbin = size(binops)[1]
|
11 |
const nops = nuna + nbin
|
12 |
const nvar = size(X)[2];
|
|
|
13 |
|
14 |
function debug(verbosity, string...)
|
15 |
verbosity > 0 ? println(string...) : nothing
|
@@ -538,3 +539,93 @@ function optimizeConstants(member::PopMember)::PopMember
|
|
538 |
end
|
539 |
|
540 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
const nbin = size(binops)[1]
|
11 |
const nops = nuna + nbin
|
12 |
const nvar = size(X)[2];
|
13 |
+
const nthreads = Threads.nthreads()
|
14 |
|
15 |
function debug(verbosity, string...)
|
16 |
verbosity > 0 ? println(string...) : nothing
|
|
|
539 |
end
|
540 |
|
541 |
|
542 |
+
# List of the best members seen all time
|
543 |
+
mutable struct HallOfFame
|
544 |
+
members::Array{PopMember, 1}
|
545 |
+
exists::Array{Bool, 1} #Whether it has been set
|
546 |
+
|
547 |
+
# Arranged by complexity - store one at each.
|
548 |
+
HallOfFame() = new([PopMember(Node(1f0), 1f9) for i=1:actualMaxsize], [false for i=1:actualMaxsize])
|
549 |
+
end
|
550 |
+
|
551 |
+
|
552 |
+
function fullRun(niterations::Integer;
|
553 |
+
npop::Integer=300,
|
554 |
+
annealing::Bool=true,
|
555 |
+
ncyclesperiteration::Integer=3000,
|
556 |
+
fractionReplaced::Float32=0.1f0,
|
557 |
+
verbosity::Integer=0,
|
558 |
+
topn::Integer=10
|
559 |
+
)
|
560 |
+
debug(verbosity, "Lets try to learn (x2^2 + cos(x3)) using regularized evolution from scratch")
|
561 |
+
debug(verbosity, "Running with $nthreads threads")
|
562 |
+
# Generate random initial populations
|
563 |
+
allPops = [Population(npop, 3) for j=1:nthreads]
|
564 |
+
bestSubPops = [Population(1) for j=1:nthreads]
|
565 |
+
# Repeat this many evolutions; we collect and migrate the best
|
566 |
+
# each time.
|
567 |
+
hallOfFame = HallOfFame()
|
568 |
+
|
569 |
+
for k=1:niterations
|
570 |
+
# Spawn threads to run indepdent evolutions, then gather them
|
571 |
+
@inbounds Threads.@threads for i=1:nthreads
|
572 |
+
allPops[i] = run(allPops[i], ncyclesperiteration, annealing, verbosity=verbosity)
|
573 |
+
bestSubPops[i] = bestSubPop(allPops[i], topn=topn)
|
574 |
+
if shouldOptimizeConstants
|
575 |
+
for j=1:bestSubPops[i].n
|
576 |
+
bestSubPops[i].members[j] = optimizeConstants(bestSubPops[i].members[j])
|
577 |
+
end
|
578 |
+
end
|
579 |
+
end
|
580 |
+
|
581 |
+
# Get best 10 models from each evolution. Copy because we re-assign later.
|
582 |
+
# bestPops = deepcopy(Population([member for pop in allPops for member in bestSubPop(pop).members]))
|
583 |
+
bestPops = deepcopy(Population([member for pop in bestSubPops for member in pop.members]))
|
584 |
+
|
585 |
+
#Update hall of fame
|
586 |
+
for member in bestPops.members
|
587 |
+
size = countNodes(member.tree)
|
588 |
+
if member.score < hallOfFame.members[size].score
|
589 |
+
hallOfFame.members[size] = deepcopy(member)
|
590 |
+
hallOfFame.exists[size] = true
|
591 |
+
end
|
592 |
+
end
|
593 |
+
|
594 |
+
dominating = PopMember[]
|
595 |
+
debug(verbosity, "Hall of Fame:")
|
596 |
+
debug(verbosity, "-----------------------------------------")
|
597 |
+
debug(verbosity, "Complexity \t MSE \t Equation")
|
598 |
+
for size=1:maxsize
|
599 |
+
if hallOfFame.exists[size]
|
600 |
+
member = hallOfFame.members[size]
|
601 |
+
numberSmallerAndBetter = sum([member.score > hallOfFame.members[i].score for i=1:(size-1)])
|
602 |
+
betterThanAllSmaller = (numberSmallerAndBetter == 0)
|
603 |
+
if betterThanAllSmaller
|
604 |
+
debug(verbosity, "$size \t $(member.score-parsimony*size) \t $(stringTree(member.tree))")
|
605 |
+
push!(dominating, member)
|
606 |
+
end
|
607 |
+
end
|
608 |
+
end
|
609 |
+
debug(verbosity, "")
|
610 |
+
|
611 |
+
# Migration
|
612 |
+
if migration
|
613 |
+
for j=1:nthreads
|
614 |
+
for k in rand(1:npop, Integer(npop*fractionReplaced))
|
615 |
+
# Copy in case one gets used twice
|
616 |
+
allPops[j].members[k] = deepcopy(bestPops.members[rand(1:size(bestPops.members)[1])])
|
617 |
+
end
|
618 |
+
end
|
619 |
+
end
|
620 |
+
|
621 |
+
# Hall of fame migration
|
622 |
+
if hofMigration && size(dominating)[1] > 0
|
623 |
+
for j=1:nthreads
|
624 |
+
for k in rand(1:npop, Integer(npop*fractionReplacedHof))
|
625 |
+
# Copy in case one gets used twice
|
626 |
+
allPops[j].members[k] = deepcopy(dominating[rand(1:size(dominating)[1])])
|
627 |
+
end
|
628 |
+
end
|
629 |
+
end
|
630 |
+
end
|
631 |
+
end
|
paralleleureqa.jl
DELETED
@@ -1,95 +0,0 @@
|
|
1 |
-
include("eureqa.jl")
|
2 |
-
import Optim
|
3 |
-
|
4 |
-
const nthreads = Threads.nthreads()
|
5 |
-
|
6 |
-
# List of the best members seen all time
|
7 |
-
mutable struct HallOfFame
|
8 |
-
members::Array{PopMember, 1}
|
9 |
-
exists::Array{Bool, 1} #Whether it has been set
|
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])
|
13 |
-
end
|
14 |
-
|
15 |
-
|
16 |
-
function fullRun(niterations::Integer;
|
17 |
-
npop::Integer=300,
|
18 |
-
annealing::Bool=true,
|
19 |
-
ncyclesperiteration::Integer=3000,
|
20 |
-
fractionReplaced::Float32=0.1f0,
|
21 |
-
verbosity::Integer=0,
|
22 |
-
topn::Integer=10
|
23 |
-
)
|
24 |
-
debug(verbosity, "Lets try to learn (x2^2 + cos(x3)) using regularized evolution from scratch")
|
25 |
-
debug(verbosity, "Running with $nthreads threads")
|
26 |
-
# Generate random initial populations
|
27 |
-
allPops = [Population(npop, 3) for j=1:nthreads]
|
28 |
-
bestSubPops = [Population(1) for j=1:nthreads]
|
29 |
-
# Repeat this many evolutions; we collect and migrate the best
|
30 |
-
# each time.
|
31 |
-
hallOfFame = HallOfFame()
|
32 |
-
|
33 |
-
for k=1:niterations
|
34 |
-
# Spawn threads to run indepdent evolutions, then gather them
|
35 |
-
@inbounds Threads.@threads for i=1:nthreads
|
36 |
-
allPops[i] = run(allPops[i], ncyclesperiteration, annealing, verbosity=verbosity)
|
37 |
-
bestSubPops[i] = bestSubPop(allPops[i], topn=topn)
|
38 |
-
if shouldOptimizeConstants
|
39 |
-
for j=1:bestSubPops[i].n
|
40 |
-
bestSubPops[i].members[j] = optimizeConstants(bestSubPops[i].members[j])
|
41 |
-
end
|
42 |
-
end
|
43 |
-
end
|
44 |
-
|
45 |
-
# Get best 10 models from each evolution. Copy because we re-assign later.
|
46 |
-
# bestPops = deepcopy(Population([member for pop in allPops for member in bestSubPop(pop).members]))
|
47 |
-
bestPops = deepcopy(Population([member for pop in bestSubPops for member in pop.members]))
|
48 |
-
|
49 |
-
#Update hall of fame
|
50 |
-
for member in bestPops.members
|
51 |
-
size = countNodes(member.tree)
|
52 |
-
if member.score < hallOfFame.members[size].score
|
53 |
-
hallOfFame.members[size] = deepcopy(member)
|
54 |
-
hallOfFame.exists[size] = true
|
55 |
-
end
|
56 |
-
end
|
57 |
-
|
58 |
-
dominating = PopMember[]
|
59 |
-
debug(verbosity, "Hall of Fame:")
|
60 |
-
debug(verbosity, "-----------------------------------------")
|
61 |
-
debug(verbosity, "Complexity \t MSE \t Equation")
|
62 |
-
for size=1:maxsize
|
63 |
-
if hallOfFame.exists[size]
|
64 |
-
member = hallOfFame.members[size]
|
65 |
-
numberSmallerAndBetter = sum([member.score > hallOfFame.members[i].score for i=1:(size-1)])
|
66 |
-
betterThanAllSmaller = (numberSmallerAndBetter == 0)
|
67 |
-
if betterThanAllSmaller
|
68 |
-
debug(verbosity, "$size \t $(member.score-parsimony*size) \t $(stringTree(member.tree))")
|
69 |
-
push!(dominating, member)
|
70 |
-
end
|
71 |
-
end
|
72 |
-
end
|
73 |
-
debug(verbosity, "")
|
74 |
-
|
75 |
-
# Migration
|
76 |
-
if migration
|
77 |
-
for j=1:nthreads
|
78 |
-
for k in rand(1:npop, Integer(npop*fractionReplaced))
|
79 |
-
# Copy in case one gets used twice
|
80 |
-
allPops[j].members[k] = deepcopy(bestPops.members[rand(1:size(bestPops.members)[1])])
|
81 |
-
end
|
82 |
-
end
|
83 |
-
end
|
84 |
-
|
85 |
-
# Hall of fame migration
|
86 |
-
if hofMigration && size(dominating)[1] > 0
|
87 |
-
for j=1:nthreads
|
88 |
-
for k in rand(1:npop, Integer(npop*fractionReplacedHof))
|
89 |
-
# Copy in case one gets used twice
|
90 |
-
allPops[j].members[k] = deepcopy(dominating[rand(1:size(dominating)[1])])
|
91 |
-
end
|
92 |
-
end
|
93 |
-
end
|
94 |
-
end
|
95 |
-
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|