Tue, 31 Dec 2024 09:34:24 -0500
Early transport sketches
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 | ||
32 | 46 | impl<F : Float, const N : usize> IterInfo<F, N> { |
47 | /// Initialise statistics with zeros. `ε` and `value` are unspecified. | |
48 | pub fn new() -> Self { | |
49 | IterInfo { | |
50 | value : F::NAN, | |
51 | n_spikes : 0, | |
52 | this_iters : 0, | |
53 | merged : 0, | |
54 | pruned : 0, | |
55 | inner_iters : 0, | |
56 | ε : F::NAN, | |
57 | postprocessing : None, | |
58 | } | |
59 | } | |
60 | } | |
61 | ||
0 | 62 | impl<F, const N : usize> LogRepr for IterInfo<F, N> where F : LogRepr + Float { |
63 | fn logrepr(&self) -> ColoredString { | |
8
ea3ca78873e8
Clean up / remove various unused FB algorithm family hacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
64 | format!("{}\t| N = {}, ε = {:.8}, inner_iters_mean = {}, merged+pruned_mean = {}+{}", |
0 | 65 | self.value.logrepr(), |
66 | self.n_spikes, | |
67 | self.ε, | |
68 | self.inner_iters as float / self.this_iters as float, | |
69 | self.merged as float / self.this_iters as float, | |
70 | self.pruned as float / self.this_iters as float, | |
71 | ).as_str().into() | |
72 | } | |
73 | } | |
74 | ||
75 | /// Branch and bound refinement settings | |
76 | #[derive(Clone, Copy, Eq, PartialEq, Serialize, Deserialize, Debug)] | |
77 | #[serde(default)] | |
78 | pub struct RefinementSettings<F : Float> { | |
79 | /// Function value tolerance multiplier for bisection tree refinement in | |
80 | /// [`alg_tools::bisection_tree::BTFN::maximise`] and related functions. | |
81 | pub tolerance_mult : F, | |
82 | /// Maximum branch and bound steps | |
83 | pub max_steps : usize, | |
84 | } | |
85 | ||
86 | #[replace_float_literals(F::cast_from(literal))] | |
87 | impl<F : Float> Default for RefinementSettings<F> { | |
88 | fn default() -> Self { | |
89 | RefinementSettings { | |
90 | tolerance_mult : 0.1, | |
91 | max_steps : 50000, | |
92 | } | |
93 | } | |
94 | } | |
95 | ||
96 | /// Data term type | |
97 | #[derive(Clone, Copy, Eq, PartialEq, Serialize, Deserialize, Debug, ValueEnum)] | |
98 | pub enum DataTerm { | |
99 | /// $\\|z\\|\_2^2/2$ | |
100 | L2Squared, | |
101 | /// $\\|z\\|\_1$ | |
102 | L1, | |
103 | } | |
104 | ||
105 | impl DataTerm { | |
106 | /// Calculate the data term value at residual $z=Aμ - b$. | |
107 | pub fn value_at_residual<F : Float, E : Euclidean<F> + Norm<F, L1>>(&self, z : E) -> F { | |
108 | match self { | |
109 | Self::L2Squared => z.norm2_squared_div2(), | |
110 | Self::L1 => z.norm(L1), | |
111 | } | |
112 | } | |
113 | } | |
32 | 114 | |
115 | /// Type for indicating norm-2-squared data fidelity or transport cost. | |
116 | #[derive(Clone, Copy, Serialize, Deserialize)] | |
117 | pub struct L2Squared; | |
118 | ||
119 | /// Trait for indicating that `Self` is Lipschitz with respect to the (semi)norm `D`. | |
120 | pub trait Lipschitz<D> { | |
121 | /// The type of floats | |
122 | type FloatType : Float; | |
123 | ||
124 | /// Returns the Lipschitz factor of `self` with respect to the (semi)norm `D`. | |
125 | fn lipschitz_factor(&self, seminorm : D) -> Option<Self::FloatType>; | |
126 | } |