Thu, 26 Feb 2026 11:38:43 -0500
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
| 32 | 1 | /*! |
| 2 | Solver for the point source localisation problem using a sliding | |
| 3 | forward-backward splitting method. | |
| 4 | */ | |
| 5 | ||
| 6 | use numeric_literals::replace_float_literals; | |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
7 | use serde::{Deserialize, Serialize}; |
| 32 | 8 | //use colored::Colorize; |
| 9 | //use nalgebra::{DVector, DMatrix}; | |
| 10 | use itertools::izip; | |
|
34
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
11 | use std::iter::Iterator; |
| 32 | 12 | |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
13 | use crate::fb::*; |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
14 | use crate::forward_model::{BoundedCurvature, BoundedCurvatureGuess}; |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
15 | use crate::measures::merging::SpikeMerging; |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
16 | use crate::measures::{DiscreteMeasure, Radon, RNDM}; |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
17 | use crate::plot::Plotter; |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
18 | use crate::prox_penalty::{ProxPenalty, StepLengthBound}; |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
19 | use crate::regularisation::SlidingRegTerm; |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
20 | use crate::types::*; |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
21 | use alg_tools::error::DynResult; |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
22 | use alg_tools::euclidean::Euclidean; |
| 35 | 23 | use alg_tools::iterate::AlgIteratorFactory; |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
24 | use alg_tools::mapping::{DifferentiableMapping, DifferentiableRealMapping}; |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
25 | use alg_tools::nalgebra_support::ToNalgebraRealField; |
| 35 | 26 | use alg_tools::norms::Norm; |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
27 | use anyhow::ensure; |
| 35 | 28 | |
| 29 | /// Transport settings for [`pointsource_sliding_fb_reg`]. | |
| 30 | #[derive(Clone, Copy, Eq, PartialEq, Serialize, Deserialize, Debug)] | |
| 31 | #[serde(default)] | |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
32 | pub struct TransportConfig<F: Float> { |
| 35 | 33 | /// Transport step length $θ$ normalised to $(0, 1)$. |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
34 | pub θ0: F, |
| 35 | 35 | /// Factor in $(0, 1)$ for decreasing transport to adapt to tolerance. |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
36 | pub adaptation: F, |
|
39
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
37 | /// A posteriori transport tolerance multiplier (C_pos) |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
38 | pub tolerance_mult_con: F, |
| 35 | 39 | } |
| 40 | ||
| 41 | #[replace_float_literals(F::cast_from(literal))] | |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
42 | impl<F: Float> TransportConfig<F> { |
| 35 | 43 | /// Check that the parameters are ok. Panics if not. |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
44 | pub fn check(&self) -> DynResult<()> { |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
45 | ensure!(self.θ0 > 0.0); |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
46 | ensure!(0.0 < self.adaptation && self.adaptation < 1.0); |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
47 | ensure!(self.tolerance_mult_con > 0.0); |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
48 | Ok(()) |
| 35 | 49 | } |
| 50 | } | |
| 51 | ||
| 52 | #[replace_float_literals(F::cast_from(literal))] | |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
53 | impl<F: Float> Default for TransportConfig<F> { |
| 35 | 54 | fn default() -> Self { |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
55 | TransportConfig { θ0: 0.9, adaptation: 0.9, tolerance_mult_con: 100.0 } |
| 35 | 56 | } |
| 57 | } | |
| 32 | 58 | |
| 59 | /// Settings for [`pointsource_sliding_fb_reg`]. | |
| 60 | #[derive(Clone, Copy, Eq, PartialEq, Serialize, Deserialize, Debug)] | |
| 61 | #[serde(default)] | |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
62 | pub struct SlidingFBConfig<F: Float> { |
| 32 | 63 | /// Step length scaling |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
64 | pub τ0: F, |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
65 | // Auxiliary variable step length scaling for [`crate::sliding_pdps::pointsource_sliding_fb_pair`] |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
66 | pub σp0: F, |
| 35 | 67 | /// Transport parameters |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
68 | pub transport: TransportConfig<F>, |
| 32 | 69 | /// Generic parameters |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
70 | pub insertion: InsertionConfig<F>, |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
71 | /// Guess for curvature bound calculations. |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
72 | pub guess: BoundedCurvatureGuess, |
| 32 | 73 | } |
| 74 | ||
| 75 | #[replace_float_literals(F::cast_from(literal))] | |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
76 | impl<F: Float> Default for SlidingFBConfig<F> { |
| 32 | 77 | fn default() -> Self { |
| 78 | SlidingFBConfig { | |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
79 | τ0: 0.99, |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
80 | σp0: 0.99, |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
81 | transport: Default::default(), |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
82 | insertion: Default::default(), |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
83 | guess: BoundedCurvatureGuess::BetterThanZero, |
| 32 | 84 | } |
| 85 | } | |
| 86 | } | |
| 87 | ||
| 35 | 88 | /// Internal type of adaptive transport step length calculation |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
89 | pub(crate) enum TransportStepLength<F: Float, G: Fn(F, F) -> F> { |
| 35 | 90 | /// Fixed, known step length |
| 44 | 91 | #[allow(dead_code)] |
| 35 | 92 | Fixed(F), |
| 93 | /// Adaptive step length, only wrt. maximum transport. | |
| 94 | /// Content of `l` depends on use case, while `g` calculates the step length from `l`. | |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
95 | AdaptiveMax { l: F, max_transport: F, g: G }, |
| 35 | 96 | /// Adaptive step length. |
| 97 | /// Content of `l` depends on use case, while `g` calculates the step length from `l`. | |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
98 | FullyAdaptive { l: F, max_transport: F, g: G }, |
| 35 | 99 | } |
| 100 | ||
|
41
b6bdb6cb4d44
Remove initial transport adaptation—it is not needed, after all
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
101 | /// Constrution of initial transport `γ1` from initial measure `μ` and `v=F'(μ)` |
|
b6bdb6cb4d44
Remove initial transport adaptation—it is not needed, after all
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
102 | /// with step lengh τ and transport step length `θ_or_adaptive`. |
| 32 | 103 | #[replace_float_literals(F::cast_from(literal))] |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
104 | pub(crate) fn initial_transport<F, G, D, const N: usize>( |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
105 | γ1: &mut RNDM<N, F>, |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
106 | μ: &mut RNDM<N, F>, |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
107 | τ: F, |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
108 | θ_or_adaptive: &mut TransportStepLength<F, G>, |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
109 | v: D, |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
110 | ) -> (Vec<F>, RNDM<N, F>) |
| 35 | 111 | where |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
112 | F: Float + ToNalgebraRealField, |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
113 | G: Fn(F, F) -> F, |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
114 | D: DifferentiableRealMapping<N, F>, |
| 35 | 115 | { |
| 116 | use TransportStepLength::*; | |
| 117 | ||
| 118 | // Save current base point and shift μ to new positions. Idea is that | |
| 119 | // μ_base(_masses) = μ^k (vector of masses) | |
| 120 | // μ_base_minus_γ0 = μ^k - π_♯^0γ^{k+1} | |
| 121 | // γ1 = π_♯^1γ^{k+1} | |
| 122 | // μ = μ^{k+1} | |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
123 | let μ_base_masses: Vec<F> = μ.iter_masses().collect(); |
| 35 | 124 | let mut μ_base_minus_γ0 = μ.clone(); // Weights will be set in the loop below. |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
125 | // Construct μ^{k+1} and π_♯^1γ^{k+1} initial candidates |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
126 | //let mut sum_norm_dv = 0.0; |
| 35 | 127 | let γ_prev_len = γ1.len(); |
| 128 | assert!(μ.len() >= γ_prev_len); | |
| 129 | γ1.extend(μ[γ_prev_len..].iter().cloned()); | |
| 130 | ||
| 131 | // Calculate initial transport and step length. | |
| 132 | // First calculate initial transported weights | |
| 133 | for (δ, ρ) in izip!(μ.iter_spikes(), γ1.iter_spikes_mut()) { | |
| 134 | // If old transport has opposing sign, the new transport will be none. | |
| 135 | ρ.α = if (ρ.α > 0.0 && δ.α < 0.0) || (ρ.α < 0.0 && δ.α > 0.0) { | |
| 136 | 0.0 | |
| 137 | } else { | |
| 138 | δ.α | |
| 139 | }; | |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
140 | } |
| 35 | 141 | |
|
41
b6bdb6cb4d44
Remove initial transport adaptation—it is not needed, after all
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
142 | // Calculate transport rays. |
| 35 | 143 | match *θ_or_adaptive { |
| 144 | Fixed(θ) => { | |
| 145 | let θτ = τ * θ; | |
| 146 | for (δ, ρ) in izip!(μ.iter_spikes(), γ1.iter_spikes_mut()) { | |
| 147 | ρ.x = δ.x - v.differential(&δ.x) * (ρ.α.signum() * θτ); | |
| 148 | } | |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
149 | } |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
150 | AdaptiveMax { l: ℓ_F, ref mut max_transport, g: ref calculate_θ } => { |
| 35 | 151 | *max_transport = max_transport.max(γ1.norm(Radon)); |
| 44 | 152 | let θτ = τ * calculate_θ(ℓ_F, *max_transport); |
| 35 | 153 | for (δ, ρ) in izip!(μ.iter_spikes(), γ1.iter_spikes_mut()) { |
| 154 | ρ.x = δ.x - v.differential(&δ.x) * (ρ.α.signum() * θτ); | |
| 155 | } | |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
156 | } |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
157 | FullyAdaptive { l: ref mut adaptive_ℓ_F, ref mut max_transport, g: ref calculate_θ } => { |
| 35 | 158 | *max_transport = max_transport.max(γ1.norm(Radon)); |
| 44 | 159 | let mut θ = calculate_θ(*adaptive_ℓ_F, *max_transport); |
|
39
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
160 | // Do two runs through the spikes to update θ, breaking if first run did not cause |
|
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
161 | // a change. |
|
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
162 | for _i in 0..=1 { |
|
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
163 | let mut changes = false; |
| 35 | 164 | for (δ, ρ) in izip!(μ.iter_spikes(), γ1.iter_spikes_mut()) { |
| 165 | let dv_x = v.differential(&δ.x); | |
|
39
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
166 | let g = &dv_x * (ρ.α.signum() * θ * τ); |
|
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
167 | ρ.x = δ.x - g; |
|
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
168 | let n = g.norm2(); |
|
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
169 | if n >= F::EPSILON { |
|
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
170 | // Estimate Lipschitz factor of ∇v |
| 44 | 171 | let this_ℓ_F = (dv_x - v.differential(&ρ.x)).norm2() / n; |
| 172 | *adaptive_ℓ_F = adaptive_ℓ_F.max(this_ℓ_F); | |
| 173 | θ = calculate_θ(*adaptive_ℓ_F, *max_transport); | |
|
39
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
174 | changes = true |
|
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
175 | } |
| 35 | 176 | } |
|
39
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
177 | if !changes { |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
178 | break; |
| 35 | 179 | } |
|
34
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
180 | } |
| 32 | 181 | } |
| 35 | 182 | } |
| 183 | ||
| 184 | // Set initial guess for μ=μ^{k+1}. | |
| 185 | for (δ, ρ, &β) in izip!(μ.iter_spikes_mut(), γ1.iter_spikes(), μ_base_masses.iter()) { | |
| 186 | if ρ.α.abs() > F::EPSILON { | |
| 187 | δ.x = ρ.x; | |
| 188 | //δ.α = ρ.α; // already set above | |
| 189 | } else { | |
| 190 | δ.α = β; | |
| 191 | } | |
| 192 | } | |
| 193 | // Calculate μ^k-π_♯^0γ^{k+1} and v̆ = A_*(A[μ_transported + μ_transported_base]-b) | |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
194 | μ_base_minus_γ0.set_masses( |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
195 | μ_base_masses |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
196 | .iter() |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
197 | .zip(γ1.iter_masses()) |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
198 | .map(|(&a, b)| a - b), |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
199 | ); |
| 35 | 200 | (μ_base_masses, μ_base_minus_γ0) |
| 201 | } | |
| 202 | ||
| 203 | /// A posteriori transport adaptation. | |
| 204 | #[replace_float_literals(F::cast_from(literal))] | |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
205 | pub(crate) fn aposteriori_transport<F, const N: usize>( |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
206 | γ1: &mut RNDM<N, F>, |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
207 | μ: &mut RNDM<N, F>, |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
208 | μ_base_minus_γ0: &mut RNDM<N, F>, |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
209 | μ_base_masses: &Vec<F>, |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
210 | extra: Option<F>, |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
211 | ε: F, |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
212 | tconfig: &TransportConfig<F>, |
| 35 | 213 | ) -> bool |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
214 | where |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
215 | F: Float + ToNalgebraRealField, |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
216 | { |
| 35 | 217 | // 1. If π_♯^1γ^{k+1} = γ1 has non-zero mass at some point y, but μ = μ^{k+1} does not, |
| 218 | // then the ansatz ∇w̃_x(y) = w^{k+1}(y) may not be satisfied. So set the mass of γ1 | |
| 219 | // at that point to zero, and retry. | |
| 220 | let mut all_ok = true; | |
| 221 | for (α_μ, α_γ1) in izip!(μ.iter_masses(), γ1.iter_masses_mut()) { | |
| 222 | if α_μ == 0.0 && *α_γ1 != 0.0 { | |
| 223 | all_ok = false; | |
| 224 | *α_γ1 = 0.0; | |
| 225 | } | |
| 226 | } | |
| 227 | ||
| 228 | // 2. Through bounding ∫ B_ω(y, z) dλ(x, y, z). | |
| 229 | // through the estimate ≤ C ‖Δ‖‖γ^{k+1}‖ for Δ := μ^{k+1}-μ^k-(π_♯^1-π_♯^0)γ^{k+1}, | |
| 230 | // which holds for some some C if the convolution kernel in 𝒟 has Lipschitz gradient. | |
| 231 | let nγ = γ1.norm(Radon); | |
|
39
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
232 | let nΔ = μ_base_minus_γ0.norm(Radon) + μ.dist_matching(&γ1) + extra.unwrap_or(0.0); |
| 46 | 233 | let t = ε * tconfig.tolerance_mult_con; |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
234 | if nγ * nΔ > t { |
| 35 | 235 | // Since t/(nγ*nΔ)<1, and the constant tconfig.adaptation < 1, |
| 236 | // this will guarantee that eventually ‖γ‖ decreases sufficiently that we | |
| 237 | // will not enter here. | |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
238 | *γ1 *= tconfig.adaptation * t / (nγ * nΔ); |
| 35 | 239 | all_ok = false |
| 240 | } | |
| 241 | ||
| 242 | if !all_ok { | |
| 243 | // Update weights for μ_base_minus_γ0 = μ^k - π_♯^0γ^{k+1} | |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
244 | μ_base_minus_γ0.set_masses( |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
245 | μ_base_masses |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
246 | .iter() |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
247 | .zip(γ1.iter_masses()) |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
248 | .map(|(&a, b)| a - b), |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
249 | ); |
| 35 | 250 | } |
| 251 | ||
| 252 | all_ok | |
| 32 | 253 | } |
| 254 | ||
| 255 | /// Iteratively solve the pointsource localisation problem using sliding forward-backward | |
| 256 | /// splitting | |
| 257 | /// | |
| 35 | 258 | /// The parametrisation is as for [`pointsource_fb_reg`]. |
| 32 | 259 | /// Inertia is currently not supported. |
| 260 | #[replace_float_literals(F::cast_from(literal))] | |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
261 | pub fn pointsource_sliding_fb_reg<F, I, Dat, Reg, Plot, P, const N: usize>( |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
262 | f: &Dat, |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
263 | reg: &Reg, |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
264 | prox_penalty: &P, |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
265 | config: &SlidingFBConfig<F>, |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
266 | iterator: I, |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
267 | mut plotter: Plot, |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
268 | μ0: Option<RNDM<N, F>>, |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
269 | ) -> DynResult<RNDM<N, F>> |
|
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
270 | where |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
271 | F: Float + ToNalgebraRealField, |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
272 | I: AlgIteratorFactory<IterInfo<F>>, |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
273 | Dat: DifferentiableMapping<RNDM<N, F>, Codomain = F> + BoundedCurvature<F>, |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
274 | Dat::DerivativeDomain: DifferentiableRealMapping<N, F> + ClosedMul<F>, |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
275 | //for<'a> Dat::Differential<'a>: Lipschitz<&'a P, FloatType = F>, |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
276 | RNDM<N, F>: SpikeMerging<F>, |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
277 | Reg: SlidingRegTerm<Loc<N, F>, F>, |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
278 | P: ProxPenalty<Loc<N, F>, Dat::DerivativeDomain, Reg, F> + StepLengthBound<F, Dat>, |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
279 | Plot: Plotter<P::ReturnMapping, Dat::DerivativeDomain, RNDM<N, F>>, |
|
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
280 | { |
| 35 | 281 | // Check parameters |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
282 | ensure!(config.τ0 > 0.0, "Invalid step length parameter"); |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
283 | config.transport.check()?; |
| 32 | 284 | |
| 285 | // Initialise iterates | |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
286 | let mut μ = μ0.unwrap_or_else(|| DiscreteMeasure::new()); |
|
34
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
287 | let mut γ1 = DiscreteMeasure::new(); |
| 35 | 288 | |
| 289 | // Set up parameters | |
|
41
b6bdb6cb4d44
Remove initial transport adaptation—it is not needed, after all
Tuomo Valkonen <tuomov@iki.fi>
parents:
39
diff
changeset
|
290 | // let opAnorm = opA.opnorm_bound(Radon, L2); |
| 35 | 291 | //let max_transport = config.max_transport.scale |
| 292 | // * reg.radon_norm_bound(b.norm2_squared() / 2.0); | |
| 293 | //let ℓ = opA.transport.lipschitz_factor(L2Squared) * max_transport; | |
| 294 | let ℓ = 0.0; | |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
295 | let τ = config.τ0 / prox_penalty.step_length_bound(&f)?; |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
296 | let (maybe_ℓ_F, maybe_transport_lip) = f.curvature_bound_components(config.guess); |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
297 | let transport_lip = maybe_transport_lip?; |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
298 | let calculate_θ = |ℓ_F, max_transport| { |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
299 | let ℓ_r = transport_lip * max_transport; |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
300 | config.transport.θ0 / (τ * (ℓ + ℓ_F + ℓ_r)) |
| 44 | 301 | }; |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
302 | let mut θ_or_adaptive = match maybe_ℓ_F { |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
303 | //Some(ℓ_F0) => TransportStepLength::Fixed(calculate_θ(ℓ_F0 * b.norm2(), 0.0)), |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
304 | Ok(ℓ_F) => TransportStepLength::AdaptiveMax { |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
305 | l: ℓ_F, // TODO: could estimate computing the real reesidual |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
306 | max_transport: 0.0, |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
307 | g: calculate_θ, |
| 44 | 308 | }, |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
309 | Err(_) => TransportStepLength::FullyAdaptive { |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
310 | l: 10.0 * F::EPSILON, // Start with something very small to estimate differentials |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
311 | max_transport: 0.0, |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
312 | g: calculate_θ, |
| 35 | 313 | }, |
| 314 | }; | |
| 315 | // We multiply tolerance by τ for FB since our subproblems depending on tolerances are scaled | |
| 316 | // by τ compared to the conditional gradient approach. | |
| 317 | let tolerance = config.insertion.tolerance * τ * reg.tolerance_scaling(); | |
| 318 | let mut ε = tolerance.initial(); | |
| 319 | ||
| 320 | // Statistics | |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
321 | let full_stats = |μ: &RNDM<N, F>, ε, stats| IterInfo { |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
322 | value: f.apply(μ) + reg.apply(μ), |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
323 | n_spikes: μ.len(), |
| 35 | 324 | ε, |
| 325 | // postprocessing: config.insertion.postprocessing.then(|| μ.clone()), | |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
326 | ..stats |
| 35 | 327 | }; |
| 32 | 328 | let mut stats = IterInfo::new(); |
| 329 | ||
| 330 | // Run the algorithm | |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
331 | for state in iterator.iter_init(|| full_stats(&μ, ε, stats.clone())) { |
| 35 | 332 | // Calculate initial transport |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
333 | let v = f.differential(&μ); |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
334 | let (μ_base_masses, mut μ_base_minus_γ0) = |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
335 | initial_transport(&mut γ1, &mut μ, τ, &mut θ_or_adaptive, v); |
|
34
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
336 | |
| 32 | 337 | // Solve finite-dimensional subproblem several times until the dual variable for the |
| 338 | // regularisation term conforms to the assumptions made for the transport above. | |
|
39
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
339 | let (maybe_d, _within_tolerances, mut τv̆) = 'adapt_transport: loop { |
| 35 | 340 | // Calculate τv̆ = τA_*(A[μ_transported + μ_transported_base]-b) |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
341 | //let residual_μ̆ = calculate_residual2(&γ1, &μ_base_minus_γ0, opA, b); |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
342 | // TODO: this could be optimised by doing the differential like the |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
343 | // old residual2. |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
344 | let μ̆ = &γ1 + &μ_base_minus_γ0; |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
345 | let mut τv̆ = f.differential(μ̆) * τ; |
| 32 | 346 | |
| 347 | // Construct μ^{k+1} by solving finite-dimensional subproblems and insert new spikes. | |
|
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
348 | let (maybe_d, within_tolerances) = prox_penalty.insert_and_reweigh( |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
349 | &mut μ, |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
350 | &mut τv̆, |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
351 | &γ1, |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
352 | Some(&μ_base_minus_γ0), |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
353 | τ, |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
354 | ε, |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
355 | &config.insertion, |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
356 | ®, |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
357 | &state, |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
358 | &mut stats, |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
359 | )?; |
| 32 | 360 | |
| 35 | 361 | // A posteriori transport adaptation. |
| 362 | if aposteriori_transport( | |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
363 | &mut γ1, |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
364 | &mut μ, |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
365 | &mut μ_base_minus_γ0, |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
366 | &μ_base_masses, |
|
39
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
367 | None, |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
368 | ε, |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
369 | &config.transport, |
| 35 | 370 | ) { |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
371 | break 'adapt_transport (maybe_d, within_tolerances, τv̆); |
| 32 | 372 | } |
| 373 | }; | |
| 374 | ||
|
34
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
375 | stats.untransported_fraction = Some({ |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
376 | assert_eq!(μ_base_masses.len(), γ1.len()); |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
377 | let (a, b) = stats.untransported_fraction.unwrap_or((0.0, 0.0)); |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
378 | let source = μ_base_masses.iter().map(|v| v.abs()).sum(); |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
379 | (a + μ_base_minus_γ0.norm(Radon), b + source) |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
380 | }); |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
381 | stats.transport_error = Some({ |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
382 | assert_eq!(μ_base_masses.len(), γ1.len()); |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
383 | let (a, b) = stats.transport_error.unwrap_or((0.0, 0.0)); |
| 35 | 384 | (a + μ.dist_matching(&γ1), b + γ1.norm(Radon)) |
|
34
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
385 | }); |
| 32 | 386 | |
|
39
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
387 | // Merge spikes. |
|
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
388 | // This crucially expects the merge routine to be stable with respect to spike locations, |
|
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
389 | // and not to performing any pruning. That is be to done below simultaneously for γ. |
|
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
390 | let ins = &config.insertion; |
|
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
391 | if ins.merge_now(&state) { |
|
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
392 | stats.merged += prox_penalty.merge_spikes( |
|
49
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
393 | &mut μ, |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
394 | &mut τv̆, |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
395 | &γ1, |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
396 | Some(&μ_base_minus_γ0), |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
397 | τ, |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
398 | ε, |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
399 | ins, |
|
6b0db7251ebe
Some documentation and factor changes related to ℓ_F and ℓ_r.
Tuomo Valkonen <tuomov@iki.fi>
parents:
46
diff
changeset
|
400 | ®, |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
401 | Some(|μ̃: &RNDM<N, F>| f.apply(μ̃)), |
|
39
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
402 | ); |
|
6316d68b58af
Merging adjustments, parameter tuning, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
37
diff
changeset
|
403 | } |
| 35 | 404 | |
|
34
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
405 | // Prune spikes with zero weight. To maintain correct ordering between μ and γ1, also the |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
406 | // latter needs to be pruned when μ is. |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
407 | // TODO: This could do with a two-vector Vec::retain to avoid copies. |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
408 | let μ_new = DiscreteMeasure::from_iter(μ.iter_spikes().filter(|δ| δ.α != F::ZERO).cloned()); |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
409 | if μ_new.len() != μ.len() { |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
410 | let mut μ_iter = μ.iter_spikes(); |
|
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
411 | γ1.prune_by(|_| μ_iter.next().unwrap().α != F::ZERO); |
| 35 | 412 | stats.pruned += μ.len() - μ_new.len(); |
|
34
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
413 | μ = μ_new; |
| 32 | 414 | } |
| 415 | ||
| 35 | 416 | let iter = state.iteration(); |
| 32 | 417 | stats.this_iters += 1; |
| 418 | ||
| 35 | 419 | // Give statistics if requested |
| 32 | 420 | state.if_verbose(|| { |
|
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
421 | plotter.plot_spikes(iter, maybe_d.as_ref(), Some(&τv̆), &μ); |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
422 | full_stats(&μ, ε, std::mem::replace(&mut stats, IterInfo::new())) |
| 35 | 423 | }); |
| 32 | 424 | |
| 35 | 425 | // Update main tolerance for next iteration |
| 426 | ε = tolerance.update(ε, iter); | |
| 427 | } | |
| 428 | ||
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
429 | //postprocess(μ, &config.insertion, f) |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
430 | postprocess(μ, &config.insertion, |μ̃| f.apply(μ̃)) |
| 32 | 431 | } |