|
1 /*! |
|
2 Basic proximal penalty based on convolution operators $𝒟$. |
|
3 */ |
|
4 |
|
5 use numeric_literals::replace_float_literals; |
|
6 use nalgebra::DVector; |
|
7 use colored::Colorize; |
|
8 |
|
9 use alg_tools::types::*; |
|
10 use alg_tools::loc::Loc; |
|
11 use alg_tools::mapping::{Mapping, RealMapping}; |
|
12 use alg_tools::nalgebra_support::ToNalgebraRealField; |
|
13 use alg_tools::norms::Linfinity; |
|
14 use alg_tools::iterate::{ |
|
15 AlgIteratorIteration, |
|
16 AlgIterator, |
|
17 }; |
|
18 use alg_tools::bisection_tree::{ |
|
19 BTFN, |
|
20 PreBTFN, |
|
21 Bounds, |
|
22 BTSearch, |
|
23 SupportGenerator, |
|
24 LocalAnalysis, |
|
25 BothGenerators, |
|
26 }; |
|
27 use crate::measures::{ |
|
28 RNDM, |
|
29 DeltaMeasure, |
|
30 Radon, |
|
31 }; |
|
32 use crate::measures::merging::{ |
|
33 SpikeMerging, |
|
34 }; |
|
35 use crate::seminorms::DiscreteMeasureOp; |
|
36 use crate::types::{ |
|
37 IterInfo, |
|
38 }; |
|
39 use crate::measures::merging::SpikeMergingMethod; |
|
40 use crate::regularisation::RegTerm; |
|
41 use super::{ProxPenalty, FBGenericConfig}; |
|
42 |
|
43 #[replace_float_literals(F::cast_from(literal))] |
|
44 impl<F, GA, BTA, S, Reg, 𝒟, G𝒟, K, const N : usize> |
|
45 ProxPenalty<F, BTFN<F, GA, BTA, N>, Reg, N> for 𝒟 |
|
46 where |
|
47 F : Float + ToNalgebraRealField, |
|
48 GA : SupportGenerator<F, N, SupportType = S, Id = usize> + Clone, |
|
49 BTA : BTSearch<F, N, Data=usize, Agg=Bounds<F>>, |
|
50 S: RealMapping<F, N> + LocalAnalysis<F, Bounds<F>, N>, |
|
51 G𝒟 : SupportGenerator<F, N, SupportType = K, Id = usize> + Clone, |
|
52 𝒟 : DiscreteMeasureOp<Loc<F, N>, F, PreCodomain = PreBTFN<F, G𝒟, N>>, |
|
53 𝒟::Codomain : RealMapping<F, N>, |
|
54 K : RealMapping<F, N> + LocalAnalysis<F, Bounds<F>, N>, |
|
55 Reg : RegTerm<F, N>, |
|
56 RNDM<F, N> : SpikeMerging<F>, |
|
57 { |
|
58 type ReturnMapping = BTFN<F, BothGenerators<GA, G𝒟>, BTA, N>; |
|
59 |
|
60 fn insert_and_reweigh<I>( |
|
61 &self, |
|
62 μ : &mut RNDM<F, N>, |
|
63 τv : &mut BTFN<F, GA, BTA, N>, |
|
64 μ_base : &RNDM<F, N>, |
|
65 ν_delta: Option<&RNDM<F, N>>, |
|
66 τ : F, |
|
67 ε : F, |
|
68 config : &FBGenericConfig<F>, |
|
69 reg : &Reg, |
|
70 state : &AlgIteratorIteration<I>, |
|
71 stats : &mut IterInfo<F, N>, |
|
72 ) -> (Option<BTFN<F, BothGenerators<GA, G𝒟>, BTA, N>>, bool) |
|
73 where |
|
74 I : AlgIterator |
|
75 { |
|
76 |
|
77 // TODO: is this inefficient to do in every iteration? |
|
78 let op𝒟norm = self.opnorm_bound(Radon, Linfinity); |
|
79 |
|
80 // Maximum insertion count and measure difference calculation depend on insertion style. |
|
81 let (max_insertions, warn_insertions) = match (state.iteration(), config.bootstrap_insertions) { |
|
82 (i, Some((l, k))) if i <= l => (k, false), |
|
83 _ => (config.max_insertions, !state.is_quiet()), |
|
84 }; |
|
85 |
|
86 let ω0 = match ν_delta { |
|
87 None => self.apply(μ_base), |
|
88 Some(ν) => self.apply(μ_base + ν), |
|
89 }; |
|
90 |
|
91 // Add points to support until within error tolerance or maximum insertion count reached. |
|
92 let mut count = 0; |
|
93 let (within_tolerances, d) = 'insertion: loop { |
|
94 if μ.len() > 0 { |
|
95 // Form finite-dimensional subproblem. The subproblem references to the original μ^k |
|
96 // from the beginning of the iteration are all contained in the immutable c and g. |
|
97 // TODO: observe negation of -τv after switch from minus_τv: finite-dimensional |
|
98 // problems have not yet been updated to sign change. |
|
99 let à = self.findim_matrix(μ.iter_locations()); |
|
100 let g̃ = DVector::from_iterator(μ.len(), |
|
101 μ.iter_locations() |
|
102 .map(|ζ| ω0.apply(ζ) - τv.apply(ζ)) |
|
103 .map(F::to_nalgebra_mixed)); |
|
104 let mut x = μ.masses_dvector(); |
|
105 |
|
106 // The gradient of the forward component of the inner objective is C^*𝒟Cx - g̃. |
|
107 // We have |C^*𝒟Cx|_2 = sup_{|z|_2 ≤ 1} ⟨z, C^*𝒟Cx⟩ = sup_{|z|_2 ≤ 1} ⟨Cz|𝒟Cx⟩ |
|
108 // ≤ sup_{|z|_2 ≤ 1} |Cz|_ℳ |𝒟Cx|_∞ ≤ sup_{|z|_2 ≤ 1} |Cz|_ℳ |𝒟| |Cx|_ℳ |
|
109 // ≤ sup_{|z|_2 ≤ 1} |z|_1 |𝒟| |x|_1 ≤ sup_{|z|_2 ≤ 1} n |z|_2 |𝒟| |x|_2 |
|
110 // = n |𝒟| |x|_2, where n is the number of points. Therefore |
|
111 let Ã_normest = op𝒟norm * F::cast_from(μ.len()); |
|
112 |
|
113 // Solve finite-dimensional subproblem. |
|
114 stats.inner_iters += reg.solve_findim(&Ã, &g̃, τ, &mut x, Ã_normest, ε, config); |
|
115 |
|
116 // Update masses of μ based on solution of finite-dimensional subproblem. |
|
117 μ.set_masses_dvector(&x); |
|
118 } |
|
119 |
|
120 // Form d = τv + 𝒟μ - ω0 = τv + 𝒟(μ - μ^k) for checking the proximate optimality |
|
121 // conditions in the predual space, and finding new points for insertion, if necessary. |
|
122 let mut d = &*τv + match ν_delta { |
|
123 None => self.preapply(μ.sub_matching(μ_base)), |
|
124 Some(ν) => self.preapply(μ.sub_matching(μ_base) - ν) |
|
125 }; |
|
126 |
|
127 // If no merging heuristic is used, let's be more conservative about spike insertion, |
|
128 // and skip it after first round. If merging is done, being more greedy about spike |
|
129 // insertion also seems to improve performance. |
|
130 let skip_by_rough_check = if let SpikeMergingMethod::None = config.merging { |
|
131 false |
|
132 } else { |
|
133 count > 0 |
|
134 }; |
|
135 |
|
136 // Find a spike to insert, if needed |
|
137 let (ξ, _v_ξ, in_bounds) = match reg.find_tolerance_violation( |
|
138 &mut d, τ, ε, skip_by_rough_check, config |
|
139 ) { |
|
140 None => break 'insertion (true, d), |
|
141 Some(res) => res, |
|
142 }; |
|
143 |
|
144 // Break if maximum insertion count reached |
|
145 if count >= max_insertions { |
|
146 break 'insertion (in_bounds, d) |
|
147 } |
|
148 |
|
149 // No point in optimising the weight here; the finite-dimensional algorithm is fast. |
|
150 *μ += DeltaMeasure { x : ξ, α : 0.0 }; |
|
151 count += 1; |
|
152 stats.inserted += 1; |
|
153 }; |
|
154 |
|
155 if !within_tolerances && warn_insertions { |
|
156 // Complain (but continue) if we failed to get within tolerances |
|
157 // by inserting more points. |
|
158 let err = format!("Maximum insertions reached without achieving \ |
|
159 subproblem solution tolerance"); |
|
160 println!("{}", err.red()); |
|
161 } |
|
162 |
|
163 (Some(d), within_tolerances) |
|
164 } |
|
165 |
|
166 fn merge_spikes( |
|
167 &self, |
|
168 μ : &mut RNDM<F, N>, |
|
169 τv : &mut BTFN<F, GA, BTA, N>, |
|
170 μ_base : &RNDM<F, N>, |
|
171 τ : F, |
|
172 ε : F, |
|
173 config : &FBGenericConfig<F>, |
|
174 reg : &Reg, |
|
175 ) -> usize |
|
176 { |
|
177 μ.merge_spikes(config.merging, |μ_candidate| { |
|
178 let mut d = &*τv + self.preapply(μ_candidate.sub_matching(μ_base)); |
|
179 reg.verify_merge_candidate(&mut d, μ_candidate, τ, ε, config) |
|
180 }) |
|
181 } |
|
182 } |