Skip to content

Commit 1897f79

Browse files
authored
Add a method hess_coord! for the Hessian of the objective (#135)
* Add a method hess_coord! for the Hessian of the objective * Add unit tests for allocations * Update tests * Remove allocation tests
1 parent 446d350 commit 1897f79

File tree

3 files changed

+39
-15
lines changed

3 files changed

+39
-15
lines changed

src/nlp.jl

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,17 @@ function _jtprod_nln!(cons, x, v, Jtv)
672672
sjacobian!(nothing, (Jtv, v), cons, x, one(eltype(Jtv)))
673673
end
674674

675+
function hess_coord!(
676+
m::ExaModel,
677+
x::AbstractVector,
678+
hess::AbstractVector;
679+
obj_weight = one(eltype(x)),
680+
)
681+
fill!(hess, zero(eltype(hess)))
682+
_obj_hess_coord!(m.objs, x, hess, obj_weight)
683+
return hess
684+
end
685+
675686
function hess_coord!(
676687
m::ExaModel,
677688
x::AbstractVector,
@@ -680,14 +691,14 @@ function hess_coord!(
680691
obj_weight = one(eltype(x)),
681692
)
682693
fill!(hess, zero(eltype(hess)))
683-
_obj_hess_coord!(m.objs, x, y, hess, obj_weight)
694+
_obj_hess_coord!(m.objs, x, hess, obj_weight)
684695
_con_hess_coord!(m.cons, x, y, hess, obj_weight)
685696
return hess
686697
end
687698

688-
_obj_hess_coord!(objs::ObjectiveNull, x, y, hess, obj_weight) = nothing
689-
function _obj_hess_coord!(objs, x, y, hess, obj_weight)
690-
_obj_hess_coord!(objs.inner, x, y, hess, obj_weight)
699+
_obj_hess_coord!(objs::ObjectiveNull, x, hess, obj_weight) = nothing
700+
function _obj_hess_coord!(objs, x, hess, obj_weight)
701+
_obj_hess_coord!(objs.inner, x, hess, obj_weight)
691702
shessian!(hess, nothing, objs, x, obj_weight, zero(eltype(hess)))
692703
end
693704

@@ -697,6 +708,18 @@ function _con_hess_coord!(cons, x, y, hess, obj_weight)
697708
shessian!(hess, nothing, cons, x, y, zero(eltype(hess)))
698709
end
699710

711+
function hprod!(
712+
m::ExaModel,
713+
x::AbstractVector,
714+
v::AbstractVector,
715+
Hv::AbstractVector;
716+
obj_weight = one(eltype(x)),
717+
)
718+
fill!(Hv, zero(eltype(Hv)))
719+
_obj_hprod!(m.objs, x, v, Hv, obj_weight)
720+
return Hv
721+
end
722+
700723
function hprod!(
701724
m::ExaModel,
702725
x::AbstractVector,
@@ -706,14 +729,14 @@ function hprod!(
706729
obj_weight = one(eltype(x)),
707730
)
708731
fill!(Hv, zero(eltype(Hv)))
709-
_obj_hprod!(m.objs, x, y, v, Hv, obj_weight)
732+
_obj_hprod!(m.objs, x, v, Hv, obj_weight)
710733
_con_hprod!(m.cons, x, y, v, Hv, obj_weight)
711734
return Hv
712735
end
713736

714-
_obj_hprod!(objs::ObjectiveNull, x, y, v, Hv, obj_weight) = nothing
715-
function _obj_hprod!(objs, x, y, v, Hv, obj_weight)
716-
_obj_hprod!(objs.inner, x, y, v, Hv, obj_weight)
737+
_obj_hprod!(objs::ObjectiveNull, x, v, Hv, obj_weight) = nothing
738+
function _obj_hprod!(objs, x, v, Hv, obj_weight)
739+
_obj_hprod!(objs.inner, x, v, Hv, obj_weight)
717740
shessian!((Hv, v), nothing, objs, x, obj_weight, zero(eltype(Hv)))
718741
end
719742

test/NLPTest/NLPTest.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ module NLPTest
22

33
using ExaModels
44
using Downloads, Test
5-
using NLPModels, JuMP, NLPModelsJuMP, PowerModels, NLPModelsIpopt, MadNLP, Percival
5+
using NLPModels, NLPModelsJuMP, NLPModelsIpopt, NLPModelsTest
6+
using JuMP, PowerModels, MadNLP, Percival
67

78
import ..BACKENDS
89

@@ -141,7 +142,6 @@ function runtests()
141142
set_optimizer_attribute(m, "print_level", MadNLP.ERROR)
142143
optimize!(m)
143144

144-
145145
m, vars1, cons1 = exa_model(backend, args)
146146
m1 = WrapperNLPModel(m)
147147

test/Project.toml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
[deps]
2+
AMDGPU = "21141c5a-9bdb-4563-92ae-f87d6854732e"
23
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
34
Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
45
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
6+
Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9"
7+
JuMP = "4076af6c-e467-56ae-b986-b466b2749572"
58
KernelAbstractions = "63c18a36-062a-441e-b654-da1e3ab1ce7c"
69
MadNLP = "2621e9c9-9eb4-46b1-8089-e8c72242dfb6"
710
NLPModels = "a4795742-8479-5a88-8948-cc11e1c8c1a6"
811
NLPModelsIpopt = "f4238b75-b362-5c4c-b852-0801c9a21d71"
912
NLPModelsJuMP = "792afdf1-32c1-5681-94e0-d7bf7a5df49e"
13+
NLPModelsTest = "7998695d-6960-4d3a-85c4-e1bceb8cd856"
1014
Percival = "01435c0c-c90d-11e9-3788-63660f8fbccc"
1115
PowerModels = "c36e90e8-916a-50a6-bd94-075b64ef4655"
1216
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
13-
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
14-
AMDGPU = "21141c5a-9bdb-4563-92ae-f87d6854732e"
15-
Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9"
16-
JuMP = "4076af6c-e467-56ae-b986-b466b2749572"
1717
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
18-
oneAPI = "8f75cd03-7ff8-4ecb-9b8f-daf728133b1b"
18+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
19+
oneAPI = "8f75cd03-7ff8-4ecb-9b8f-daf728133b1b"

0 commit comments

Comments
 (0)