Skip to content

Commit

Permalink
✅ Test hprod with obj_weight in consistency and fix brownden
Browse files Browse the repository at this point in the history
Closes #230
  • Loading branch information
abelsiqueira committed Jan 26, 2020
1 parent cd1c751 commit 5347657
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
48 changes: 26 additions & 22 deletions test/consistency.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,22 +122,24 @@ function consistent_functions(nlps; rtol=1.0e-8, exclude=[])
v = 10 * [-(-1.0)^i for i = 1:n]

if !(hprod in exclude)
Hvs = Any[hprod(nlp, x, v) for nlp in nlps]
Hopvs = Any[hess_op(nlp, x) * v for nlp in nlps]
Hvmin = minimum(map(norm, Hvs))
for i = 1:N
for j = i+1:N
@test isapprox(Hvs[i], Hvs[j], atol=rtol * max(Hvmin, 1.0))
@test isapprox(Hvs[i], Hopvs[j], atol=rtol * max(Hvmin, 1.0))
for σ = [1.0; 0.5]
Hvs = Any[hprod(nlp, x, v, obj_weight=σ) for nlp in nlps]
Hopvs = Any[hess_op(nlp, x, obj_weight=σ) * v for nlp in nlps]
Hvmin = minimum(map(norm, Hvs))
for i = 1:N
for j = i+1:N
@test isapprox(Hvs[i], Hvs[j], atol=rtol * max(Hvmin, 1.0))
@test isapprox(Hvs[i], Hopvs[j], atol=rtol * max(Hvmin, 1.0))
end
tmphv = hprod!(nlps[i], x, v, tmp_n, obj_weight=σ)
@test isapprox(Hvs[i], tmp_n, atol=rtol * max(Hvmin, 1.0))
@test isapprox(tmphv, tmp_n, atol=rtol * max(Hvmin, 1.0))
fill!(tmp_n, 0)
H = hess_op!(nlps[i], x, tmp_n, obj_weight=σ)
res = H * v
@test isapprox(res, Hvs[i], atol=rtol * max(Hvmin, 1.0))
@test isapprox(res, tmp_n, atol=rtol * max(Hvmin, 1.0))
end
tmphv = hprod!(nlps[i], x, v, tmp_n)
@test isapprox(Hvs[i], tmp_n, atol=rtol * max(Hvmin, 1.0))
@test isapprox(tmphv, tmp_n, atol=rtol * max(Hvmin, 1.0))
fill!(tmp_n, 0)
H = hess_op!(nlps[i], x, tmp_n)
res = H * v
@test isapprox(res, Hvs[i], atol=rtol * max(Hvmin, 1.0))
@test isapprox(res, tmp_n, atol=rtol * max(Hvmin, 1.0))
end
end

Expand Down Expand Up @@ -305,13 +307,15 @@ function consistent_functions(nlps; rtol=1.0e-8, exclude=[])
end

if !(hprod in exclude)
Lps = Any[hprod(nlp, x, y, v) for nlp in nlps]
Hopvs = Any[hess_op(nlp, x, y) * v for nlp in nlps]
Lpmin = minimum(map(norm, Lps))
for i = 1:N-1
for j = i+1:N
@test isapprox(Lps[i], Lps[j], atol=rtol * max(Lpmin, 1.0))
@test isapprox(Lps[i], Hopvs[j], atol=rtol * max(Lpmin, 1.0))
for σ = [1.0; 0.5]
Lps = Any[hprod(nlp, x, y, v, obj_weight=σ) for nlp in nlps]
Hopvs = Any[hess_op(nlp, x, y, obj_weight=σ) * v for nlp in nlps]
Lpmin = minimum(map(norm, Lps))
for i = 1:N-1
for j = i+1:N
@test isapprox(Lps[i], Lps[j], atol=rtol * max(Lpmin, 1.0))
@test isapprox(Lps[i], Hopvs[j], atol=rtol * max(Lpmin, 1.0))
end
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions test/problems/brownden.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function NLPModels.hess_coord!(nlp :: BROWNDEN, x :: AbstractVector, vals :: Abs
return vals
end

function NLPModels.hprod!(nlp :: BROWNDEN, x :: AbstractVector, v :: AbstractVector, Hv :: AbstractVector; obj_weight=1.0)
function NLPModels.hprod!(nlp :: BROWNDEN, x :: AbstractVector{T}, v :: AbstractVector{T}, Hv :: AbstractVector{T}; obj_weight=one(T)) where T
increment!(nlp, :neval_hprod)
α(x,i) = x[1] + x[2] * i/5 - exp(i/5)
β(x,i) = x[3] + x[4] * sin(i/5) - cos(i/5)
Expand All @@ -97,7 +97,7 @@ function NLPModels.hprod!(nlp :: BROWNDEN, x :: AbstractVector, v :: AbstractVec
vi, wi = [1; i/5; 0; 0], [0; 0; 1; sin(i/5)]
zi = αi * vi + βi * wi
θi = αi^2 + βi^2
Hv .+= (4 * dot(vi, v) * vi + 4 * dot(wi, v) * wi) * θi + 8 * dot(zi, v) * zi
Hv .+= obj_weight * ((4 * dot(vi, v) * vi + 4 * dot(wi, v) * wi) * θi + 8 * dot(zi, v) * zi)
end
return Hv
end

0 comments on commit 5347657

Please sign in to comment.