Tue, 31 Dec 2024 09:34:24 -0500
Early transport sketches
0 | 1 | // The main documentation is in the README. |
6
bcb508479948
README fine-tuning and build.rs for uglifying it for rustdoc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
2 | // We need to uglify it in build.rs because rustdoc is stuck in the past. |
bcb508479948
README fine-tuning and build.rs for uglifying it for rustdoc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
3 | #![doc = include_str!(concat!(env!("OUT_DIR"), "/README_uglified.md"))] |
0 | 4 | |
5 | // We use unicode. We would like to use much more of it than Rust allows. | |
6 | // Live with it. Embrace it. | |
7 | #![allow(uncommon_codepoints)] | |
8 | #![allow(mixed_script_confusables)] | |
9 | #![allow(confusable_idents)] | |
6
bcb508479948
README fine-tuning and build.rs for uglifying it for rustdoc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
10 | // Linear operators may be written e.g. as `opA`, to keep the capital letters of mathematical |
bcb508479948
README fine-tuning and build.rs for uglifying it for rustdoc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
11 | // convention while referring to the type (trait) of the operator as `A`. |
0 | 12 | #![allow(non_snake_case)] |
13 | #![feature(drain_filter)] | |
14 | ||
15 | use clap::Parser; | |
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
16 | use serde::{Serialize, Deserialize}; |
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
17 | use serde_json; |
0 | 18 | use itertools::Itertools; |
5
df971c81282e
Warn when trying to run an unoptimised executable
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
19 | use std::num::NonZeroUsize; |
df971c81282e
Warn when trying to run an unoptimised executable
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
20 | |
0 | 21 | use alg_tools::parallelism::{ |
22 | set_num_threads, | |
23 | set_max_threads, | |
24 | }; | |
25 | ||
26 | pub mod types; | |
27 | pub mod measures; | |
28 | pub mod fourier; | |
29 | pub mod kernels; | |
30 | pub mod seminorms; | |
32 | 31 | pub mod transport; |
0 | 32 | pub mod forward_model; |
33 | pub mod plot; | |
34 | pub mod subproblem; | |
35 | pub mod tolerance; | |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
36 | pub mod regularisation; |
32 | 37 | pub mod dataterm; |
0 | 38 | pub mod fb; |
32 | 39 | pub mod sliding_fb; |
0 | 40 | pub mod frank_wolfe; |
41 | pub mod pdps; | |
42 | pub mod run; | |
43 | pub mod rand_distr; | |
44 | pub mod experiments; | |
45 | ||
46 | use types::{float, ClapFloat}; | |
47 | use run::{ | |
48 | DefaultAlgorithm, | |
49 | PlotLevel, | |
50 | Named, | |
51 | AlgorithmConfig, | |
52 | }; | |
53 | use experiments::DefaultExperiment; | |
54 | use measures::merging::SpikeMergingMethod; | |
55 | use DefaultExperiment::*; | |
56 | use DefaultAlgorithm::*; | |
57 | ||
58 | /// Command line parameters | |
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
59 | #[derive(Parser, Debug, Serialize)] |
0 | 60 | #[clap( |
61 | about = env!("CARGO_PKG_DESCRIPTION"), | |
62 | author = env!("CARGO_PKG_AUTHORS"), | |
63 | version = env!("CARGO_PKG_VERSION"), | |
64 | after_help = "Pass --help for longer descriptions.", | |
65 | after_long_help = "", | |
66 | )] | |
67 | pub struct CommandLineArgs { | |
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
68 | #[arg(long, short = 'm', value_name = "M", default_value_t = 2000)] |
0 | 69 | /// Maximum iteration count |
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
70 | max_iter : usize, |
0 | 71 | |
72 | #[arg(long, short = 'n', value_name = "N")] | |
73 | /// Output status every N iterations. Set to 0 to disable. | |
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
74 | /// |
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
75 | /// The default is to output status based on logarithmic increments. |
0 | 76 | verbose_iter : Option<usize>, |
77 | ||
78 | #[arg(long, short = 'q')] | |
79 | /// Don't display iteration progress | |
80 | quiet : bool, | |
81 | ||
82 | /// List of experiments to perform. | |
83 | #[arg(value_enum, value_name = "EXPERIMENT", | |
84 | default_values_t = [Experiment1D, Experiment1DFast, | |
85 | Experiment2D, Experiment2DFast, | |
86 | Experiment1D_L1])] | |
87 | experiments : Vec<DefaultExperiment>, | |
88 | ||
89 | /// Default algorithm configration(s) to use on the experiments. | |
90 | /// | |
91 | /// Not all algorithms are available for all the experiments. | |
92 | /// In particular, only PDPS is available for the experiments with L¹ data term. | |
93 | #[arg(value_enum, value_name = "ALGORITHM", long, short = 'a', | |
32 | 94 | default_values_t = [FB, FISTA, PDPS, SlidingFB, FW, FWRelax])] |
0 | 95 | algorithm : Vec<DefaultAlgorithm>, |
96 | ||
97 | /// Saved algorithm configration(s) to use on the experiments | |
98 | #[arg(value_name = "JSON_FILE", long)] | |
99 | saved_algorithm : Vec<String>, | |
100 | ||
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
101 | /// Plot saving scheme |
0 | 102 | #[arg(value_enum, long, short = 'p', default_value_t = PlotLevel::Data)] |
103 | plot : PlotLevel, | |
104 | ||
105 | /// Directory for saving results | |
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
106 | #[arg(long, short = 'o', required = true, default_value = "out")] |
0 | 107 | outdir : String, |
108 | ||
109 | #[arg(long, help_heading = "Multi-threading", default_value = "4")] | |
110 | /// Maximum number of threads | |
111 | max_threads : usize, | |
112 | ||
113 | #[arg(long, help_heading = "Multi-threading")] | |
114 | /// Number of threads. Overrides the maximum number. | |
115 | num_threads : Option<usize>, | |
116 | ||
117 | #[clap(flatten, next_help_heading = "Experiment overrides")] | |
118 | /// Experiment setup overrides | |
119 | experiment_overrides : ExperimentOverrides<float>, | |
120 | ||
121 | #[clap(flatten, next_help_heading = "Algorithm overrides")] | |
122 | /// Algorithm parametrisation overrides | |
123 | algoritm_overrides : AlgorithmOverrides<float>, | |
124 | } | |
125 | ||
126 | /// Command line experiment setup overrides | |
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
127 | #[derive(Parser, Debug, Serialize, Deserialize)] |
0 | 128 | pub struct ExperimentOverrides<F : ClapFloat> { |
129 | #[arg(long)] | |
130 | /// Regularisation parameter override. | |
131 | /// | |
132 | /// Only use if running just a single experiment, as different experiments have different | |
133 | /// regularisation parameters. | |
134 | alpha : Option<F>, | |
135 | ||
136 | #[arg(long)] | |
137 | /// Gaussian noise variance override | |
138 | variance : Option<F>, | |
139 | ||
140 | #[arg(long, value_names = &["MAGNITUDE", "PROBABILITY"])] | |
141 | /// Salt and pepper noise override. | |
142 | salt_and_pepper : Option<Vec<F>>, | |
143 | ||
144 | #[arg(long)] | |
145 | /// Noise seed | |
146 | noise_seed : Option<u64>, | |
147 | } | |
148 | ||
149 | /// Command line algorithm parametrisation overrides | |
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
150 | #[derive(Parser, Debug, Serialize, Deserialize)] |
0 | 151 | pub struct AlgorithmOverrides<F : ClapFloat> { |
152 | #[arg(long, value_names = &["COUNT", "EACH"])] | |
153 | /// Override bootstrap insertion iterations for --algorithm. | |
154 | /// | |
155 | /// The first parameter is the number of bootstrap insertion iterations, and the second | |
156 | /// the maximum number of iterations on each of them. | |
157 | bootstrap_insertions : Option<Vec<usize>>, | |
158 | ||
159 | #[arg(long, requires = "algorithm")] | |
160 | /// Primal step length parameter override for --algorithm. | |
161 | /// | |
162 | /// Only use if running just a single algorithm, as different algorithms have different | |
163 | /// regularisation parameters. Does not affect the algorithms fw and fwrelax. | |
164 | tau0 : Option<F>, | |
165 | ||
166 | #[arg(long, requires = "algorithm")] | |
167 | /// Dual step length parameter override for --algorithm. | |
168 | /// | |
169 | /// Only use if running just a single algorithm, as different algorithms have different | |
170 | /// regularisation parameters. Only affects PDPS. | |
171 | sigma0 : Option<F>, | |
172 | ||
173 | #[arg(value_enum, long)] | |
174 | /// PDPS acceleration, when available. | |
175 | acceleration : Option<pdps::Acceleration>, | |
176 | ||
177 | #[arg(long)] | |
178 | /// Perform postprocess weight optimisation for saved iterations | |
179 | /// | |
180 | /// Only affects FB, FISTA, and PDPS. | |
181 | postprocessing : Option<bool>, | |
182 | ||
183 | #[arg(value_name = "n", long)] | |
184 | /// Merging frequency, if merging enabled (every n iterations) | |
185 | /// | |
186 | /// Only affects FB, FISTA, and PDPS. | |
187 | merge_every : Option<usize>, | |
188 | ||
189 | #[arg(value_enum, long)]//, value_parser = SpikeMergingMethod::<float>::value_parser())] | |
190 | /// Merging strategy | |
191 | /// | |
192 | /// Either the string "none", or a radius value for heuristic merging. | |
193 | merging : Option<SpikeMergingMethod<F>>, | |
194 | ||
195 | #[arg(value_enum, long)]//, value_parser = SpikeMergingMethod::<float>::value_parser())] | |
196 | /// Final merging strategy | |
197 | /// | |
198 | /// Either the string "none", or a radius value for heuristic merging. | |
199 | /// Only affects FB, FISTA, and PDPS. | |
200 | final_merging : Option<SpikeMergingMethod<F>>, | |
20
90f77ad9a98d
Added command line option for (power) tolerance
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
201 | |
90f77ad9a98d
Added command line option for (power) tolerance
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
202 | #[arg(long, value_names = &["ε", "θ", "p"])] |
90f77ad9a98d
Added command line option for (power) tolerance
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
203 | /// Set the tolerance to ε_k = ε/(1+θk)^p |
90f77ad9a98d
Added command line option for (power) tolerance
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
204 | tolerance : Option<Vec<F>>, |
90f77ad9a98d
Added command line option for (power) tolerance
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
205 | |
0 | 206 | } |
207 | ||
208 | /// The entry point for the program. | |
209 | pub fn main() { | |
210 | let cli = CommandLineArgs::parse(); | |
211 | ||
5
df971c81282e
Warn when trying to run an unoptimised executable
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
212 | #[cfg(debug_assertions)] |
df971c81282e
Warn when trying to run an unoptimised executable
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
213 | { |
df971c81282e
Warn when trying to run an unoptimised executable
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
214 | use colored::Colorize; |
df971c81282e
Warn when trying to run an unoptimised executable
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
215 | println!("{}", format!("\n\ |
df971c81282e
Warn when trying to run an unoptimised executable
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
216 | ********\n\ |
df971c81282e
Warn when trying to run an unoptimised executable
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
217 | WARNING: Compiled without optimisations; {}\n\ |
df971c81282e
Warn when trying to run an unoptimised executable
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
218 | Please recompile with `--release` flag.\n\ |
df971c81282e
Warn when trying to run an unoptimised executable
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
219 | ********\n\ |
df971c81282e
Warn when trying to run an unoptimised executable
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
220 | ", "performance will be poor!".blink() |
df971c81282e
Warn when trying to run an unoptimised executable
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
221 | ).red()); |
df971c81282e
Warn when trying to run an unoptimised executable
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
222 | } |
df971c81282e
Warn when trying to run an unoptimised executable
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
223 | |
0 | 224 | if let Some(n_threads) = cli.num_threads { |
225 | let n = NonZeroUsize::new(n_threads).expect("Invalid thread count"); | |
226 | set_num_threads(n); | |
227 | } else { | |
228 | let m = NonZeroUsize::new(cli.max_threads).expect("Invalid maximum thread count"); | |
229 | set_max_threads(m); | |
230 | } | |
231 | ||
232 | for experiment_shorthand in cli.experiments.iter().unique() { | |
233 | let experiment = experiment_shorthand.get_experiment(&cli.experiment_overrides).unwrap(); | |
234 | let mut algs : Vec<Named<AlgorithmConfig<float>>> | |
235 | = cli.algorithm.iter() | |
236 | .map(|alg| experiment.algorithm_defaults(*alg, &cli.algoritm_overrides)) | |
237 | .collect(); | |
238 | for filename in cli.saved_algorithm.iter() { | |
239 | let f = std::fs::File::open(filename).unwrap(); | |
240 | let alg = serde_json::from_reader(f).unwrap(); | |
241 | algs.push(alg); | |
242 | } | |
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
243 | experiment.runall(&cli, (!algs.is_empty()).then_some(algs)) |
0 | 244 | .unwrap() |
245 | } | |
246 | } |