|
103 | 103 |
|
104 | 104 | function NLPModels.grad!(nlp::NLPModelsAdaptor, x::AbstractVector, g::AbstractVector) |
105 | 105 | nlp.cache.f.grad(g, x, nlp.cache.p) |
| 106 | + return g |
106 | 107 | end |
107 | 108 |
|
108 | 109 | function NLPModels.cons!(nlp::NLPModelsAdaptor, x::AbstractVector, c::AbstractVector) |
@@ -266,11 +267,13 @@ function _get_nnzj(f) |
266 | 267 | end |
267 | 268 | end |
268 | 269 |
|
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 |
271 | 273 |
|
272 | 274 | 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) |
274 | 277 | elseif hess_proto isa SparseMatrixCSC |
275 | 278 | # Only count lower triangle |
276 | 279 | I, J, _ = findnz(hess_proto) |
@@ -309,7 +312,7 @@ function __map_optimizer_args(cache, |
309 | 312 | nvar; |
310 | 313 | ncon, |
311 | 314 | nnzj = _get_nnzj(cache.f), |
312 | | - nnzh = _get_nnzh(cache.f), |
| 315 | + nnzh = _get_nnzh(cache.f, ncon, nvar), |
313 | 316 | x0 = cache.u0, |
314 | 317 | y0 = zeros(eltype(cache.u0), ncon), |
315 | 318 | lvar, |
|
0 commit comments