@@ -174,12 +174,12 @@ function instantiate_function(
174
174
conshess_colors = f. cons_hess_colorvec
175
175
if cons != = nothing && f. cons_h === nothing
176
176
fncs = [@closure (x) -> cons_oop (x)[i] for i in 1 : num_cons]
177
- extras_cons_hess = Vector {DifferentiationInterface.SparseHessianExtras} (undef, length (fncs))
178
- for ind in 1 : num_cons
179
- extras_cons_hess[ind] = prepare_hessian (fncs[ind], soadtype, x)
180
- end
181
- conshess_sparsity = [ sum (sparse, cons)]
182
- conshess_colors = getfield .(extras_cons_hess, Ref ( :colors ) )
177
+ # extras_cons_hess = Vector(undef, length(fncs))
178
+ # for ind in 1:num_cons
179
+ # extras_cons_hess[ind] = prepare_hessian(fncs[ind], soadtype, x)
180
+ # end
181
+ # conshess_sparsity = getfield.(extras_cons_hess, :sparsity)
182
+ # conshess_colors = getfield.(extras_cons_hess, :colors)
183
183
function cons_h (H, θ)
184
184
for i in 1 : num_cons
185
185
hessian! (fncs[i], H[i], soadtype, θ)
@@ -189,56 +189,33 @@ function instantiate_function(
189
189
cons_h = (res, θ) -> f. cons_h (res, θ, p)
190
190
end
191
191
192
- function lagrangian (x, σ = one (eltype (x)))
193
- θ = x[1 : end - num_cons]
194
- λ = x[end - num_cons+ 1 : end ]
195
- return σ * _f (θ) + dot (λ, cons_oop (θ))
192
+ function lagrangian (x, σ = one (eltype (x)), λ = ones (eltype (x), num_cons))
193
+ return σ * _f (x) + dot (λ, cons_oop (x))
196
194
end
197
195
196
+ lag_hess_prototype = f. lag_hess_prototype
198
197
if f. lag_h === nothing
199
- lag_extras = prepare_hessian (lagrangian, soadtype, vcat (x, ones ( eltype (x), num_cons)) )
198
+ lag_extras = prepare_hessian (lagrangian, soadtype, x )
200
199
lag_hess_prototype = lag_extras. sparsity
201
-
202
- function lag_h (H:: Matrix , θ, σ, λ)
203
- @show size (H)
204
- @show size (θ)
205
- @show size (λ)
200
+
201
+ function lag_h (H:: AbstractMatrix , θ, σ, λ)
206
202
if σ == zero (eltype (θ))
207
203
cons_h (H, θ)
208
204
H *= λ
209
205
else
210
- hessian! (lagrangian, H, soadtype, vcat (θ, λ) , lag_extras)
206
+ hessian! (x -> lagrangian (x, σ, λ), H, soadtype, θ , lag_extras)
211
207
end
212
208
end
213
209
214
210
function lag_h (h, θ, σ, λ)
215
- # @show h
216
- sparseHproto = findnz (lag_extras. sparsity)
217
- H = sparse (sparseHproto[1 ], sparseHproto[2 ], zeros (eltype (θ), length (sparseHproto[1 ])))
218
- if σ == zero (eltype (θ))
219
- cons_h (H, θ)
220
- H *= λ
221
- else
222
- hessian! (lagrangian, H, soadtype, vcat (θ, λ), lag_extras)
223
- k = 0
224
- rows, cols, _ = findnz (H)
225
- for (i, j) in zip (rows, cols)
226
- if i <= j
227
- k += 1
228
- h[k] = σ * H[i, j]
229
- end
230
- end
231
- k = 0
232
- for λi in λ
233
- if Hi isa SparseMatrixCSC
234
- rows, cols, _ = findnz (Hi)
235
- for (i, j) in zip (rows, cols)
236
- if i <= j
237
- k += 1
238
- h[k] += λi * Hi[i, j]
239
- end
240
- end
241
- end
211
+ H = eltype (θ).(lag_hess_prototype)
212
+ hessian! (x -> lagrangian (x, σ, λ), H, soadtype, θ, lag_extras)
213
+ k = 0
214
+ rows, cols, _ = findnz (H)
215
+ for (i, j) in zip (rows, cols)
216
+ if i <= j
217
+ k += 1
218
+ h[k] = H[i, j]
242
219
end
243
220
end
244
221
end
0 commit comments