Skip to content
This repository was archived by the owner on Aug 25, 2025. It is now read-only.

Commit f0a527b

Browse files
Remove data from cache
1 parent a7c5a89 commit f0a527b

File tree

2 files changed

+81
-13
lines changed

2 files changed

+81
-13
lines changed

src/cache.jl

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ struct AnalysisResults
55
constraints::Union{Nothing, Vector{AnalysisResult}}
66
end
77

8-
struct OptimizationCache{F, RC, LB, UB, LC, UC, S, O, D, P, C, M} <:
8+
struct OptimizationCache{F, RC, LB, UB, LC, UC, S, O, P, C, M} <:
99
SciMLBase.AbstractOptimizationCache
1010
f::F
1111
reinit_cache::RC
@@ -15,15 +15,14 @@ struct OptimizationCache{F, RC, LB, UB, LC, UC, S, O, D, P, C, M} <:
1515
ucons::UC
1616
sense::S
1717
opt::O
18-
data::D
1918
progress::P
2019
callback::C
2120
manifold::M
2221
analysis_results::AnalysisResults
2322
solver_args::NamedTuple
2423
end
2524

26-
function OptimizationCache(prob::SciMLBase.OptimizationProblem, opt, data = DEFAULT_DATA;
25+
function OptimizationCache(prob::SciMLBase.OptimizationProblem, opt;
2726
callback = DEFAULT_CALLBACK,
2827
maxiters::Union{Number, Nothing} = nothing,
2928
maxtime::Union{Number, Nothing} = nothing,
@@ -150,21 +149,20 @@ function OptimizationCache(prob::SciMLBase.OptimizationProblem, opt, data = DEFA
150149

151150
return OptimizationCache(f, reinit_cache, prob.lb, prob.ub, prob.lcons,
152151
prob.ucons, prob.sense,
153-
opt, data, progress, callback, manifold, AnalysisResults(obj_res, cons_res),
152+
opt, progress, callback, manifold, AnalysisResults(obj_res, cons_res),
154153
merge((; maxiters, maxtime, abstol, reltol),
155154
NamedTuple(kwargs)))
156155
end
157156

158-
function SciMLBase.__init(prob::SciMLBase.OptimizationProblem, opt,
159-
data = DEFAULT_DATA;
157+
function SciMLBase.__init(prob::SciMLBase.OptimizationProblem, opt;
160158
callback = DEFAULT_CALLBACK,
161159
maxiters::Union{Number, Nothing} = nothing,
162160
maxtime::Union{Number, Nothing} = nothing,
163161
abstol::Union{Number, Nothing} = nothing,
164162
reltol::Union{Number, Nothing} = nothing,
165163
progress = false,
166164
kwargs...)
167-
return OptimizationCache(prob, opt, data; maxiters, maxtime, abstol, callback,
165+
return OptimizationCache(prob, opt; maxiters, maxtime, abstol, callback,
168166
reltol, progress,
169167
kwargs...)
170168
end

src/function.jl

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,87 @@ end
114114
function OptimizationBase.instantiate_function(
115115
f::OptimizationFunction{true}, x, ::SciMLBase.NoAD,
116116
p, num_cons = 0; kwargs...)
117-
grad = f.grad === nothing ? nothing : (G, x, args...) -> f.grad(G, x, p, args...)
118-
fg = f.fg === nothing ? nothing : (G, x, args...) -> f.fg(G, x, p, args...)
119-
hess = f.hess === nothing ? nothing : (H, x, args...) -> f.hess(H, x, p, args...)
120-
fgh = f.fgh === nothing ? nothing : (G, H, x, args...) -> f.fgh(G, H, x, p, args...)
121-
hv = f.hv === nothing ? nothing : (H, x, v, args...) -> f.hv(H, x, v, p, args...)
117+
if f.grad === nothing
118+
grad = nothing
119+
else
120+
function grad(G, x)
121+
return f.grad(G, x, p)
122+
end
123+
if p != SciMLBase.NullParameters()
124+
function grad(G, x, p)
125+
return f.grad(G, x, p)
126+
end
127+
end
128+
end
129+
if f.fg === nothing
130+
fg = nothing
131+
else
132+
function fg(G, x)
133+
return f.fg(G, x, p)
134+
end
135+
if p != SciMLBase.NullParameters()
136+
function fg(G, x, p)
137+
return f.fg(G, x, p)
138+
end
139+
end
140+
end
141+
if f.hess === nothing
142+
hess = nothing
143+
else
144+
function hess(H, x)
145+
return f.hess(H, x, p)
146+
end
147+
if p != SciMLBase.NullParameters()
148+
function hess(H, x, p)
149+
return f.hess(H, x, p)
150+
end
151+
end
152+
end
153+
154+
if f.fgh === nothing
155+
fgh = nothing
156+
else
157+
function fgh(G, H, x)
158+
return f.fgh(G, H, x, p)
159+
end
160+
if p != SciMLBase.NullParameters()
161+
function fgh(G, H, x, p)
162+
return f.fgh(G, H, x, p)
163+
end
164+
end
165+
end
166+
167+
if f.hv === nothing
168+
hv = nothing
169+
else
170+
function hv(H, x, v)
171+
return f.hv(H, x, v, p)
172+
end
173+
if p != SciMLBase.NullParameters()
174+
function hv(H, x, v, p)
175+
return f.hv(H, x, v, p)
176+
end
177+
end
178+
end
179+
122180
cons = f.cons === nothing ? nothing : (res, x) -> f.cons(res, x, p)
123181
cons_j = f.cons_j === nothing ? nothing : (res, x) -> f.cons_j(res, x, p)
124182
cons_vjp = f.cons_vjp === nothing ? nothing : (res, x) -> f.cons_vjp(res, x, p)
125183
cons_jvp = f.cons_jvp === nothing ? nothing : (res, x) -> f.cons_jvp(res, x, p)
126184
cons_h = f.cons_h === nothing ? nothing : (res, x) -> f.cons_h(res, x, p)
127-
lag_h = f.lag_h === nothing ? nothing : (res, x) -> f.lag_h(res, x, p)
185+
186+
if f.lag_h === nothing
187+
lag_h = nothing
188+
else
189+
function lag_h(res, x)
190+
return f.lag_h(res, x, p)
191+
end
192+
if p != SciMLBase.NullParameters()
193+
function lag_h(res, x, p)
194+
return f.lag_h(res, x, p)
195+
end
196+
end
197+
end
128198
hess_prototype = f.hess_prototype === nothing ? nothing :
129199
convert.(eltype(x), f.hess_prototype)
130200
cons_jac_prototype = f.cons_jac_prototype === nothing ? nothing :

0 commit comments

Comments
 (0)