Tue, 08 Apr 2025 13:31:39 -0500
Bump alg_tools version requirement
|
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
1 | /*! |
|
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
2 | Iterative algorithms for solving the finite-dimensional subproblem with non-negativity constraints. |
|
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
3 | */ |
| 0 | 4 | |
| 5 | use nalgebra::{DVector, DMatrix}; | |
| 6 | use numeric_literals::replace_float_literals; | |
| 7 | use itertools::{izip, Itertools}; | |
| 8 | use colored::Colorize; | |
|
34
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
9 | use std::cmp::Ordering::*; |
| 0 | 10 | |
| 11 | use alg_tools::iter::Mappable; | |
| 12 | use alg_tools::error::NumericalError; | |
| 13 | use alg_tools::iterate::{ | |
| 14 | AlgIteratorFactory, | |
| 15 | AlgIteratorState, | |
| 16 | Step, | |
| 17 | }; | |
| 18 | use alg_tools::linops::GEMV; | |
| 19 | use alg_tools::nalgebra_support::ToNalgebraRealField; | |
| 20 | ||
| 21 | use crate::types::*; | |
|
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
22 | use super::{ |
|
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
23 | InnerMethod, |
|
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
24 | InnerSettings |
|
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
25 | }; |
|
34
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
26 | use super::l1squared_nonneg::max_interval_dist_to_zero; |
| 0 | 27 | |
| 28 | /// Compute the proximal operator of $x \mapsto x + \delta\_{[0, \infty)}$, i.e., | |
| 29 | /// the non-negativity contrained soft-thresholding operator. | |
| 30 | #[inline] | |
| 31 | #[replace_float_literals(F::cast_from(literal))] | |
|
34
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
32 | pub(super) fn nonneg_soft_thresholding<F : Float>(v : F, λ : F) -> F { |
| 0 | 33 | (v - λ).max(0.0) |
| 34 | } | |
| 35 | ||
|
34
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
36 | /// Returns the ∞-norm minimal subdifferential of $x ↦ x^⊤Ax/2 - g^⊤ x + λ\vec 1^⊤ x + δ_{≥ 0}(x)$ |
|
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
37 | /// at $x$. |
|
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
38 | /// |
|
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
39 | /// `v` will be modified and cannot be trusted to contain useful values afterwards. |
|
34
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
40 | #[replace_float_literals(F::cast_from(literal))] |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
41 | fn min_subdifferential<F : Float + nalgebra::RealField>( |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
42 | v : &mut DVector<F>, |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
43 | mA : &DMatrix<F>, |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
44 | x : &DVector<F>, |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
45 | g : &DVector<F>, |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
46 | λ : F |
|
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
47 | ) -> F { |
|
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
48 | v.copy_from(g); |
|
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
49 | mA.gemv(v, 1.0, x, -1.0); // v = Ax - g |
|
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
50 | let mut val = 0.0; |
|
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
51 | for (&v_i, &x_i) in izip!(v.iter(), x.iter()) { |
|
34
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
52 | let (mut lb, mut ub) = (v_i, v_i); |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
53 | match x_i.partial_cmp(&0.0) { |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
54 | Some(Greater) => { lb += λ; ub += λ }, |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
55 | // Less should not happen |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
56 | Some(Less|Equal) => { lb = F::NEG_INFINITY; ub += λ }, |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
57 | None => {}, |
|
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
58 | } |
|
34
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
59 | val = max_interval_dist_to_zero(val, lb, ub); |
|
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
60 | } |
|
34
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
61 | val |
|
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
62 | } |
|
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
63 | |
| 0 | 64 | /// Forward-backward splitting implementation of [`quadratic_nonneg`]. |
| 65 | /// For detailed documentation of the inputs and outputs, refer to there. | |
| 66 | /// | |
| 67 | /// The `λ` component of the model is handled in the proximal step instead of the gradient step | |
| 68 | /// for potential performance improvements. | |
| 69 | #[replace_float_literals(F::cast_from(literal).to_nalgebra_mixed())] | |
| 70 | pub fn quadratic_nonneg_fb<F, I>( | |
| 71 | mA : &DMatrix<F::MixedType>, | |
| 72 | g : &DVector<F::MixedType>, | |
| 73 | //c_ : F, | |
| 74 | λ_ : F, | |
| 75 | x : &mut DVector<F::MixedType>, | |
| 76 | τ_ : F, | |
| 77 | iterator : I | |
| 78 | ) -> usize | |
| 79 | where F : Float + ToNalgebraRealField, | |
| 80 | I : AlgIteratorFactory<F> | |
| 81 | { | |
| 82 | let mut xprev = x.clone(); | |
| 83 | //let c = c_.to_nalgebra_mixed(); | |
| 84 | let λ = λ_.to_nalgebra_mixed(); | |
| 85 | let τ = τ_.to_nalgebra_mixed(); | |
| 86 | let τλ = τ * λ; | |
| 87 | let mut v = DVector::zeros(x.len()); | |
| 88 | let mut iters = 0; | |
| 89 | ||
| 90 | iterator.iterate(|state| { | |
| 91 | // Replace `x` with $x - τ[Ax-g]= [x + τg]- τAx$ | |
| 92 | v.copy_from(g); // v = g | |
| 93 | v.axpy(1.0, x, τ); // v = x + τ*g | |
| 94 | v.sygemv(-τ, mA, x, 1.0); // v = [x + τg]- τAx | |
| 95 | let backup = state.if_verbose(|| { | |
| 96 | xprev.copy_from(x) | |
| 97 | }); | |
| 98 | // Calculate the proximal map | |
| 99 | x.iter_mut().zip(v.iter()).for_each(|(x_i, &v_i)| { | |
| 100 | *x_i = nonneg_soft_thresholding(v_i, τλ); | |
| 101 | }); | |
| 102 | ||
| 103 | iters +=1; | |
| 104 | ||
| 105 | backup.map(|_| { | |
|
34
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
106 | F::from_nalgebra_mixed(min_subdifferential(&mut v, mA, x, g, λ)) |
| 0 | 107 | }) |
| 108 | }); | |
| 109 | ||
| 110 | iters | |
| 111 | } | |
| 112 | ||
| 113 | /// Semismooth Newton implementation of [`quadratic_nonneg`]. | |
| 114 | /// | |
| 115 | /// For detailed documentation of the inputs, refer to there. | |
| 116 | /// This function returns the number of iterations taken if there was no inversion failure, | |
| 117 | /// | |
| 118 | /// ## Method derivation | |
| 119 | /// | |
| 120 | /// **The below may look like garbage. Sorry, but rustdoc is obsolete rubbish | |
| 121 | /// that doesn't directly support by-now standard-in-markdown LaTeX math. Instead it | |
| 122 | /// forces one into unreliable KaTeX autorender postprocessing andescape hell and that | |
| 123 | /// it doesn't even process correctly.** | |
| 124 | /// | |
| 125 | /// <p> | |
| 126 | /// For the objective | |
| 127 | /// $$ | |
| 128 | /// J(x) = \frac{1}{2} x^⊤Ax - g^⊤ x + λ{\vec 1}^⊤ x + c + δ_{≥ 0}(x), | |
| 129 | /// $$ | |
| 130 | /// we have the optimality condition | |
| 131 | /// $$ | |
| 132 | /// x - \mathop{\mathrm{prox}}_{τλ{\vec 1}^⊤ + δ_{≥ 0}}(x - τ[Ax-g^⊤]) = 0, | |
| 133 | /// $$ | |
| 134 | /// which we write as | |
| 135 | /// $$ | |
| 136 | /// x - [G ∘ F](x)=0 | |
| 137 | /// $$ | |
| 138 | /// for | |
| 139 | /// $$ | |
| 140 | /// G(x) = \mathop{\mathrm{prox}}_{λ{\vec 1}^⊤ + δ_{≥ 0}} | |
| 141 | /// \quad\text{and}\quad | |
| 142 | /// F(x) = x - τ Ax + τ g^⊤ | |
| 143 | /// $$ | |
| 144 | /// We can use Newton derivative chain rule to compute | |
| 145 | /// $D_N[G ∘ F](x) = D_N G(F(x)) D_N F(x)$, where | |
| 146 | /// $D_N F(x) = \mathop{\mathrm{Id}} - τ A$, | |
| 147 | /// and $[D_N G(F(x))]_i = 1$ for inactive coordinates and $=0$ for active coordinates. | |
| 148 | /// </p> | |
| 149 | /// | |
| 150 | /// <p> | |
| 151 | /// The method itself involves solving $D_N[Id - G ∘ F](x^k) s^k = - [Id - G ∘ F](x^k)$ and | |
| 152 | /// updating $x^{k+1} = x^k + s^k$. Consequently | |
| 153 | /// $$ | |
| 154 | /// s^k - D_N G(F(x^k)) [s^k - τ As^k] = - x^k + [G ∘ F](x^k) | |
| 155 | /// $$ | |
| 156 | /// For $𝒜$ the set of active coordinates and $ℐ$ the set of inactive coordinates, this | |
| 157 | /// expands as | |
| 158 | /// $$ | |
| 159 | /// [τ A_{ℐ × ℐ}]s^k_ℐ = - x^k_ℐ + [G ∘ F](x^k)_ℐ - [τ A_{ℐ × 𝒜}]s^k_𝒜 | |
| 160 | /// $$ | |
| 161 | /// and | |
| 162 | /// $$ | |
| 163 | /// s^k_𝒜 = - x^k_𝒜 + [G ∘ F](x^k)_𝒜. | |
| 164 | /// $$ | |
| 165 | /// Thus on $𝒜$ the update $[x^k + s^k]_𝒜 = [G ∘ F](x^k)_𝒜$ is just the forward-backward update. | |
| 166 | /// </p> | |
| 167 | /// | |
| 168 | /// <p> | |
| 169 | /// We need to detect stopping by a subdifferential and return $x$ satisfying $x ≥ 0$, | |
| 170 | /// which is in general not true for the SSN. We therefore use that $[G ∘ F](x^k)$ is a valid | |
| 171 | /// forward-backward step. | |
| 172 | /// </p> | |
| 173 | #[replace_float_literals(F::cast_from(literal).to_nalgebra_mixed())] | |
| 174 | pub fn quadratic_nonneg_ssn<F, I>( | |
| 175 | mA : &DMatrix<F::MixedType>, | |
| 176 | g : &DVector<F::MixedType>, | |
| 177 | //c_ : F, | |
| 178 | λ_ : F, | |
| 179 | x : &mut DVector<F::MixedType>, | |
| 180 | τ_ : F, | |
| 181 | iterator : I | |
| 182 | ) -> Result<usize, NumericalError> | |
| 183 | where F : Float + ToNalgebraRealField, | |
| 184 | I : AlgIteratorFactory<F> | |
| 185 | { | |
| 186 | let n = x.len(); | |
| 187 | let mut xprev = x.clone(); | |
| 188 | let mut v = DVector::zeros(n); | |
| 189 | //let c = c_.to_nalgebra_mixed(); | |
| 190 | let λ = λ_.to_nalgebra_mixed(); | |
| 191 | let τ = τ_.to_nalgebra_mixed(); | |
| 192 | let τλ = τ * λ; | |
| 193 | let mut inact : Vec<bool> = Vec::from_iter(std::iter::repeat(false).take(n)); | |
| 194 | let mut s = DVector::zeros(0); | |
| 195 | let mut decomp = nalgebra::linalg::LU::new(DMatrix::zeros(0, 0)); | |
| 196 | let mut iters = 0; | |
| 197 | ||
| 198 | let res = iterator.iterate_fallible(|state| { | |
| 199 | // 1. Perform delayed SSN-update based on previously computed step on active | |
| 200 | // coordinates. The step is delayed to the beginning of the loop because | |
| 201 | // the SSN step may violate constraints, so we arrange `x` to contain at the | |
| 202 | // end of the loop the valid FB step that forms part of the SSN step | |
| 203 | let mut si = s.iter(); | |
| 204 | for (&ast, x_i, xprev_i) in izip!(inact.iter(), x.iter_mut(), xprev.iter_mut()) { | |
| 205 | if ast { | |
| 206 | *x_i = *xprev_i + *si.next().unwrap() | |
| 207 | } | |
| 208 | *xprev_i = *x_i; | |
| 209 | } | |
| 210 | ||
| 211 | //xprev.copy_from(x); | |
| 212 | ||
| 213 | // 2. Calculate FB step. | |
| 214 | // 2.1. Replace `x` with $x⁻ - τ[Ax⁻-g]= [x⁻ + τg]- τAx⁻$ | |
| 215 | x.axpy(τ, g, 1.0); // x = x⁻ + τ*g | |
| 216 | x.sygemv(-τ, mA, &xprev, 1.0); // x = [x⁻ + τg]- τAx⁻ | |
| 217 | // 2.2. Calculate prox and set of active coordinates at the same time | |
| 218 | let mut act_changed = false; | |
| 219 | let mut n_inact = 0; | |
| 220 | for (x_i, ast) in izip!(x.iter_mut(), inact.iter_mut()) { | |
| 221 | if *x_i > τλ { | |
| 222 | *x_i -= τλ; | |
| 223 | if !*ast { | |
| 224 | act_changed = true; | |
| 225 | *ast = true; | |
| 226 | } | |
| 227 | n_inact += 1; | |
| 228 | } else { | |
| 229 | *x_i = 0.0; | |
| 230 | if *ast { | |
| 231 | act_changed = true; | |
| 232 | *ast = false; | |
| 233 | } | |
| 234 | } | |
| 235 | } | |
| 236 | ||
| 237 | // *** x now contains forward-backward step *** | |
| 238 | ||
| 239 | // 3. Solve SSN step `s`. | |
| 240 | // 3.1 Construct [τ A_{ℐ × ℐ}] if the set of inactive coordinates has changed. | |
| 241 | if act_changed { | |
| 242 | let decomp_iter = inact.iter().cartesian_product(inact.iter()).zip(mA.iter()); | |
| 243 | let decomp_constr = decomp_iter.filter_map(|((&i_inact, &j_inact), &mAij)| { | |
| 244 | //(i_inact && j_inact).then_some(mAij * τ) | |
| 245 | (i_inact && j_inact).then_some(mAij) // 🔺 below matches removal of τ | |
| 246 | }); | |
| 247 | let mat = DMatrix::from_iterator(n_inact, n_inact, decomp_constr); | |
| 248 | decomp = nalgebra::linalg::LU::new(mat); | |
| 249 | } | |
| 250 | ||
| 251 | // 3.2 Solve `s` = $s_ℐ^k$ from | |
| 252 | // $[τ A_{ℐ × ℐ}]s^k_ℐ = - x^k_ℐ + [G ∘ F](x^k)_ℐ - [τ A_{ℐ × 𝒜}]s^k_𝒜$. | |
| 253 | // With current variable setup we have $[G ∘ F](x^k) = $`x` and $x^k = x⁻$ = `xprev`, | |
| 254 | // so the system to solve is $[τ A_{ℐ × ℐ}]s^k_ℐ = (x-x⁻)_ℐ - [τ A_{ℐ × 𝒜}](x-x⁻)_𝒜$ | |
| 255 | // The matrix $[τ A_{ℐ × ℐ}]$ we have already LU-decomposed above into `decomp`. | |
| 256 | s = if n_inact > 0 { | |
| 257 | // 3.2.1 Construct `rhs` = $(x-x⁻)_ℐ - [τ A_{ℐ × 𝒜}](x-x⁻)_𝒜$ | |
| 258 | let inactfilt = inact.iter().copied(); | |
| 259 | let rhs_iter = izip!(x.iter(), xprev.iter(), mA.row_iter()).filter_zip(inactfilt); | |
| 260 | let rhs_constr = rhs_iter.map(|(&x_i, &xprev_i, mAi)| { | |
| 261 | // Calculate row i of [τ A_{ℐ × 𝒜}]s^k_𝒜 = [τ A_{ℐ × 𝒜}](x-xprev)_𝒜 | |
| 262 | let actfilt = inact.iter().copied().map(std::ops::Not::not); | |
| 263 | let actit = izip!(x.iter(), xprev.iter(), mAi.iter()).filter_zip(actfilt); | |
| 264 | let actpart = actit.map(|(&x_j, &xprev_j, &mAij)| { | |
| 265 | mAij * (x_j - xprev_j) | |
| 266 | }).sum(); | |
| 267 | // Subtract it from [x-prev]_i | |
| 268 | //x_i - xprev_i - τ * actpart | |
| 269 | (x_i - xprev_i) / τ - actpart // 🔺 change matches removal of τ above | |
| 270 | }); | |
| 271 | let mut rhs = DVector::from_iterator(n_inact, rhs_constr); | |
| 272 | assert_eq!(rhs.len(), n_inact); | |
| 273 | // Solve the system | |
| 274 | if !decomp.solve_mut(&mut rhs) { | |
| 275 | return Step::Failure(NumericalError( | |
| 276 | "Failed to solve linear system for subproblem SSN." | |
| 277 | )) | |
| 278 | } | |
| 279 | rhs | |
| 280 | } else { | |
| 281 | DVector::zeros(0) | |
| 282 | }; | |
| 283 | ||
| 284 | iters += 1; | |
| 285 | ||
| 286 | // 4. Report solution quality | |
| 287 | state.if_verbose(|| { | |
| 288 | // Calculate subdifferential at the FB step `x` that hasn't yet had `s` yet added. | |
|
34
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
289 | F::from_nalgebra_mixed(min_subdifferential(&mut v, mA, x, g, λ)) |
| 0 | 290 | }) |
| 291 | }); | |
| 292 | ||
| 293 | res.map(|_| iters) | |
| 294 | } | |
| 295 | ||
| 296 | /// This function applies an iterative method for the solution of the quadratic non-negativity | |
| 297 | /// constrained problem | |
| 298 | /// <div>$$ | |
|
34
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
299 | /// \min_{x ∈ ℝ^n} \frac{1}{2} x^⊤Ax - g^⊤ x + λ{\vec 1}^⊤ x + δ_{≥ 0}(x). |
| 0 | 300 | /// $$</div> |
| 301 | /// Semismooth Newton or forward-backward are supported based on the setting in `method`. | |
| 302 | /// The parameter `mA` is matrix $A$, and `g` and `λ` are as in the mathematical formulation. | |
| 303 | /// The constant $c$ does not need to be provided. The step length parameter is `τ` while | |
| 304 | /// `x` contains the initial iterate and on return the final one. The `iterator` controls | |
| 305 | /// stopping. The “verbose” value output by all methods is the $ℓ\_∞$ distance of some | |
| 306 | /// subdifferential of the objective to zero. | |
| 307 | /// | |
| 308 | /// Interior point methods could offer a further alternative, for example, the one in: | |
| 309 | /// | |
| 310 | /// * Valkonen T. - _A method for weighted projections to the positive definite | |
| 311 | /// cone_, <https://doi.org/10.1080/02331934.2014.929680>. | |
| 312 | /// | |
| 313 | /// This function returns the number of iterations taken. | |
| 314 | pub fn quadratic_nonneg<F, I>( | |
| 315 | mA : &DMatrix<F::MixedType>, | |
| 316 | g : &DVector<F::MixedType>, | |
| 317 | λ : F, | |
| 318 | x : &mut DVector<F::MixedType>, | |
|
34
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
319 | mA_normest : F, |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
320 | inner : &InnerSettings<F>, |
| 0 | 321 | iterator : I |
| 322 | ) -> usize | |
| 323 | where F : Float + ToNalgebraRealField, | |
| 324 | I : AlgIteratorFactory<F> | |
| 325 | { | |
|
34
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
326 | let inner_τ = inner.fb_τ0 / mA_normest; |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
327 | |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
328 | match inner.method { |
|
39
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
329 | InnerMethod::FB => |
|
34
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
330 | quadratic_nonneg_fb(mA, g, λ, x, inner_τ, iterator), |
| 0 | 331 | InnerMethod::SSN => |
|
34
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
332 | quadratic_nonneg_ssn(mA, g, λ, x, inner_τ, iterator).unwrap_or_else(|e| { |
| 0 | 333 | println!("{}", format!("{e}. Using FB fallback.").red()); |
| 334 | let ins = InnerSettings::<F>::default(); | |
|
34
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
335 | quadratic_nonneg_fb(mA, g, λ, x, inner_τ, ins.iterator_options) |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
336 | }), |
|
39
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
337 | other => unimplemented!("${other:?} is unimplemented"), |
| 0 | 338 | } |
| 339 | } |