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