Skip to content
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
2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Initial formatting with JuliaFormatter
255e0da95fbaf2c11193246cab41b541b15f9c20
40 changes: 19 additions & 21 deletions benchmark/benchmarks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ function gabor(x, phi)
end

tests = (;
first_order=(;
loss_generator=(X, Y) -> (phi -> sum(i -> (gabor(X[i], phi) - Y[i])^2, eachindex(X, Y))),
init_phi=() -> [1.0, 6.0],
true_phi=() -> [0.0, 16.6],
domain=() -> LinRange(-15.0, 15.0, 64),
options=() -> Optim.Options(iterations=100),
optimizers=[
first_order = (;
loss_generator = (X, Y) ->
(phi -> sum(i -> (gabor(X[i], phi) - Y[i])^2, eachindex(X, Y))),
init_phi = () -> [1.0, 6.0],
true_phi = () -> [0.0, 16.6],
domain = () -> LinRange(-15.0, 15.0, 64),
options = () -> Optim.Options(iterations = 100),
optimizers = [
:Adam,
:AdaMax,
:BFGS,
Expand All @@ -34,22 +35,19 @@ for order in keys(tests), optimizer in tests[order].optimizers
isdefined(@__MODULE__, optimizer) || continue
SUITE["multivariate"]["solvers"][order][optimizer] = @benchmarkable(
optimize(loss, init_phi, opt, options),
setup = (
test = $(tests[order]);
init_phi = test.init_phi();
true_phi = test.true_phi();
opt = $(eval(optimizer))();
options = test.options();
rng = MersenneTwister(0);
X = collect(test.domain());
noise = 0.1 * randn(rng, length(X));
Y = map(i -> gabor(X[i], true_phi) + noise[i], eachindex(X, noise));
loss = test.loss_generator(X, Y);
seed!(1)
)
setup = (test = $(tests[order]);
init_phi = test.init_phi();
true_phi = test.true_phi();
opt = $(eval(optimizer))();
options = test.options();
rng = MersenneTwister(0);
X = collect(test.domain());
noise = 0.1 * randn(rng, length(X));
Y = map(i -> gabor(X[i], true_phi) + noise[i], eachindex(X, noise));
loss = test.loss_generator(X, Y);
seed!(1))
)
end


results = run(SUITE)

8 changes: 6 additions & 2 deletions docs/generate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ for example in filter!(x -> endswith(x, ".jl"), readdir(EXAMPLEDIR))
script = Literate.script(input, GENERATEDDIR)
code = strip(read(script, String))
mdpost(str) = replace(str, "@__CODE__" => code)
Literate.markdown(input, GENERATEDDIR, postprocess = mdpost,
documenter = !(example in ONLYSTATIC))
Literate.markdown(
input,
GENERATEDDIR,
postprocess = mdpost,
documenter = !(example in ONLYSTATIC),
)
Literate.notebook(input, GENERATEDDIR, execute = !(example in ONLYSTATIC))
end
87 changes: 44 additions & 43 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,61 +7,62 @@ using Optim
using Documenter
using DocumenterCitations

bib = CitationBibliography(joinpath(@__DIR__, "src", "refs.bib"); style=:authoryear)
bib = CitationBibliography(joinpath(@__DIR__, "src", "refs.bib"); style = :authoryear)

# use include("Rosenbrock.jl") etc
# Generate examples
include("generate.jl")

cp(joinpath(@__DIR__, "..", "LICENSE.md"),
joinpath(@__DIR__, "src", "LICENSE.md"); force = true)
cp(
joinpath(@__DIR__, "..", "LICENSE.md"),
joinpath(@__DIR__, "src", "LICENSE.md");
force = true,
)

#run('mv ../CONTRIBUTING.md ./dev/CONTRIBUTING.md') # TODO: Should we use the $odir/CONTRIBUTING.md file instead?
makedocs(
doctest = false,
sitename = "Optim",
pages = [
"Home" => "index.md",
"Tutorials" => [
"Minimizing a function" => "user/minimization.md",
"Gradients and Hessians" => "user/gradientsandhessians.md",
"Configurable Options" => "user/config.md",
"Linesearch" => "algo/linesearch.md",
"Algorithm choice" => "user/algochoice.md",
"Preconditioners" => "algo/precondition.md",
"Complex optimization" => "algo/complex.md",
"Manifolds" => "algo/manifolds.md",
"Tips and tricks" => "user/tipsandtricks.md",
"Interior point Newton" => "examples/generated/ipnewton_basics.md",
"Maximum likelihood estimation" => "examples/generated/maxlikenlm.md",
"Conditional maximum likelihood estimation" => "examples/generated/rasch.md",
"Home" => "index.md",
"Tutorials" => [
"Minimizing a function" => "user/minimization.md",
"Gradients and Hessians" => "user/gradientsandhessians.md",
"Configurable Options" => "user/config.md",
"Linesearch" => "algo/linesearch.md",
"Algorithm choice" => "user/algochoice.md",
"Preconditioners" => "algo/precondition.md",
"Complex optimization" => "algo/complex.md",
"Manifolds" => "algo/manifolds.md",
"Tips and tricks" => "user/tipsandtricks.md",
"Interior point Newton" => "examples/generated/ipnewton_basics.md",
"Maximum likelihood estimation" => "examples/generated/maxlikenlm.md",
"Conditional maximum likelihood estimation" => "examples/generated/rasch.md",
],
"Algorithms" => [
"Gradient Free" => [
"Nelder Mead" => "algo/nelder_mead.md",
"Simulated Annealing" => "algo/simulated_annealing.md",
"Simulated Annealing w/ bounds" => "algo/samin.md",
"Particle Swarm" => "algo/particle_swarm.md",
],
"Gradient Required" => [
"Adam and AdaMax" => "algo/adam_adamax.md",
"Conjugate Gradient" => "algo/cg.md",
"Gradient Descent" => "algo/gradientdescent.md",
"(L-)BFGS" => "algo/lbfgs.md",
"Acceleration" => "algo/ngmres.md",
],
"Hessian Required" => [
"Newton" => "algo/newton.md",
"Newton with Trust Region" => "algo/newton_trust_region.md",
"Interior point Newton" => "algo/ipnewton.md",
],
],
"Algorithms" => [
"Gradient Free" => [
"Nelder Mead" => "algo/nelder_mead.md",
"Simulated Annealing" => "algo/simulated_annealing.md",
"Simulated Annealing w/ bounds" => "algo/samin.md",
"Particle Swarm" => "algo/particle_swarm.md",
],
"Gradient Required" => [
"Adam and AdaMax" => "algo/adam_adamax.md",
"Conjugate Gradient" => "algo/cg.md",
"Gradient Descent" => "algo/gradientdescent.md",
"(L-)BFGS" => "algo/lbfgs.md",
"Acceleration" => "algo/ngmres.md",
],
"Hessian Required" => [
"Newton" => "algo/newton.md",
"Newton with Trust Region" => "algo/newton_trust_region.md",
"Interior point Newton" => "algo/ipnewton.md",
]
],
"Contributing" => "dev/contributing.md",
"License" => "LICENSE.md",
],
"Contributing" => "dev/contributing.md",
"License" => "LICENSE.md",
],
plugins = [bib],
)

deploydocs(
repo = "github.com/JuliaNLSolvers/Optim.jl.git",
)
deploydocs(repo = "github.com/JuliaNLSolvers/Optim.jl.git")
71 changes: 37 additions & 34 deletions docs/src/examples/ipnewton_basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ import NLSolversBase: clear! #hide
# The objective and its derivatives are given by


fun(x) = (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2
fun(x) = (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2

function fun_grad!(g, x)
g[1] = -2.0 * (1.0 - x[1]) - 400.0 * (x[2] - x[1]^2) * x[1]
g[2] = 200.0 * (x[2] - x[1]^2)
g[1] = -2.0 * (1.0 - x[1]) - 400.0 * (x[2] - x[1]^2) * x[1]
g[2] = 200.0 * (x[2] - x[1]^2)
end

function fun_hess!(h, x)
h[1, 1] = 2.0 - 400.0 * x[2] + 1200.0 * x[1]^2
h[1, 2] = -400.0 * x[1]
h[2, 1] = -400.0 * x[1]
h[2, 2] = 200.0
h[1, 1] = 2.0 - 400.0 * x[2] + 1200.0 * x[1]^2
h[1, 2] = -400.0 * x[1]
h[2, 1] = -400.0 * x[1]
h[2, 2] = 200.0
end;

# ## Optimization interface
Expand All @@ -68,7 +68,8 @@ end;
x0 = [0.0, 0.0]
df = TwiceDifferentiable(fun, fun_grad!, fun_hess!, x0)

lx = [-0.5, -0.5]; ux = [0.5, 0.5]
lx = [-0.5, -0.5];
ux = [0.5, 0.5];
dfc = TwiceDifferentiableConstraints(lx, ux)

res = optimize(df, dfc, x0, IPNewton())
Expand Down Expand Up @@ -96,15 +97,17 @@ res = optimize(df, dfc, x0, IPNewton())
# `Inf` bounds or empty arrays.
# **Note that we must pass the correct type information to the empty `lx` and `ux`**

lx = fill(-Inf, 2); ux = fill(Inf, 2)
lx = fill(-Inf, 2);
ux = fill(Inf, 2);
dfc = TwiceDifferentiableConstraints(lx, ux)

clear!(df)
res = optimize(df, dfc, x0, IPNewton())
@test Optim.converged(res) #src
@test Optim.minimum(res) < 0.0 + sqrt(eps()) #src

lx = Float64[]; ux = Float64[]
lx = Float64[];
ux = Float64[];
dfc = TwiceDifferentiableConstraints(lx, ux)

clear!(df)
Expand Down Expand Up @@ -138,13 +141,13 @@ res = optimize(df, dfc, x0, IPNewton())

con_c!(c, x) = (c[1] = x[1]^2 + x[2]^2; c)
function con_jacobian!(J, x)
J[1,1] = 2*x[1]
J[1,2] = 2*x[2]
J[1, 1] = 2 * x[1]
J[1, 2] = 2 * x[2]
J
end
function con_h!(h, x, λ)
h[1,1] += λ[1]*2
h[2,2] += λ[1]*2
h[1, 1] += λ[1] * 2
h[2, 2] += λ[1] * 2
end;

# **Note that `con_h!` adds the `λ`-weighted Hessian value of each
Expand All @@ -154,10 +157,11 @@ end;
# We can then optimize the Rosenbrock function inside the ball of radius
# ``0.5``.

lx = Float64[]; ux = Float64[]
lc = [-Inf]; uc = [0.5^2]
dfc = TwiceDifferentiableConstraints(con_c!, con_jacobian!, con_h!,
lx, ux, lc, uc)
lx = Float64[];
ux = Float64[];
lc = [-Inf];
uc = [0.5^2];
dfc = TwiceDifferentiableConstraints(con_c!, con_jacobian!, con_h!, lx, ux, lc, uc)
res = optimize(df, dfc, x0, IPNewton())
@test Optim.converged(res) #src
@test Optim.minimum(res) ≈ 0.2966215688829263 #src
Expand All @@ -167,8 +171,7 @@ res = optimize(df, dfc, x0, IPNewton())
# inner and outer radii ``0.1`` and ``0.5`` respectively.

lc = [0.1^2]
dfc = TwiceDifferentiableConstraints(con_c!, con_jacobian!, con_h!,
lx, ux, lc, uc)
dfc = TwiceDifferentiableConstraints(con_c!, con_jacobian!, con_h!, lx, ux, lc, uc)
res = optimize(df, dfc, x0, IPNewton())
@test Optim.converged(res) #src
@test Optim.minimum(res) ≈ 0.2966215688829255 #src
Expand All @@ -189,41 +192,41 @@ res = optimize(df, dfc, x0, IPNewton())

function con2_c!(c, x)
c[1] = x[1]^2 + x[2]^2 ## First constraint
c[2] = x[2]*sin(x[1])-x[1] ## Second constraint
c[2] = x[2] * sin(x[1]) - x[1] ## Second constraint
c
end
function con2_jacobian!(J, x)
## First constraint
J[1,1] = 2*x[1]
J[1,2] = 2*x[2]
J[1, 1] = 2 * x[1]
J[1, 2] = 2 * x[2]
## Second constraint
J[2,1] = x[2]*cos(x[1])-1.0
J[2,2] = sin(x[1])
J[2, 1] = x[2] * cos(x[1]) - 1.0
J[2, 2] = sin(x[1])
J
end
function con2_h!(h, x, λ)
## First constraint
h[1,1] += λ[1]*2
h[2,2] += λ[1]*2
h[1, 1] += λ[1] * 2
h[2, 2] += λ[1] * 2
## Second constraint
h[1,1] += λ[2]*x[2]*-sin(x[1])
h[1,2] += λ[2]*cos(x[1])
h[1, 1] += λ[2] * x[2] * -sin(x[1])
h[1, 2] += λ[2] * cos(x[1])
## Symmetrize h
h[2,1] = h[1,2]
h[2, 1] = h[1, 2]
h
end;

# We generate the constraint objects and call `IPNewton` with
# initial guess ``x_0 = (0.25,0.25)``.

x0 = [0.25, 0.25]
lc = [-Inf, 0.0]; uc = [0.5^2, 0.0]
dfc = TwiceDifferentiableConstraints(con2_c!, con2_jacobian!, con2_h!,
lx, ux, lc, uc)
lc = [-Inf, 0.0];
uc = [0.5^2, 0.0];
dfc = TwiceDifferentiableConstraints(con2_c!, con2_jacobian!, con2_h!, lx, ux, lc, uc)
res = optimize(df, dfc, x0, IPNewton())
@test Optim.converged(res) #src
@test Optim.minimum(res) ≈ 1.0 #src
@test isapprox(Optim.minimizer(res), zeros(2), atol=sqrt(eps())) #src
@test isapprox(Optim.minimizer(res), zeros(2), atol = sqrt(eps())) #src

#md # ## [Plain Program](@id ipnewton_basics-plain-program)
#md #
Expand Down
Loading
Loading