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

[documentation] Explain how we can provide a sparsity pattern #286

Merged
Merged
Changes from all 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
52 changes: 52 additions & 0 deletions docs/src/sparse.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,58 @@ Prior to version 0.8.0, the default detector was `SymbolicSparsityDetector()` fr
- **Coloring**: A `coloring` must be of type `ADTypes.AbstractColoringAlgorithm`.
The default algorithm is `GreedyColoringAlgorithm()` from the package `SparseMatrixColorings.jl`.

If the sparsity pattern of the Jacobian of the constraint or the Hessian of the Lagrangian is available, you can directly provide them.
amontoison marked this conversation as resolved.
Show resolved Hide resolved
```@example ex2
using SparseArrays, ADNLPModels, NLPModels

nvar = 10
ncon = 5

f(x) = sum((x[i] - i)^2 for i = 1:nvar) + x[nvar] * sum(x[j] for j = 1:nvar-1)

H = SparseMatrixCSC{Bool, Int64}(
[ 1 0 0 0 0 0 0 0 0 1 ;
0 1 0 0 0 0 0 0 0 1 ;
0 0 1 0 0 0 0 0 0 1 ;
0 0 0 1 0 0 0 0 0 1 ;
0 0 0 0 1 0 0 0 0 1 ;
0 0 0 0 0 1 0 0 0 1 ;
0 0 0 0 0 0 1 0 0 1 ;
0 0 0 0 0 0 0 1 0 1 ;
0 0 0 0 0 0 0 0 1 1 ;
1 1 1 1 1 1 1 1 1 1 ]
)

function c!(cx, x)
cx[1] = x[1] + x[2]
cx[2] = x[1] + x[2] + x[3]
cx[3] = x[2] + x[3] + x[4]
cx[4] = x[3] + x[4] + x[5]
cx[5] = x[4] + x[5]
return cx
end

J = SparseMatrixCSC{Bool, Int64}(
[ 1 1 0 0 0 ;
1 1 1 0 0 ;
0 1 1 1 0 ;
0 0 1 1 1 ;
0 0 0 1 1 ]
)

T = Float64
x0 = -ones(T, nvar)
lvar = zeros(T, nvar)
uvar = 2 * ones(T, nvar)
lcon = -0.5 * ones(T, ncon)
ucon = 0.5 * ones(T, ncon)

J_backend = ADNLPModels.SparseADJacobian(nvar, f, ncon, c!, J)
H_backend = ADNLPModels.SparseADHessian(nvar, f, ncon, c!, H)

nlp = ADNLPModel!(f, x0, lvar, uvar, c!, lcon, ucon, jacobian_backend=J_backend, hessian_backend=H_backend)
```

The package [`SparseConnectivityTracer.jl`](https://github.com/adrhill/SparseConnectivityTracer.jl) is used to compute the sparsity pattern of Jacobians and Hessians.
The evaluation of the number of directional derivatives and the seeds required to compute compressed Jacobians and Hessians is performed using [`SparseMatrixColorings.jl`](https://github.com/gdalle/SparseMatrixColorings.jl).
As of release v0.8.1, it has replaced [`ColPack.jl`](https://github.com/exanauts/ColPack.jl).
Expand Down
Loading