Skip to content

Commit 9c4f74d

Browse files
Merge pull request #809 from arismavridis/patch-1
Added precompilation for nonnegative least squares
2 parents b2e0d1d + 38471df commit 9c4f74d

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

lib/OptimizationOptimJL/Project.toml

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Optim = "429524aa-4258-5aef-a3af-852621145aeb"
88
Optimization = "7f7a1694-90dd-40f0-9382-eb1efda571ba"
99
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
1010
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
11+
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
1112

1213
[compat]
1314
Optim = "1"

lib/OptimizationOptimJL/src/OptimizationOptimJL.jl

+30
Original file line numberDiff line numberDiff line change
@@ -453,4 +453,34 @@ function SciMLBase.__solve(cache::OptimizationCache{
453453
stats = stats)
454454
end
455455

456+
using PrecompileTools
457+
PrecompileTools.@compile_workload begin
458+
459+
function obj_f(x, p)
460+
A = p[1]
461+
b = p[2]
462+
return sum((A * x - b) .^ 2)
463+
end
464+
465+
function solve_nonnegative_least_squares(A, b, solver)
466+
467+
optf = Optimization.OptimizationFunction(obj_f, Optimization.AutoForwardDiff())
468+
prob = Optimization.OptimizationProblem(optf, ones(size(A, 2)), (A, b), lb=zeros(size(A, 2)), ub=Inf * ones(size(A, 2)))
469+
x = OptimizationOptimJL.solve(prob, solver, maxiters=5000, maxtime=100)
470+
471+
return x
472+
end
473+
474+
solver_list = [OptimizationOptimJL.LBFGS(),
475+
OptimizationOptimJL.ConjugateGradient(),
476+
OptimizationOptimJL.GradientDescent(),
477+
OptimizationOptimJL.BFGS()]
478+
479+
for solver in solver_list
480+
x = solve_nonnegative_least_squares(rand(4, 4), rand(4), solver)
481+
x = solve_nonnegative_least_squares(rand(35, 35), rand(35), solver)
482+
x = solve_nonnegative_least_squares(rand(35, 10), rand(35), solver)
483+
end
484+
end
485+
456486
end

0 commit comments

Comments
 (0)