Skip to content

Commit 701f012

Browse files
committed
Fixing jacobian and hessians of constraints
1 parent dd45dc7 commit 701f012

File tree

2 files changed

+12
-41
lines changed

2 files changed

+12
-41
lines changed

src/function.jl

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -76,35 +76,19 @@ function instantiate_function(f, x, ::AutoForwardDiff{_chunksize}, p) where _chu
7676
end
7777

7878
if cons !== nothing && f.cons_j === nothing
79-
if f.num_cons == 1
80-
cjconfig = ForwardDiff.JacobianConfig(cons, x, ForwardDiff.Chunk{chunksize}())
81-
cons_j = (res,θ) -> ForwardDiff.jacobian!(res, cons, θ, cjconfig)
82-
else
83-
cons_j = function (res, θ)
84-
for i in 1:f.num_cons
85-
consi = (x) -> cons(x)[i:i]
86-
cjconfig = ForwardDiff.JacobianConfig(consi, θ, ForwardDiff.Chunk{chunksize}())
87-
ForwardDiff.jacobian!(res[i], consi, θ, cjconfig)
88-
end
89-
end
79+
cons_j = function (J, θ)
80+
cjconfig = ForwardDiff.JacobianConfig(cons, θ, ForwardDiff.Chunk{chunksize}())
81+
ForwardDiff.jacobian!(J, cons, θ, cjconfig)
9082
end
9183
else
9284
cons_j = f.cons_j
9385
end
9486

9587
if cons !== nothing && f.cons_h === nothing
96-
if f.num_cons == 1
97-
firstcons = (args...) -> first(cons(args...))
98-
cons_h = function (res, θ)
99-
hess_config_cache = ForwardDiff.HessianConfig(firstcons, θ, ForwardDiff.Chunk{chunksize}())
100-
ForwardDiff.hessian!(res, firstcons, θ, hess_config_cache)
101-
end
102-
else
103-
cons_h = function (res, θ)
104-
for i in 1:f.num_cons
105-
hess_config_cache = ForwardDiff.HessianConfig(x -> cons(x)[i], θ, ForwardDiff.Chunk{chunksize}())
106-
ForwardDiff.hessian!(res[i], (x) -> cons(x)[i], θ, hess_config_cache, Val{false}())
107-
end
88+
cons_h = function (res, θ)
89+
for i in 1:f.num_cons
90+
hess_config_cache = ForwardDiff.HessianConfig(x -> cons(x)[i], θ,ForwardDiff.Chunk{chunksize}())
91+
ForwardDiff.hessian!(res[i], (x) -> cons(x)[i], θ, hess_config_cache,Val{false}())
10892
end
10993
end
11094
else

src/solve.jl

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -234,28 +234,15 @@ function __solve(prob::OptimizationProblem, opt::Optim.ConstrainedOptimizer;cb =
234234
cons! = (res, θ) -> res .= f.cons(θ);
235235

236236
cons_j! = function(J, x)
237-
if f.num_cons > 1
238-
res = [zeros(1,size(J,2)) for i in 1:size(J,1)]
239-
f.cons_j(res, x)
240-
J = reduce(vcat,res)
241-
else
242-
f.cons_j(J, x)
243-
end
237+
f.cons_j(J, x)
244238
end
245239

246240
cons_hl! = function (h, θ, λ)
247-
if f.num_cons > 1
248-
res = [similar(h) for i in 1:length(λ)]
249-
f.cons_h(res, θ)
250-
h .= zeros(size(h))
251-
for i in 1:length(λ)
252-
h += λ[i]*res[i]
253-
end
254-
else
255-
f.cons_h(h, θ)
256-
h += λ[1]*h
241+
res = [similar(h) for i in 1:length(λ)]
242+
f.cons_h(res, θ)
243+
for i in 1:length(λ)
244+
h .+= λ[i]*res[i]
257245
end
258-
259246
end
260247

261248
lb = prob.lb === nothing ? [] : prob.lb

0 commit comments

Comments
 (0)