File size: 5,170 Bytes
2a2a517
6e5f7ce
 
2a2a517
 
 
 
 
 
a369299
 
b364345
 
 
 
 
 
6e5f7ce
b364345
2a2a517
 
b364345
2a2a517
b364345
 
9c27796
 
b364345
9c27796
 
 
b364345
 
 
 
6e5f7ce
9c27796
 
2a2a517
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c27796
 
6e5f7ce
 
b364345
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
include("eureqa.jl")

println("Lets try to learn (x2^2 + cos(x3) + 5) using regularized evolution from scratch")
const nthreads = Threads.nthreads()
println("Running with $nthreads threads")
const npop = 100
const annealing = true
const niterations = 30
const ncyclesperiteration = 10000

# Generate random initial populations
allPops = [Population(npop, 3) for j=1:nthreads]
bestScore = Inf
# Repeat this many evolutions; we collect and migrate the best
# each time.
for k=1:4
    # Spawn independent evolutions

    # Gather them
    @inbounds Threads.@threads for i=1:nthreads
        allPops[i] = run(allPops[i], ncyclesperiteration, annealing)
    end

    # Get best 10 models for each processes. Copy because we re-assign later.
    bestPops = deepcopy(Population([member for pop in allPops for member in bestSubPop(pop).members]))
    bestCurScoreIdx = argmin([bestPops.members[member].score for member=1:bestPops.n])
    bestCurScore = bestPops.members[bestCurScoreIdx].score
    println(bestCurScore, " is the score for ", stringTree(bestPops.members[bestCurScoreIdx].tree))

    # Migration
    for j=1:nthreads
        for k in rand(1:npop, 50)
            # Copy in case one gets copied twice
            allPops[j].members[k] = deepcopy(bestPops.members[rand(1:size(bestPops.members)[1])])
        end
    end
end

## Possibly calls once for every thread? But works.

# using Distributed
# addprocs(8)
# @everywhere const nthreads = 8

# @everywhere include("eureqa.jl")

# println("Lets try to learn (x2^2 + cos(x3) + 5) using regularized evolution from scratch")
# @everywhere const npop = 100
# @everywhere const annealing = false
# @everywhere const niterations = 30
# @everywhere const ncyclesperiteration = 10000

# # Generate random initial populations

# # Create a mapping for running the algorithm on all processes
# @everywhere f = (pop,)->run(pop, ncyclesperiteration, annealing)


# @everywhere allPops = [Population(npop, 3) for j=1:nthreads]
# @everywhere bestScore = Inf
# # Repeat this many evolutions; we collect and migrate the best
# # each time.
# for k=1:4
    # # Spawn independent evolutions
    # @everywhere futures = [@spawnat :any f(allPops[i]) for i=1:nthreads]

    # # Gather them
    # for i=1:nthreads
        # @everywhere allPops[i] = fetch(futures[i])
    # end
    # # Get best 10 models for each processes. Copy because we re-assign later.
    # @everywhere bestPops = deepcopy(Population([member for pop in allPops for member in bestSubPop(pop).members]))
    # @everywhere bestCurScoreIdx = argmin([bestPops.members[member].score for member=1:bestPops.n])
    # @everywhere bestCurScore = bestPops.members[bestCurScoreIdx].score
    # println(bestCurScore, " is the score for ", stringTree(bestPops.members[bestCurScoreIdx].tree))

    # # Migration
    # for j=1:nthreads
        # for k in rand(1:npop, 50)
            # # Copy in case one gets copied twice
            # @everywhere allPops[j].members[k] = deepcopy(bestPops.members[rand(1:size(bestPops.members)[1])])
        # end
    # end
# end





# julia> @everywhere include_string(Main, $(read("count_heads.jl", String)), "count_heads.jl")

# julia> a = @spawnat :any count_heads(100000000)
# Future(2, 1, 6, nothing)

# julia> b = @spawnat :any count_heads(100000000)
# Future(3, 1, 7, nothing)

# julia> fetch(a)+fetch(b)
# 100001564

# allPops = [Population(npop, 3) for j=1:nthreads]
# bestScore = Inf
# for i=1:10
    # tmpPops = fetch(pmap(f, allPops))
    # allPops[1:nthreads] = tmpPops[1:nthreads]
    # # Get best 11 models for each processes
    # bestPops = Population([member for pop in allPops for member in bestSubPop(pop).members])
    # bestCurScoreIdx = argmin([bestPops.members[member].score for member=1:bestPops.n])
    # bestCurScore = bestPops.members[bestCurScoreIdx].score
    # println(bestCurScore, " is the score for ", stringTree(bestPops.members[bestCurScoreIdx].tree))
# end


# function update(allPops::Array{Population, 1}, bestScore::Float64)
    # # Map it over our workers
    # #global allPops = deepcopy(pmap(f, deepcopy(allPops)))
    # #curAllPops = deepcopy(pmap(f, allPops))
    # curAllPops = pmap(f, allPops)
    # for j=1:nthreads
        # allPops[j] = curAllPops[j]
    # end

    # # Get best 10 models for each processes
    # bestPops = Population([member for pop in allPops for member in bestSubPop(pop).members])
    # bestCurScoreIdx = argmin([bestPops.members[member].score for member=1:bestPops.n])
    # bestCurScore = bestPops.members[bestCurScoreIdx].score
    # if bestCurScore < bestScore
        # bestScore = bestCurScore
        # println(bestScore, " is the score for ", stringTree(bestPops.members[bestCurScoreIdx].tree))
    # end

    # # Migration
    # for j=1:nthreads
        # allPops[j].members[1:50] = deepcopy(bestPops.members[rand(1:bestPops.n, 50)])
    # end
    # return allPops, bestScore
# end


# function runExperiment()
    # # Do niterations cycles
    # allPops = [Population(npop, 3) for j=1:nthreads]
    # bestScore = Inf
    # for i=1:niterations
        # allPops, bestScore = update(allPops, bestScore)
    # end

    # return bestScore
# end

# runExperiment()