Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes for 0.5 function changes #164

Merged
merged 1 commit into from
Jan 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions test/brent.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
f(x) = 2x^2+3x+1
f_b(x) = 2x^2+3x+1

results = optimize(f, -2.0, 1.0, method = :brent)
results = optimize(f_b, -2.0, 1.0, method = :brent)

@assert results.converged
@assert abs(results.minimum+0.75) < 1e-7
8 changes: 0 additions & 8 deletions test/callbacks.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@

function cb(tr::OptimizationTrace)
@test tr.states[end].iteration % 3 == 0
end

function cb(os::OptimizationState)
@test os.iteration % 3 == 0
end

for method in (:nelder_mead,
:simulated_annealing)
ot_run = false
Expand Down
12 changes: 6 additions & 6 deletions test/gradient_descent.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
function f_gd(x)
function f_gd_1(x)
(x[1] - 5.0)^2
end

function g_gd(x, storage)
function g_gd_1(x, storage)
storage[1] = 2.0 * (x[1] - 5.0)
end

initial_x = [0.0]

d = DifferentiableFunction(f_gd, g_gd)
d = DifferentiableFunction(f_gd_1, g_gd_1)

results = Optim.gradient_descent(d, initial_x)
@assert isempty(results.trace.states)
Expand All @@ -17,16 +17,16 @@ results = Optim.gradient_descent(d, initial_x)

eta = 0.9

function f_gd(x)
function f_gd_2(x)
(1.0 / 2.0) * (x[1]^2 + eta * x[2]^2)
end

function g_gd(x, storage)
function g_gd_2(x, storage)
storage[1] = x[1]
storage[2] = eta * x[2]
end

d = DifferentiableFunction(f_gd, g_gd)
d = DifferentiableFunction(f_gd_2, g_gd_2)

results = Optim.gradient_descent(d, [1.0, 1.0])
@assert isempty(results.trace.states)
Expand Down
8 changes: 4 additions & 4 deletions test/nelder_mead.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ for (name, prob) in Optim.UnconstrainedProblems.examples
end
end

function f(x::Vector)
function f_nm(x::Vector)
(100.0 - x[1])^2 + x[2]^2
end

function rosenbrock(x::Vector)
function rosenbrock_nm(x::Vector)
(1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2
end

initial_x = [0.0, 0.0]

results = Optim.nelder_mead(f, initial_x)
results = Optim.nelder_mead(f_nm, initial_x)

@assert results.f_converged
@assert norm(results.minimum - [100.0, 0.0]) < 0.01
@assert length(results.trace.states) == 0

results = Optim.nelder_mead(rosenbrock, initial_x)
results = Optim.nelder_mead(rosenbrock_nm, initial_x)

@assert results.f_converged
@assert norm(results.minimum - [1.0, 1.0]) < 0.01
Expand Down
16 changes: 8 additions & 8 deletions test/newton.jl
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
using Optim

function f(x::Vector)
function f_1(x::Vector)
(x[1] - 5.0)^4
end

function g!(x::Vector, storage::Vector)
function g!_1(x::Vector, storage::Vector)
storage[1] = 4.0 * (x[1] - 5.0)^3
end

function h!(x::Vector, storage::Matrix)
function h!_1(x::Vector, storage::Matrix)
storage[1, 1] = 12.0 * (x[1] - 5.0)^2
end

d = TwiceDifferentiableFunction(f, g!, h!)
d = TwiceDifferentiableFunction(f_1, g!_1, h!_1)

results = Optim.newton(d, [0.0])
@assert length(results.trace.states) == 0
Expand All @@ -21,23 +21,23 @@ results = Optim.newton(d, [0.0])

eta = 0.9

function f(x::Vector)
function f_2(x::Vector)
(1.0 / 2.0) * (x[1]^2 + eta * x[2]^2)
end

function g!(x::Vector, storage::Vector)
function g!_2(x::Vector, storage::Vector)
storage[1] = x[1]
storage[2] = eta * x[2]
end

function h!(x::Vector, storage::Matrix)
function h!_2(x::Vector, storage::Matrix)
storage[1, 1] = 1.0
storage[1, 2] = 0.0
storage[2, 1] = 0.0
storage[2, 2] = eta
end

d = TwiceDifferentiableFunction(f, g!, h!)
d = TwiceDifferentiableFunction(f_2, g!_2, h!_2)
results = Optim.newton(d, [127.0, 921.0])
@assert length(results.trace.states) == 0
@assert results.gr_converged
Expand Down
8 changes: 4 additions & 4 deletions test/simulated_annealing.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
srand(1)

function f(x::Vector)
function f_s(x::Vector)
(x[1] - 5.0)^4
end

results = Optim.simulated_annealing(f, [0.0])
results = Optim.simulated_annealing(f_s, [0.0])
@assert norm(results.minimum - [5.0]) < 0.1

function rosenbrock(x::Vector)
function rosenbrock_s(x::Vector)
(1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2
end

results = Optim.simulated_annealing(rosenbrock, [0.0, 0.0])
results = Optim.simulated_annealing(rosenbrock_s, [0.0, 0.0])
@assert norm(results.minimum - [1.0, 1.0]) < 0.1