Skip to content

Commit c023f8c

Browse files
committed
Use SmallestLast() as default ordering for SMC.jl
1 parent d4b2dd3 commit c023f8c

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

docs/src/sparse.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,12 @@ These arguments specify the sparsity pattern detector and the coloring algorithm
4545
```@example ex1
4646
import SparseConnectivityTracer.TracerLocalSparsityDetector
4747
48+
detector = TracerLocalSparsityDetector()
49+
4850
set_adbackend!(
4951
nlp,
50-
jacobian_backend = ADNLPModels.SparseADJacobian(nvar, f, ncon, c!, detector=TracerLocalSparsityDetector()),
51-
hessian_backend = ADNLPModels.SparseADHessian(nvar, f, ncon, c!, detector=TracerLocalSparsityDetector()),
52+
jacobian_backend = ADNLPModels.SparseADJacobian(nvar, f, ncon, c!; detector),
53+
hessian_backend = ADNLPModels.SparseADHessian(nvar, f, ncon, c!; detector),
5254
)
5355
```
5456

@@ -59,14 +61,17 @@ set_adbackend!(
5961
```@example ex1
6062
using SparseMatrixColorings
6163
64+
order = SmallestLast() # NaturalOrder(), RandomOrder(), ...
65+
coloring_algorithm = GreedyColoringAlgorithm{:substitution}(order)
66+
6267
set_adbackend!(
6368
nlp,
64-
hessian_backend = ADNLPModels.SparseADHessian(nvar, f, ncon, c!, coloring_algorithm=GreedyColoringAlgorithm{:substitution}()),
69+
hessian_backend = ADNLPModels.SparseADHessian(nvar, f, ncon, c!; coloring_algorithm)
6570
)
6671
```
6772

68-
The `GreedyColoringAlgorithm{:direct}()` performs column coloring for Jacobians and star coloring for Hessians.
69-
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.
73+
The `GreedyColoringAlgorithm{:direct}(order)` performs column coloring for Jacobians and star coloring for Hessians.
74+
In contrast, `GreedyColoringAlgorithm{:substitution}(order)` 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.
7075
However, it necessitates storing the compressed sparse Hessian, while `:direct` coloring only requires storage for one column of the compressed Hessian.
7176

7277
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.

src/sparse_hessian.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function SparseADHessian(
2222
ncon,
2323
c!;
2424
x0::AbstractVector = rand(nvar),
25-
coloring_algorithm::AbstractColoringAlgorithm = GreedyColoringAlgorithm{:direct}(),
25+
coloring_algorithm::AbstractColoringAlgorithm = GreedyColoringAlgorithm{:direct}(SmallestLast()),
2626
detector::AbstractSparsityDetector = TracerSparsityDetector(),
2727
show_time::Bool = false,
2828
kwargs...,
@@ -41,7 +41,7 @@ function SparseADHessian(
4141
c!,
4242
H::SparseMatrixCSC{Bool, Int64};
4343
x0::S = rand(nvar),
44-
coloring_algorithm::AbstractColoringAlgorithm = GreedyColoringAlgorithm{:direct}(),
44+
coloring_algorithm::AbstractColoringAlgorithm = GreedyColoringAlgorithm{:direct}(SmallestLast()),
4545
show_time::Bool = false,
4646
kwargs...,
4747
) where {S}
@@ -143,7 +143,7 @@ function SparseReverseADHessian(
143143
ncon,
144144
c!;
145145
x0::AbstractVector = rand(nvar),
146-
coloring_algorithm::AbstractColoringAlgorithm = GreedyColoringAlgorithm{:substitution}(),
146+
coloring_algorithm::AbstractColoringAlgorithm = GreedyColoringAlgorithm{:substitution}(SmallestLast()),
147147
detector::AbstractSparsityDetector = TracerSparsityDetector(),
148148
show_time::Bool = false,
149149
kwargs...,
@@ -162,7 +162,7 @@ function SparseReverseADHessian(
162162
c!,
163163
H::SparseMatrixCSC{Bool, Int};
164164
x0::AbstractVector{T} = rand(nvar),
165-
coloring_algorithm::AbstractColoringAlgorithm = GreedyColoringAlgorithm{:substitution}(),
165+
coloring_algorithm::AbstractColoringAlgorithm = GreedyColoringAlgorithm{:substitution}(SmallestLast()),
166166
show_time::Bool = false,
167167
kwargs...,
168168
) where {T}

src/sparse_jacobian.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function SparseADJacobian(
1717
ncon,
1818
c!;
1919
x0::AbstractVector = rand(nvar),
20-
coloring_algorithm::AbstractColoringAlgorithm = GreedyColoringAlgorithm{:direct}(),
20+
coloring_algorithm::AbstractColoringAlgorithm = GreedyColoringAlgorithm{:direct}(SmallestLast()),
2121
detector::AbstractSparsityDetector = TracerSparsityDetector(),
2222
show_time::Bool = false,
2323
kwargs...,
@@ -37,7 +37,7 @@ function SparseADJacobian(
3737
c!,
3838
J::SparseMatrixCSC{Bool, Int};
3939
x0::AbstractVector{T} = rand(nvar),
40-
coloring_algorithm::AbstractColoringAlgorithm = GreedyColoringAlgorithm{:direct}(),
40+
coloring_algorithm::AbstractColoringAlgorithm = GreedyColoringAlgorithm{:direct}(SmallestLast()),
4141
show_time::Bool = false,
4242
kwargs...,
4343
) where {T}

0 commit comments

Comments
 (0)