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