Skip to content

Commit 40b6cbc

Browse files
SebastianM-Cclaude
andcommitted
assume dense Hessian if no prototype is provided
Co-authored-by: Claude <noreply@anthropic.com>
1 parent 76fd254 commit 40b6cbc

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

lib/OptimizationMadNLP/src/OptimizationMadNLP.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ end
103103

104104
function NLPModels.grad!(nlp::NLPModelsAdaptor, x::AbstractVector, g::AbstractVector)
105105
nlp.cache.f.grad(g, x, nlp.cache.p)
106+
return g
106107
end
107108

108109
function NLPModels.cons!(nlp::NLPModelsAdaptor, x::AbstractVector, c::AbstractVector)
@@ -266,11 +267,13 @@ function _get_nnzj(f)
266267
end
267268
end
268269

269-
function _get_nnzh(f)
270-
hess_proto = f.lag_hess_prototype
270+
function _get_nnzh(f, ncon, nvar)
271+
# For constrained problems, use Lagrangian Hessian; for unconstrained, use objective Hessian
272+
hess_proto = ncon > 0 ? f.lag_hess_prototype : f.hess_prototype
271273

272274
if isnothing(hess_proto)
273-
return 0 # Unknown, let NLPModels compute it
275+
# No prototype provided - assume dense Hessian
276+
return div(nvar * (nvar + 1), 2)
274277
elseif hess_proto isa SparseMatrixCSC
275278
# Only count lower triangle
276279
I, J, _ = findnz(hess_proto)
@@ -309,7 +312,7 @@ function __map_optimizer_args(cache,
309312
nvar;
310313
ncon,
311314
nnzj = _get_nnzj(cache.f),
312-
nnzh = _get_nnzh(cache.f),
315+
nnzh = _get_nnzh(cache.f, ncon, nvar),
313316
x0 = cache.u0,
314317
y0 = zeros(eltype(cache.u0), ncon),
315318
lvar,

0 commit comments

Comments
 (0)