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

Use Optim's iterator interface #4

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
20 changes: 13 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,22 @@ notifications:
# Do not build my PR twice. Use the build on branch.
if: NOT (type == pull_request AND head_repo == tkf/SteadyStateFit.jl)

before_install:
- unset JULIA_PROJECT
- julia -e 'using Pkg; pkg"add https://github.com/tkf/Run.jl"'
install:
- julia -e 'using Run; Run.prepare_test()'
script:
- julia -e 'using Run; Run.test()'
after_success:
- julia -e 'using Pkg; Pkg.add("Coverage"); using Coverage; Codecov.submit(process_folder())'
- julia -e 'using Pkg; Pkg.add("Coverage"); using Coverage; Coveralls.submit(process_folder())'
- julia -e 'using Run; Run.after_success_test()'

jobs:
include:
- stage: Documentation
julia: 1.2
script: julia --project=docs --color=yes -e '
using Pkg;
Pkg.develop(PackageSpec(path=pwd()));
Pkg.instantiate();
include("docs/make.jl");'
install:
- julia -e 'using Run; Run.prepare_docs()'
script:
- julia -e 'using Run; Run.docs()'
after_success: skip
31 changes: 10 additions & 21 deletions src/solvers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,21 @@ function solve(
x0 = get(sso.p, sso.parameterlens);
kwargs...,
)
return Optim.optimize(
iter = Optim.optimizing(
Optim.only_fg!(sso),
x0,
method,
setupoptions(sso, options);
options;
kwargs...,
)
return Optim.OptimizationResults(_solve!(iter, sso))
end

# A hacky solution: To use the steady state of the last iteration,
# `updatesteadystates!` is called via `callback` option.

function setupoptions(sso::SteadyStateObjective, options::Optim.Options)
@assert options.show_every == 1
options = @set options.extended_trace = true
options = @set options.callback = StateUpdater(sso, options.callback)
return options
end

struct StateUpdater{T, C}
sso::T
callback::C
end

function (su::StateUpdater)(os)
# os :: OptimizationState
updatesteadystates!(su.sso, os.metadata["x"])
su.callback === nothing ? false : su.callback(os)
function _solve!(iter, sso)
local istate
for istate′ in iter
istate = istate′
updatesteadystates!(sso, Optim.minimizer(istate))
end
return istate
end
Loading