Skip to content

Commit 8310517

Browse files
Merge pull request #726 from SciML/lbfgsb
Use lbfgsb as the default solver
2 parents efe6038 + 534d43a commit 8310517

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
77
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
88
ConsoleProgressMonitor = "88cd18e8-d9cc-4ea6-8889-5259c0d15c8b"
99
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
10+
LBFGSB = "5be7bae1-8223-5378-bac3-9e7378a2f6e6"
1011
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1112
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
1213
LoggingExtras = "e6f89c97-d47a-5376-807f-9c37f3926c36"
@@ -24,6 +25,7 @@ ADTypes = "0.2.5"
2425
ArrayInterface = "7.6"
2526
ConsoleProgressMonitor = "0.1.1"
2627
DocStringExtensions = "0.9"
28+
LBFGSB = "0.4.1"
2729
LinearAlgebra = "1.10"
2830
Logging = "1.10"
2931
LoggingExtras = "0.4, 1"

src/Optimization.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export ObjSense, MaxSense, MinSense
2323

2424
include("utils.jl")
2525
include("state.jl")
26+
include("lbfgsb.jl")
2627

2728
export solve
2829

src/lbfgsb.jl

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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

test/lbfgsb.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Optimization
2+
using ForwardDiff, Zygote, ReverseDiff, FiniteDiff, Tracker
3+
using ModelingToolkit, Enzyme, Random
4+
5+
x0 = zeros(2)
6+
rosenbrock(x, p = nothing) = (1 - x[1])^2 + 100 * (x[2] - x[1]^2)^2
7+
l1 = rosenbrock(x0)
8+
9+
optf = OptimizationFunction(rosenbrock, AutoForwardDiff())
10+
prob = OptimizationProblem(optf, x0)
11+
res = solve(prob, Optimization.LBFGS(), maxiters = 100)
12+
13+
@test res.u[1.0, 1.0] atol=1e-3
14+
15+
optf = OptimizationFunction(rosenbrock, AutoZygote())
16+
prob = OptimizationProblem(optf, x0, lb = [0.0, 0.0], ub = [0.3, 0.3])
17+
res = solve(prob, Optimization.LBFGS(), maxiters = 100)
18+
19+
@test res.u[0.3, 0.09] atol=1e-3

0 commit comments

Comments
 (0)