MilesCranmer commited on
Commit
b364345
1 Parent(s): 9c27796

Fix other parallelization issue with switch to @spawnat over @pmap

Browse files
Files changed (2) hide show
  1. eureqa.jl +9 -3
  2. paralleleureqa.jl +86 -33
eureqa.jl CHANGED
@@ -17,6 +17,8 @@ const parsimony = 0.01
17
  # How much to scale temperature by (T between 0 and 1)
18
  const alpha = 10.0
19
 
 
 
20
 
21
 
22
 
@@ -188,7 +190,8 @@ function mutateConstant(
188
  node = randomNode(tree)
189
  end
190
 
191
- maxChange = T + 1.0
 
192
  factor = maxChange^rand()
193
  makeConstBigger = rand() > 0.5
194
 
@@ -310,12 +313,13 @@ function iterate(
310
  weights = [8, 1, 1, 1]
311
  weights /= sum(weights)
312
  cweights = cumsum(weights)
 
313
 
314
  if mutationChoice < cweights[1]
315
  tree = mutateConstant(tree, T)
316
  elseif mutationChoice < cweights[2]
317
  tree = mutateOperator(tree)
318
- elseif mutationChoice < cweights[3]
319
  tree = appendRandomOp(tree)
320
  elseif mutationChoice < cweights[4]
321
  tree = deleteRandomOp(tree)
@@ -407,6 +411,7 @@ end
407
  function regEvolCycle(pop::Population, T::Float64)::Population
408
  for i=1:Int(pop.n/10)
409
  baby = iterateSample(pop, T)
 
410
  oldest = argmin([pop.members[member].birth for member=1:pop.n])
411
  pop.members[oldest] = baby
412
  end
@@ -427,8 +432,9 @@ function run(
427
  if annealing
428
  pop = regEvolCycle(pop, allT[iT])
429
  else
430
- pop = regEvolCycle(pop, 0.0)
431
  end
432
  end
433
  return pop
434
  end
 
 
17
  # How much to scale temperature by (T between 0 and 1)
18
  const alpha = 10.0
19
 
20
+ const maxsize = 20
21
+
22
 
23
 
24
 
 
190
  node = randomNode(tree)
191
  end
192
 
193
+ bottom = 0.1
194
+ maxChange = T + 1.0 + bottom
195
  factor = maxChange^rand()
196
  makeConstBigger = rand() > 0.5
197
 
 
313
  weights = [8, 1, 1, 1]
314
  weights /= sum(weights)
315
  cweights = cumsum(weights)
316
+ n = countNodes(tree)
317
 
318
  if mutationChoice < cweights[1]
319
  tree = mutateConstant(tree, T)
320
  elseif mutationChoice < cweights[2]
321
  tree = mutateOperator(tree)
322
+ elseif mutationChoice < cweights[3] && n < maxsize
323
  tree = appendRandomOp(tree)
324
  elseif mutationChoice < cweights[4]
325
  tree = deleteRandomOp(tree)
 
411
  function regEvolCycle(pop::Population, T::Float64)::Population
412
  for i=1:Int(pop.n/10)
413
  baby = iterateSample(pop, T)
414
+ #printTree(baby.tree)
415
  oldest = argmin([pop.members[member].birth for member=1:pop.n])
416
  pop.members[oldest] = baby
417
  end
 
432
  if annealing
433
  pop = regEvolCycle(pop, allT[iT])
434
  else
435
+ pop = regEvolCycle(pop, 1.0)
436
  end
437
  end
438
  return pop
439
  end
440
+
paralleleureqa.jl CHANGED
@@ -1,14 +1,14 @@
1
  using Distributed
2
- const nthreads = 8
3
- addprocs(nthreads)
4
 
5
  @everywhere include("eureqa.jl")
6
 
7
  println("Lets try to learn (x2^2 + cos(x3) + 5) using regularized evolution from scratch")
8
- const npop = 100
9
- const annealing = false
10
- const niterations = 10
11
- const ncyclesperiteration = 1000
12
 
13
  # Generate random initial populations
14
 
@@ -16,43 +16,96 @@ const ncyclesperiteration = 1000
16
  @everywhere f = (pop,)->run(pop, ncyclesperiteration, annealing)
17
 
18
 
19
- function update(allPops::Array{Population, 1}, bestScore::Float64, pool::AbstractWorkerPool)
20
- # Map it over our workers
21
- #global allPops = deepcopy(pmap(f, deepcopy(allPops)))
22
- curAllPops = deepcopy(pmap(f, allPops))
23
- for j=1:nthreads
24
- allPops[j] = curAllPops[j]
25
- end
26
 
27
- # Get best 10 models for each processes
28
- bestPops = Population([member for pop in allPops for member in bestSubPop(pop).members])
 
 
 
 
29
  bestCurScoreIdx = argmin([bestPops.members[member].score for member=1:bestPops.n])
30
  bestCurScore = bestPops.members[bestCurScoreIdx].score
31
- if bestCurScore < bestScore
32
- bestScore = bestCurScore
33
- println(bestScore, " is the score for ", stringTree(bestPops.members[bestCurScoreIdx].tree))
34
- end
35
 
36
  # Migration
37
  for j=1:nthreads
38
- allPops[j].members[1:50] = deepcopy(bestPops.members[rand(1:bestPops.n, 50)])
 
 
 
39
  end
40
- return allPops, bestScore
41
  end
42
 
43
 
44
- function runExperiment()
45
- # Do niterations cycles
46
- allPops = [Population(npop, 3) for j=1:nthreads]
47
- bestScore = Inf
48
- #pool = CachingPool(workers())
49
- pool = WorkerPool(workers())
50
- for i=1:niterations
51
- allPops, bestScore = update(allPops, bestScore, pool)
52
- end
53
 
54
- return bestScore
55
- end
56
 
57
- runExperiment()
58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  using Distributed
2
+ addprocs(8)
3
+ @everywhere const nthreads = 8
4
 
5
  @everywhere include("eureqa.jl")
6
 
7
  println("Lets try to learn (x2^2 + cos(x3) + 5) using regularized evolution from scratch")
8
+ @everywhere const npop = 100
9
+ @everywhere const annealing = false
10
+ @everywhere const niterations = 30
11
+ @everywhere const ncyclesperiteration = 10000
12
 
13
  # Generate random initial populations
14
 
 
16
  @everywhere f = (pop,)->run(pop, ncyclesperiteration, annealing)
17
 
18
 
19
+ allPops = [Population(npop, 3) for j=1:nthreads]
20
+ bestScore = Inf
21
+ # Repeat this many evolutions; we collect and migrate the best
22
+ # each time.
23
+ for k=1:4
24
+ # Spawn independent evolutions
25
+ futures = [@spawnat :any f(allPops[i]) for i=1:nthreads]
26
 
27
+ # Gather them
28
+ for i=1:nthreads
29
+ allPops[i] = fetch(futures[i])
30
+ end
31
+ # Get best 10 models for each processes. Copy because we re-assign later.
32
+ bestPops = deepcopy(Population([member for pop in allPops for member in bestSubPop(pop).members]))
33
  bestCurScoreIdx = argmin([bestPops.members[member].score for member=1:bestPops.n])
34
  bestCurScore = bestPops.members[bestCurScoreIdx].score
35
+ println(bestCurScore, " is the score for ", stringTree(bestPops.members[bestCurScoreIdx].tree))
 
 
 
36
 
37
  # Migration
38
  for j=1:nthreads
39
+ for k in rand(1:npop, 50)
40
+ # Copy in case one gets copied twice
41
+ allPops[j].members[k] = deepcopy(bestPops.members[rand(1:size(bestPops.members)[1])])
42
+ end
43
  end
 
44
  end
45
 
46
 
 
 
 
 
 
 
 
 
 
47
 
 
 
48
 
 
49
 
50
+ # julia> @everywhere include_string(Main, $(read("count_heads.jl", String)), "count_heads.jl")
51
+
52
+ # julia> a = @spawnat :any count_heads(100000000)
53
+ # Future(2, 1, 6, nothing)
54
+
55
+ # julia> b = @spawnat :any count_heads(100000000)
56
+ # Future(3, 1, 7, nothing)
57
+
58
+ # julia> fetch(a)+fetch(b)
59
+ # 100001564
60
+
61
+ # allPops = [Population(npop, 3) for j=1:nthreads]
62
+ # bestScore = Inf
63
+ # for i=1:10
64
+ # tmpPops = fetch(pmap(f, allPops))
65
+ # allPops[1:nthreads] = tmpPops[1:nthreads]
66
+ # # Get best 11 models for each processes
67
+ # bestPops = Population([member for pop in allPops for member in bestSubPop(pop).members])
68
+ # bestCurScoreIdx = argmin([bestPops.members[member].score for member=1:bestPops.n])
69
+ # bestCurScore = bestPops.members[bestCurScoreIdx].score
70
+ # println(bestCurScore, " is the score for ", stringTree(bestPops.members[bestCurScoreIdx].tree))
71
+ # end
72
+
73
+
74
+ # function update(allPops::Array{Population, 1}, bestScore::Float64)
75
+ # # Map it over our workers
76
+ # #global allPops = deepcopy(pmap(f, deepcopy(allPops)))
77
+ # #curAllPops = deepcopy(pmap(f, allPops))
78
+ # curAllPops = pmap(f, allPops)
79
+ # for j=1:nthreads
80
+ # allPops[j] = curAllPops[j]
81
+ # end
82
+
83
+ # # Get best 10 models for each processes
84
+ # bestPops = Population([member for pop in allPops for member in bestSubPop(pop).members])
85
+ # bestCurScoreIdx = argmin([bestPops.members[member].score for member=1:bestPops.n])
86
+ # bestCurScore = bestPops.members[bestCurScoreIdx].score
87
+ # if bestCurScore < bestScore
88
+ # bestScore = bestCurScore
89
+ # println(bestScore, " is the score for ", stringTree(bestPops.members[bestCurScoreIdx].tree))
90
+ # end
91
+
92
+ # # Migration
93
+ # for j=1:nthreads
94
+ # allPops[j].members[1:50] = deepcopy(bestPops.members[rand(1:bestPops.n, 50)])
95
+ # end
96
+ # return allPops, bestScore
97
+ # end
98
+
99
+
100
+ # function runExperiment()
101
+ # # Do niterations cycles
102
+ # allPops = [Population(npop, 3) for j=1:nthreads]
103
+ # bestScore = Inf
104
+ # for i=1:niterations
105
+ # allPops, bestScore = update(allPops, bestScore)
106
+ # end
107
+
108
+ # return bestScore
109
+ # end
110
+
111
+ # runExperiment()