Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docs] improve docstrings in src/nlp.jl #2537

Merged
merged 9 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 91 additions & 18 deletions src/Test/test_nonlinear.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
end

function MOI.features_available(d::HS071)
features = [:Grad, :Jac, :ExprGraph]
features = [:Grad, :Jac, :JacVec, :ExprGraph]
if d.enable_hessian
push!(features, :Hess)
end
Expand Down Expand Up @@ -99,6 +99,26 @@
return
end

function MOI.constraint_gradient_structure(::HS071, ::Int)
return [1, 2, 3, 4]

Check warning on line 103 in src/Test/test_nonlinear.jl

View check run for this annotation

Codecov / codecov/patch

src/Test/test_nonlinear.jl#L102-L103

Added lines #L102 - L103 were not covered by tests
end

function MOI.eval_constraint_gradient(::HS071, ∇g, x, i)
@assert 1 <= i <= 2
if i == 1
∇g[1] = x[2] * x[3] * x[4] # 1,1
∇g[2] = x[1] * x[3] * x[4] # 1,2
∇g[3] = x[1] * x[2] * x[4] # 1,3
∇g[4] = x[1] * x[2] * x[3] # 1,4

Check warning on line 112 in src/Test/test_nonlinear.jl

View check run for this annotation

Codecov / codecov/patch

src/Test/test_nonlinear.jl#L106-L112

Added lines #L106 - L112 were not covered by tests
else
∇g[5] = 2 * x[1] # 2,1
∇g[6] = 2 * x[2] # 2,2
∇g[7] = 2 * x[3] # 2,3
∇g[8] = 2 * x[4] # 2,4

Check warning on line 117 in src/Test/test_nonlinear.jl

View check run for this annotation

Codecov / codecov/patch

src/Test/test_nonlinear.jl#L114-L117

Added lines #L114 - L117 were not covered by tests
end
return

Check warning on line 119 in src/Test/test_nonlinear.jl

View check run for this annotation

Codecov / codecov/patch

src/Test/test_nonlinear.jl#L119

Added line #L119 was not covered by tests
end

function MOI.jacobian_structure(::HS071)
return Tuple{Int64,Int64}[
(1, 1),
Expand All @@ -112,22 +132,6 @@
]
end

function MOI.hessian_lagrangian_structure(d::HS071)
@assert d.enable_hessian
return Tuple{Int64,Int64}[
(1, 1),
(2, 1),
(2, 2),
(3, 1),
(3, 2),
(3, 3),
(4, 1),
(4, 2),
(4, 3),
(4, 4),
]
end

function MOI.eval_constraint_jacobian(::HS071, J, x)
# Constraint (row) 1
J[1] = x[2] * x[3] * x[4] # 1,1
Expand All @@ -142,6 +146,17 @@
return
end

function MOI.eval_constraint_jacobian_product(d::HS071, y, x, w)
y .= zero(eltype(y))
indices = MOI.jacobian_structure(d)
J = zeros(length(indices))
MOI.eval_constraint_jacobian(d, J, x)
for ((i, j), val) in zip(indices, J)
y[i] += val * w[j]
end
return

Check warning on line 157 in src/Test/test_nonlinear.jl

View check run for this annotation

Codecov / codecov/patch

src/Test/test_nonlinear.jl#L149-L157

Added lines #L149 - L157 were not covered by tests
end

function MOI.eval_constraint_jacobian_transpose_product(::HS071, y, x, w)
y[1] = (x[2] * x[3] * x[4]) * w[1] + (2 * x[1]) * w[2]
y[2] = (x[1] * x[3] * x[4]) * w[1] + (2 * x[2]) * w[2]
Expand All @@ -150,6 +165,64 @@
return
end

function MOI.hessian_objective_structure(d::HS071)
return [(1, 1), (2, 1), (3, 1), (4, 1), (4, 2), (4, 3)]

Check warning on line 169 in src/Test/test_nonlinear.jl

View check run for this annotation

Codecov / codecov/patch

src/Test/test_nonlinear.jl#L168-L169

Added lines #L168 - L169 were not covered by tests
end

function MOI.eval_hessian_objective(d::HS071, H, x)
@assert d.enable_hessian
H[1] = 2 * x[4] # 1,1
H[2] = x[4] # 2,1
H[3] = x[4] # 3,1
H[4] = 2 * x[1] + x[2] + x[3] # 4,1
H[5] = x[1] # 4,2
H[6] = x[1] # 4,3
return

Check warning on line 180 in src/Test/test_nonlinear.jl

View check run for this annotation

Codecov / codecov/patch

src/Test/test_nonlinear.jl#L172-L180

Added lines #L172 - L180 were not covered by tests
end

function MOI.hessian_constraint_structure(d::HS071, i::Int)
@assert 1 <= i <= 2
if i == 1
return [(2, 1), (3, 1), (3, 2), (4, 1), (4, 2), (4, 3)]

Check warning on line 186 in src/Test/test_nonlinear.jl

View check run for this annotation

Codecov / codecov/patch

src/Test/test_nonlinear.jl#L183-L186

Added lines #L183 - L186 were not covered by tests
else
return [(1, 1), (2, 2), (3, 3), (4, 4)]

Check warning on line 188 in src/Test/test_nonlinear.jl

View check run for this annotation

Codecov / codecov/patch

src/Test/test_nonlinear.jl#L188

Added line #L188 was not covered by tests
end
end

function MOI.eval_hessian_constraint(d::HS071, H, x, i)
@assert d.enable_hessian
if i == 1
H[1] = x[3] * x[4] # 2,1
H[2] = x[2] * x[4] # 3,1
H[3] = x[1] * x[4] # 3,2
H[4] = x[2] * x[3] # 4,1
H[5] = x[1] * x[3] # 4,2
H[6] = x[1] * x[2] # 4,3

Check warning on line 200 in src/Test/test_nonlinear.jl

View check run for this annotation

Codecov / codecov/patch

src/Test/test_nonlinear.jl#L192-L200

Added lines #L192 - L200 were not covered by tests
else
H[1] = 2.0 # 1,1
H[2] = 2.0 # 2,2
H[3] = 2.0 # 3,3
H[4] = 2.0 # 4,4

Check warning on line 205 in src/Test/test_nonlinear.jl

View check run for this annotation

Codecov / codecov/patch

src/Test/test_nonlinear.jl#L202-L205

Added lines #L202 - L205 were not covered by tests
end
return

Check warning on line 207 in src/Test/test_nonlinear.jl

View check run for this annotation

Codecov / codecov/patch

src/Test/test_nonlinear.jl#L207

Added line #L207 was not covered by tests
end

function MOI.hessian_lagrangian_structure(d::HS071)
@assert d.enable_hessian
return Tuple{Int64,Int64}[
(1, 1),
(2, 1),
(2, 2),
(3, 1),
(3, 2),
(3, 3),
(4, 1),
(4, 2),
(4, 3),
(4, 4),
]
end

function MOI.eval_hessian_lagrangian(d::HS071, H, x, σ, μ)
@assert d.enable_hessian
# Again, only lower left triangle
Expand All @@ -175,7 +248,7 @@
H[1] += μ[2] * 2 # 1,1
H[3] += μ[2] * 2 # 2,2
H[6] += μ[2] * 2 # 3,3
H[10] += μ[2] * 2
H[10] += μ[2] * 2 # 4,4
return
end

Expand Down
Loading
Loading