Wed, 22 Mar 2023 20:37:49 +0200
Bump version
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::*; | |
18 | use crate::kernels::{SupportProductFirst as Prod}; | |
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, |
0 | 24 | Named, |
25 | DefaultAlgorithm, | |
26 | AlgorithmConfig | |
27 | }; | |
28 | //use crate::fb::FBGenericConfig; | |
29 | 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
|
30 | use crate::regularisation::Regularisation; |
0 | 31 | |
32 | /// Experiments shorthands, to be used with the command line parser | |
33 | ||
34 | #[derive(ValueEnum, Debug, Copy, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)] | |
35 | #[allow(non_camel_case_types)] | |
36 | pub enum DefaultExperiment { | |
37 | /// One dimension, cut gaussian spread, 2-norm-squared data fidelity | |
38 | #[clap(name = "1d")] | |
39 | Experiment1D, | |
40 | /// One dimension, “fast” spread, 2-norm-squared data fidelity | |
41 | #[clap(name = "1d_fast")] | |
42 | Experiment1DFast, | |
43 | /// Two dimensions, cut gaussian spread, 2-norm-squared data fidelity | |
44 | #[clap(name = "2d")] | |
45 | Experiment2D, | |
46 | /// Two dimensions, “fast” spread, 2-norm-squared data fidelity | |
47 | #[clap(name = "2d_fast")] | |
48 | Experiment2DFast, | |
49 | /// One dimension, cut gaussian spread, 1-norm data fidelity | |
50 | #[clap(name = "1d_l1")] | |
51 | Experiment1D_L1, | |
52 | /// One dimension, ‘“fast” spread, 1-norm data fidelity | |
53 | #[clap(name = "1d_l1_fast")] | |
54 | Experiment1D_L1_Fast, | |
55 | /// Two dimensions, cut gaussian spread, 1-norm data fidelity | |
56 | #[clap(name = "2d_l1")] | |
57 | Experiment2D_L1, | |
58 | /// Two dimensions, “fast” spread, 1-norm data fidelity | |
59 | #[clap(name = "2d_l1_fast")] | |
60 | Experiment2D_L1_Fast, | |
61 | } | |
62 | ||
63 | macro_rules! make_float_constant { | |
64 | ($name:ident = $value:expr) => { | |
65 | #[derive(Debug, Copy, Eq, PartialEq, Clone, Serialize, Deserialize)] | |
66 | #[serde(into = "float")] | |
67 | struct $name; | |
68 | impl Into<float> for $name { | |
69 | #[inline] | |
70 | fn into(self) -> float { $value } | |
71 | } | |
72 | impl Constant for $name { | |
73 | type Type = float; | |
74 | fn value(&self) -> float { $value } | |
75 | } | |
76 | } | |
77 | } | |
78 | ||
79 | /// Ground-truth measure spike locations and magnitudes for 1D experiments | |
80 | static MU_TRUE_1D_BASIC : [(float, float); 4] = [ | |
81 | (0.10, 10.0), | |
82 | (0.30, 2.0), | |
83 | (0.70, 3.0), | |
84 | (0.80, 5.0) | |
85 | ]; | |
86 | ||
87 | /// Ground-truth measure spike locations and magnitudes for 2D experiments | |
88 | static MU_TRUE_2D_BASIC : [([float; 2], float); 4] = [ | |
89 | ([0.15, 0.15], 10.0), | |
90 | ([0.75, 0.45], 2.0), | |
91 | ([0.80, 0.50], 4.0), | |
92 | ([0.30, 0.70], 5.0) | |
93 | ]; | |
94 | ||
95 | //#[replace_float_literals(F::cast_from(literal))] | |
96 | impl DefaultExperiment { | |
97 | /// Convert the experiment shorthand into a runnable experiment configuration. | |
98 | pub fn get_experiment(&self, cli : &ExperimentOverrides<float>) -> DynResult<Box<dyn RunnableExperiment<float>>> { | |
99 | let name = "pointsource".to_string() | |
100 | + self.to_possible_value().unwrap().get_name(); | |
101 | ||
102 | let kernel_plot_width = 0.2; | |
103 | ||
104 | const BASE_SEED : u64 = 915373234; | |
105 | ||
106 | const N_SENSORS_1D : usize = 100; | |
107 | make_float_constant!(SensorWidth1D = 0.4/(N_SENSORS_1D as float)); | |
108 | ||
109 | const N_SENSORS_2D : usize = 16; | |
110 | make_float_constant!(SensorWidth2D = 0.4/(N_SENSORS_2D as float)); | |
111 | ||
112 | const N_SENSORS_2D_MORE : usize = 32; | |
113 | make_float_constant!(SensorWidth2DMore = 0.4/(N_SENSORS_2D_MORE as float)); | |
114 | ||
115 | make_float_constant!(Variance1 = 0.05.powi(2)); | |
116 | make_float_constant!(CutOff1 = 0.15); | |
117 | make_float_constant!(Hat1 = 0.16); | |
118 | ||
119 | // We use a different step length for PDPS in 2D experiments | |
120 | let pdps_2d = || { | |
121 | let τ0 = 3.0; | |
122 | PDPSConfig { | |
123 | τ0, | |
124 | σ0 : 0.99 / τ0, | |
125 | .. Default::default() | |
126 | } | |
127 | }; | |
128 | ||
129 | // We add a hash of the experiment name to the configured | |
130 | // noise seed to not use the same noise for different experiments. | |
131 | let mut h = DefaultHasher::new(); | |
132 | name.hash(&mut h); | |
133 | let noise_seed = cli.noise_seed.unwrap_or(BASE_SEED) + h.finish(); | |
134 | ||
135 | use DefaultExperiment::*; | |
136 | Ok(match self { | |
137 | Experiment1D => { | |
138 | let base_spread = Gaussian { variance : Variance1 }; | |
139 | 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
|
140 | Box::new(Named { name, data : ExperimentV2 { |
0 | 141 | domain : [[0.0, 1.0]].into(), |
142 | 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
|
143 | regularisation : Regularisation::NonnegRadon(cli.alpha.unwrap_or(0.09)), |
0 | 144 | noise_distr : SerializableNormal::new(0.0, cli.variance.unwrap_or(0.2))?, |
145 | dataterm : DataTerm::L2Squared, | |
146 | μ_hat : MU_TRUE_1D_BASIC.into(), | |
147 | sensor : BallIndicator { r : SensorWidth1D, exponent : Linfinity }, | |
148 | spread : Prod(spread_cutoff, base_spread), | |
149 | kernel : Prod(AutoConvolution(spread_cutoff), base_spread), | |
150 | kernel_plot_width, | |
151 | noise_seed, | |
152 | algorithm_defaults: HashMap::new(), | |
153 | }}) | |
154 | }, | |
155 | Experiment1DFast => { | |
156 | 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
|
157 | Box::new(Named { name, data : ExperimentV2 { |
0 | 158 | domain : [[0.0, 1.0]].into(), |
159 | 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
|
160 | regularisation : Regularisation::NonnegRadon(cli.alpha.unwrap_or(0.06)), |
0 | 161 | noise_distr : SerializableNormal::new(0.0, cli.variance.unwrap_or(0.2))?, |
162 | dataterm : DataTerm::L2Squared, | |
163 | μ_hat : MU_TRUE_1D_BASIC.into(), | |
164 | sensor : BallIndicator { r : SensorWidth1D, exponent : Linfinity }, | |
165 | spread : base_spread, | |
166 | kernel : base_spread, | |
167 | kernel_plot_width, | |
168 | noise_seed, | |
169 | algorithm_defaults: HashMap::new(), | |
170 | }}) | |
171 | }, | |
172 | Experiment2D => { | |
173 | let base_spread = Gaussian { variance : Variance1 }; | |
174 | 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
|
175 | Box::new(Named { name, data : ExperimentV2 { |
0 | 176 | domain : [[0.0, 1.0]; 2].into(), |
177 | 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
|
178 | regularisation : Regularisation::NonnegRadon(cli.alpha.unwrap_or(0.19)), |
0 | 179 | noise_distr : SerializableNormal::new(0.0, cli.variance.unwrap_or(0.25))?, |
180 | dataterm : DataTerm::L2Squared, | |
181 | μ_hat : MU_TRUE_2D_BASIC.into(), | |
182 | sensor : BallIndicator { r : SensorWidth2D, exponent : Linfinity }, | |
183 | spread : Prod(spread_cutoff, base_spread), | |
184 | kernel : Prod(AutoConvolution(spread_cutoff), base_spread), | |
185 | kernel_plot_width, | |
186 | noise_seed, | |
187 | algorithm_defaults: HashMap::from([ | |
188 | (DefaultAlgorithm::PDPS, AlgorithmConfig::PDPS(pdps_2d())) | |
189 | ]), | |
190 | }}) | |
191 | }, | |
192 | Experiment2DFast => { | |
193 | 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
|
194 | Box::new(Named { name, data : ExperimentV2 { |
0 | 195 | domain : [[0.0, 1.0]; 2].into(), |
196 | 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
|
197 | regularisation : Regularisation::NonnegRadon(cli.alpha.unwrap_or(0.12)), |
0 | 198 | noise_distr : SerializableNormal::new(0.0, cli.variance.unwrap_or(0.15))?, //0.25 |
199 | dataterm : DataTerm::L2Squared, | |
200 | μ_hat : MU_TRUE_2D_BASIC.into(), | |
201 | sensor : BallIndicator { r : SensorWidth2D, exponent : Linfinity }, | |
202 | spread : base_spread, | |
203 | kernel : base_spread, | |
204 | kernel_plot_width, | |
205 | noise_seed, | |
206 | algorithm_defaults: HashMap::from([ | |
207 | (DefaultAlgorithm::PDPS, AlgorithmConfig::PDPS(pdps_2d())) | |
208 | ]), | |
209 | }}) | |
210 | }, | |
211 | Experiment1D_L1 => { | |
212 | let base_spread = Gaussian { variance : Variance1 }; | |
213 | 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
|
214 | Box::new(Named { name, data : ExperimentV2 { |
0 | 215 | domain : [[0.0, 1.0]].into(), |
216 | 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
|
217 | regularisation : Regularisation::NonnegRadon(cli.alpha.unwrap_or(0.1)), |
0 | 218 | noise_distr : SaltAndPepper::new( |
219 | cli.salt_and_pepper.as_ref().map_or(0.6, |v| v[0]), | |
220 | cli.salt_and_pepper.as_ref().map_or(0.4, |v| v[1]) | |
221 | )?, | |
222 | dataterm : DataTerm::L1, | |
223 | μ_hat : MU_TRUE_1D_BASIC.into(), | |
224 | sensor : BallIndicator { r : SensorWidth1D, exponent : Linfinity }, | |
225 | spread : Prod(spread_cutoff, base_spread), | |
226 | kernel : Prod(AutoConvolution(spread_cutoff), base_spread), | |
227 | kernel_plot_width, | |
228 | noise_seed, | |
229 | algorithm_defaults: HashMap::new(), | |
230 | }}) | |
231 | }, | |
232 | Experiment1D_L1_Fast => { | |
233 | 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
|
234 | Box::new(Named { name, data : ExperimentV2 { |
0 | 235 | domain : [[0.0, 1.0]].into(), |
236 | 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
|
237 | regularisation : Regularisation::NonnegRadon(cli.alpha.unwrap_or(0.12)), |
0 | 238 | noise_distr : SaltAndPepper::new( |
239 | cli.salt_and_pepper.as_ref().map_or(0.6, |v| v[0]), | |
240 | cli.salt_and_pepper.as_ref().map_or(0.4, |v| v[1]) | |
241 | )?, | |
242 | dataterm : DataTerm::L1, | |
243 | μ_hat : MU_TRUE_1D_BASIC.into(), | |
244 | sensor : BallIndicator { r : SensorWidth1D, exponent : Linfinity }, | |
245 | spread : base_spread, | |
246 | kernel : base_spread, | |
247 | kernel_plot_width, | |
248 | noise_seed, | |
249 | algorithm_defaults: HashMap::new(), | |
250 | }}) | |
251 | }, | |
252 | Experiment2D_L1 => { | |
253 | let base_spread = Gaussian { variance : Variance1 }; | |
254 | 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
|
255 | Box::new(Named { name, data : ExperimentV2 { |
0 | 256 | domain : [[0.0, 1.0]; 2].into(), |
257 | 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
|
258 | regularisation : Regularisation::NonnegRadon(cli.alpha.unwrap_or(0.35)), |
0 | 259 | noise_distr : SaltAndPepper::new( |
260 | cli.salt_and_pepper.as_ref().map_or(0.8, |v| v[0]), | |
261 | cli.salt_and_pepper.as_ref().map_or(0.2, |v| v[1]) | |
262 | )?, | |
263 | dataterm : DataTerm::L1, | |
264 | μ_hat : MU_TRUE_2D_BASIC.into(), | |
265 | sensor : BallIndicator { r : SensorWidth2D, exponent : Linfinity }, | |
266 | spread : Prod(spread_cutoff, base_spread), | |
267 | kernel : Prod(AutoConvolution(spread_cutoff), base_spread), | |
268 | kernel_plot_width, | |
269 | noise_seed, | |
270 | algorithm_defaults: HashMap::from([ | |
271 | (DefaultAlgorithm::PDPS, AlgorithmConfig::PDPS(pdps_2d())) | |
272 | ]), | |
273 | }}) | |
274 | }, | |
275 | Experiment2D_L1_Fast => { | |
276 | 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
|
277 | Box::new(Named { name, data : ExperimentV2 { |
0 | 278 | domain : [[0.0, 1.0]; 2].into(), |
279 | 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
|
280 | regularisation : Regularisation::NonnegRadon(cli.alpha.unwrap_or(0.40)), |
0 | 281 | noise_distr : SaltAndPepper::new( |
282 | cli.salt_and_pepper.as_ref().map_or(0.8, |v| v[0]), | |
283 | cli.salt_and_pepper.as_ref().map_or(0.2, |v| v[1]) | |
284 | )?, | |
285 | dataterm : DataTerm::L1, | |
286 | μ_hat : MU_TRUE_2D_BASIC.into(), | |
287 | sensor : BallIndicator { r : SensorWidth2D, exponent : Linfinity }, | |
288 | spread : base_spread, | |
289 | kernel : base_spread, | |
290 | kernel_plot_width, | |
291 | noise_seed, | |
292 | algorithm_defaults: HashMap::from([ | |
293 | (DefaultAlgorithm::PDPS, AlgorithmConfig::PDPS(pdps_2d())) | |
294 | ]), | |
295 | }}) | |
296 | }, | |
297 | }) | |
298 | } | |
299 | } | |
300 |