Spaces:
Sleeping
Sleeping
Commit
·
21ee78d
1
Parent(s):
f50d9d6
Refactored Till Population and moved a part to randomMutations
Browse files- julia/Population.jl +40 -0
- julia/randomMutations.jl +9 -0
- julia/sr.jl +1 -47
julia/Population.jl
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# A list of members of the population, with easy constructors,
|
| 2 |
+
# which allow for random generation of new populations
|
| 3 |
+
mutable struct Population
|
| 4 |
+
members::Array{PopMember, 1}
|
| 5 |
+
n::Integer
|
| 6 |
+
|
| 7 |
+
Population(pop::Array{PopMember, 1}) = new(pop, size(pop)[1])
|
| 8 |
+
Population(npop::Integer) = new([PopMember(genRandomTree(3)) for i=1:npop], npop)
|
| 9 |
+
Population(npop::Integer, nlength::Integer) = new([PopMember(genRandomTree(nlength)) for i=1:npop], npop)
|
| 10 |
+
|
| 11 |
+
end
|
| 12 |
+
|
| 13 |
+
# Sample 10 random members of the population, and make a new one
|
| 14 |
+
function samplePop(pop::Population)::Population
|
| 15 |
+
idx = rand(1:pop.n, ns)
|
| 16 |
+
return Population(pop.members[idx])
|
| 17 |
+
end
|
| 18 |
+
|
| 19 |
+
# Sample the population, and get the best member from that sample
|
| 20 |
+
function bestOfSample(pop::Population)::PopMember
|
| 21 |
+
sample = samplePop(pop)
|
| 22 |
+
best_idx = argmin([sample.members[member].score for member=1:sample.n])
|
| 23 |
+
return sample.members[best_idx]
|
| 24 |
+
end
|
| 25 |
+
|
| 26 |
+
function finalizeScores(pop::Population)::Population
|
| 27 |
+
need_recalculate = batching
|
| 28 |
+
if need_recalculate
|
| 29 |
+
@inbounds @simd for member=1:pop.n
|
| 30 |
+
pop.members[member].score = scoreFunc(pop.members[member].tree)
|
| 31 |
+
end
|
| 32 |
+
end
|
| 33 |
+
return pop
|
| 34 |
+
end
|
| 35 |
+
|
| 36 |
+
# Return best 10 examples
|
| 37 |
+
function bestSubPop(pop::Population; topn::Integer=10)::Population
|
| 38 |
+
best_idx = sortperm([pop.members[member].score for member=1:pop.n])
|
| 39 |
+
return Population(pop.members[best_idx[1:topn]])
|
| 40 |
+
end
|
julia/randomMutations.jl
CHANGED
|
@@ -227,4 +227,13 @@ function deleteRandomOp(tree::Node)::Node
|
|
| 227 |
end
|
| 228 |
end
|
| 229 |
return tree
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
end
|
|
|
|
| 227 |
end
|
| 228 |
end
|
| 229 |
return tree
|
| 230 |
+
end
|
| 231 |
+
|
| 232 |
+
# Create a random equation by appending random operators
|
| 233 |
+
function genRandomTree(length::Integer)::Node
|
| 234 |
+
tree = Node(1.0f0)
|
| 235 |
+
for i=1:length
|
| 236 |
+
tree = appendRandomOp(tree)
|
| 237 |
+
end
|
| 238 |
+
return tree
|
| 239 |
end
|
julia/sr.jl
CHANGED
|
@@ -156,56 +156,10 @@ function iterate(member::PopMember, T::Float32, curmaxsize::Integer, frequencyCo
|
|
| 156 |
return PopMember(tree, afterLoss)
|
| 157 |
end
|
| 158 |
|
| 159 |
-
# Create a random equation by appending random operators
|
| 160 |
-
function genRandomTree(length::Integer)::Node
|
| 161 |
-
tree = Node(1.0f0)
|
| 162 |
-
for i=1:length
|
| 163 |
-
tree = appendRandomOp(tree)
|
| 164 |
-
end
|
| 165 |
-
return tree
|
| 166 |
-
end
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
# A list of members of the population, with easy constructors,
|
| 170 |
-
# which allow for random generation of new populations
|
| 171 |
-
mutable struct Population
|
| 172 |
-
members::Array{PopMember, 1}
|
| 173 |
-
n::Integer
|
| 174 |
-
|
| 175 |
-
Population(pop::Array{PopMember, 1}) = new(pop, size(pop)[1])
|
| 176 |
-
Population(npop::Integer) = new([PopMember(genRandomTree(3)) for i=1:npop], npop)
|
| 177 |
-
Population(npop::Integer, nlength::Integer) = new([PopMember(genRandomTree(nlength)) for i=1:npop], npop)
|
| 178 |
-
|
| 179 |
-
end
|
| 180 |
|
| 181 |
-
|
| 182 |
-
function samplePop(pop::Population)::Population
|
| 183 |
-
idx = rand(1:pop.n, ns)
|
| 184 |
-
return Population(pop.members[idx])
|
| 185 |
-
end
|
| 186 |
-
|
| 187 |
-
# Sample the population, and get the best member from that sample
|
| 188 |
-
function bestOfSample(pop::Population)::PopMember
|
| 189 |
-
sample = samplePop(pop)
|
| 190 |
-
best_idx = argmin([sample.members[member].score for member=1:sample.n])
|
| 191 |
-
return sample.members[best_idx]
|
| 192 |
-
end
|
| 193 |
|
| 194 |
-
function finalizeScores(pop::Population)::Population
|
| 195 |
-
need_recalculate = batching
|
| 196 |
-
if need_recalculate
|
| 197 |
-
@inbounds @simd for member=1:pop.n
|
| 198 |
-
pop.members[member].score = scoreFunc(pop.members[member].tree)
|
| 199 |
-
end
|
| 200 |
-
end
|
| 201 |
-
return pop
|
| 202 |
-
end
|
| 203 |
|
| 204 |
-
# Return best 10 examples
|
| 205 |
-
function bestSubPop(pop::Population; topn::Integer=10)::Population
|
| 206 |
-
best_idx = sortperm([pop.members[member].score for member=1:pop.n])
|
| 207 |
-
return Population(pop.members[best_idx[1:topn]])
|
| 208 |
-
end
|
| 209 |
|
| 210 |
# Pass through the population several times, replacing the oldest
|
| 211 |
# with the fittest of a small subsample
|
|
|
|
| 156 |
return PopMember(tree, afterLoss)
|
| 157 |
end
|
| 158 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
+
include("Population.jl")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
# Pass through the population several times, replacing the oldest
|
| 165 |
# with the fittest of a small subsample
|