Tue, 06 Dec 2022 14:12:20 +0200
v1.0.0-pre-arxiv (missing arXiv links)
0 | 1 | //! Type definitions and re-exports |
2 | ||
3 | use numeric_literals::replace_float_literals; | |
4 | ||
5 | use colored::ColoredString; | |
6 | use serde::{Serialize, Deserialize}; | |
7 | use clap::ValueEnum; | |
8 | use alg_tools::iterate::LogRepr; | |
9 | use alg_tools::euclidean::Euclidean; | |
10 | use alg_tools::norms::{Norm, L1}; | |
11 | ||
12 | pub use alg_tools::types::*; | |
13 | pub use alg_tools::loc::Loc; | |
14 | pub use alg_tools::sets::Cube; | |
15 | ||
16 | use crate::measures::DiscreteMeasure; | |
17 | ||
18 | /// [`Float`] with extra display and string conversion traits such that [`clap`] doesn't choke up. | |
19 | pub trait ClapFloat : Float | |
20 | + std::str::FromStr<Err=std::num::ParseFloatError> | |
21 | + std::fmt::Display {} | |
22 | impl ClapFloat for f32 {} | |
23 | impl ClapFloat for f64 {} | |
24 | ||
25 | /// Structure for storing iteration statistics | |
26 | #[derive(Debug, Clone, Serialize)] | |
27 | pub struct IterInfo<F : Float, const N : usize> { | |
28 | /// Function value | |
29 | pub value : F, | |
30 | /// Number of speaks | |
31 | pub n_spikes : usize, | |
32 | /// Number of iterations this statistic covers | |
33 | pub this_iters : usize, | |
34 | /// Number of spikes removed by merging since last IterInfo statistic | |
35 | pub merged : usize, | |
36 | /// Number of spikes removed by pruning since last IterInfo statistic | |
37 | pub pruned : usize, | |
38 | /// Number of inner iterations since last IterInfo statistic | |
39 | pub inner_iters : usize, | |
40 | /// Current tolerance | |
41 | pub ε : F, | |
42 | /// Solve fin.dim problem for this measure to get the optimal `value`. | |
43 | pub postprocessing : Option<DiscreteMeasure<Loc<F, N>, F>>, | |
44 | } | |
45 | ||
46 | impl<F, const N : usize> LogRepr for IterInfo<F, N> where F : LogRepr + Float { | |
47 | fn logrepr(&self) -> ColoredString { | |
8
ea3ca78873e8
Clean up / remove various unused FB algorithm family hacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
48 | format!("{}\t| N = {}, ε = {:.8}, inner_iters_mean = {}, merged+pruned_mean = {}+{}", |
0 | 49 | self.value.logrepr(), |
50 | self.n_spikes, | |
51 | self.ε, | |
52 | self.inner_iters as float / self.this_iters as float, | |
53 | self.merged as float / self.this_iters as float, | |
54 | self.pruned as float / self.this_iters as float, | |
55 | ).as_str().into() | |
56 | } | |
57 | } | |
58 | ||
59 | /// Branch and bound refinement settings | |
60 | #[derive(Clone, Copy, Eq, PartialEq, Serialize, Deserialize, Debug)] | |
61 | #[serde(default)] | |
62 | pub struct RefinementSettings<F : Float> { | |
63 | /// Function value tolerance multiplier for bisection tree refinement in | |
64 | /// [`alg_tools::bisection_tree::BTFN::maximise`] and related functions. | |
65 | pub tolerance_mult : F, | |
66 | /// Maximum branch and bound steps | |
67 | pub max_steps : usize, | |
68 | } | |
69 | ||
70 | #[replace_float_literals(F::cast_from(literal))] | |
71 | impl<F : Float> Default for RefinementSettings<F> { | |
72 | fn default() -> Self { | |
73 | RefinementSettings { | |
74 | tolerance_mult : 0.1, | |
75 | max_steps : 50000, | |
76 | } | |
77 | } | |
78 | } | |
79 | ||
80 | /// Data term type | |
81 | #[derive(Clone, Copy, Eq, PartialEq, Serialize, Deserialize, Debug, ValueEnum)] | |
82 | pub enum DataTerm { | |
83 | /// $\\|z\\|\_2^2/2$ | |
84 | L2Squared, | |
85 | /// $\\|z\\|\_1$ | |
86 | L1, | |
87 | } | |
88 | ||
89 | impl DataTerm { | |
90 | /// Calculate the data term value at residual $z=Aμ - b$. | |
91 | pub fn value_at_residual<F : Float, E : Euclidean<F> + Norm<F, L1>>(&self, z : E) -> F { | |
92 | match self { | |
93 | Self::L2Squared => z.norm2_squared_div2(), | |
94 | Self::L1 => z.norm(L1), | |
95 | } | |
96 | } | |
97 | } | |
98 |