MilesCranmer commited on
Commit
d85f644
1 Parent(s): e987d40

Got Distributed.jl working

Browse files
Files changed (2) hide show
  1. julia/loop.jl +117 -0
  2. julia/sr.jl +0 -101
julia/loop.jl ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ using Distributed
2
+ const nprocs = 4
3
+ addprocs(4)
4
+ @everywhere include(".dataset_28330894764081783777.jl")
5
+ @everywhere include(".hyperparams_28330894764081783777.jl")
6
+ @everywhere include("sr.jl")
7
+
8
+
9
+ # 1. Start a population on every process
10
+ allPops = Future[]
11
+ bestSubPops = [Population(1) for j=1:nprocs]
12
+ hallOfFame = HallOfFame()
13
+
14
+ for i=1:nprocs
15
+ npop=300
16
+ future = @spawnat :any Population(npop, 3)
17
+ push!(allPops, future)
18
+ end
19
+
20
+ npop=300
21
+ ncyclesperiteration=3000
22
+ fractionReplaced=0.1f0
23
+ verbosity=convert(Int, 1e9)
24
+ topn=10
25
+ niterations=10
26
+
27
+
28
+ # # 2. Start the cycle on every process:
29
+ for i=1:nprocs
30
+ allPops[i] = @spawnat :any run(fetch(allPops[i]), ncyclesperiteration, verbosity=verbosity)
31
+ end
32
+ println("Started!")
33
+ cycles_complete = nprocs * 10
34
+ while cycles_complete > 0
35
+ for i=1:nprocs
36
+ if isready(allPops[i])
37
+ cur_pop = fetch(allPops[i])
38
+ bestSubPops[i] = bestSubPop(cur_pop, topn=topn)
39
+
40
+ #Try normal copy...
41
+ bestPops = Population([member for pop in bestSubPops for member in pop.members])
42
+
43
+ for member in cur_pop.members
44
+ size = countNodes(member.tree)
45
+ if member.score < hallOfFame.members[size].score
46
+ hallOfFame.members[size] = deepcopy(member)
47
+ hallOfFame.exists[size] = true
48
+ end
49
+ end
50
+
51
+ # Dominating pareto curve - must be better than all simpler equations
52
+ dominating = PopMember[]
53
+ open(hofFile, "w") do io
54
+ debug(verbosity, "\n")
55
+ debug(verbosity, "Hall of Fame:")
56
+ debug(verbosity, "-----------------------------------------")
57
+ debug(verbosity, "Complexity \t MSE \t Equation")
58
+ println(io,"Complexity|MSE|Equation")
59
+ for size=1:actualMaxsize
60
+ if hallOfFame.exists[size]
61
+ member = hallOfFame.members[size]
62
+ curMSE = MSE(evalTreeArray(member.tree), y)
63
+ numberSmallerAndBetter = sum([curMSE > MSE(evalTreeArray(hallOfFame.members[i].tree), y) for i=1:(size-1)])
64
+ betterThanAllSmaller = (numberSmallerAndBetter == 0)
65
+ if betterThanAllSmaller
66
+ debug(verbosity, "$size \t $(curMSE) \t $(stringTree(member.tree))")
67
+ println(io, "$size|$(curMSE)|$(stringTree(member.tree))")
68
+ push!(dominating, member)
69
+ end
70
+ end
71
+ end
72
+ debug(verbosity, "")
73
+ end
74
+
75
+ # Try normal copy otherwise.
76
+ if migration
77
+ for k in rand(1:npop, round(Integer, npop*fractionReplaced))
78
+ to_copy = rand(1:size(bestPops.members)[1])
79
+ cur_pop.members[k] = PopMember(
80
+ copyNode(bestPops.members[to_copy].tree),
81
+ bestPops.members[to_copy].score)
82
+ end
83
+ end
84
+
85
+ if hofMigration && size(dominating)[1] > 0
86
+ for k in rand(1:npop, round(Integer, npop*fractionReplacedHof))
87
+ # Copy in case one gets used twice
88
+ to_copy = rand(1:size(dominating)[1])
89
+ cur_pop.members[k] = PopMember(
90
+ copyNode(dominating[to_copy].tree)
91
+ )
92
+ end
93
+ end
94
+
95
+ allPops[i] = @spawnat :any let
96
+ tmp_pop = run(cur_pop, ncyclesperiteration, verbosity=verbosity)
97
+ for j=1:tmp_pop.n
98
+ if rand() < 0.1
99
+ tmp_pop.members[j].tree = simplifyTree(tmp_pop.members[j].tree)
100
+ tmp_pop.members[j].tree = combineOperators(tmp_pop.members[j].tree)
101
+ if shouldOptimizeConstants
102
+ tmp_pop.members[j] = optimizeConstants(tmp_pop.members[j])
103
+ end
104
+ end
105
+ end
106
+ tmp_pop
107
+ end
108
+
109
+ global cycles_complete -= 1
110
+ end
111
+ end
112
+ sleep(1e-3)
113
+ end
114
+
115
+ rmprocs(nprocs)
116
+
117
+
julia/sr.jl CHANGED
@@ -738,104 +738,3 @@ mutable struct HallOfFame
738
  end
