Fri, 02 Dec 2022 18:14:03 +0200
Clean up / remove various unused FB algorithm family hacks.
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)] |
6
bcb508479948
README fine-tuning and build.rs for uglifying it for rustdoc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
13 | // We need the drain filter for inertial prune. |
0 | 14 | #![feature(drain_filter)] |
15 | ||
16 | use clap::Parser; | |
17 | use itertools::Itertools; | |
18 | use serde_json; | |
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::iterate::Verbose; |
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 | pub mod forward_model; | |
33 | pub mod plot; | |
34 | pub mod subproblem; | |
35 | pub mod tolerance; | |
36 | pub mod fb; | |
37 | pub mod frank_wolfe; | |
38 | pub mod pdps; | |
39 | pub mod run; | |
40 | pub mod rand_distr; | |
41 | pub mod experiments; | |
42 | ||
43 | use types::{float, ClapFloat}; | |
44 | use run::{ | |
45 | DefaultAlgorithm, | |
46 | Configuration, | |
47 | PlotLevel, | |
48 | Named, | |
49 | AlgorithmConfig, | |
50 | }; | |
51 | use experiments::DefaultExperiment; | |
52 | use measures::merging::SpikeMergingMethod; | |
53 | use DefaultExperiment::*; | |
54 | use DefaultAlgorithm::*; | |
55 | ||
56 | /// Command line parameters | |
57 | #[derive(Parser, Debug)] | |
58 | #[clap( | |
59 | about = env!("CARGO_PKG_DESCRIPTION"), | |
60 | author = env!("CARGO_PKG_AUTHORS"), | |
61 | version = env!("CARGO_PKG_VERSION"), | |
62 | after_help = "Pass --help for longer descriptions.", | |
63 | after_long_help = "", | |
64 | )] | |
65 | pub struct CommandLineArgs { | |
66 | #[arg(long, short = 'm', value_name = "M")] | |
67 | /// Maximum iteration count | |
68 | max_iter : Option<usize>, | |
69 | ||
70 | #[arg(long, short = 'n', value_name = "N")] | |
71 | /// Output status every N iterations. Set to 0 to disable. | |
72 | verbose_iter : Option<usize>, | |
73 | ||
74 | #[arg(long, short = 'q')] | |
75 | /// Don't display iteration progress | |
76 | quiet : bool, | |
77 | ||
78 | /// List of experiments to perform. | |
79 | #[arg(value_enum, value_name = "EXPERIMENT", | |
80 | default_values_t = [Experiment1D, Experiment1DFast, | |
81 | Experiment2D, Experiment2DFast, | |
82 | Experiment1D_L1])] | |
83 | experiments : Vec<DefaultExperiment>, | |
84 | ||
85 | /// Default algorithm configration(s) to use on the experiments. | |
86 | /// | |
87 | /// Not all algorithms are available for all the experiments. | |
88 | /// In particular, only PDPS is available for the experiments with L¹ data term. | |
89 | #[arg(value_enum, value_name = "ALGORITHM", long, short = 'a', | |
90 | default_values_t = [FB, FISTA, PDPS, FW, FWRelax])] | |
91 | algorithm : Vec<DefaultAlgorithm>, | |
92 | ||
93 | /// Saved algorithm configration(s) to use on the experiments | |
94 | #[arg(value_name = "JSON_FILE", long)] | |
95 | saved_algorithm : Vec<String>, | |
96 | ||
97 | /// Write plots for every verbose iteration | |
98 | #[arg(value_enum, long, short = 'p', default_value_t = PlotLevel::Data)] | |
99 | plot : PlotLevel, | |
100 | ||
101 | /// Directory for saving results | |
102 | #[arg(long, short = 'o', default_value = "out")] | |
103 | outdir : String, | |
104 | ||
105 | #[arg(long, help_heading = "Multi-threading", default_value = "4")] | |
106 | /// Maximum number of threads | |
107 | max_threads : usize, | |
108 | ||
109 | #[arg(long, help_heading = "Multi-threading")] | |
110 | /// Number of threads. Overrides the maximum number. | |
111 | num_threads : Option<usize>, | |
112 | ||
113 | #[clap(flatten, next_help_heading = "Experiment overrides")] | |
114 | /// Experiment setup overrides | |
115 | experiment_overrides : ExperimentOverrides<float>, | |
116 | ||
117 | #[clap(flatten, next_help_heading = "Algorithm overrides")] | |
118 | /// Algorithm parametrisation overrides | |
119 | algoritm_overrides : AlgorithmOverrides<float>, | |
120 | } | |
121 | ||
122 | /// Command line experiment setup overrides | |
123 | #[derive(Parser, Debug)] | |
124 | pub struct ExperimentOverrides<F : ClapFloat> { | |
125 | #[arg(long)] | |
126 | /// Regularisation parameter override. | |
127 | /// | |
128 | /// Only use if running just a single experiment, as different experiments have different | |
129 | /// regularisation parameters. | |
130 | alpha : Option<F>, | |
131 | ||
132 | #[arg(long)] | |
133 | /// Gaussian noise variance override | |
134 | variance : Option<F>, | |
135 | ||
136 | #[arg(long, value_names = &["MAGNITUDE", "PROBABILITY"])] | |
137 | /// Salt and pepper noise override. | |
138 | salt_and_pepper : Option<Vec<F>>, | |
139 | ||
140 | #[arg(long)] | |
141 | /// Noise seed | |
142 | noise_seed : Option<u64>, | |
143 | } | |
144 | ||
145 | /// Command line algorithm parametrisation overrides | |
146 | #[derive(Parser, Debug)] | |
147 | pub struct AlgorithmOverrides<F : ClapFloat> { | |
148 | #[arg(long, value_names = &["COUNT", "EACH"])] | |
149 | /// Override bootstrap insertion iterations for --algorithm. | |
150 | /// | |
151 | /// The first parameter is the number of bootstrap insertion iterations, and the second | |
152 | /// the maximum number of iterations on each of them. | |
153 | bootstrap_insertions : Option<Vec<usize>>, | |
154 | ||
155 | #[arg(long, requires = "algorithm")] | |
156 | /// Primal step length parameter override for --algorithm. | |
157 | /// | |
158 | /// Only use if running just a single algorithm, as different algorithms have different | |
159 | /// regularisation parameters. Does not affect the algorithms fw and fwrelax. | |
160 | tau0 : Option<F>, | |
161 | ||
162 | #[arg(long, requires = "algorithm")] | |
163 | /// Dual step length parameter override for --algorithm. | |
164 | /// | |
165 | /// Only use if running just a single algorithm, as different algorithms have different | |
166 | /// regularisation parameters. Only affects PDPS. | |
167 | sigma0 : Option<F>, | |
168 | ||
169 | #[arg(value_enum, long)] | |
170 | /// PDPS acceleration, when available. | |
171 | acceleration : Option<pdps::Acceleration>, | |
172 | ||
173 | #[arg(long)] | |
174 | /// Perform postprocess weight optimisation for saved iterations | |
175 | /// | |
176 | /// Only affects FB, FISTA, and PDPS. | |
177 | postprocessing : Option<bool>, | |
178 | ||
179 | #[arg(value_name = "n", long)] | |
180 | /// Merging frequency, if merging enabled (every n iterations) | |
181 | /// | |
182 | /// Only affects FB, FISTA, and PDPS. | |
183 | merge_every : Option<usize>, | |
184 | ||
185 | #[arg(value_enum, long)]//, value_parser = SpikeMergingMethod::<float>::value_parser())] | |
186 | /// Merging strategy | |
187 | /// | |
188 | /// Either the string "none", or a radius value for heuristic merging. | |
189 | merging : Option<SpikeMergingMethod<F>>, | |
190 | ||
191 | #[arg(value_enum, long)]//, value_parser = SpikeMergingMethod::<float>::value_parser())] | |
192 | /// Final merging strategy | |
193 | /// | |
194 | /// Either the string "none", or a radius value for heuristic merging. | |
195 | /// Only affects FB, FISTA, and PDPS. | |
196 | final_merging : Option<SpikeMergingMethod<F>>, | |
197 | } | |
198 | ||
199 | /// The entry point for the program. | |
200 | pub fn main() { | |
201 | let cli = CommandLineArgs::parse(); | |
202 | ||
5
df971c81282e
Warn when trying to run an unoptimised executable
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
203 | #[cfg(debug_assertions)] |
df971c81282e
Warn when trying to run an unoptimised executable
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
204 | { |
df971c81282e
Warn when trying to run an unoptimised executable
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
205 | use colored::Colorize; |
df971c81282e
Warn when trying to run an unoptimised executable
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
206 | println!("{}", format!("\n\ |
df971c81282e
Warn when trying to run an unoptimised executable
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
207 | ********\n\ |
df971c81282e
Warn when trying to run an unoptimised executable
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
208 | WARNING: Compiled without optimisations; {}\n\ |
df971c81282e
Warn when trying to run an unoptimised executable
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
209 | Please recompile with `--release` flag.\n\ |
df971c81282e
Warn when trying to run an unoptimised executable
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
210 | ********\n\ |
df971c81282e
Warn when trying to run an unoptimised executable
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
211 | ", "performance will be poor!".blink() |
df971c81282e
Warn when trying to run an unoptimised executable
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
212 | ).red()); |
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 | |
0 | 215 | if let Some(n_threads) = cli.num_threads { |
216 | let n = NonZeroUsize::new(n_threads).expect("Invalid thread count"); | |
217 | set_num_threads(n); | |
218 | } else { | |
219 | let m = NonZeroUsize::new(cli.max_threads).expect("Invalid maximum thread count"); | |
220 | set_max_threads(m); | |
221 | } | |
222 | ||
223 | for experiment_shorthand in cli.experiments.iter().unique() { | |
224 | let experiment = experiment_shorthand.get_experiment(&cli.experiment_overrides).unwrap(); | |
225 | let mut config : Configuration<float> = experiment.default_config(); | |
226 | let mut algs : Vec<Named<AlgorithmConfig<float>>> | |
227 | = cli.algorithm.iter() | |
228 | .map(|alg| experiment.algorithm_defaults(*alg, &cli.algoritm_overrides)) | |
229 | .collect(); | |
230 | for filename in cli.saved_algorithm.iter() { | |
231 | let f = std::fs::File::open(filename).unwrap(); | |
232 | let alg = serde_json::from_reader(f).unwrap(); | |
233 | algs.push(alg); | |
234 | } | |
235 | cli.max_iter.map(|m| config.iterator_options.max_iter = m); | |
236 | cli.verbose_iter.map(|n| config.iterator_options.verbose_iter = Verbose::Every(n)); | |
237 | config.plot = cli.plot; | |
238 | config.iterator_options.quiet = cli.quiet; | |
239 | config.outdir = cli.outdir.clone(); | |
240 | if !algs.is_empty() { | |
241 | config.algorithms = algs.clone(); | |
242 | } | |
243 | ||
244 | experiment.runall(config) | |
245 | .unwrap() | |
246 | } | |
247 | } |