Thu, 23 Jan 2025 23:35:28 +0100
Generic proximal penalty support
0 | 1 | /*! |
2 | Experimental setups. | |
3 | */ | |
4 | ||
5 | //use numeric_literals::replace_float_literals; | |
6 | use serde::{Serialize, Deserialize}; | |
7 | use clap::ValueEnum; | |
8 | use std::collections::HashMap; | |
9 | use std::hash::{Hash, Hasher}; | |
10 | use std::collections::hash_map::DefaultHasher; | |
11 | ||
12 | use alg_tools::bisection_tree::*; | |
13 | use alg_tools::error::DynResult; | |
14 | use alg_tools::norms::Linfinity; | |
15 | ||
16 | use crate::ExperimentOverrides; | |
17 | use crate::kernels::*; | |
34
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
18 | use crate::kernels::SupportProductFirst as Prod; |
0 | 19 | use crate::pdps::PDPSConfig; |
20 | use crate::types::*; | |
21 | use crate::run::{ | |
22 | RunnableExperiment, | |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
23 | ExperimentV2, |
35 | 24 | ExperimentBiased, |
0 | 25 | Named, |
26 | DefaultAlgorithm, | |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
27 | AlgorithmConfig, |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
28 | ProxTerm |
0 | 29 | }; |
30 | //use crate::fb::FBGenericConfig; | |
31 | use crate::rand_distr::{SerializableNormal, SaltAndPepper}; | |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
32 | use crate::regularisation::Regularisation; |
35 | 33 | use alg_tools::euclidean::Euclidean; |
34 | use alg_tools::instance::Instance; | |
35 | use alg_tools::mapping::Mapping; | |
36 | use alg_tools::operator_arithmetic::{MappingSum, Weighted}; | |
0 | 37 | |
38 | /// Experiments shorthands, to be used with the command line parser | |
39 | ||
40 | #[derive(ValueEnum, Debug, Copy, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)] | |
41 | #[allow(non_camel_case_types)] | |
42 | pub enum DefaultExperiment { | |
43 | /// One dimension, cut gaussian spread, 2-norm-squared data fidelity | |
44 | #[clap(name = "1d")] | |
45 | Experiment1D, | |
46 | /// One dimension, “fast” spread, 2-norm-squared data fidelity | |
47 | #[clap(name = "1d_fast")] | |
48 | Experiment1DFast, | |
49 | /// Two dimensions, cut gaussian spread, 2-norm-squared data fidelity | |
50 | #[clap(name = "2d")] | |
51 | Experiment2D, | |
52 | /// Two dimensions, “fast” spread, 2-norm-squared data fidelity | |
53 | #[clap(name = "2d_fast")] | |
54 | Experiment2DFast, | |
55 | /// One dimension, cut gaussian spread, 1-norm data fidelity | |
56 | #[clap(name = "1d_l1")] | |
57 | Experiment1D_L1, | |
58 | /// One dimension, ‘“fast” spread, 1-norm data fidelity | |
59 | #[clap(name = "1d_l1_fast")] | |
60 | Experiment1D_L1_Fast, | |
61 | /// Two dimensions, cut gaussian spread, 1-norm data fidelity | |
62 | #[clap(name = "2d_l1")] | |
63 | Experiment2D_L1, | |
64 | /// Two dimensions, “fast” spread, 1-norm data fidelity | |
65 | #[clap(name = "2d_l1_fast")] | |
66 | Experiment2D_L1_Fast, | |
35 | 67 | /// One dimension, “fast” spread, 2-norm-squared data fidelity with extra TV-regularised bias |
68 | #[clap(name = "1d_tv_fast")] | |
69 | Experiment1D_TV_Fast, | |
0 | 70 | } |
71 | ||
72 | macro_rules! make_float_constant { | |
73 | ($name:ident = $value:expr) => { | |
74 | #[derive(Debug, Copy, Eq, PartialEq, Clone, Serialize, Deserialize)] | |
75 | #[serde(into = "float")] | |
76 | struct $name; | |
77 | impl Into<float> for $name { | |
78 | #[inline] | |
79 | fn into(self) -> float { $value } | |
80 | } | |
81 | impl Constant for $name { | |
82 | type Type = float; | |
83 | fn value(&self) -> float { $value } | |
84 | } | |
85 | } | |
86 | } | |
87 | ||
88 | /// Ground-truth measure spike locations and magnitudes for 1D experiments | |
89 | static MU_TRUE_1D_BASIC : [(float, float); 4] = [ | |
90 | (0.10, 10.0), | |
91 | (0.30, 2.0), | |
92 | (0.70, 3.0), | |
93 | (0.80, 5.0) | |
94 | ]; | |
95 | ||
96 | /// Ground-truth measure spike locations and magnitudes for 2D experiments | |
97 | static MU_TRUE_2D_BASIC : [([float; 2], float); 4] = [ | |
98 | ([0.15, 0.15], 10.0), | |
99 | ([0.75, 0.45], 2.0), | |
100 | ([0.80, 0.50], 4.0), | |
101 | ([0.30, 0.70], 5.0) | |
102 | ]; | |
103 | ||
35 | 104 | /// The $\{0,1\}$-valued characteristic function of a ball as a [`Mapping`]. |
105 | #[derive(Debug,Copy,Clone,Serialize,PartialEq)] | |
106 | struct BallCharacteristic<F : Float, const N : usize> { | |
107 | pub center : Loc<F, N>, | |
108 | pub radius : F, | |
109 | } | |
110 | ||
111 | impl<F : Float, const N : usize> Mapping<Loc<F, N>> for BallCharacteristic<F, N> { | |
112 | type Codomain =F; | |
113 | ||
114 | fn apply<I : Instance<Loc<F, N>>>(&self, i : I) -> F { | |
115 | if self.center.dist2(i) <= self.radius { | |
116 | F::ONE | |
117 | } else { | |
118 | F::ZERO | |
119 | } | |
120 | } | |
121 | } | |
122 | ||
0 | 123 | //#[replace_float_literals(F::cast_from(literal))] |
124 | impl DefaultExperiment { | |
125 | /// Convert the experiment shorthand into a runnable experiment configuration. | |
126 | pub fn get_experiment(&self, cli : &ExperimentOverrides<float>) -> DynResult<Box<dyn RunnableExperiment<float>>> { | |
127 | let name = "pointsource".to_string() | |
128 | + self.to_possible_value().unwrap().get_name(); | |
129 | ||
130 | let kernel_plot_width = 0.2; | |
131 | ||
132 | const BASE_SEED : u64 = 915373234; | |
133 | ||
134 | const N_SENSORS_1D : usize = 100; | |
135 | make_float_constant!(SensorWidth1D = 0.4/(N_SENSORS_1D as float)); | |
136 | ||
137 | const N_SENSORS_2D : usize = 16; | |
138 | make_float_constant!(SensorWidth2D = 0.4/(N_SENSORS_2D as float)); | |
139 | ||
140 | const N_SENSORS_2D_MORE : usize = 32; | |
141 | make_float_constant!(SensorWidth2DMore = 0.4/(N_SENSORS_2D_MORE as float)); | |
142 | ||
143 | make_float_constant!(Variance1 = 0.05.powi(2)); | |
144 | make_float_constant!(CutOff1 = 0.15); | |
145 | make_float_constant!(Hat1 = 0.16); | |
35 | 146 | make_float_constant!(HatBias = 0.05); |
0 | 147 | |
148 | // We use a different step length for PDPS in 2D experiments | |
149 | let pdps_2d = || { | |
150 | let τ0 = 3.0; | |
151 | PDPSConfig { | |
152 | τ0, | |
153 | σ0 : 0.99 / τ0, | |
154 | .. Default::default() | |
155 | } | |
156 | }; | |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
157 | let defaults_2d = HashMap::from([ |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
158 | (DefaultAlgorithm::PDPS, AlgorithmConfig::PDPS(pdps_2d(), ProxTerm::Wave)), |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
159 | (DefaultAlgorithm::RadonPDPS, AlgorithmConfig::PDPS(pdps_2d(), ProxTerm::RadonSquared)) |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
160 | ]); |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
161 | |
0 | 162 | // We add a hash of the experiment name to the configured |
163 | // noise seed to not use the same noise for different experiments. | |
164 | let mut h = DefaultHasher::new(); | |
165 | name.hash(&mut h); | |
166 | let noise_seed = cli.noise_seed.unwrap_or(BASE_SEED) + h.finish(); | |
167 | ||
168 | use DefaultExperiment::*; | |
169 | Ok(match self { | |
170 | Experiment1D => { | |
171 | let base_spread = Gaussian { variance : Variance1 }; | |
172 | let spread_cutoff = BallIndicator { r : CutOff1, exponent : Linfinity }; | |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
173 | Box::new(Named { name, data : ExperimentV2 { |
0 | 174 | domain : [[0.0, 1.0]].into(), |
175 | sensor_count : [N_SENSORS_1D], | |
25
79943be70720
Implement non-negativity constraints for the conditional gradient methods
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
176 | regularisation : Regularisation::NonnegRadon(cli.alpha.unwrap_or(0.09)), |
0 | 177 | noise_distr : SerializableNormal::new(0.0, cli.variance.unwrap_or(0.2))?, |
178 | dataterm : DataTerm::L2Squared, | |
179 | μ_hat : MU_TRUE_1D_BASIC.into(), | |
180 | sensor : BallIndicator { r : SensorWidth1D, exponent : Linfinity }, | |
181 | spread : Prod(spread_cutoff, base_spread), | |
182 | kernel : Prod(AutoConvolution(spread_cutoff), base_spread), | |
183 | kernel_plot_width, | |
184 | noise_seed, | |
185 | algorithm_defaults: HashMap::new(), | |
186 | }}) | |
187 | }, | |
188 | Experiment1DFast => { | |
189 | let base_spread = HatConv { radius : Hat1 }; | |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
190 | Box::new(Named { name, data : ExperimentV2 { |
0 | 191 | domain : [[0.0, 1.0]].into(), |
192 | sensor_count : [N_SENSORS_1D], | |
25
79943be70720
Implement non-negativity constraints for the conditional gradient methods
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
193 | regularisation : Regularisation::NonnegRadon(cli.alpha.unwrap_or(0.06)), |
0 | 194 | noise_distr : SerializableNormal::new(0.0, cli.variance.unwrap_or(0.2))?, |
195 | dataterm : DataTerm::L2Squared, | |
196 | μ_hat : MU_TRUE_1D_BASIC.into(), | |
197 | sensor : BallIndicator { r : SensorWidth1D, exponent : Linfinity }, | |
198 | spread : base_spread, | |
199 | kernel : base_spread, | |
200 | kernel_plot_width, | |
201 | noise_seed, | |
202 | algorithm_defaults: HashMap::new(), | |
203 | }}) | |
204 | }, | |
205 | Experiment2D => { | |
206 | let base_spread = Gaussian { variance : Variance1 }; | |
207 | let spread_cutoff = BallIndicator { r : CutOff1, exponent : Linfinity }; | |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
208 | Box::new(Named { name, data : ExperimentV2 { |
0 | 209 | domain : [[0.0, 1.0]; 2].into(), |
210 | sensor_count : [N_SENSORS_2D; 2], | |
25
79943be70720
Implement non-negativity constraints for the conditional gradient methods
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
211 | regularisation : Regularisation::NonnegRadon(cli.alpha.unwrap_or(0.19)), |
0 | 212 | noise_distr : SerializableNormal::new(0.0, cli.variance.unwrap_or(0.25))?, |
213 | dataterm : DataTerm::L2Squared, | |
214 | μ_hat : MU_TRUE_2D_BASIC.into(), | |
215 | sensor : BallIndicator { r : SensorWidth2D, exponent : Linfinity }, | |
216 | spread : Prod(spread_cutoff, base_spread), | |
217 | kernel : Prod(AutoConvolution(spread_cutoff), base_spread), | |
218 | kernel_plot_width, | |
219 | noise_seed, | |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
220 | algorithm_defaults: defaults_2d, |
0 | 221 | }}) |
222 | }, | |
223 | Experiment2DFast => { | |
224 | let base_spread = HatConv { radius : Hat1 }; | |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
225 | Box::new(Named { name, data : ExperimentV2 { |
0 | 226 | domain : [[0.0, 1.0]; 2].into(), |
227 | sensor_count : [N_SENSORS_2D; 2], | |
25
79943be70720
Implement non-negativity constraints for the conditional gradient methods
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
228 | regularisation : Regularisation::NonnegRadon(cli.alpha.unwrap_or(0.12)), |
0 | 229 | noise_distr : SerializableNormal::new(0.0, cli.variance.unwrap_or(0.15))?, //0.25 |
230 | dataterm : DataTerm::L2Squared, | |
231 | μ_hat : MU_TRUE_2D_BASIC.into(), | |
232 | sensor : BallIndicator { r : SensorWidth2D, exponent : Linfinity }, | |
233 | spread : base_spread, | |
234 | kernel : base_spread, | |
235 | kernel_plot_width, | |
236 | noise_seed, | |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
237 | algorithm_defaults: defaults_2d, |
0 | 238 | }}) |
239 | }, | |
240 | Experiment1D_L1 => { | |
241 | let base_spread = Gaussian { variance : Variance1 }; | |
242 | let spread_cutoff = BallIndicator { r : CutOff1, exponent : Linfinity }; | |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
243 | Box::new(Named { name, data : ExperimentV2 { |
0 | 244 | domain : [[0.0, 1.0]].into(), |
245 | sensor_count : [N_SENSORS_1D], | |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
246 | regularisation : Regularisation::NonnegRadon(cli.alpha.unwrap_or(0.1)), |
0 | 247 | noise_distr : SaltAndPepper::new( |
248 | cli.salt_and_pepper.as_ref().map_or(0.6, |v| v[0]), | |
249 | cli.salt_and_pepper.as_ref().map_or(0.4, |v| v[1]) | |
250 | )?, | |
251 | dataterm : DataTerm::L1, | |
252 | μ_hat : MU_TRUE_1D_BASIC.into(), | |
253 | sensor : BallIndicator { r : SensorWidth1D, exponent : Linfinity }, | |
254 | spread : Prod(spread_cutoff, base_spread), | |
255 | kernel : Prod(AutoConvolution(spread_cutoff), base_spread), | |
256 | kernel_plot_width, | |
257 | noise_seed, | |
258 | algorithm_defaults: HashMap::new(), | |
259 | }}) | |
260 | }, | |
261 | Experiment1D_L1_Fast => { | |
262 | let base_spread = HatConv { radius : Hat1 }; | |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
263 | Box::new(Named { name, data : ExperimentV2 { |
0 | 264 | domain : [[0.0, 1.0]].into(), |
265 | sensor_count : [N_SENSORS_1D], | |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
266 | regularisation : Regularisation::NonnegRadon(cli.alpha.unwrap_or(0.12)), |
0 | 267 | noise_distr : SaltAndPepper::new( |
268 | cli.salt_and_pepper.as_ref().map_or(0.6, |v| v[0]), | |
269 | cli.salt_and_pepper.as_ref().map_or(0.4, |v| v[1]) | |
270 | )?, | |
271 | dataterm : DataTerm::L1, | |
272 | μ_hat : MU_TRUE_1D_BASIC.into(), | |
273 | sensor : BallIndicator { r : SensorWidth1D, exponent : Linfinity }, | |
274 | spread : base_spread, | |
275 | kernel : base_spread, | |
276 | kernel_plot_width, | |
277 | noise_seed, | |
278 | algorithm_defaults: HashMap::new(), | |
279 | }}) | |
280 | }, | |
281 | Experiment2D_L1 => { | |
282 | let base_spread = Gaussian { variance : Variance1 }; | |
283 | let spread_cutoff = BallIndicator { r : CutOff1, exponent : Linfinity }; | |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
284 | Box::new(Named { name, data : ExperimentV2 { |
0 | 285 | domain : [[0.0, 1.0]; 2].into(), |
286 | sensor_count : [N_SENSORS_2D; 2], | |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
287 | regularisation : Regularisation::NonnegRadon(cli.alpha.unwrap_or(0.35)), |
0 | 288 | noise_distr : SaltAndPepper::new( |
289 | cli.salt_and_pepper.as_ref().map_or(0.8, |v| v[0]), | |
290 | cli.salt_and_pepper.as_ref().map_or(0.2, |v| v[1]) | |
291 | )?, | |
292 | dataterm : DataTerm::L1, | |
293 | μ_hat : MU_TRUE_2D_BASIC.into(), | |
294 | sensor : BallIndicator { r : SensorWidth2D, exponent : Linfinity }, | |
295 | spread : Prod(spread_cutoff, base_spread), | |
296 | kernel : Prod(AutoConvolution(spread_cutoff), base_spread), | |
297 | kernel_plot_width, | |
298 | noise_seed, | |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
299 | algorithm_defaults: defaults_2d, |
0 | 300 | }}) |
301 | }, | |
302 | Experiment2D_L1_Fast => { | |
303 | let base_spread = HatConv { radius : Hat1 }; | |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
304 | Box::new(Named { name, data : ExperimentV2 { |
0 | 305 | domain : [[0.0, 1.0]; 2].into(), |
306 | sensor_count : [N_SENSORS_2D; 2], | |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
307 | regularisation : Regularisation::NonnegRadon(cli.alpha.unwrap_or(0.40)), |
0 | 308 | noise_distr : SaltAndPepper::new( |
309 | cli.salt_and_pepper.as_ref().map_or(0.8, |v| v[0]), | |
310 | cli.salt_and_pepper.as_ref().map_or(0.2, |v| v[1]) | |
311 | )?, | |
312 | dataterm : DataTerm::L1, | |
313 | μ_hat : MU_TRUE_2D_BASIC.into(), | |
314 | sensor : BallIndicator { r : SensorWidth2D, exponent : Linfinity }, | |
315 | spread : base_spread, | |
316 | kernel : base_spread, | |
317 | kernel_plot_width, | |
318 | noise_seed, | |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
319 | algorithm_defaults: defaults_2d, |
0 | 320 | }}) |
321 | }, | |
35 | 322 | Experiment1D_TV_Fast => { |
323 | let base_spread = HatConv { radius : HatBias }; | |
324 | Box::new(Named { name, data : ExperimentBiased { | |
325 | λ : 0.02, | |
326 | bias : MappingSum::new([ | |
327 | Weighted::new(1.0, BallCharacteristic{ center : 0.3.into(), radius : 0.2 }), | |
328 | Weighted::new(0.5, BallCharacteristic{ center : 0.6.into(), radius : 0.3 }), | |
329 | ]), | |
330 | base : ExperimentV2 { | |
331 | domain : [[0.0, 1.0]].into(), | |
332 | sensor_count : [N_SENSORS_1D], | |
333 | regularisation : Regularisation::NonnegRadon(cli.alpha.unwrap_or(0.2)), | |
334 | noise_distr : SerializableNormal::new(0.0, cli.variance.unwrap_or(0.1))?, | |
335 | dataterm : DataTerm::L2Squared, | |
336 | μ_hat : MU_TRUE_1D_BASIC.into(), | |
337 | sensor : BallIndicator { r : SensorWidth1D, exponent : Linfinity }, | |
338 | spread : base_spread, | |
339 | kernel : base_spread, | |
340 | kernel_plot_width, | |
341 | noise_seed, | |
342 | algorithm_defaults: HashMap::new(), | |
343 | }, | |
344 | }}) | |
345 | }, | |
0 | 346 | }) |
347 | } | |
348 | } | |
349 |