Mon, 17 Feb 2025 14:10:52 -0500
Make some math in documentation render
34 | 1 | /*! |
2 | Iterative algorithms for solving the finite-dimensional subproblem without constraints. | |
3 | */ | |
4 | ||
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
5 | use itertools::izip; |
34 | 6 | use nalgebra::DVector; |
7 | use numeric_literals::replace_float_literals; | |
8 | use std::cmp::Ordering::*; | |
9 | ||
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
10 | use alg_tools::iterate::{AlgIteratorFactory, AlgIteratorState}; |
34 | 11 | use alg_tools::nalgebra_support::ToNalgebraRealField; |
12 | use alg_tools::nanleast::NaNLeast; | |
13 | use alg_tools::norms::{Dist, L1}; | |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
14 | use std::iter::zip; |
34 | 15 | |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
16 | use super::l1squared_nonneg::max_interval_dist_to_zero; |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
17 | use super::unconstrained::soft_thresholding; |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
18 | use super::{InnerMethod, InnerSettings}; |
34 | 19 | use crate::types::*; |
20 | ||
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
21 | /// Calculate $\prox_f(x)$ for $f(x)=\frac{β}{2}\norm{x-y}_1^2$. |
34 | 22 | /// |
23 | /// To derive an algorithm for this, we can assume that $y=0$, as | |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
24 | /// $\prox\_f(x) = \prox\_{f_0}(x - y) - y$ for $f\_0=\frac{β}{2}\norm{x}\_1^2$. |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
25 | /// Now, the optimality conditions for $w = \prox\_f(x)$ are |
34 | 26 | /// $$ |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
27 | /// 0 ∈ w-x + β\norm{w}\_1\sign w. |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
28 | /// $$ |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
29 | /// Clearly then $w = \soft\_{β\norm{w}\_1}(x)$. |
34 | 30 | /// Thus the components of $x$ with smallest absolute value will be zeroed out. |
31 | /// Denoting by $w'$ the non-zero components, and by $x'$ the corresponding components | |
32 | /// of $x$, and by $m$ their count, multipying the corresponding lines of (*) by $\sign x'$, | |
33 | /// we obtain | |
34 | /// $$ | |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
35 | /// \norm{x'}\_1 = (1+βm)\norm{w'}\_1. |
34 | 36 | /// $$ |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
37 | /// That is, $\norm{w}\_1=\norm{w'}\_1=\norm{x'}\_1/(1+βm)$. |
34 | 38 | /// Thus, sorting $x$ by absolute value, and sequentially in order eliminating the smallest |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
39 | /// elements, we can easily calculate what $\norm{w}\_1$ should be for that choice, and |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
40 | /// then easily calculate $w = \soft_{β\norm{w}\_1}(x)$. We just have to verify that |
34 | 41 | /// the resulting $w$ has the same norm. There's a shortcut to this, as we work |
42 | /// sequentially: just check that the smallest assumed-nonzero component $i$ satisfies the | |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
43 | /// condition of soft-thresholding to remain non-zero: $|x\_i|>τ\norm{x'}/(1+τm)$. |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
44 | /// Clearly, if this condition fails for $x\_i$, it will fail for all the components |
34 | 45 | /// already exluced. While, if it holds, it will hold for all components not excluded. |
46 | #[replace_float_literals(F::cast_from(literal))] | |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
47 | pub(super) fn l1squared_prox<F: Float + nalgebra::RealField>( |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
48 | sorted_abs: &mut DVector<F>, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
49 | x: &mut DVector<F>, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
50 | y: &DVector<F>, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
51 | β: F, |
34 | 52 | ) { |
53 | sorted_abs.copy_from(x); | |
54 | sorted_abs.axpy(-1.0, y, 1.0); | |
55 | sorted_abs.apply(|z_i| *z_i = num_traits::abs(*z_i)); | |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
56 | sorted_abs |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
57 | .as_mut_slice() |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
58 | .sort_unstable_by(|a, b| NaNLeast(*a).cmp(&NaNLeast(*b))); |
34 | 59 | |
60 | let mut n = sorted_abs.sum(); | |
61 | for (m, az_i) in zip((1..=x.len() as u32).rev(), sorted_abs) { | |
62 | // test first | |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
63 | let tmp = β * n / (1.0 + β * F::cast_from(m)); |
34 | 64 | if *az_i <= tmp { |
65 | // Fail | |
66 | n -= *az_i; | |
67 | } else { | |
68 | // Success | |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
69 | x.zip_apply(y, |x_i, y_i| { |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
70 | *x_i = y_i + soft_thresholding(*x_i - y_i, tmp) |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
71 | }); |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
72 | return; |
34 | 73 | } |
74 | } | |
75 | // m = 0 should always work, but x is zero. | |
76 | x.fill(0.0); | |
77 | } | |
78 | ||
79 | /// Returns the ∞-norm minimal subdifferential of $x ↦ (β/2)|x-y|_1^2 - g^⊤ x + λ\|x\|₁$ at $x$. | |
80 | /// | |
81 | /// `v` will be modified and cannot be trusted to contain useful values afterwards. | |
82 | #[replace_float_literals(F::cast_from(literal))] | |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
83 | fn min_subdifferential<F: Float + nalgebra::RealField>( |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
84 | y: &DVector<F>, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
85 | x: &DVector<F>, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
86 | g: &DVector<F>, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
87 | λ: F, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
88 | β: F, |
34 | 89 | ) -> F { |
90 | let mut val = 0.0; | |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
91 | let tmp = β * y.dist(x, L1); |
34 | 92 | for (&g_i, &x_i, y_i) in izip!(g.iter(), x.iter(), y.iter()) { |
93 | let (mut lb, mut ub) = (-g_i, -g_i); | |
94 | match x_i.partial_cmp(y_i) { | |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
95 | Some(Greater) => { |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
96 | lb += tmp; |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
97 | ub += tmp |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
98 | } |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
99 | Some(Less) => { |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
100 | lb -= tmp; |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
101 | ub -= tmp |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
102 | } |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
103 | Some(Equal) => { |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
104 | lb -= tmp; |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
105 | ub += tmp |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
106 | } |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
107 | None => {} |
34 | 108 | } |
109 | match x_i.partial_cmp(&0.0) { | |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
110 | Some(Greater) => { |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
111 | lb += λ; |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
112 | ub += λ |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
113 | } |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
114 | Some(Less) => { |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
115 | lb -= λ; |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
116 | ub -= λ |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
117 | } |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
118 | Some(Equal) => { |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
119 | lb -= λ; |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
120 | ub += λ |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
121 | } |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
122 | None => {} |
34 | 123 | }; |
124 | val = max_interval_dist_to_zero(val, lb, ub); | |
125 | } | |
126 | val | |
127 | } | |
128 | ||
129 | /// PDPS implementation of [`l1squared_unconstrained`]. | |
130 | /// For detailed documentation of the inputs and outputs, refer to there. | |
131 | /// | |
132 | /// The `λ` component of the model is handled in the proximal step instead of the gradient step | |
133 | /// for potential performance improvements. | |
134 | #[replace_float_literals(F::cast_from(literal).to_nalgebra_mixed())] | |
135 | pub fn l1squared_unconstrained_pdps<F, I>( | |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
136 | y: &DVector<F::MixedType>, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
137 | g: &DVector<F::MixedType>, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
138 | λ_: F, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
139 | β_: F, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
140 | x: &mut DVector<F::MixedType>, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
141 | τ_: F, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
142 | σ_: F, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
143 | iterator: I, |
34 | 144 | ) -> usize |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
145 | where |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
146 | F: Float + ToNalgebraRealField, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
147 | I: AlgIteratorFactory<F>, |
34 | 148 | { |
149 | let λ = λ_.to_nalgebra_mixed(); | |
150 | let β = β_.to_nalgebra_mixed(); | |
151 | let τ = τ_.to_nalgebra_mixed(); | |
152 | let σ = σ_.to_nalgebra_mixed(); | |
153 | let mut w = DVector::zeros(x.len()); | |
154 | let mut tmp = DVector::zeros(x.len()); | |
155 | let mut xprev = x.clone(); | |
156 | let mut iters = 0; | |
157 | ||
158 | iterator.iterate(|state| { | |
159 | // Primal step: x^{k+1} = prox_{τ|.-y|_1^2}(x^k - τ (w^k - g)) | |
160 | x.axpy(-τ, &w, 1.0); | |
161 | x.axpy(τ, g, 1.0); | |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
162 | l1squared_prox(&mut tmp, x, y, τ * β); |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
163 | |
34 | 164 | // Dual step: w^{k+1} = proj_{[-λ,λ]}(w^k + σ(2x^{k+1}-x^k)) |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
165 | w.axpy(2.0 * σ, x, 1.0); |
34 | 166 | w.axpy(-σ, &xprev, 1.0); |
167 | w.apply(|w_i| *w_i = num_traits::clamp(*w_i, -λ, λ)); | |
168 | xprev.copy_from(x); | |
169 | ||
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
170 | iters += 1; |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
171 | |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
172 | state.if_verbose(|| F::from_nalgebra_mixed(min_subdifferential(y, x, g, λ, β))) |
34 | 173 | }); |
174 | ||
175 | iters | |
176 | } | |
177 | ||
42
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
178 | /// Alternative PDPS implementation of [`l1squared_unconstrained`]. |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
179 | /// For detailed documentation of the inputs and outputs, refer to there. |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
180 | /// |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
181 | /// By not dualising the 1-norm, this should produce more sparse solutions than |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
182 | /// [`l1squared_unconstrained_pdps`]. |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
183 | /// |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
184 | /// The `λ` component of the model is handled in the proximal step instead of the gradient step |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
185 | /// for potential performance improvements. |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
186 | /// The parameter `θ` is used to multiply the rescale the operator (identity) of the PDPS model. |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
187 | /// We rewrite |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
188 | /// <div>$$ |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
189 | /// \begin{split} |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
190 | /// & \min_{x ∈ ℝ^n} \frac{β}{2} |x-y|_1^2 - g^⊤ x + λ\|x\|₁ \\ |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
191 | /// & = \min_{x ∈ ℝ^n} \max_{w} ⟨θ w, x⟩ - g^⊤ x + λ\|x\|₁ |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
192 | /// - \left(x ↦ \frac{β}{2θ} |x-y|_1^2 \right)^*(w). |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
193 | /// \end{split} |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
194 | /// $$</div> |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
195 | #[replace_float_literals(F::cast_from(literal).to_nalgebra_mixed())] |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
196 | pub fn l1squared_unconstrained_pdps_alt<F, I>( |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
197 | y: &DVector<F::MixedType>, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
198 | g: &DVector<F::MixedType>, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
199 | λ_: F, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
200 | β_: F, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
201 | x: &mut DVector<F::MixedType>, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
202 | τ_: F, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
203 | σ_: F, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
204 | θ_: F, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
205 | iterator: I, |
42
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
206 | ) -> usize |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
207 | where |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
208 | F: Float + ToNalgebraRealField, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
209 | I: AlgIteratorFactory<F>, |
42
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
210 | { |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
211 | let λ = λ_.to_nalgebra_mixed(); |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
212 | let τ = τ_.to_nalgebra_mixed(); |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
213 | let σ = σ_.to_nalgebra_mixed(); |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
214 | let θ = θ_.to_nalgebra_mixed(); |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
215 | let β = β_.to_nalgebra_mixed(); |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
216 | let σθ = σ * θ; |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
217 | let τθ = τ * θ; |
42
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
218 | let mut w = DVector::zeros(x.len()); |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
219 | let mut tmp = DVector::zeros(x.len()); |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
220 | let mut xprev = x.clone(); |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
221 | let mut iters = 0; |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
222 | |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
223 | iterator.iterate(|state| { |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
224 | // Primal step: x^{k+1} = soft_τλ(x^k - τ(θ w^k -g)) |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
225 | x.axpy(-τθ, &w, 1.0); |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
226 | x.axpy(τ, g, 1.0); |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
227 | x.apply(|x_i| *x_i = soft_thresholding(*x_i, τ * λ)); |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
228 | |
42
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
229 | // Dual step: with g(x) = (β/(2θ))‖x-y‖₁² and q = w^k + σ(2x^{k+1}-x^k), |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
230 | // we compute w^{k+1} = prox_{σg^*}(q) for |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
231 | // = q - σ prox_{g/σ}(q/σ) |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
232 | // = q - σ prox_{(β/(2θσ))‖.-y‖₁²}(q/σ) |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
233 | // = σ(q/σ - prox_{(β/(2θσ))‖.-y‖₁²}(q/σ)) |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
234 | // where q/σ = w^k/σ + (2x^{k+1}-x^k), |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
235 | w /= σ; |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
236 | w.axpy(2.0, x, 1.0); |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
237 | w.axpy(-1.0, &xprev, 1.0); |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
238 | xprev.copy_from(&w); // use xprev as temporary variable |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
239 | l1squared_prox(&mut tmp, &mut xprev, y, β / σθ); |
42
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
240 | w -= &xprev; |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
241 | w *= σ; |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
242 | xprev.copy_from(x); |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
243 | |
42
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
244 | iters += 1; |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
245 | |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
246 | state.if_verbose(|| F::from_nalgebra_mixed(min_subdifferential(y, x, g, λ, β))) |
42
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
247 | }); |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
248 | |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
249 | iters |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
250 | } |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
251 | |
34 | 252 | /// This function applies an iterative method for the solution of the problem |
253 | /// <div>$$ | |
254 | /// \min_{x ∈ ℝ^n} \frac{β}{2} |x-y|_1^2 - g^⊤ x + λ\|x\|₁. | |
255 | /// $$</div> | |
256 | /// Only PDPS is supported. | |
257 | /// | |
258 | /// This function returns the number of iterations taken. | |
259 | #[replace_float_literals(F::cast_from(literal))] | |
260 | pub fn l1squared_unconstrained<F, I>( | |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
261 | y: &DVector<F::MixedType>, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
262 | g: &DVector<F::MixedType>, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
263 | λ: F, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
264 | β: F, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
265 | x: &mut DVector<F::MixedType>, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
266 | inner: &InnerSettings<F>, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
267 | iterator: I, |
34 | 268 | ) -> usize |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
269 | where |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
270 | F: Float + ToNalgebraRealField, |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
271 | I: AlgIteratorFactory<F>, |
34 | 272 | { |
42
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
273 | // Estimate of ‖K‖ for K=θ Id. |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
274 | let inner_θ = 1.0; |
6a7365d73e4c
Fixes to Radon norm prox term inner algorithm
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
275 | let normest = inner_θ; |
34 | 276 | |
277 | let (inner_τ, inner_σ) = (inner.pdps_τσ0.0 / normest, inner.pdps_τσ0.1 / normest); | |
278 | ||
279 | match inner.method { | |
54
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
280 | InnerMethod::PDPS => { |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
281 | l1squared_unconstrained_pdps_alt(y, g, λ, β, x, inner_τ, inner_σ, inner_θ, iterator) |
b3312eee105c
Make some math in documentation render
Tuomo Valkonen <tuomov@iki.fi>
parents:
42
diff
changeset
|
282 | } |
39
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
283 | other => unimplemented!("${other:?} is unimplemented"), |
34 | 284 | } |
285 | } |