Thu, 23 Jan 2025 23:35:28 +0100
Generic proximal penalty support
| 35 | 1 | /*! |
| 2 | Solver for the point source localisation problem using a | |
| 3 | primal-dual proximal splitting with a forward step. | |
| 4 | */ | |
| 5 | ||
| 6 | use numeric_literals::replace_float_literals; | |
| 7 | use serde::{Serialize, Deserialize}; | |
| 8 | ||
| 9 | use alg_tools::iterate::AlgIteratorFactory; | |
| 10 | use alg_tools::euclidean::Euclidean; | |
|
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
11 | use alg_tools::mapping::{Mapping, DifferentiableRealMapping, Instance}; |
| 35 | 12 | use alg_tools::norms::Norm; |
| 13 | use alg_tools::direct_product::Pair; | |
| 14 | use alg_tools::nalgebra_support::ToNalgebraRealField; | |
| 15 | use alg_tools::linops::{ | |
| 16 | BoundedLinear, AXPY, GEMV, Adjointable, IdOp, | |
| 17 | }; | |
| 18 | use alg_tools::convex::{Conjugable, Prox}; | |
|
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
19 | use alg_tools::norms::{L2, PairNorm}; |
| 35 | 20 | |
| 21 | use crate::types::*; | |
| 22 | use crate::measures::{DiscreteMeasure, Radon, RNDM}; | |
| 23 | use crate::measures::merging::SpikeMerging; | |
| 24 | use crate::forward_model::{ | |
| 25 | ForwardModel, | |
| 26 | AdjointProductPairBoundedBy, | |
| 27 | }; | |
| 28 | use crate::plot::{ | |
| 29 | SeqPlotter, | |
| 30 | Plotting, | |
| 31 | PlotLookup | |
| 32 | }; | |
| 33 | use crate::fb::*; | |
| 34 | use crate::regularisation::RegTerm; | |
| 35 | use crate::dataterm::calculate_residual; | |
| 36 | ||
| 37 | /// Settings for [`pointsource_forward_pdps_pair`]. | |
| 38 | #[derive(Clone, Copy, Eq, PartialEq, Serialize, Deserialize, Debug)] | |
| 39 | #[serde(default)] | |
| 40 | pub struct ForwardPDPSConfig<F : Float> { | |
| 41 | /// Primal step length scaling. | |
| 42 | pub τ0 : F, | |
| 43 | /// Primal step length scaling. | |
| 44 | pub σp0 : F, | |
| 45 | /// Dual step length scaling. | |
| 46 | pub σd0 : F, | |
| 47 | /// Generic parameters | |
| 48 | pub insertion : FBGenericConfig<F>, | |
| 49 | } | |
| 50 | ||
| 51 | #[replace_float_literals(F::cast_from(literal))] | |
| 52 | impl<F : Float> Default for ForwardPDPSConfig<F> { | |
| 53 | fn default() -> Self { | |
| 54 | let τ0 = 0.99; | |
| 55 | ForwardPDPSConfig { | |
| 56 | τ0, | |
| 57 | σd0 : 0.1, | |
| 58 | σp0 : 0.99, | |
| 59 | insertion : Default::default() | |
| 60 | } | |
| 61 | } | |
| 62 | } | |
| 63 | ||
| 64 | type MeasureZ<F, Z, const N : usize> = Pair<RNDM<F, N>, Z>; | |
| 65 | ||
| 66 | /// Iteratively solve the pointsource localisation with an additional variable | |
| 67 | /// using primal-dual proximal splitting with a forward step. | |
| 68 | #[replace_float_literals(F::cast_from(literal))] | |
| 69 | pub fn pointsource_forward_pdps_pair< | |
|
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
70 | F, I, A, S, Reg, P, Z, R, Y, /*KOpM, */ KOpZ, H, const N : usize |
| 35 | 71 | >( |
|
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
72 | opA : &A, |
| 35 | 73 | b : &A::Observable, |
| 74 | reg : Reg, | |
|
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
75 | prox_penalty : &P, |
| 35 | 76 | config : &ForwardPDPSConfig<F>, |
| 77 | iterator : I, | |
| 78 | mut plotter : SeqPlotter<F, N>, | |
| 79 | //opKμ : KOpM, | |
| 80 | opKz : &KOpZ, | |
| 81 | fnR : &R, | |
| 82 | fnH : &H, | |
| 83 | mut z : Z, | |
| 84 | mut y : Y, | |
| 85 | ) -> MeasureZ<F, Z, N> | |
| 86 | where | |
| 87 | F : Float + ToNalgebraRealField, | |
| 88 | I : AlgIteratorFactory<IterInfo<F, N>>, | |
| 89 | A : ForwardModel< | |
| 90 | MeasureZ<F, Z, N>, | |
| 91 | F, | |
| 92 | PairNorm<Radon, L2, L2>, | |
|
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
93 | PreadjointCodomain = Pair<S, Z>, |
| 35 | 94 | > |
|
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
95 | + AdjointProductPairBoundedBy<MeasureZ<F, Z, N>, P, IdOp<Z>, FloatType=F>, |
|
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
96 | S: DifferentiableRealMapping<F, N>, |
|
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
97 | for<'b> &'b A::Observable : std::ops::Neg<Output=A::Observable> + Instance<A::Observable>, |
| 35 | 98 | PlotLookup : Plotting<N>, |
| 99 | RNDM<F, N> : SpikeMerging<F>, | |
| 100 | Reg : RegTerm<F, N>, | |
|
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
101 | P : ProxPenalty<F, S, Reg, N>, |
| 35 | 102 | KOpZ : BoundedLinear<Z, L2, L2, F, Codomain=Y> |
| 103 | + GEMV<F, Z> | |
| 104 | + Adjointable<Z, Y, AdjointCodomain = Z>, | |
| 105 | for<'b> KOpZ::Adjoint<'b> : GEMV<F, Y>, | |
| 106 | Y : AXPY<F> + Euclidean<F, Output=Y> + Clone + ClosedAdd, | |
| 107 | for<'b> &'b Y : Instance<Y>, | |
| 108 | Z : AXPY<F, Owned=Z> + Euclidean<F, Output=Z> + Clone + Norm<F, L2>, | |
| 109 | for<'b> &'b Z : Instance<Z>, | |
| 110 | R : Prox<Z, Codomain=F>, | |
| 111 | H : Conjugable<Y, F, Codomain=F>, | |
| 112 | for<'b> H::Conjugate<'b> : Prox<Y>, | |
| 113 | { | |
| 114 | ||
| 115 | // Check parameters | |
| 116 | assert!(config.τ0 > 0.0 && | |
| 117 | config.τ0 < 1.0 && | |
| 118 | config.σp0 > 0.0 && | |
| 119 | config.σp0 < 1.0 && | |
| 120 | config.σd0 > 0.0 && | |
| 121 | config.σp0 * config.σd0 <= 1.0, | |
| 122 | "Invalid step length parameters"); | |
| 123 | ||
| 124 | // Initialise iterates | |
| 125 | let mut μ = DiscreteMeasure::new(); | |
| 126 | let mut residual = calculate_residual(Pair(&μ, &z), opA, b); | |
| 127 | ||
| 128 | // Set up parameters | |
|
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
129 | let bigM = 0.0; //opKμ.adjoint_product_bound(prox_penalty).unwrap().sqrt(); |
| 35 | 130 | let nKz = opKz.opnorm_bound(L2, L2); |
| 131 | let opIdZ = IdOp::new(); | |
|
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
132 | let (l, l_z) = opA.adjoint_product_pair_bound(prox_penalty, &opIdZ).unwrap(); |
| 35 | 133 | // We need to satisfy |
| 134 | // | |
| 135 | // τσ_dM(1-σ_p L_z)/(1 - τ L) + [σ_p L_z + σ_pσ_d‖K_z‖^2] < 1 | |
| 136 | // ^^^^^^^^^^^^^^^^^^^^^^^^^ | |
| 137 | // with 1 > σ_p L_z and 1 > τ L. | |
| 138 | // | |
| 139 | // To do so, we first solve σ_p and σ_d from standard PDPS step length condition | |
| 140 | // ^^^^^ < 1. then we solve τ from the rest. | |
| 141 | let σ_d = config.σd0 / nKz; | |
| 142 | let σ_p = config.σp0 / (l_z + config.σd0 * nKz); | |
| 143 | // Observe that = 1 - ^^^^^^^^^^^^^^^^^^^^^ = 1 - σ_{p,0} | |
| 144 | // We get the condition τσ_d M (1-σ_p L_z) < (1-σ_{p,0})*(1-τ L) | |
| 145 | // ⟺ τ [ σ_d M (1-σ_p L_z) + (1-σ_{p,0}) L ] < (1-σ_{p,0}) | |
| 146 | let φ = 1.0 - config.σp0; | |
| 147 | let a = 1.0 - σ_p * l_z; | |
| 148 | let τ = config.τ0 * φ / ( σ_d * bigM * a + φ * l ); | |
| 149 | // Acceleration is not currently supported | |
| 150 | // let γ = dataterm.factor_of_strong_convexity(); | |
| 151 | let ω = 1.0; | |
| 152 | ||
| 153 | // We multiply tolerance by τ for FB since our subproblems depending on tolerances are scaled | |
| 154 | // by τ compared to the conditional gradient approach. | |
| 155 | let tolerance = config.insertion.tolerance * τ * reg.tolerance_scaling(); | |
| 156 | let mut ε = tolerance.initial(); | |
| 157 | ||
| 158 | let starH = fnH.conjugate(); | |
| 159 | ||
| 160 | // Statistics | |
| 161 | let full_stats = |residual : &A::Observable, μ : &RNDM<F, N>, z : &Z, ε, stats| IterInfo { | |
| 162 | value : residual.norm2_squared_div2() + fnR.apply(z) | |
| 163 | + reg.apply(μ) + fnH.apply(/* opKμ.apply(μ) + */ opKz.apply(z)), | |
| 164 | n_spikes : μ.len(), | |
| 165 | ε, | |
| 166 | // postprocessing: config.insertion.postprocessing.then(|| μ.clone()), | |
| 167 | .. stats | |
| 168 | }; | |
| 169 | let mut stats = IterInfo::new(); | |
| 170 | ||
| 171 | // Run the algorithm | |
| 172 | for state in iterator.iter_init(|| full_stats(&residual, &μ, &z, ε, stats.clone())) { | |
| 173 | // Calculate initial transport | |
|
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
174 | let Pair(mut τv, τz) = opA.preadjoint().apply(residual * τ); |
| 35 | 175 | let z_base = z.clone(); |
| 176 | let μ_base = μ.clone(); | |
| 177 | ||
| 178 | // 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
|
179 | let (maybe_d, _within_tolerances) = prox_penalty.insert_and_reweigh( |
|
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
180 | &mut μ, &mut τv, &μ_base, None, |
| 35 | 181 | τ, ε, &config.insertion, |
| 182 | ®, &state, &mut stats, | |
| 183 | ); | |
| 184 | ||
| 185 | // // Merge spikes. | |
| 186 | // // This expects the prune below to prune γ. | |
| 187 | // // TODO: This may not work correctly in all cases. | |
| 188 | // let ins = &config.insertion; | |
| 189 | // if ins.merge_now(&state) { | |
| 190 | // if let SpikeMergingMethod::None = ins.merging { | |
| 191 | // } else { | |
| 192 | // stats.merged += μ.merge_spikes(ins.merging, |μ_candidate| { | |
| 193 | // let ν = μ_candidate.sub_matching(&γ1)-&μ_base_minus_γ0; | |
| 194 | // let mut d = &τv̆ + op𝒟.preapply(ν); | |
| 195 | // reg.verify_merge_candidate(&mut d, μ_candidate, τ, ε, ins) | |
| 196 | // }); | |
| 197 | // } | |
| 198 | // } | |
| 199 | ||
| 200 | // Prune spikes with zero weight. | |
| 201 | stats.pruned += prune_with_stats(&mut μ); | |
| 202 | ||
| 203 | // Do z variable primal update | |
| 204 | z.axpy(-σ_p/τ, τz, 1.0); // TODO: simplify nasty factors | |
| 205 | opKz.adjoint().gemv(&mut z, -σ_p, &y, 1.0); | |
| 206 | z = fnR.prox(σ_p, z); | |
| 207 | // Do dual update | |
| 208 | // opKμ.gemv(&mut y, σ_d*(1.0 + ω), &μ, 1.0); // y = y + σ_d K[(1+ω)(μ,z)^{k+1}] | |
| 209 | opKz.gemv(&mut y, σ_d*(1.0 + ω), &z, 1.0); | |
| 210 | // opKμ.gemv(&mut y, -σ_d*ω, μ_base, 1.0);// y = y + σ_d K[(1+ω)(μ,z)^{k+1} - ω (μ,z)^k]-b | |
| 211 | opKz.gemv(&mut y, -σ_d*ω, z_base, 1.0);// y = y + σ_d K[(1+ω)(μ,z)^{k+1} - ω (μ,z)^k]-b | |
| 212 | y = starH.prox(σ_d, y); | |
| 213 | ||
| 214 | // Update residual | |
| 215 | residual = calculate_residual(Pair(&μ, &z), opA, b); | |
| 216 | ||
| 217 | // Update step length parameters | |
| 218 | // let ω = pdpsconfig.acceleration.accelerate(&mut τ, &mut σ, γ); | |
| 219 | ||
| 220 | // Give statistics if requested | |
| 221 | let iter = state.iteration(); | |
| 222 | stats.this_iters += 1; | |
| 223 | ||
| 224 | state.if_verbose(|| { | |
|
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
225 | plotter.plot_spikes(iter, maybe_d.as_ref(), Some(&τv), &μ); |
| 35 | 226 | full_stats(&residual, &μ, &z, ε, std::mem::replace(&mut stats, IterInfo::new())) |
| 227 | }); | |
| 228 | ||
| 229 | // Update main tolerance for next iteration | |
| 230 | ε = tolerance.update(ε, iter); | |
| 231 | } | |
| 232 | ||
| 233 | let fit = |μ̃ : &RNDM<F, N>| { | |
| 234 | (opA.apply(Pair(μ̃, &z))-b).norm2_squared_div2() | |
| 235 | //+ fnR.apply(z) + reg.apply(μ) | |
| 236 | + fnH.apply(/* opKμ.apply(&μ̃) + */ opKz.apply(&z)) | |
| 237 | }; | |
| 238 | ||
| 239 | μ.merge_spikes_fitness(config.insertion.merging, fit, |&v| v); | |
| 240 | μ.prune(); | |
| 241 | Pair(μ, z) | |
| 242 | } |