Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create more docs examples and link from README (#114)
* Add README examples to docs * Update README with docs links * Don't execute examples when on Travis
- Loading branch information
Showing
8 changed files
with
150 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,23 @@ | ||
# generate examples | ||
import Literate | ||
|
||
# We shouldn't run the examples that require Optim in Travis/CI, | ||
# because an update in LineSearches may be breaking with the | ||
# most recently tagged Optim version. | ||
if get(ENV, "CI", "") == "true" | ||
ONLYSTATIC = ["optim_linesearch.jl", "optim_initialstep.jl"] | ||
else | ||
ONLYSTATIC = ["",] | ||
end | ||
|
||
EXAMPLEDIR = joinpath(@__DIR__, "src", "examples") | ||
GENERATEDDIR = joinpath(@__DIR__, "src", "examples", "generated") | ||
for example in filter!(r"\.jl$", readdir(EXAMPLEDIR)) | ||
input = abspath(joinpath(EXAMPLEDIR, example)) | ||
script = Literate.script(input, GENERATEDDIR) | ||
code = strip(read(script, String)) | ||
mdpost(str) = replace(str, "@__CODE__" => code) | ||
Literate.markdown(input, GENERATEDDIR, postprocess = mdpost) | ||
Literate.notebook(input, GENERATEDDIR, execute = true) | ||
Literate.markdown(input, GENERATEDDIR, postprocess = mdpost, | ||
documenter = !(example in ONLYSTATIC)) | ||
Literate.notebook(input, GENERATEDDIR, execute = !(example in ONLYSTATIC)) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# # Optim initial step length guess | ||
# | ||
#src TODO: Find a way to run these with Literate when deploying via Travis | ||
#src TODO: This file must currently be run locally and not on CI, and then | ||
#src TODO: the md file must be copied over to the correct directory. | ||
#src TODO: The reason is that there may be breaking changes between Optim and LineSearches, | ||
#src TODO: so we don't want that to mess up JuliaCIBot | ||
#- | ||
#- | ||
#md # !!! tip | ||
#md # This example is also available as a Jupyter notebook: | ||
#md # [`optim_initialstep.ipynb`](@__NBVIEWER_ROOT_URL__examples/generated/optim_initialstep.ipynb) | ||
#- | ||
# | ||
# This example shows how to use the initial step length procedures | ||
# with [Optim](https://github.com/JuliaNLSolvers/Optim.jl). We solve | ||
# the Rosenbrock problem with two different procedures. | ||
# | ||
# First, run `Newton` with the (default) initial guess and line search procedures. | ||
using Optim, LineSearches | ||
import OptimTestProblems.MultivariateProblems | ||
UP = MultivariateProblems.UnconstrainedProblems | ||
prob = UP.examples["Rosenbrock"] | ||
|
||
algo_st = Newton(alphaguess = InitialStatic(), linesearch = HagerZhang()) | ||
res_st = Optim.optimize(prob.f, prob.g!, prob.h!, prob.initial_x, method=algo_st) | ||
|
||
|
||
# We can now try with the initial step length guess from Hager and Zhang. | ||
algo_hz = Newton(alphaguess = InitialHagerZhang(α0=1.0), linesearch = HagerZhang()) | ||
res_hz = Optim.optimize(prob.f, prob.g!, prob.h!, prob.initial_x, method=algo_hz) | ||
|
||
|
||
# From the result we see that this has reduced the number of function and gradient calls, but increased the number of iterations. | ||
|
||
## Test the results #src | ||
using Base.Test #src | ||
@test Optim.f_calls(res_hz) < Optim.f_calls(res_st) #src | ||
@test Optim.g_calls(res_hz) < Optim.g_calls(res_st) #src | ||
@test Optim.iterations(res_hz) > Optim.iterations(res_st) #src |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# # Optim line search | ||
# | ||
#src TODO: Find a way to run these with Literate when deploying via Travis | ||
#src TODO: This file must currently be run locally and not on CI, and then | ||
#src TODO: the md file must be copied over to the correct directory. | ||
#src TODO: The reason is that there may be breaking changes between Optim and LineSearches, | ||
#src TODO: so we don't want that to mess up JuliaCIBot | ||
#- | ||
#md # !!! tip | ||
#md # This example is also available as a Jupyter notebook: | ||
#md # [`optim_linesearch.ipynb`](@__NBVIEWER_ROOT_URL__examples/generated/optim_linesearch.ipynb) | ||
#- | ||
# | ||
# This example shows how to use `LineSearches` with | ||
# [Optim](https://github.com/JuliaNLSolvers/Optim.jl). We solve the | ||
# Rosenbrock problem with two different line search algorithms. | ||
# | ||
# First, run `Newton` with the default line search algorithm: | ||
|
||
using Optim, LineSearches | ||
import OptimTestProblems.MultivariateProblems | ||
UP = MultivariateProblems.UnconstrainedProblems | ||
prob = UP.examples["Rosenbrock"] | ||
|
||
algo_hz = Newton(linesearch = HagerZhang()) | ||
res_hz = Optim.optimize(prob.f, prob.g!, prob.h!, prob.initial_x, method=algo_hz) | ||
|
||
# Now we can try `Newton` with the cubic backtracking line search, | ||
# which reduced the number of objective and gradient calls. | ||
|
||
algo_bt3 = Newton(linesearch = BackTracking(order=3)) | ||
res_bt3 = Optim.optimize(prob.f, prob.g!, prob.h!, prob.initial_x, method=algo_bt3) | ||
|
||
|
||
## Test the results #src | ||
using Base.Test #src | ||
@test Optim.f_calls(res_bt3) < Optim.f_calls(res_hz) #src | ||
@test Optim.g_calls(res_bt3) < Optim.g_calls(res_hz) #src |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters