Skip to content

Commit cf7a50e

Browse files
New Subpackage for LBFGS
1 parent e2b7cea commit cf7a50e

File tree

5 files changed

+94
-35
lines changed

5 files changed

+94
-35
lines changed

lib/OptimizationLBFGS/Project.toml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name = "OptimizationLBFGS"
2+
uuid = "22f7324a-a79d-40f2-bebe-3af60c77bd15"
3+
authors = ["paramthakkar123 <[email protected]>"]
4+
version = "0.1.0"
5+
6+
[deps]
7+
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
8+
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
9+
LBFGSB = "5be7bae1-8223-5378-bac3-9e7378a2f6e6"
10+
MLUtils = "f1d291b0-491e-4a28-83b9-f70985020b54"
11+
Optimization = "7f7a1694-90dd-40f0-9382-eb1efda571ba"
12+
OptimizationBase = "bca83a33-5cc9-4baa-983d-23429ab6bcbb"
13+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
14+
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
15+
16+
[compat]
17+
DocStringExtensions = "0.9.5"
18+
ForwardDiff = "1.0.1"
19+
LBFGSB = "0.4.1"
20+
MLUtils = "0.4.8"
21+
Optimization = "4.4.0"
22+
OptimizationBase = "2.10.0"
23+
Test = "1.11.0"
24+
Zygote = "0.7.10"

src/lbfgsb.jl renamed to lib/OptimizationLBFGS/src/OptimizationLBFGS.jl

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
using Optimization.SciMLBase, LBFGSB
1+
module OptimizationLBFGS
2+
3+
using Optimization
4+
using DocStringExtensions
5+
using LBFGSB
6+
using OptimizationBase.SciMLBase: OptimizationStats, OptimizationFunction
7+
using OptimizationBase: ReturnCode
8+
using OptimizationBase.LinearAlgebra: norm
9+
using Optimization: deduce_retcode
210

311
"""
412
$(TYPEDEF)
@@ -33,7 +41,7 @@ function task_message_to_string(task::Vector{UInt8})
3341
return String(task)
3442
end
3543

36-
function __map_optimizer_args(cache::Optimization.OptimizationCache, opt::LBFGS;
44+
function __map_optimizer_args(cache::OptimizationCache, opt::LBFGS;
3745
callback = nothing,
3846
maxiters::Union{Number, Nothing} = nothing,
3947
maxtime::Union{Number, Nothing} = nothing,
@@ -125,7 +133,7 @@ function SciMLBase.__solve(cache::OptimizationCache{
125133
cons_tmp[eq_inds] .= cons_tmp[eq_inds] - cache.lcons[eq_inds]
126134
cons_tmp[ineq_inds] .= cons_tmp[ineq_inds] .- cache.ucons[ineq_inds]
127135
opt_state = Optimization.OptimizationState(
128-
u = θ, objective = x[1], p = cache.p, iter = iter_count[])
136+
u = θ, objective = x[1])
129137
if cache.callback(opt_state, x...)
130138
error("Optimization halted by callback.")
131139
end
@@ -205,7 +213,7 @@ function SciMLBase.__solve(cache::OptimizationCache{
205213
end
206214
end
207215

208-
stats = Optimization.OptimizationStats(; iterations = maxiters,
216+
stats = OptimizationStats(; iterations = maxiters,
209217
time = 0.0, fevals = maxiters, gevals = maxiters)
210218
return SciMLBase.build_solution(
211219
cache, cache.opt, res[2], cache.f(res[2], cache.p)[1],
@@ -216,7 +224,7 @@ function SciMLBase.__solve(cache::OptimizationCache{
216224
x = cache.f(θ, cache.p)
217225
iter_count[] += 1
218226
opt_state = Optimization.OptimizationState(
219-
u = θ, objective = x[1], p = cache.p, iter = iter_count[])
227+
u = θ, objective = x[1])
220228
if cache.callback(opt_state, x...)
221229
error("Optimization halted by callback.")
222230
end
@@ -257,3 +265,5 @@ function SciMLBase.__solve(cache::OptimizationCache{
257265
retcode = opt_ret, original = optimizer)
258266
end
259267
end
268+
269+
end
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using OptimizationBase
2+
using OptimizationBase: ReturnCode
3+
using OptimizationBase.SciMLBase: OptimizationFunction, OptimizationProblem
4+
using ForwardDiff, Zygote
5+
using OptimizationLBFGS
6+
using MLUtils
7+
using LBFGSB
8+
using Test
9+
10+
x0 = zeros(2)
11+
rosenbrock(x, p = nothing) = (1 - x[1])^2 + 100 * (x[2] - x[1]^2)^2
12+
l1 = rosenbrock(x0)
13+
14+
optf = OptimizationFunction(rosenbrock, OptimizationBase.AutoForwardDiff())
15+
prob = OptimizationProblem(optf, x0)
16+
@time res = solve(prob, OptimizationLBFGS.LBFGS(), maxiters = 100)
17+
@test res.retcode == ReturnCode.Success
18+
19+
prob = OptimizationProblem(optf, x0, lb = [-1.0, -1.0], ub = [1.0, 1.0])
20+
@time res = solve(prob, OptimizationLBFGS.LBFGS(), maxiters = 100)
21+
@test res.retcode == ReturnCode.Success
22+
23+
function con2_c(res, x, p)
24+
res .= [x[1]^2 + x[2]^2, (x[2] * sin(x[1]) + x[1]) - 5]
25+
end
26+
27+
optf = OptimizationFunction(rosenbrock, OptimizationBase.AutoZygote(), cons = con2_c)
28+
prob = OptimizationProblem(optf, x0, lcons = [1.0, -Inf],
29+
ucons = [1.0, 0.0], lb = [-1.0, -1.0],
30+
ub = [1.0, 1.0])
31+
@time res = solve(prob, OptimizationLBFGS.LBFGS(), maxiters = 100)
32+
@test res.retcode == SciMLBase.ReturnCode.Success
33+
34+
x0 = (-pi):0.001:pi
35+
y0 = sin.(x0)
36+
data = MLUtils.DataLoader((x0, y0), batchsize = 126)
37+
function loss(coeffs, data)
38+
ypred = [evalpoly(data[1][i], coeffs) for i in eachindex(data[1])]
39+
return sum(abs2, ypred .- data[2])
40+
end
41+
42+
function cons1(res, coeffs, p = nothing)
43+
res[1] = coeffs[1] * coeffs[5] - 1
44+
return nothing
45+
end
46+
47+
optf = OptimizationFunction(loss, AutoSparseForwardDiff(), cons = cons1)
48+
callback = (st, l) -> (@show l; return false)
49+
50+
initpars = rand(5)
51+
l0 = optf(initpars, (x0, y0))
52+
prob = OptimizationProblem(optf, initpars, (x0, y0), lcons = [-Inf], ucons = [0.5],
53+
lb = [-10.0, -10.0, -10.0, -10.0, -10.0], ub = [10.0, 10.0, 10.0, 10.0, 10.0])
54+
opt1 = solve(prob, OptimizationLBFGS.LBFGS(), maxiters = 1000, callback = callback)
55+
@test opt1.objective < l0

src/Optimization.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ export ObjSense, MaxSense, MinSense
2222

2323
include("utils.jl")
2424
include("state.jl")
25-
include("lbfgsb.jl")
2625
include("sophia.jl")
2726
include("auglag.jl")
2827

test/native.jl

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,6 @@
11
using Optimization
22
using ForwardDiff, Zygote, ReverseDiff, FiniteDiff
33
using Test
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-
@time res = solve(prob, Optimization.LBFGS(), maxiters = 100)
12-
@test res.retcode == Optimization.SciMLBase.ReturnCode.Success
13-
14-
prob = OptimizationProblem(optf, x0, lb = [-1.0, -1.0], ub = [1.0, 1.0])
15-
@time res = solve(prob, Optimization.LBFGS(), maxiters = 100)
16-
@test res.retcode == Optimization.SciMLBase.ReturnCode.Success
17-
18-
function con2_c(res, x, p)
19-
res .= [x[1]^2 + x[2]^2, (x[2] * sin(x[1]) + x[1]) - 5]
20-
end
21-
22-
optf = OptimizationFunction(rosenbrock, AutoZygote(), cons = con2_c)
23-
prob = OptimizationProblem(optf, x0, lcons = [1.0, -Inf],
24-
ucons = [1.0, 0.0], lb = [-1.0, -1.0],
25-
ub = [1.0, 1.0])
26-
@time res = solve(prob, Optimization.LBFGS(), maxiters = 100)
27-
@test res.retcode == SciMLBase.ReturnCode.Success
28-
294
using MLUtils, OptimizationOptimisers
305

316
x0 = (-pi):0.001:pi
@@ -46,10 +21,6 @@ callback = (st, l) -> (@show l; return false)
4621

4722
initpars = rand(5)
4823
l0 = optf(initpars, (x0, y0))
49-
prob = OptimizationProblem(optf, initpars, (x0, y0), lcons = [-Inf], ucons = [0.5],
50-
lb = [-10.0, -10.0, -10.0, -10.0, -10.0], ub = [10.0, 10.0, 10.0, 10.0, 10.0])
51-
opt1 = solve(prob, Optimization.LBFGS(), maxiters = 1000, callback = callback)
52-
@test opt1.objective < l0
5324

5425
prob = OptimizationProblem(optf, initpars, data, lcons = [-Inf], ucons = [1],
5526
lb = [-10.0, -10.0, -10.0, -10.0, -10.0], ub = [10.0, 10.0, 10.0, 10.0, 10.0])

0 commit comments

Comments
 (0)