Spaces:
Sleeping
Sleeping
Commit
·
a79a3fb
1
Parent(s):
515c017
Refactored optimization too, performance normal
Browse files- julia/optimization.jl +47 -0
- julia/sr.jl +0 -46
julia/optimization.jl
CHANGED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Proxy function for optimization
|
| 2 |
+
function optFunc(x::Array{Float32, 1}, tree::Node)::Float32
|
| 3 |
+
setConstants(tree, x)
|
| 4 |
+
return scoreFunc(tree)
|
| 5 |
+
end
|
| 6 |
+
|
| 7 |
+
# Use Nelder-Mead to optimize the constants in an equation
|
| 8 |
+
function optimizeConstants(member::PopMember)::PopMember
|
| 9 |
+
nconst = countConstants(member.tree)
|
| 10 |
+
if nconst == 0
|
| 11 |
+
return member
|
| 12 |
+
end
|
| 13 |
+
x0 = getConstants(member.tree)
|
| 14 |
+
f(x::Array{Float32,1})::Float32 = optFunc(x, member.tree)
|
| 15 |
+
if size(x0)[1] == 1
|
| 16 |
+
algorithm = Optim.Newton
|
| 17 |
+
else
|
| 18 |
+
algorithm = Optim.NelderMead
|
| 19 |
+
end
|
| 20 |
+
|
| 21 |
+
try
|
| 22 |
+
result = Optim.optimize(f, x0, algorithm(), Optim.Options(iterations=100))
|
| 23 |
+
# Try other initial conditions:
|
| 24 |
+
for i=1:nrestarts
|
| 25 |
+
tmpresult = Optim.optimize(f, x0 .* (1f0 .+ 5f-1*randn(Float32, size(x0)[1])), algorithm(), Optim.Options(iterations=100))
|
| 26 |
+
if tmpresult.minimum < result.minimum
|
| 27 |
+
result = tmpresult
|
| 28 |
+
end
|
| 29 |
+
end
|
| 30 |
+
|
| 31 |
+
if Optim.converged(result)
|
| 32 |
+
setConstants(member.tree, result.minimizer)
|
| 33 |
+
member.score = convert(Float32, result.minimum)
|
| 34 |
+
member.birth = getTime()
|
| 35 |
+
else
|
| 36 |
+
setConstants(member.tree, x0)
|
| 37 |
+
end
|
| 38 |
+
catch error
|
| 39 |
+
# Fine if optimization encountered domain error, just return x0
|
| 40 |
+
if isa(error, AssertionError)
|
| 41 |
+
setConstants(member.tree, x0)
|
| 42 |
+
else
|
| 43 |
+
throw(error)
|
| 44 |
+
end
|
| 45 |
+
end
|
| 46 |
+
return member
|
| 47 |
+
end
|
julia/sr.jl
CHANGED
|
@@ -42,53 +42,7 @@ include("regEvolCycle.jl")
|
|
| 42 |
include("run.jl")
|
| 43 |
|
| 44 |
include("optimization.jl")
|
| 45 |
-
# Proxy function for optimization
|
| 46 |
-
function optFunc(x::Array{Float32, 1}, tree::Node)::Float32
|
| 47 |
-
setConstants(tree, x)
|
| 48 |
-
return scoreFunc(tree)
|
| 49 |
-
end
|
| 50 |
-
|
| 51 |
-
# Use Nelder-Mead to optimize the constants in an equation
|
| 52 |
-
function optimizeConstants(member::PopMember)::PopMember
|
| 53 |
-
nconst = countConstants(member.tree)
|
| 54 |
-
if nconst == 0
|
| 55 |
-
return member
|
| 56 |
-
end
|
| 57 |
-
x0 = getConstants(member.tree)
|
| 58 |
-
f(x::Array{Float32,1})::Float32 = optFunc(x, member.tree)
|
| 59 |
-
if size(x0)[1] == 1
|
| 60 |
-
algorithm = Optim.Newton
|
| 61 |
-
else
|
| 62 |
-
algorithm = Optim.NelderMead
|
| 63 |
-
end
|
| 64 |
|
| 65 |
-
try
|
| 66 |
-
result = Optim.optimize(f, x0, algorithm(), Optim.Options(iterations=100))
|
| 67 |
-
# Try other initial conditions:
|
| 68 |
-
for i=1:nrestarts
|
| 69 |
-
tmpresult = Optim.optimize(f, x0 .* (1f0 .+ 5f-1*randn(Float32, size(x0)[1])), algorithm(), Optim.Options(iterations=100))
|
| 70 |
-
if tmpresult.minimum < result.minimum
|
| 71 |
-
result = tmpresult
|
| 72 |
-
end
|
| 73 |
-
end
|
| 74 |
-
|
| 75 |
-
if Optim.converged(result)
|
| 76 |
-
setConstants(member.tree, result.minimizer)
|
| 77 |
-
member.score = convert(Float32, result.minimum)
|
| 78 |
-
member.birth = getTime()
|
| 79 |
-
else
|
| 80 |
-
setConstants(member.tree, x0)
|
| 81 |
-
end
|
| 82 |
-
catch error
|
| 83 |
-
# Fine if optimization encountered domain error, just return x0
|
| 84 |
-
if isa(error, AssertionError)
|
| 85 |
-
setConstants(member.tree, x0)
|
| 86 |
-
else
|
| 87 |
-
throw(error)
|
| 88 |
-
end
|
| 89 |
-
end
|
| 90 |
-
return member
|
| 91 |
-
end
|
| 92 |
|
| 93 |
|
| 94 |
function fullRun(niterations::Integer;
|
|
|
|
| 42 |
include("run.jl")
|
| 43 |
|
| 44 |
include("optimization.jl")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
|
| 48 |
function fullRun(niterations::Integer;
|