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