|
| 1 | +using Optimization.SciMLBase, LBFGSB |
| 2 | + |
| 3 | +""" |
| 4 | +$(TYPEDEF) |
| 5 | +
|
| 6 | +[L-BFGS-B](https://en.wikipedia.org/wiki/Limited-memory_BFGS#L-BFGS-B) Nonlinear Optimization Code from [LBFGSB](https://github.com/Gnimuc/LBFGSB.jl/tree/master). |
| 7 | +It is a quasi-Newton optimization algorithm that supports bounds. |
| 8 | +
|
| 9 | +References |
| 10 | +
|
| 11 | +- R. H. Byrd, P. Lu and J. Nocedal. A Limited Memory Algorithm for Bound Constrained Optimization, (1995), SIAM Journal on Scientific and Statistical Computing , 16, 5, pp. 1190-1208. |
| 12 | +- C. Zhu, R. H. Byrd and J. Nocedal. L-BFGS-B: Algorithm 778: L-BFGS-B, FORTRAN routines for large scale bound constrained optimization (1997), ACM Transactions on Mathematical Software, Vol 23, Num. 4, pp. 550 - 560. |
| 13 | +- J.L. Morales and J. Nocedal. L-BFGS-B: Remark on Algorithm 778: L-BFGS-B, FORTRAN routines for large scale bound constrained optimization (2011), to appear in ACM Transactions on Mathematical Software. |
| 14 | +""" |
| 15 | +@kwdef struct LBFGS |
| 16 | + m::Int = 10 |
| 17 | +end |
| 18 | + |
| 19 | +SciMLBase.supports_opt_cache_interface(::LBFGS) = true |
| 20 | +SciMLBase.allowsbounds(::LBFGS) = true |
| 21 | +# SciMLBase.requiresgradient(::LBFGS) = true |
| 22 | + |
| 23 | +function SciMLBase.__init(prob::SciMLBase.OptimizationProblem, |
| 24 | + opt::LBFGS, |
| 25 | + data = Optimization.DEFAULT_DATA; save_best = true, |
| 26 | + callback = (args...) -> (false), |
| 27 | + progress = false, kwargs...) |
| 28 | + return OptimizationCache(prob, opt, data; save_best, callback, progress, |
| 29 | + kwargs...) |
| 30 | +end |
| 31 | + |
| 32 | +function SciMLBase.__solve(cache::OptimizationCache{ |
| 33 | + F, |
| 34 | + RC, |
| 35 | + LB, |
| 36 | + UB, |
| 37 | + LC, |
| 38 | + UC, |
| 39 | + S, |
| 40 | + O, |
| 41 | + D, |
| 42 | + P, |
| 43 | + C |
| 44 | +}) where { |
| 45 | + F, |
| 46 | + RC, |
| 47 | + LB, |
| 48 | + UB, |
| 49 | + LC, |
| 50 | + UC, |
| 51 | + S, |
| 52 | + O <: |
| 53 | + LBFGS, |
| 54 | + D, |
| 55 | + P, |
| 56 | + C |
| 57 | +} |
| 58 | + if cache.data != Optimization.DEFAULT_DATA |
| 59 | + maxiters = length(cache.data) |
| 60 | + data = cache.data |
| 61 | + else |
| 62 | + maxiters = Optimization._check_and_convert_maxiters(cache.solver_args.maxiters) |
| 63 | + data = Optimization.take(cache.data, maxiters) |
| 64 | + end |
| 65 | + |
| 66 | + local x |
| 67 | + |
| 68 | + _loss = function (θ) |
| 69 | + x = cache.f(θ, cache.p) |
| 70 | + opt_state = Optimization.OptimizationState(u = θ, objective = x[1]) |
| 71 | + if cache.callback(opt_state, x...) |
| 72 | + error("Optimization halted by callback.") |
| 73 | + end |
| 74 | + return x[1] |
| 75 | + end |
| 76 | + |
| 77 | + t0 = time() |
| 78 | + if cache.lb !== nothing && cache.ub !== nothing |
| 79 | + res = lbfgsb(_loss, cache.f.grad, cache.u0; m = cache.opt.m, maxiter = maxiters, |
| 80 | + lb = cache.lb, ub = cache.ub) |
| 81 | + else |
| 82 | + res = lbfgsb(_loss, cache.f.grad, cache.u0; m = cache.opt.m, maxiter = maxiters) |
| 83 | + end |
| 84 | + |
| 85 | + t1 = time() |
| 86 | + stats = Optimization.OptimizationStats(; iterations = maxiters, |
| 87 | + time = t1 - t0, fevals = maxiters, gevals = maxiters) |
| 88 | + |
| 89 | + return SciMLBase.build_solution(cache, cache.opt, res[2], res[1], stats = stats) |
| 90 | +end |
0 commit comments