Tue, 31 Dec 2024 09:34:24 -0500
Early transport sketches
0 | 1 | /*! |
2 | This module provides [`RunnableExperiment`] for running chosen algorithms on a chosen experiment. | |
3 | */ | |
4 | ||
5 | use numeric_literals::replace_float_literals; | |
6 | use colored::Colorize; | |
7 | use serde::{Serialize, Deserialize}; | |
8 | use serde_json; | |
9 | use nalgebra::base::DVector; | |
10 | use std::hash::Hash; | |
11 | use chrono::{DateTime, Utc}; | |
12 | use cpu_time::ProcessTime; | |
13 | use clap::ValueEnum; | |
14 | use std::collections::HashMap; | |
15 | use std::time::Instant; | |
16 | ||
17 | use rand::prelude::{ | |
18 | StdRng, | |
19 | SeedableRng | |
20 | }; | |
21 | use rand_distr::Distribution; | |
22 | ||
23 | use alg_tools::bisection_tree::*; | |
24 | use alg_tools::iterate::{ | |
25 | Timed, | |
26 | AlgIteratorOptions, | |
27 | Verbose, | |
28 | AlgIteratorFactory, | |
29 | }; | |
30 | use alg_tools::logger::Logger; | |
31 | use alg_tools::error::DynError; | |
32 | use alg_tools::tabledump::TableDump; | |
33 | use alg_tools::sets::Cube; | |
32 | 34 | use alg_tools::mapping::{RealMapping, Differentiable}; |
0 | 35 | use alg_tools::nalgebra_support::ToNalgebraRealField; |
36 | use alg_tools::euclidean::Euclidean; | |
37 | use alg_tools::lingrid::lingrid; | |
38 | use alg_tools::sets::SetOrd; | |
39 | ||
40 | use crate::kernels::*; | |
41 | use crate::types::*; | |
42 | use crate::measures::*; | |
43 | use crate::measures::merging::SpikeMerging; | |
44 | use crate::forward_model::*; | |
45 | use crate::fb::{ | |
46 | FBConfig, | |
32 | 47 | FBGenericConfig, |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
48 | pointsource_fb_reg, |
32 | 49 | pointsource_fista_reg, |
50 | }; | |
51 | use crate::sliding_fb::{ | |
52 | SlidingFBConfig, | |
53 | pointsource_sliding_fb_reg | |
0 | 54 | }; |
55 | use crate::pdps::{ | |
56 | PDPSConfig, | |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
57 | pointsource_pdps_reg, |
0 | 58 | }; |
59 | use crate::frank_wolfe::{ | |
60 | FWConfig, | |
61 | FWVariant, | |
25
79943be70720
Implement non-negativity constraints for the conditional gradient methods
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
62 | pointsource_fw_reg, |
79943be70720
Implement non-negativity constraints for the conditional gradient methods
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
63 | WeightOptim, |
0 | 64 | }; |
65 | use crate::subproblem::InnerSettings; | |
66 | use crate::seminorms::*; | |
67 | use crate::plot::*; | |
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
68 | use crate::{AlgorithmOverrides, CommandLineArgs}; |
20
90f77ad9a98d
Added command line option for (power) tolerance
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
69 | use crate::tolerance::Tolerance; |
32 | 70 | use crate::regularisation::{ |
71 | Regularisation, | |
72 | RadonRegTerm, | |
73 | NonnegRadonRegTerm | |
74 | }; | |
75 | use crate::dataterm::{ | |
76 | L1, | |
77 | L2Squared | |
78 | }; | |
79 | use alg_tools::norms::L2; | |
0 | 80 | |
81 | /// Available algorithms and their configurations | |
82 | #[derive(Copy, Clone, Debug, Serialize, Deserialize)] | |
83 | pub enum AlgorithmConfig<F : Float> { | |
84 | FB(FBConfig<F>), | |
32 | 85 | FISTA(FBConfig<F>), |
0 | 86 | FW(FWConfig<F>), |
87 | PDPS(PDPSConfig<F>), | |
32 | 88 | SlidingFB(SlidingFBConfig<F>), |
0 | 89 | } |
90 | ||
20
90f77ad9a98d
Added command line option for (power) tolerance
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
91 | fn unpack_tolerance<F : Float>(v : &Vec<F>) -> Tolerance<F> { |
90f77ad9a98d
Added command line option for (power) tolerance
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
92 | assert!(v.len() == 3); |
90f77ad9a98d
Added command line option for (power) tolerance
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
93 | Tolerance::Power { initial : v[0], factor : v[1], exponent : v[2] } |
90f77ad9a98d
Added command line option for (power) tolerance
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
94 | } |
90f77ad9a98d
Added command line option for (power) tolerance
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
95 | |
0 | 96 | impl<F : ClapFloat> AlgorithmConfig<F> { |
97 | /// Override supported parameters based on the command line. | |
98 | pub fn cli_override(self, cli : &AlgorithmOverrides<F>) -> Self { | |
99 | let override_fb_generic = |g : FBGenericConfig<F>| { | |
100 | FBGenericConfig { | |
101 | bootstrap_insertions : cli.bootstrap_insertions | |
102 | .as_ref() | |
103 | .map_or(g.bootstrap_insertions, | |
104 | |n| Some((n[0], n[1]))), | |
105 | merge_every : cli.merge_every.unwrap_or(g.merge_every), | |
106 | merging : cli.merging.clone().unwrap_or(g.merging), | |
107 | final_merging : cli.final_merging.clone().unwrap_or(g.final_merging), | |
20
90f77ad9a98d
Added command line option for (power) tolerance
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
108 | tolerance: cli.tolerance.as_ref().map(unpack_tolerance).unwrap_or(g.tolerance), |
0 | 109 | .. g |
110 | } | |
111 | }; | |
112 | ||
113 | use AlgorithmConfig::*; | |
114 | match self { | |
115 | FB(fb) => FB(FBConfig { | |
116 | τ0 : cli.tau0.unwrap_or(fb.τ0), | |
117 | insertion : override_fb_generic(fb.insertion), | |
118 | .. fb | |
119 | }), | |
32 | 120 | FISTA(fb) => FISTA(FBConfig { |
121 | τ0 : cli.tau0.unwrap_or(fb.τ0), | |
122 | insertion : override_fb_generic(fb.insertion), | |
123 | .. fb | |
124 | }), | |
0 | 125 | PDPS(pdps) => PDPS(PDPSConfig { |
126 | τ0 : cli.tau0.unwrap_or(pdps.τ0), | |
127 | σ0 : cli.sigma0.unwrap_or(pdps.σ0), | |
128 | acceleration : cli.acceleration.unwrap_or(pdps.acceleration), | |
129 | insertion : override_fb_generic(pdps.insertion), | |
130 | .. pdps | |
131 | }), | |
132 | FW(fw) => FW(FWConfig { | |
133 | merging : cli.merging.clone().unwrap_or(fw.merging), | |
20
90f77ad9a98d
Added command line option for (power) tolerance
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
134 | tolerance : cli.tolerance.as_ref().map(unpack_tolerance).unwrap_or(fw.tolerance), |
0 | 135 | .. fw |
32 | 136 | }), |
137 | SlidingFB(sfb) => SlidingFB(SlidingFBConfig { | |
138 | τ0 : cli.tau0.unwrap_or(sfb.τ0), | |
139 | insertion : override_fb_generic(sfb.insertion), | |
140 | .. sfb | |
141 | }), | |
0 | 142 | } |
143 | } | |
144 | } | |
145 | ||
146 | /// Helper struct for tagging and [`AlgorithmConfig`] or [`Experiment`] with a name. | |
147 | #[derive(Clone, Debug, Serialize, Deserialize)] | |
148 | pub struct Named<Data> { | |
149 | pub name : String, | |
150 | #[serde(flatten)] | |
151 | pub data : Data, | |
152 | } | |
153 | ||
154 | /// Shorthand algorithm configurations, to be used with the command line parser | |
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
155 | #[derive(ValueEnum, Debug, Copy, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)] |
0 | 156 | pub enum DefaultAlgorithm { |
157 | /// The μFB forward-backward method | |
158 | #[clap(name = "fb")] | |
159 | FB, | |
160 | /// The μFISTA inertial forward-backward method | |
161 | #[clap(name = "fista")] | |
162 | FISTA, | |
163 | /// The “fully corrective” conditional gradient method | |
164 | #[clap(name = "fw")] | |
165 | FW, | |
166 | /// The “relaxed conditional gradient method | |
167 | #[clap(name = "fwrelax")] | |
168 | FWRelax, | |
169 | /// The μPDPS primal-dual proximal splitting method | |
170 | #[clap(name = "pdps")] | |
171 | PDPS, | |
32 | 172 | /// The Sliding FB method |
173 | #[clap(name = "sliding_fb", alias = "sfb")] | |
174 | SlidingFB, | |
0 | 175 | } |
176 | ||
177 | impl DefaultAlgorithm { | |
178 | /// Returns the algorithm configuration corresponding to the algorithm shorthand | |
179 | pub fn default_config<F : Float>(&self) -> AlgorithmConfig<F> { | |
180 | use DefaultAlgorithm::*; | |
181 | match *self { | |
182 | FB => AlgorithmConfig::FB(Default::default()), | |
32 | 183 | FISTA => AlgorithmConfig::FISTA(Default::default()), |
0 | 184 | FW => AlgorithmConfig::FW(Default::default()), |
185 | FWRelax => AlgorithmConfig::FW(FWConfig{ | |
186 | variant : FWVariant::Relaxed, | |
187 | .. Default::default() | |
188 | }), | |
189 | PDPS => AlgorithmConfig::PDPS(Default::default()), | |
32 | 190 | SlidingFB => AlgorithmConfig::SlidingFB(Default::default()), |
0 | 191 | } |
192 | } | |
193 | ||
194 | /// Returns the [`Named`] algorithm corresponding to the algorithm shorthand | |
195 | pub fn get_named<F : Float>(&self) -> Named<AlgorithmConfig<F>> { | |
196 | self.to_named(self.default_config()) | |
197 | } | |
198 | ||
199 | pub fn to_named<F : Float>(self, alg : AlgorithmConfig<F>) -> Named<AlgorithmConfig<F>> { | |
200 | let name = self.to_possible_value().unwrap().get_name().to_string(); | |
201 | Named{ name , data : alg } | |
202 | } | |
203 | } | |
204 | ||
205 | ||
206 | // // Floats cannot be hashed directly, so just hash the debug formatting | |
207 | // // for use as file identifier. | |
208 | // impl<F : Float> Hash for AlgorithmConfig<F> { | |
209 | // fn hash<H: Hasher>(&self, state: &mut H) { | |
210 | // format!("{:?}", self).hash(state); | |
211 | // } | |
212 | // } | |
213 | ||
214 | /// Plotting level configuration | |
215 | #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Serialize, ValueEnum, Debug)] | |
216 | pub enum PlotLevel { | |
217 | /// Plot nothing | |
218 | #[clap(name = "none")] | |
219 | None, | |
220 | /// Plot problem data | |
221 | #[clap(name = "data")] | |
222 | Data, | |
223 | /// Plot iterationwise state | |
224 | #[clap(name = "iter")] | |
225 | Iter, | |
226 | } | |
227 | ||
228 | type DefaultBT<F, const N : usize> = BT< | |
229 | DynamicDepth, | |
230 | F, | |
231 | usize, | |
232 | Bounds<F>, | |
233 | N | |
234 | >; | |
235 | type DefaultSeminormOp<F, K, const N : usize> = ConvolutionOp<F, K, DefaultBT<F, N>, N>; | |
236 | type DefaultSG<F, Sensor, Spread, const N : usize> = SensorGrid::< | |
237 | F, | |
238 | Sensor, | |
239 | Spread, | |
240 | DefaultBT<F, N>, | |
241 | N | |
242 | >; | |
243 | ||
244 | /// This is a dirty workaround to rust-csv not supporting struct flattening etc. | |
245 | #[derive(Serialize)] | |
246 | struct CSVLog<F> { | |
247 | iter : usize, | |
248 | cpu_time : f64, | |
249 | value : F, | |
250 | post_value : F, | |
251 | n_spikes : usize, | |
252 | inner_iters : usize, | |
253 | merged : usize, | |
254 | pruned : usize, | |
255 | this_iters : usize, | |
256 | } | |
257 | ||
258 | /// Collected experiment statistics | |
259 | #[derive(Clone, Debug, Serialize)] | |
260 | struct ExperimentStats<F : Float> { | |
261 | /// Signal-to-noise ratio in decibels | |
262 | ssnr : F, | |
263 | /// Proportion of noise in the signal as a number in $[0, 1]$. | |
264 | noise_ratio : F, | |
265 | /// When the experiment was run (UTC) | |
266 | when : DateTime<Utc>, | |
267 | } | |
268 | ||
269 | #[replace_float_literals(F::cast_from(literal))] | |
270 | impl<F : Float> ExperimentStats<F> { | |
271 | /// Calculate [`ExperimentStats`] based on a noisy `signal` and the separated `noise` signal. | |
272 | fn new<E : Euclidean<F>>(signal : &E, noise : &E) -> Self { | |
273 | let s = signal.norm2_squared(); | |
274 | let n = noise.norm2_squared(); | |
275 | let noise_ratio = (n / s).sqrt(); | |
276 | let ssnr = 10.0 * (s / n).log10(); | |
277 | ExperimentStats { | |
278 | ssnr, | |
279 | noise_ratio, | |
280 | when : Utc::now(), | |
281 | } | |
282 | } | |
283 | } | |
284 | /// Collected algorithm statistics | |
285 | #[derive(Clone, Debug, Serialize)] | |
286 | struct AlgorithmStats<F : Float> { | |
287 | /// Overall CPU time spent | |
288 | cpu_time : F, | |
289 | /// Real time spent | |
290 | elapsed : F | |
291 | } | |
292 | ||
293 | ||
294 | /// A wrapper for [`serde_json::to_writer_pretty`] that takes a filename as input | |
295 | /// and outputs a [`DynError`]. | |
296 | fn write_json<T : Serialize>(filename : String, data : &T) -> DynError { | |
297 | serde_json::to_writer_pretty(std::fs::File::create(filename)?, data)?; | |
298 | Ok(()) | |
299 | } | |
300 | ||
301 | ||
302 | /// Struct for experiment configurations | |
303 | #[derive(Debug, Clone, Serialize)] | |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
304 | pub struct ExperimentV2<F, NoiseDistr, S, K, P, const N : usize> |
0 | 305 | where F : Float, |
306 | [usize; N] : Serialize, | |
307 | NoiseDistr : Distribution<F>, | |
308 | S : Sensor<F, N>, | |
309 | P : Spread<F, N>, | |
310 | K : SimpleConvolutionKernel<F, N>, | |
311 | { | |
312 | /// Domain $Ω$. | |
313 | pub domain : Cube<F, N>, | |
314 | /// Number of sensors along each dimension | |
315 | pub sensor_count : [usize; N], | |
316 | /// Noise distribution | |
317 | pub noise_distr : NoiseDistr, | |
318 | /// Seed for random noise generation (for repeatable experiments) | |
319 | pub noise_seed : u64, | |
320 | /// Sensor $θ$; $θ * ψ$ forms the forward operator $𝒜$. | |
321 | pub sensor : S, | |
322 | /// Spread $ψ$; $θ * ψ$ forms the forward operator $𝒜$. | |
323 | pub spread : P, | |
324 | /// Kernel $ρ$ of $𝒟$. | |
325 | pub kernel : K, | |
326 | /// True point sources | |
327 | pub μ_hat : DiscreteMeasure<Loc<F, N>, F>, | |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
328 | /// Regularisation term and parameter |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
329 | pub regularisation : Regularisation<F>, |
0 | 330 | /// For plotting : how wide should the kernels be plotted |
331 | pub kernel_plot_width : F, | |
332 | /// Data term | |
333 | pub dataterm : DataTerm, | |
334 | /// A map of default configurations for algorithms | |
335 | #[serde(skip)] | |
336 | pub algorithm_defaults : HashMap<DefaultAlgorithm, AlgorithmConfig<F>>, | |
337 | } | |
338 | ||
339 | /// Trait for runnable experiments | |
340 | pub trait RunnableExperiment<F : ClapFloat> { | |
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
341 | /// Run all algorithms provided, or default algorithms if none provided, on the experiment. |
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
342 | fn runall(&self, cli : &CommandLineArgs, |
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
343 | algs : Option<Vec<Named<AlgorithmConfig<F>>>>) -> DynError; |
0 | 344 | |
345 | /// Return algorithm default config | |
346 | fn algorithm_defaults(&self, alg : DefaultAlgorithm, cli : &AlgorithmOverrides<F>) | |
347 | -> Named<AlgorithmConfig<F>>; | |
348 | } | |
349 | ||
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
350 | // *** macro boilerplate *** |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
351 | macro_rules! impl_experiment { |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
352 | ($type:ident, $reg_field:ident, $reg_convert:path) => { |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
353 | // *** macro *** |
0 | 354 | impl<F, NoiseDistr, S, K, P, const N : usize> RunnableExperiment<F> for |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
355 | Named<$type<F, NoiseDistr, S, K, P, N>> |
0 | 356 | where F : ClapFloat + nalgebra::RealField + ToNalgebraRealField<MixedType=F>, |
357 | [usize; N] : Serialize, | |
23
9869fa1e0ccd
Print out experiment information when running it
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
358 | S : Sensor<F, N> + Copy + Serialize + std::fmt::Debug, |
9869fa1e0ccd
Print out experiment information when running it
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
359 | P : Spread<F, N> + Copy + Serialize + std::fmt::Debug, |
32 | 360 | Convolution<S, P>: Spread<F, N> + Bounded<F> + LocalAnalysis<F, Bounds<F>, N> + Copy |
361 | // TODO: shold not have differentiability as a requirement, but | |
362 | // decide availability of sliding based on it. | |
363 | //+ for<'b> Differentiable<&'b Loc<F, N>, Output = Loc<F, N>>, | |
364 | // TODO: very weird that rust only compiles with Differentiable | |
365 | // instead of the above one on references, which is required by | |
366 | // poitsource_sliding_fb_reg. | |
367 | + Differentiable<Loc<F, N>, Output = Loc<F, N>> | |
368 | + Lipschitz<L2>, | |
369 | // <DefaultSG<F, S, P, N> as ForwardModel<Loc<F, N>, F>::PreadjointCodomain : for<'b> Differentiable<&'b Loc<F, N>, Output = Loc<F, N>>, | |
0 | 370 | AutoConvolution<P> : BoundedBy<F, K>, |
32 | 371 | K : SimpleConvolutionKernel<F, N> + LocalAnalysis<F, Bounds<F>, N> |
23
9869fa1e0ccd
Print out experiment information when running it
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
372 | + Copy + Serialize + std::fmt::Debug, |
32 | 373 | //+ Differentiable<Loc<F, N>, Output = Loc<F, N>>, // TODO: shouldn't need to assume differentiability |
0 | 374 | Cube<F, N>: P2Minimise<Loc<F, N>, F> + SetOrd, |
375 | PlotLookup : Plotting<N>, | |
376 | DefaultBT<F, N> : SensorGridBT<F, S, P, N, Depth=DynamicDepth> + BTSearch<F, N>, | |
377 | BTNodeLookup: BTNode<F, usize, Bounds<F>, N>, | |
378 | DiscreteMeasure<Loc<F, N>, F> : SpikeMerging<F>, | |
23
9869fa1e0ccd
Print out experiment information when running it
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
379 | NoiseDistr : Distribution<F> + Serialize + std::fmt::Debug { |
0 | 380 | |
381 | fn algorithm_defaults(&self, alg : DefaultAlgorithm, cli : &AlgorithmOverrides<F>) | |
382 | -> Named<AlgorithmConfig<F>> { | |
383 | alg.to_named( | |
384 | self.data | |
385 | .algorithm_defaults | |
386 | .get(&alg) | |
387 | .map_or_else(|| alg.default_config(), | |
388 | |config| config.clone()) | |
389 | .cli_override(cli) | |
390 | ) | |
391 | } | |
392 | ||
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
393 | fn runall(&self, cli : &CommandLineArgs, |
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
394 | algs : Option<Vec<Named<AlgorithmConfig<F>>>>) -> DynError { |
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
395 | // Get experiment configuration |
0 | 396 | let &Named { |
397 | name : ref experiment_name, | |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
398 | data : $type { |
0 | 399 | domain, sensor_count, ref noise_distr, sensor, spread, kernel, |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
400 | ref μ_hat, /*regularisation,*/ kernel_plot_width, dataterm, noise_seed, |
0 | 401 | .. |
402 | } | |
403 | } = self; | |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
404 | let regularisation = $reg_convert(self.data.$reg_field); |
0 | 405 | |
23
9869fa1e0ccd
Print out experiment information when running it
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
406 | println!("{}\n{}", |
9869fa1e0ccd
Print out experiment information when running it
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
407 | format!("Performing experiment {}…", experiment_name).cyan(), |
9869fa1e0ccd
Print out experiment information when running it
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
408 | format!("{:?}", &self.data).bright_black()); |
9869fa1e0ccd
Print out experiment information when running it
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
409 | |
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
410 | // Set up output directory |
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
411 | let prefix = format!("{}/{}/", cli.outdir, self.name); |
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
412 | |
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
413 | // Set up algorithms |
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
414 | let iterator_options = AlgIteratorOptions{ |
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
415 | max_iter : cli.max_iter, |
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
416 | verbose_iter : cli.verbose_iter |
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
417 | .map_or(Verbose::Logarithmic(10), |
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
418 | |n| Verbose::Every(n)), |
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
419 | quiet : cli.quiet, |
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
420 | }; |
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
421 | let algorithms = match (algs, self.data.dataterm) { |
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
422 | (Some(algs), _) => algs, |
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
423 | (None, DataTerm::L2Squared) => vec![DefaultAlgorithm::FB.get_named()], |
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
424 | (None, DataTerm::L1) => vec![DefaultAlgorithm::PDPS.get_named()], |
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
425 | }; |
0 | 426 | |
427 | // Set up operators | |
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
428 | let depth = DynamicDepth(8); |
0 | 429 | let opA = DefaultSG::new(domain, sensor_count, sensor, spread, depth); |
430 | let op𝒟 = DefaultSeminormOp::new(depth, domain, kernel); | |
431 | ||
432 | // Set up random number generator. | |
433 | let mut rng = StdRng::seed_from_u64(noise_seed); | |
434 | ||
435 | // Generate the data and calculate SSNR statistic | |
436 | let b_hat = opA.apply(μ_hat); | |
437 | let noise = DVector::from_distribution(b_hat.len(), &noise_distr, &mut rng); | |
438 | let b = &b_hat + &noise; | |
439 | // Need to wrap calc_ssnr into a function to hide ultra-lame nalgebra::RealField | |
440 | // overloading log10 and conflicting with standard NumTraits one. | |
441 | let stats = ExperimentStats::new(&b, &noise); | |
442 | ||
443 | // Save experiment configuration and statistics | |
444 | let mkname_e = |t| format!("{prefix}{t}.json", prefix = prefix, t = t); | |
445 | std::fs::create_dir_all(&prefix)?; | |
446 | write_json(mkname_e("experiment"), self)?; | |
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
447 | write_json(mkname_e("config"), cli)?; |
0 | 448 | write_json(mkname_e("stats"), &stats)?; |
449 | ||
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
450 | plotall(cli, &prefix, &domain, &sensor, &kernel, &spread, |
0 | 451 | &μ_hat, &op𝒟, &opA, &b_hat, &b, kernel_plot_width)?; |
452 | ||
453 | // Run the algorithm(s) | |
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
454 | for named @ Named { name : alg_name, data : alg } in algorithms.iter() { |
0 | 455 | let this_prefix = format!("{}{}/", prefix, alg_name); |
456 | ||
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
457 | let running = || if !cli.quiet { |
0 | 458 | println!("{}\n{}\n{}", |
459 | format!("Running {} on experiment {}…", alg_name, experiment_name).cyan(), | |
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
460 | format!("{:?}", iterator_options).bright_black(), |
0 | 461 | format!("{:?}", alg).bright_black()); |
462 | }; | |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
463 | let not_implemented = || { |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
464 | let msg = format!("Algorithm “{alg_name}” not implemented for \ |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
465 | dataterm {dataterm:?} and regularisation {regularisation:?}. \ |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
466 | Skipping.").red(); |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
467 | eprintln!("{}", msg); |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
468 | }; |
0 | 469 | // Create Logger and IteratorFactory |
470 | let mut logger = Logger::new(); | |
25
79943be70720
Implement non-negativity constraints for the conditional gradient methods
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
471 | let reg : Box<dyn WeightOptim<_, _, _, N>> = match regularisation { |
79943be70720
Implement non-negativity constraints for the conditional gradient methods
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
472 | Regularisation::Radon(α) => Box::new(RadonRegTerm(α)), |
79943be70720
Implement non-negativity constraints for the conditional gradient methods
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
473 | Regularisation::NonnegRadon(α) => Box::new(NonnegRadonRegTerm(α)), |
79943be70720
Implement non-negativity constraints for the conditional gradient methods
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
474 | }; |
79943be70720
Implement non-negativity constraints for the conditional gradient methods
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
475 | let findim_data = reg.prepare_optimise_weights(&opA, &b); |
0 | 476 | let inner_config : InnerSettings<F> = Default::default(); |
477 | let inner_it = inner_config.iterator_options; | |
478 | let logmap = |iter, Timed { cpu_time, data }| { | |
479 | let IterInfo { | |
480 | value, | |
481 | n_spikes, | |
482 | inner_iters, | |
483 | merged, | |
484 | pruned, | |
485 | postprocessing, | |
486 | this_iters, | |
487 | .. | |
488 | } = data; | |
25
79943be70720
Implement non-negativity constraints for the conditional gradient methods
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
489 | let post_value = match (postprocessing, dataterm) { |
79943be70720
Implement non-negativity constraints for the conditional gradient methods
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
490 | (Some(mut μ), DataTerm::L2Squared) => { |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
491 | // Comparison postprocessing is only implemented for the case handled |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
492 | // by the FW variants. |
25
79943be70720
Implement non-negativity constraints for the conditional gradient methods
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
493 | reg.optimise_weights( |
79943be70720
Implement non-negativity constraints for the conditional gradient methods
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
494 | &mut μ, &opA, &b, &findim_data, &inner_config, |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
495 | inner_it |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
496 | ); |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
497 | dataterm.value_at_residual(opA.apply(&μ) - &b) |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
498 | + regularisation.apply(&μ) |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
499 | }, |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
500 | _ => value, |
0 | 501 | }; |
502 | CSVLog { | |
503 | iter, | |
504 | value, | |
505 | post_value, | |
506 | n_spikes, | |
507 | cpu_time : cpu_time.as_secs_f64(), | |
508 | inner_iters, | |
509 | merged, | |
510 | pruned, | |
511 | this_iters | |
512 | } | |
513 | }; | |
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
514 | let iterator = iterator_options.instantiate() |
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
515 | .timed() |
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
516 | .mapped(logmap) |
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
517 | .into_log(&mut logger); |
0 | 518 | let plotgrid = lingrid(&domain, &[if N==1 { 1000 } else { 100 }; N]); |
519 | ||
520 | // Create plotter and directory if needed. | |
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
521 | let plot_count = if cli.plot >= PlotLevel::Iter { 2000 } else { 0 }; |
0 | 522 | let plotter = SeqPlotter::new(this_prefix, plot_count, plotgrid); |
523 | ||
524 | // Run the algorithm | |
525 | let start = Instant::now(); | |
526 | let start_cpu = ProcessTime::now(); | |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
527 | let μ = match alg { |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
528 | AlgorithmConfig::FB(ref algconfig) => { |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
529 | match (regularisation, dataterm) { |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
530 | (Regularisation::NonnegRadon(α), DataTerm::L2Squared) => { |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
531 | running(); |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
532 | pointsource_fb_reg( |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
533 | &opA, &b, NonnegRadonRegTerm(α), &op𝒟, algconfig, |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
534 | iterator, plotter |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
535 | ) |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
536 | }, |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
537 | (Regularisation::Radon(α), DataTerm::L2Squared) => { |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
538 | running(); |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
539 | pointsource_fb_reg( |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
540 | &opA, &b, RadonRegTerm(α), &op𝒟, algconfig, |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
541 | iterator, plotter |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
542 | ) |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
543 | }, |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
544 | _ => { |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
545 | not_implemented(); |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
546 | continue |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
547 | } |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
548 | } |
0 | 549 | }, |
32 | 550 | AlgorithmConfig::FISTA(ref algconfig) => { |
551 | match (regularisation, dataterm) { | |
552 | (Regularisation::NonnegRadon(α), DataTerm::L2Squared) => { | |
553 | running(); | |
554 | pointsource_fista_reg( | |
555 | &opA, &b, NonnegRadonRegTerm(α), &op𝒟, algconfig, | |
556 | iterator, plotter | |
557 | ) | |
558 | }, | |
559 | (Regularisation::Radon(α), DataTerm::L2Squared) => { | |
560 | running(); | |
561 | pointsource_fista_reg( | |
562 | &opA, &b, RadonRegTerm(α), &op𝒟, algconfig, | |
563 | iterator, plotter | |
564 | ) | |
565 | }, | |
566 | _ => { | |
567 | not_implemented(); | |
568 | continue | |
569 | } | |
570 | } | |
571 | }, | |
572 | AlgorithmConfig::SlidingFB(ref algconfig) => { | |
573 | match (regularisation, dataterm) { | |
574 | (Regularisation::NonnegRadon(α), DataTerm::L2Squared) => { | |
575 | running(); | |
576 | pointsource_sliding_fb_reg( | |
577 | &opA, &b, NonnegRadonRegTerm(α), &op𝒟, algconfig, | |
578 | iterator, plotter | |
579 | ) | |
580 | }, | |
581 | (Regularisation::Radon(α), DataTerm::L2Squared) => { | |
582 | running(); | |
583 | pointsource_sliding_fb_reg( | |
584 | &opA, &b, RadonRegTerm(α), &op𝒟, algconfig, | |
585 | iterator, plotter | |
586 | ) | |
587 | }, | |
588 | _ => { | |
589 | not_implemented(); | |
590 | continue | |
591 | } | |
592 | } | |
593 | }, | |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
594 | AlgorithmConfig::PDPS(ref algconfig) => { |
0 | 595 | running(); |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
596 | match (regularisation, dataterm) { |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
597 | (Regularisation::NonnegRadon(α), DataTerm::L2Squared) => { |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
598 | pointsource_pdps_reg( |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
599 | &opA, &b, NonnegRadonRegTerm(α), &op𝒟, algconfig, |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
600 | iterator, plotter, L2Squared |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
601 | ) |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
602 | }, |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
603 | (Regularisation::Radon(α),DataTerm::L2Squared) => { |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
604 | pointsource_pdps_reg( |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
605 | &opA, &b, RadonRegTerm(α), &op𝒟, algconfig, |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
606 | iterator, plotter, L2Squared |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
607 | ) |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
608 | }, |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
609 | (Regularisation::NonnegRadon(α), DataTerm::L1) => { |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
610 | pointsource_pdps_reg( |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
611 | &opA, &b, NonnegRadonRegTerm(α), &op𝒟, algconfig, |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
612 | iterator, plotter, L1 |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
613 | ) |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
614 | }, |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
615 | (Regularisation::Radon(α), DataTerm::L1) => { |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
616 | pointsource_pdps_reg( |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
617 | &opA, &b, RadonRegTerm(α), &op𝒟, algconfig, |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
618 | iterator, plotter, L1 |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
619 | ) |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
620 | }, |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
621 | } |
0 | 622 | }, |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
623 | AlgorithmConfig::FW(ref algconfig) => { |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
624 | match (regularisation, dataterm) { |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
625 | (Regularisation::Radon(α), DataTerm::L2Squared) => { |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
626 | running(); |
25
79943be70720
Implement non-negativity constraints for the conditional gradient methods
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
627 | pointsource_fw_reg(&opA, &b, RadonRegTerm(α), |
79943be70720
Implement non-negativity constraints for the conditional gradient methods
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
628 | algconfig, iterator, plotter) |
79943be70720
Implement non-negativity constraints for the conditional gradient methods
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
629 | }, |
79943be70720
Implement non-negativity constraints for the conditional gradient methods
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
630 | (Regularisation::NonnegRadon(α), DataTerm::L2Squared) => { |
79943be70720
Implement non-negativity constraints for the conditional gradient methods
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
631 | running(); |
79943be70720
Implement non-negativity constraints for the conditional gradient methods
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
632 | pointsource_fw_reg(&opA, &b, NonnegRadonRegTerm(α), |
79943be70720
Implement non-negativity constraints for the conditional gradient methods
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
633 | algconfig, iterator, plotter) |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
634 | }, |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
635 | _ => { |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
636 | not_implemented(); |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
637 | continue |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
638 | } |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
639 | } |
0 | 640 | } |
641 | }; | |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
642 | |
0 | 643 | let elapsed = start.elapsed().as_secs_f64(); |
644 | let cpu_time = start_cpu.elapsed().as_secs_f64(); | |
645 | ||
646 | println!("{}", format!("Elapsed {elapsed}s (CPU time {cpu_time}s)… ").yellow()); | |
647 | ||
648 | // Save results | |
649 | println!("{}", "Saving results…".green()); | |
650 | ||
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
651 | let mkname = |t| format!("{prefix}{alg_name}_{t}"); |
0 | 652 | |
653 | write_json(mkname("config.json"), &named)?; | |
654 | write_json(mkname("stats.json"), &AlgorithmStats { cpu_time, elapsed })?; | |
655 | μ.write_csv(mkname("reco.txt"))?; | |
656 | logger.write_csv(mkname("log.txt"))?; | |
657 | } | |
658 | ||
659 | Ok(()) | |
660 | } | |
661 | } | |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
662 | // *** macro end boiler plate *** |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
663 | }} |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
664 | // *** actual code *** |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
665 | |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
666 | impl_experiment!(ExperimentV2, regularisation, std::convert::identity); |
0 | 667 | |
668 | /// Plot experiment setup | |
669 | #[replace_float_literals(F::cast_from(literal))] | |
670 | fn plotall<F, Sensor, Kernel, Spread, 𝒟, A, const N : usize>( | |
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
671 | cli : &CommandLineArgs, |
0 | 672 | prefix : &String, |
673 | domain : &Cube<F, N>, | |
674 | sensor : &Sensor, | |
675 | kernel : &Kernel, | |
676 | spread : &Spread, | |
677 | μ_hat : &DiscreteMeasure<Loc<F, N>, F>, | |
678 | op𝒟 : &𝒟, | |
679 | opA : &A, | |
680 | b_hat : &A::Observable, | |
681 | b : &A::Observable, | |
682 | kernel_plot_width : F, | |
683 | ) -> DynError | |
684 | where F : Float + ToNalgebraRealField, | |
685 | Sensor : RealMapping<F, N> + Support<F, N> + Clone, | |
686 | Spread : RealMapping<F, N> + Support<F, N> + Clone, | |
687 | Kernel : RealMapping<F, N> + Support<F, N>, | |
688 | Convolution<Sensor, Spread> : RealMapping<F, N> + Support<F, N>, | |
689 | 𝒟 : DiscreteMeasureOp<Loc<F, N>, F>, | |
690 | 𝒟::Codomain : RealMapping<F, N>, | |
691 | A : ForwardModel<Loc<F, N>, F>, | |
692 | A::PreadjointCodomain : RealMapping<F, N> + Bounded<F>, | |
693 | PlotLookup : Plotting<N>, | |
694 | Cube<F, N> : SetOrd { | |
695 | ||
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
696 | if cli.plot < PlotLevel::Data { |
0 | 697 | return Ok(()) |
698 | } | |
699 | ||
700 | let base = Convolution(sensor.clone(), spread.clone()); | |
701 | ||
702 | let resolution = if N==1 { 100 } else { 40 }; | |
703 | let pfx = |n| format!("{}{}", prefix, n); | |
704 | let plotgrid = lingrid(&[[-kernel_plot_width, kernel_plot_width]; N].into(), &[resolution; N]); | |
705 | ||
706 | PlotLookup::plot_into_file(sensor, plotgrid, pfx("sensor"), "sensor".to_string()); | |
707 | PlotLookup::plot_into_file(kernel, plotgrid, pfx("kernel"), "kernel".to_string()); | |
708 | PlotLookup::plot_into_file(spread, plotgrid, pfx("spread"), "spread".to_string()); | |
709 | PlotLookup::plot_into_file(&base, plotgrid, pfx("base_sensor"), "base_sensor".to_string()); | |
710 | ||
711 | let plotgrid2 = lingrid(&domain, &[resolution; N]); | |
712 | ||
713 | let ω_hat = op𝒟.apply(μ_hat); | |
714 | let noise = opA.preadjoint().apply(opA.apply(μ_hat) - b); | |
715 | PlotLookup::plot_into_file(&ω_hat, plotgrid2, pfx("omega_hat"), "ω̂".to_string()); | |
716 | PlotLookup::plot_into_file(&noise, plotgrid2, pfx("omega_noise"), | |
717 | "noise Aᵀ(Aμ̂ - b)".to_string()); | |
718 | ||
719 | let preadj_b = opA.preadjoint().apply(b); | |
720 | let preadj_b_hat = opA.preadjoint().apply(b_hat); | |
721 | //let bounds = preadj_b.bounds().common(&preadj_b_hat.bounds()); | |
722 | PlotLookup::plot_into_file_spikes( | |
723 | "Aᵀb".to_string(), &preadj_b, | |
724 | "Aᵀb̂".to_string(), Some(&preadj_b_hat), | |
725 | plotgrid2, None, &μ_hat, | |
726 | pfx("omega_b") | |
727 | ); | |
728 | ||
729 | // Save true solution and observables | |
730 | let pfx = |n| format!("{}{}", prefix, n); | |
731 | μ_hat.write_csv(pfx("orig.txt"))?; | |
732 | opA.write_observable(&b_hat, pfx("b_hat"))?; | |
733 | opA.write_observable(&b, pfx("b_noisy")) | |
734 | } |