739
 
740
 
741
- function fullRun(niterations::Integer;
742
- npop::Integer=300,
743
- ncyclesperiteration::Integer=3000,
744
- fractionReplaced::Float32=0.1f0,
745
- verbosity::Integer=0,
746
- topn::Integer=10
747
- )
748
- debug(verbosity, "Running with $nthreads threads")
749
- # Generate random initial populations
750
- allPops = [Population(npop, 3) for j=1:nthreads]
751
- bestSubPops = [Population(1) for j=1:nthreads]
752
- # Repeat this many evolutions; we collect and migrate the best
753
- # each time.
754
- hallOfFame = HallOfFame()
755
-
756
- for k=1:niterations
757
- # Spawn threads to run indepdent evolutions, then gather them
758
- start_time = time()
759
- @inbounds Threads.@threads for i=1:nthreads
760
- allPops[i] = run(allPops[i], ncyclesperiteration, verbosity=verbosity)
761
- for j=1:allPops[i].n
762
- if rand() < 0.1
763
- allPops[i].members[j].tree = simplifyTree(allPops[i].members[j].tree)
764
- allPops[i].members[j].tree = combineOperators(allPops[i].members[j].tree)
765
- if shouldOptimizeConstants
766
- allPops[i].members[j] = optimizeConstants(allPops[i].members[j])
767
- end
768
- end
769
- end
770
- bestSubPops[i] = bestSubPop(allPops[i], topn=topn)
771
- end
772
- total_time = time() - start_time
773
-
774
- number_equations_created = ncyclesperiteration * nthreads * npop / ns
775
- equations_created_per_second = number_equations_created / total_time
776
-
777
- # Get best 10 models from each evolution. Copy because we re-assign later.
778
- # bestPops = deepcopy(Population([member for pop in allPops for member in bestSubPop(pop).members]))
779
- bestPops = deepcopy(Population([member for pop in bestSubPops for member in pop.members]))
780
-
781
- #Update hall of fame
782
- for pop in allPops
783
- for member in pop.members
784
- size = countNodes(member.tree)
785
- if member.score < hallOfFame.members[size].score
786
- hallOfFame.members[size] = deepcopy(member)
787
- hallOfFame.exists[size] = true
788
- end
789
- end
790
- end
791
-
792
- # Dominating pareto curve - must be better than all simpler equations
793
- dominating = PopMember[]
794
- open(hofFile, "w") do io
795
- debug(verbosity, "\n")
796
- if k == 1
797
- debug(verbosity, "Total cycles per second (first round is slower due to compilation): $(round(equations_created_per_second, sigdigits=3))\n")
798
- else
799
- debug(verbosity, "Total cycles per second: $(round(equations_created_per_second, sigdigits=3))\n")
800
- end
801
- debug(verbosity, "Hall of Fame:")
802
- debug(verbosity, "-----------------------------------------")
803
- debug(verbosity, "Complexity \t MSE \t Equation")
804
- println(io,"Complexity|MSE|Equation")
805
- for size=1:actualMaxsize
806
- if hallOfFame.exists[size]
807
- member = hallOfFame.members[size]
808
- curMSE = MSE(evalTreeArray(member.tree), y)
809
- numberSmallerAndBetter = sum([curMSE > MSE(evalTreeArray(hallOfFame.members[i].tree), y) for i=1:(size-1)])
810
- betterThanAllSmaller = (numberSmallerAndBetter == 0)
811
- if betterThanAllSmaller
812
- debug(verbosity, "$size \t $(curMSE) \t $(stringTree(member.tree))")
813
- println(io, "$size|$(curMSE)|$(stringTree(member.tree))")
814
- push!(dominating, member)
815
- end
816
- end
817
- end
818
- debug(verbosity, "")
819
- end
820
-
821
- # Migration
822
- if migration
823
- for j=1:nthreads
824
- for k in rand(1:npop, round(Integer, npop*fractionReplaced))
825
- # Copy in case one gets used twice
826
- allPops[j].members[k] = deepcopy(bestPops.members[rand(1:size(bestPops.members)[1])])
827
- end
828
- end
829
- end
830
-
831
- # Hall of fame migration
832
- if hofMigration && size(dominating)[1] > 0
833
- for j=1:nthreads
834
- for k in rand(1:npop, round(Integer, npop*fractionReplacedHof))
835
- # Copy in case one gets used twice
836
- allPops[j].members[k] = deepcopy(dominating[rand(1:size(dominating)[1])])
837
- end
838
- end
839
- end
840
- end
841
- end
 
738
  end
739
 
740