Skip to content

Commit 82221f6

Browse files
committed
perf(OptimizationIpopt)!: cache the vectors used by GetIpoptCurrentIterate
Note that this breaking change as one needs to copy the vectors if their historical values are needed.
1 parent 778b609 commit 82221f6

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

lib/OptimizationIpopt/src/callback.jl

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ struct IpoptState
1010
alpha_du::Float64
1111
alpha_pr::Float64
1212
ls_trials::Cint
13+
u::Vector{Float64}
1314
z_L::Vector{Float64}
1415
z_U::Vector{Float64}
16+
g::Vector{Float64}
1517
lambda::Vector{Float64}
1618
end
1719

@@ -23,6 +25,21 @@ struct IpoptProgressLogger{C, P}
2325
num_cons::Int
2426
maxiters::Union{Nothing, Int}
2527
iterations::Ref{Int}
28+
# caches for GetIpoptCurrentIterate
29+
u::Vector{Float64}
30+
z_L::Vector{Float64}
31+
z_U::Vector{Float64}
32+
g::Vector{Float64}
33+
lambda::Vector{Float64}
34+
end
35+
36+
function IpoptProgressLogger(progress::Bool, callback::C, prob::P, n::Int, num_cons:::Int,
37+
maxiters::Union{Nothing, Int}, iterations::Ref{Int}) where {C, P}
38+
# Initialize caches
39+
u, z_L, z_U = zeros(n), zeros(n), zeros(n)
40+
g, lambda = zeros(num_cons), zeros(num_cons)
41+
IpoptProgressLogger(
42+
progress, callback, prob, n, num_cons, maxiters, iterations, u, z_L, z_U, g, lambda)
2643
end
2744

2845
function (cb::IpoptProgressLogger)(
@@ -38,12 +55,9 @@ function (cb::IpoptProgressLogger)(
3855
alpha_pr::Float64,
3956
ls_trials::Cint
4057
)
41-
n = cb.n
42-
m = cb.num_cons
43-
u, z_L, z_U = zeros(n), zeros(n), zeros(n)
44-
g, lambda = zeros(m), zeros(m)
4558
scaled = false
46-
Ipopt.GetIpoptCurrentIterate(cb.prob, scaled, n, u, z_L, z_U, m, g, lambda)
59+
Ipopt.GetIpoptCurrentIterate(
60+
cb.prob, scaled, cb.n, cb.u, cb.z_L, cb.z_U, cb.num_cons, cb.g, cb.lambda)
4761

4862
original = IpoptState(
4963
alg_mod,
@@ -57,9 +71,11 @@ function (cb::IpoptProgressLogger)(
5771
alpha_du,
5872
alpha_pr,
5973
ls_trials,
60-
z_L,
61-
z_U,
62-
lambda
74+
cb.u,
75+
cb.z_L,
76+
cb.z_U,
77+
cb.g,
78+
cb.lambda
6379
)
6480

6581
opt_state = Optimization.OptimizationState(;

0 commit comments

Comments
 (0)