Skip to content

Instantly share code, notes, and snippets.

@bicycle1885
Created March 7, 2015 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bicycle1885/d0aaace2eab0ba9eec52 to your computer and use it in GitHub Desktop.
Save bicycle1885/d0aaace2eab0ba9eec52 to your computer and use it in GitHub Desktop.
Benchmark of Nelder-Mead algorithms
diff --git a/src/ANMS.jl b/src/ANMS.jl
index b235efd..5f93caa 100644
--- a/src/ANMS.jl
+++ b/src/ANMS.jl
@@ -72,6 +72,7 @@ function nelder_mead(f::Function, x₀::Vector{Float64}; iterations::Int=1_000_0
u = zeros(n)
for i in 1:n
τ = x₀[i] == 0.0 ? 0.00025 : 0.05
+ τ = 1.0
u[i] = 1.0
x = x₀ .+ τ * u
u[i] = 0.0
@@ -214,6 +215,8 @@ function nelder_mead(f::Function, x₀::Vector{Float64}; iterations::Int=1_000_0
break
end
end
+ # same as Optim.jl
+ fvalconv = domconv = sqrt(var(fvalues) * (n / (n+1))) < ftol
iter += 1
end
function quadratic(x)
x = x .- 1.0
dot(x, x)
end
function rosenbrock(x)
N = length(x)
fval = 0.0
for i in 1:div(N, 2)
fval += 100(x[2i-1]^2 - x[2i])^2 + (x[2i-1] - 1.0)^2
end
fval
end
const f = rosenbrock
const ns = [2, 5, 10, 20, 50]
const I = 10
import ANMS
let
println("--- ANMS ---")
x₀ = zeros(2)
ANMS.nelder_mead(f, x₀)
for n in ns
x₀ = zeros(n)
println("$n: ")
gc()
local fmin
@time for i in 1:I
_, fmin = ANMS.nelder_mead(f, x₀, iterations=1_000_000)
end
@show fmin
end
println()
end
import Optim
let
println("--- Optim ---")
x₀ = zeros(2)
Optim.nelder_mead(f, x₀)
for n in ns
x₀ = zeros(n)
println("$n: ")
gc()
local fmin
@time for i in 1:I
r = Optim.nelder_mead(f, x₀, iterations=1_000_000)
fmin = r.f_minimum
end
@show fmin
end
println()
end
--- ANMS ---
2:
elapsed time: 0.010157602 seconds (157552 bytes allocated)
fmin => 0.0
5:
elapsed time: 0.00133118 seconds (889680 bytes allocated)
fmin => 2.874692204466751e-8
10:
elapsed time: 0.003661411 seconds (2564752 bytes allocated)
fmin => 5.704532655372583e-8
20:
elapsed time: 0.011579524 seconds (8133728 bytes allocated)
fmin => 5.1698486524263214e-8
50:
elapsed time: 0.068112864 seconds (42861936 bytes allocated)
fmin => 8.513696765777041e-8
--- Optim ---
2:
elapsed time: 0.017564181 seconds (348568 bytes allocated)
fmin => 9.397767141614407e-10
5:
elapsed time: 0.002295282 seconds (2651920 bytes allocated)
fmin => 1.3461476385934277e-9
10:
elapsed time: 0.016708881 seconds (20459840 bytes allocated)
fmin => 1.142383173134101e-7
20:
elapsed time: 0.115632867 seconds (108467840 bytes allocated, 38.78% gc time)
fmin => 5.944810588990531e-8
50:
elapsed time: 2.397011401 seconds (2835856800 bytes allocated, 49.88% gc time)
fmin => 3.663031017751178e-8
--- ANMS ---
2:
elapsed time: 0.009974135 seconds (146488 bytes allocated)
fmin => 7.868156252247752e-9
5:
elapsed time: 0.000866841 seconds (427600 bytes allocated)
fmin => 2.071245431037171e-8
10:
elapsed time: 0.003445336 seconds (1678768 bytes allocated)
fmin => 3.996436273244788e-8
20:
elapsed time: 0.016988347 seconds (6119008 bytes allocated)
fmin => 7.81905861025263e-8
50:
elapsed time: 0.274938347 seconds (62309152 bytes allocated, 7.80% gc time)
fmin => 8.238820707411458e-7
--- Optim ---
2:
elapsed time: 0.017885417 seconds (615528 bytes allocated)
fmin => 8.940245976539097e-10
5:
elapsed time: 0.008627849 seconds (9746640 bytes allocated)
fmin => 1.250866767091252e-9
10:
elapsed time: 0.099422094 seconds (84184160 bytes allocated, 27.03% gc time)
fmin => 0.010763954938890423
20:
elapsed time: 0.822331694 seconds (754486560 bytes allocated, 42.30% gc time)
fmin => 0.7307076508015659
50:
elapsed time: 31.874317729 seconds (36538050560 bytes allocated, 51.20% gc time)
fmin => 1.1993608845052381
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment