You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
(get_nnzj(nlp), get_nnzh(nlp)) # number of nonzeros elements in the Jacobian and Hessian
21
+
(get_nnzj(nlp), get_nnzh(nlp)) # Number of nonzero elements in the Jacobian and Hessian
22
22
```
23
23
24
24
```@example ex1
@@ -31,24 +31,66 @@ x = rand(T, 2)
31
31
H = hess(nlp, x)
32
32
```
33
33
34
-
The available backends for sparse derivatives (`SparseADJacobian`, `SparseADHessian` and `SparseReverseADHessian`) have keyword arguments `detector` and `coloring_algorithm` to specify the sparsity pattern detector and the coloring algorithm, respectively.
34
+
The backends available for sparse derivatives (`SparseADJacobian`, `SparseADHessian`, and `SparseReverseADHessian`) allow for customization through keyword arguments such as `detector` and `coloring_algorithm`.
35
+
These arguments specify the sparsity pattern detector and the coloring algorithm, respectively.
35
36
36
37
- A **`detector`** must be of type `ADTypes.AbstractSparsityDetector`.
37
-
The default detector is `TracerSparsityDetector()` from the package `SparseConnectivityTracer.jl`.
38
-
Prior to version 0.8.0, the default detector was `SymbolicSparsityDetector()` from `Symbolics.jl`.
38
+
The default detector is `TracerSparsityDetector()` from the package `SparseConnectivityTracer.jl`.
39
+
Prior to version 0.8.0, the default was `SymbolicSparsityDetector()` from `Symbolics.jl`.
39
40
40
41
- A **`coloring_algorithm`** must be of type `SparseMatrixColorings.GreedyColoringAlgorithm`.
41
-
The default algorithm is `GreedyColoringAlgorithm{:direct}()` for `SparseADJacobian` and `SparseADHessian`, while it is `GreedyColoringAlgorithm{:substitution}()` for `SparseReverseADHessian`.
42
-
These algorithms are available in the package `SparseMatrixColorings.jl`.
42
+
The default algorithm is `GreedyColoringAlgorithm{:direct}()` for `SparseADJacobian` and `SparseADHessian`, while it is `GreedyColoringAlgorithm{:substitution}()` for `SparseReverseADHessian`.
43
+
These algorithms are provided by the package `SparseMatrixColorings.jl`.
43
44
44
45
The `GreedyColoringAlgorithm{:direct}()` performs column coloring for Jacobians and star coloring for Hessians.
45
-
In contrast, `GreedyColoringAlgorithm{:substitution}()` applies acyclic coloring for Hessians.
46
-
The `:substitution` coloring mode usually finds fewer colors than the `:direct` mode and thus fewer directional derivatives are needed to recover all non-zeros of the sparse Hessian.
47
-
However, it requires storing the compressed sparse Hessian, while `:direct` coloring only stores one column of the compressed Hessian.
46
+
In contrast, `GreedyColoringAlgorithm{:substitution}()` applies acyclic coloring for Hessians. The `:substitution` mode generally requires fewer colors than `:direct`, thus fewer directional derivatives are needed to reconstruct the sparse Hessian.
47
+
However, it necessitates storing the compressed sparse Hessian, while `:direct` coloring only requires storage for one column of the compressed Hessian.
48
48
49
-
The `:direct` coloring mode is numerically more stable and may be preferable for highly ill-conditioned Hessian as it doesn't require solving triangular systems to compute the non-zeros from the compressed Hessian.
49
+
The `:direct` coloring mode is numerically more stable and may be preferable for highly ill-conditioned Hessians, as it avoids solving triangular systems to compute nonzero entries from the compressed Hessian.
50
+
51
+
## Extracting sparsity patterns
52
+
53
+
`ADNLPModels.jl` provides the function [`get_sparsity_pattern`](@ref) to retrieve the sparsity patterns of the Jacobian or Hessian from a model.
54
+
55
+
```@example ex3
56
+
using SparseArrays, ADNLPModels, NLPModels
57
+
58
+
nvar = 10
59
+
ncon = 5
60
+
61
+
f(x) = sum((x[i] - i)^2 for i = 1:nvar) + x[nvar] * sum(x[j] for j = 1:nvar-1)
The section ["providing the sparsity pattern for sparse derivatives"](@ref sparsity-pattern) illustrates this feature with a more advanced application.
146
+
147
+
### Acknowledgements
148
+
103
149
The package [`SparseConnectivityTracer.jl`](https://github.com/adrhill/SparseConnectivityTracer.jl) is used to compute the sparsity pattern of Jacobians and Hessians.
104
150
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).
105
151
As of release v0.8.1, it has replaced [`ColPack.jl`](https://github.com/exanauts/ColPack.jl).
Copy file name to clipboardExpand all lines: src/sparsity_pattern.jl
+77Lines changed: 77 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -1,3 +1,5 @@
1
+
export get_sparsity_pattern
2
+
1
3
"""
2
4
compute_jacobian_sparsity(c, x0; detector)
3
5
compute_jacobian_sparsity(c!, cx, x0; detector)
@@ -51,3 +53,78 @@ function compute_hessian_sparsity(
51
53
S = ADTypes.hessian_sparsity(lagrangian, x0, detector)
52
54
return S
53
55
end
56
+
57
+
"""
58
+
S = get_sparsity_pattern(model::ADModel, derivative::Symbol)
59
+
60
+
Retrieve the sparsity pattern of a Jacobian or Hessian from an `ADModel`.
61
+
For the Hessian, only the lower triangular part of its sparsity pattern is returned.
62
+
The user can reconstruct the upper triangular part by exploiting symmetry.
63
+
64
+
To compute the sparsity pattern, the model must use a sparse backend.
65
+
Supported backends include `SparseADJacobian`, `SparseADHessian`, and `SparseReverseADHessian`.
66
+
67
+
#### Input arguments
68
+
69
+
* `model`: An automatic differentiation model (either `AbstractADNLPModel` or `AbstractADNLSModel`).
70
+
* `derivative`: The type of derivative for which the sparsity pattern is needed. The supported values are `:jacobian`, `:hessian`, `:jacobian_residual` and `:hessian_residual`.
71
+
72
+
#### Output argument
73
+
74
+
* `S`: A sparse matrix of type `SparseMatrixCSC{Bool,Int}` indicating the sparsity pattern of the requested derivative.
0 commit comments