Sun, 20 Oct 2024 23:53:43 -0500
Simplify iterate facility for-loop mechanism
0 | 1 | /*! |
5 | 2 | Tools for separating the computational steps of an iterative algorithm from stopping rules |
3 | and reporting. | |
0 | 4 | |
5 | 5 | The computational step is to be implemented as a closure. That closure gets passed a `state` |
6 | parameter that implements an [`if_verbose`][AlgIteratorState::if_verbose] method that is to | |
7 | be called to determine whether function values or other potentially costly things need to be | |
8 | calculated on that iteration. The parameter of [`if_verbose`][AlgIteratorState::if_verbose] is | |
9 | another closure that does the necessary computation. | |
0 | 10 | |
11 | ## Simple example | |
12 | ||
13 | ```rust | |
5 | 14 | # use alg_tools::types::*; |
15 | # use alg_tools::iterate::*; | |
16 | let mut iter = AlgIteratorOptions{ | |
17 | max_iter : 100, | |
18 | verbose_iter : Verbose::Every(10), | |
19 | .. Default::default() | |
20 | }; | |
0 | 21 | let mut x = 1 as float; |
41
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
22 | # let mut iter_clone = iter.clone(); |
0 | 23 | iter.iterate(|state|{ |
24 | // This is our computational step | |
25 | x = x + x.sqrt(); | |
26 | state.if_verbose(||{ | |
27 | // return current value when requested | |
28 | return x | |
29 | }) | |
41
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
30 | }); |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
31 | // or alternatively (avoiding problems with moves) |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
32 | # iter = iter_clone; |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
33 | for state in iter.iter() { |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
34 | // This is our computational step |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
35 | x = x + x.sqrt(); |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
36 | state.if_verbose(||{ |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
37 | // return current value when requested |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
38 | return x |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
39 | }) |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
40 | } |
0 | 41 | ``` |
42 | There is no colon after `state.if_verbose`, because we need to return its value. If you do something after the step, you need to store the result in a variable and return it from the main computational step afterwards. | |
43 | ||
44 | This will print out | |
45 | ```output | |
46 | 10/100 J=31.051164 | |
47 | 20/100 J=108.493699 | |
48 | 30/100 J=234.690039 | |
49 | 40/100 J=410.056327 | |
50 | 50/100 J=634.799262 | |
51 | 60/100 J=909.042928 | |
52 | 70/100 J=1232.870172 | |
53 | 80/100 J=1606.340254 | |
54 | 90/100 J=2029.497673 | |
55 | 100/100 J=2502.377071 | |
56 | ``` | |
57 | */ | |
58 | ||
5 | 59 | use colored::{Colorize, ColoredString}; |
0 | 60 | use core::fmt::Debug; |
61 | use serde::{Serialize, Deserialize}; | |
62 | use cpu_time::ProcessTime; | |
63 | use std::marker::PhantomData; | |
64 | use std::time::Duration; | |
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
65 | use std::error::Error; |
41
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
66 | use std::cell::RefCell; |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
67 | use std::rc::Rc; |
0 | 68 | use crate::types::*; |
69 | use crate::logger::*; | |
70 | ||
5 | 71 | /// Create the displayed presentation for log items. |
0 | 72 | pub trait LogRepr : Debug { |
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
73 | fn logrepr(&self) -> ColoredString { format!("« {self:?} »").as_str().into() } |
0 | 74 | } |
75 | ||
76 | impl LogRepr for str { | |
77 | fn logrepr(&self) -> ColoredString { self.into() } | |
78 | } | |
79 | ||
80 | impl LogRepr for String { | |
81 | fn logrepr(&self) -> ColoredString { self.as_str().into() } | |
82 | } | |
83 | ||
84 | impl<T> LogRepr for T where T : Num { | |
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
85 | fn logrepr(&self) -> ColoredString { format!("J={self}").as_str().into() } |
0 | 86 | } |
87 | ||
88 | impl<V> LogRepr for Option<V> where V : LogRepr { | |
89 | fn logrepr(&self) -> ColoredString { | |
90 | match self { | |
91 | None => { "===missing value===".red() } | |
92 | Some(v) => { v.logrepr() } | |
93 | } | |
94 | } | |
95 | } | |
96 | ||
5 | 97 | /// Helper struct for returning results annotated with an additional string to |
98 | /// [`if_verbose`][AlgIteratorState::if_verbose]. The [`LogRepr`] implementation will | |
99 | /// display that string when so decided by the specific [`AlgIterator`] in use. | |
0 | 100 | #[derive(Debug,Clone)] |
101 | pub struct Annotated<F>(pub F, pub String); | |
102 | ||
103 | impl<V> LogRepr for Annotated<V> where V : LogRepr { | |
104 | fn logrepr(&self) -> ColoredString { | |
105 | format!("{}\t| {}", self.0.logrepr(), self.1).as_str().into() | |
106 | } | |
107 | } | |
108 | ||
109 | ||
110 | /// Basic log item. | |
111 | #[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)] | |
112 | pub struct LogItem<V> { | |
113 | pub iter : usize, | |
114 | // This causes [`csv`] to crash. | |
115 | //#[serde(flatten)] | |
116 | pub data : V | |
117 | } | |
118 | ||
5 | 119 | impl<V> LogItem<V> { |
120 | /// Creates a new log item | |
121 | fn new(iter : usize, data : V) -> Self { | |
122 | LogItem{ iter, data } | |
123 | } | |
0 | 124 | } |
125 | ||
5 | 126 | /// State of an [`AlgIterator`]. |
127 | /// | |
128 | /// This is the parameter obtained by the closure passed to [`AlgIterator::iterate`] or | |
129 | /// [`AlgIteratorFactory::iterate`]. | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
130 | pub trait AlgIteratorState : Sized { |
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
131 | /// Call `call_objective` if this is a verbose iteration. |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
132 | /// |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
133 | /// Verbosity depends on the [`AlgIterator`] that produced this state. |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
134 | /// |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
135 | /// The closure `calc_objective` should return an arbitrary value of type `V`, to be inserted |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
136 | /// into the log, or whatever is deemed by the [`AlgIterator`]. For usage instructions see the |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
137 | /// [module documentation][self]. |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
138 | fn if_verbose<V, E : Error>(self, calc_objective : impl FnMut() -> V) -> Step<V, Self, E>; |
0 | 139 | |
5 | 140 | /// Returns the current iteration count. |
0 | 141 | fn iteration(&self) -> usize; |
31
50a77e4efcbb
Add is_quiet to AlgIteratorState as well.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
142 | |
50a77e4efcbb
Add is_quiet to AlgIteratorState as well.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
143 | /// Indicates whether the iterator is quiet |
50a77e4efcbb
Add is_quiet to AlgIteratorState as well.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
144 | fn is_quiet(&self) -> bool; |
0 | 145 | } |
146 | ||
147 | /// Result of a step of an [`AlgIterator`] | |
148 | #[derive(Debug, Serialize)] | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
149 | pub enum Step<V, S, Fail : Error = std::convert::Infallible> { |
0 | 150 | /// Iteration should be terminated |
151 | Terminated, | |
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
152 | /// Iteration should be terminated due to failure |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
153 | Failure(Fail), |
0 | 154 | /// No result this iteration (due to verbosity settings) |
155 | Quiet, | |
156 | /// Result of this iteration (due to verbosity settings) | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
157 | Result(V, S), |
0 | 158 | } |
159 | ||
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
160 | impl<V, S, E : Error> Step<V, S, E> { |
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
161 | /// Maps the value contained within the `Step`, if any, by the closure `f`. |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
162 | pub fn map<U>(self, mut f : impl FnMut(V) -> U) -> Step<U, S, E> { |
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
163 | match self { |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
164 | Step::Result(v, s) => Step::Result(f(v), s), |
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
165 | Step::Failure(e) => Step::Failure(e), |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
166 | Step::Quiet => Step::Quiet, |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
167 | Step::Terminated => Step::Terminated, |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
168 | } |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
169 | } |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
170 | } |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
171 | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
172 | impl<V, S, E : Error> Default for Step<V, S, E> { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
173 | fn default() -> Self { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
174 | Step::Quiet |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
175 | } |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
176 | } |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
177 | |
5 | 178 | /// An iterator for algorithms, produced by [`AlgIteratorFactory::prepare`]. |
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
179 | /// |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
180 | /// Typically not accessed directly, but transparently produced by an [`AlgIteratorFactory`]. |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
181 | /// Every [`AlgIteratorFactory`] has to implement a corresponding `AlgIterator`. |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
182 | pub trait AlgIterator : Sized { |
5 | 183 | /// The state type |
0 | 184 | type State : AlgIteratorState; |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
185 | /// The output type for [`Self::poststep`] and [`Self::step`]. |
0 | 186 | type Output; |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
187 | /// The input type for [`Self::poststep`]. |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
188 | type Input; |
0 | 189 | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
190 | /// Advance the iterator, performing `step_fn` with the state |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
191 | fn step<F, E>(&mut self, step_fn : &mut F) -> Step<Self::Output, Self::State, E> |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
192 | where F : FnMut(Self::State) -> Step<Self::Input, Self::State, E>, |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
193 | E : Error { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
194 | self.prestep().map_or(Step::Terminated, |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
195 | |state| self.poststep(step_fn(state))) |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
196 | } |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
197 | |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
198 | /// Initial stage of advancing the iterator, before the actual step |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
199 | fn prestep(&mut self) -> Option<Self::State>; |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
200 | |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
201 | /// Handle step result |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
202 | fn poststep<E>(&mut self, result : Step<Self::Input, Self::State, E>) |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
203 | -> Step<Self::Output, Self::State, E> |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
204 | where E : Error; |
0 | 205 | |
206 | /// Return current iteration count. | |
207 | fn iteration(&self) -> usize { | |
208 | self.state().iteration() | |
209 | } | |
210 | ||
5 | 211 | /// Return current state. |
0 | 212 | fn state(&self) -> Self::State; |
213 | ||
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
214 | /// Iterate the `AlgIterator` until termination, erforming `step_fn` on each step. |
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
215 | /// |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
216 | /// Returns either `()` or an error if the step closure terminated in [`Step::Failure´]. |
0 | 217 | #[inline] |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
218 | fn iterate<F, E>(&mut self, mut step_fn : F) -> Result<(), E> |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
219 | where F : FnMut(Self::State) -> Step<Self::Input, Self::State, E>, |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
220 | E : Error { |
0 | 221 | loop { |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
222 | match self.step(&mut step_fn) { |
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
223 | Step::Terminated => return Ok(()), |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
224 | Step::Failure(e) => return Err(e), |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
225 | _ => {}, |
0 | 226 | } |
227 | } | |
228 | } | |
229 | } | |
230 | ||
5 | 231 | /// A factory for producing an [`AlgIterator`]. |
0 | 232 | /// |
5 | 233 | /// For usage instructions see the [module documentation][self]. |
0 | 234 | pub trait AlgIteratorFactory<V> : Sized { |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
235 | type Iter : AlgIterator<State = Self::State, Input = V, Output = Self::Output>; |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
236 | |
5 | 237 | /// The state type of the corresponding [`AlgIterator`]. |
238 | /// A reference to this is passed to the closures passed to methods such as [`Self::iterate`]. | |
0 | 239 | type State : AlgIteratorState; |
5 | 240 | /// The output type of the corresponding [`AlgIterator`]. |
241 | /// This is the output of the closures passed to methods such as [`Self::iterate`] after | |
242 | /// mappings performed by each [`AlgIterator`] implementation. | |
0 | 243 | type Output; |
244 | ||
245 | /// Prepare an [`AlgIterator`], consuming the factory. | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
246 | fn prepare(self) -> Self::Iter; |
0 | 247 | |
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
248 | /// Iterate the the closure `step`. |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
249 | /// |
5 | 250 | /// The closure should accept a `state` parameter (satisfying the trait [`AlgIteratorState`]). |
251 | /// It should return the output of | |
252 | /// `state.`[`if_verbose`][AlgIteratorState::if_verbose], [`Step::Terminated`] to indicate | |
253 | /// completion for other reason, or [`Step::Failure`] for termination for failure. | |
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
254 | /// |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
255 | /// This method is equivalent to [`Self::prepare`] followed by [`AlgIterator::iterate`]. |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
256 | #[inline] |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
257 | fn iterate_fallible<F, E>(self, step : F) -> Result<(), E> |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
258 | where F : FnMut(Self::State) -> Step<V, Self::State, E>, |
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
259 | E : Error { |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
260 | self.prepare().iterate(step) |
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
261 | } |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
262 | |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
263 | /// Iterate the the closure `step`. |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
264 | /// |
5 | 265 | /// The closure should accept a `state` parameter (satisfying the trait [`AlgIteratorState`]), |
266 | /// It should return the output of | |
267 | /// `state.`[`if_verbose`][AlgIteratorState::if_verbose]. | |
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
268 | /// |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
269 | /// For usage instructions see the [module documentation][self]. |
0 | 270 | /// |
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
271 | /// This method is equivalent to [`Self::prepare`] followed by [`AlgIterator::iterate`] |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
272 | /// with the error type `E=`[`std::convert::Infallible`]. |
0 | 273 | #[inline] |
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
274 | fn iterate<F>(self, step : F) |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
275 | where F : FnMut(Self::State) -> Step<V, Self::State> { |
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
276 | self.iterate_fallible(step).unwrap_or_default() |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
277 | } |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
278 | |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
279 | /// Iterate the closure `step` with data produced by `datasource`. |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
280 | /// |
5 | 281 | /// The closure should accept a `state` parameter (satisfying the trait [`AlgIteratorState`]), |
282 | /// and a data parameter taken from `datasource`. It should return the output of | |
283 | /// `state.`[`if_verbose`][AlgIteratorState::if_verbose], [`Step::Terminated`] to indicate | |
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
284 | /// completion for other reason, or [`Step::Failure`] for termination for failure. |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
285 | /// |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
286 | /// If the `datasource` runs out of data, the iterator is considered having terminated |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
287 | /// successsfully. |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
288 | /// |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
289 | /// For usage instructions see the [module documentation][self]. |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
290 | #[inline] |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
291 | fn iterate_data_fallible<F, D, I, E>(self, mut datasource : I, mut step : F) |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
292 | -> Result<(), E> |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
293 | where F : FnMut(Self::State, D) -> Step<V, Self::State, E>, |
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
294 | I : Iterator<Item = D>, |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
295 | E : Error { |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
296 | self.prepare().iterate(move |state| { |
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
297 | datasource.next().map_or(Step::Terminated, |d| step(state, d)) |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
298 | }) |
0 | 299 | } |
300 | ||
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
301 | /// Iterate the closure `step` with data produced by `datasource`. |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
302 | /// |
5 | 303 | /// The closure should accept a `state` parameter (satisfying the trait [`AlgIteratorState`]), |
304 | /// and a data parameter taken from `datasource`. It should return the output of | |
305 | /// `state.`[`if_verbose`][AlgIteratorState::if_verbose]. | |
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
306 | /// |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
307 | /// If the `datasource` runs out of data, the iterator is considered having terminated |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
308 | /// successsfully. |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
309 | /// |
0 | 310 | /// For usage instructions see the [module documentation][self]. |
311 | #[inline] | |
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
312 | fn iterate_data<F, D, I>(self, datasource : I, step : F) |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
313 | where F : FnMut(Self::State, D) -> Step<V, Self::State>, |
0 | 314 | I : Iterator<Item = D> { |
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
315 | self.iterate_data_fallible(datasource, step).unwrap_or_default() |
0 | 316 | } |
317 | ||
318 | // fn make_iterate<'own>(self) | |
319 | // -> Box<dyn (FnMut(dyn FnMut(&Self::State) -> Option<V>) -> ()) + 'own> { | |
320 | // Box::new(move |step| self.iterate(step)) | |
321 | // } | |
322 | ||
323 | // fn make_iterate_data<'own, D, I>(self, datasource : I) | |
324 | // -> Box<dyn (FnMut(dyn FnMut(&Self::State, D) -> Option<V>) -> ()) + 'own> | |
325 | // where I : Iterator<Item = D> + 'own { | |
326 | // Box::new(move |step| self.iterate_data(step, datasource)) | |
327 | // } | |
328 | ||
329 | /// Add logging to the iterator produced by the factory. | |
5 | 330 | /// |
331 | /// Returns a new factory whose corresponding [`AlgIterator`] only inserts that data into the | |
332 | /// log without passing it onwards. | |
333 | /// | |
334 | /// Use as: | |
335 | /// ```rust | |
336 | /// # use alg_tools::iterate::*; | |
337 | /// # use alg_tools::logger::*; | |
338 | /// let iter = AlgIteratorOptions::default(); | |
339 | /// let mut log = Logger::new(); | |
340 | /// iter.into_log(&mut log).iterate(|state|{ | |
341 | /// // perform iterations | |
342 | /// state.if_verbose(||{ | |
343 | /// // calculate and return function value or other displayed data v | |
344 | /// return 0 | |
345 | /// }) | |
346 | /// }) | |
347 | /// ``` | |
0 | 348 | fn into_log<'log>(self, logger : &'log mut Logger<Self::Output>) |
349 | -> LoggingIteratorFactory<'log, Self::Output, Self> | |
350 | where Self : Sized { | |
351 | LoggingIteratorFactory { | |
352 | base_options : self, | |
353 | logger, | |
354 | } | |
355 | } | |
356 | ||
357 | /// Map the output of the iterator produced by the factory. | |
5 | 358 | /// |
359 | /// Returns a new factory. | |
0 | 360 | fn mapped<U, G>(self, map : G) |
361 | -> MappingIteratorFactory<G, Self> | |
362 | where Self : Sized, | |
363 | G : Fn(usize, Self::Output) -> U { | |
364 | MappingIteratorFactory { | |
365 | base_options : self, | |
366 | map | |
367 | } | |
368 | } | |
369 | ||
5 | 370 | /// Adds iteration number to the output. |
371 | /// | |
372 | /// Returns a new factory. | |
373 | /// Typically followed by [`Self::into_log`]. | |
0 | 374 | fn with_iteration_number(self) |
375 | -> MappingIteratorFactory<fn(usize, Self::Output) -> LogItem<Self::Output>, Self> | |
376 | where Self : Sized { | |
5 | 377 | self.mapped(LogItem::new) |
0 | 378 | } |
379 | ||
380 | /// Add timing to the iterator produced by the factory. | |
381 | fn timed(self) -> TimingIteratorFactory<Self> | |
382 | where Self : Sized { | |
383 | TimingIteratorFactory(self) | |
384 | } | |
385 | ||
386 | /// Add value stopping threshold to the iterator produce by the factory | |
387 | fn stop_target(self, target : Self::Output) -> ValueIteratorFactory<Self::Output, Self> | |
388 | where Self : Sized, | |
389 | Self::Output : Num { | |
390 | ValueIteratorFactory { base_options : self, target : target } | |
391 | } | |
392 | ||
393 | /// Add stall stopping to the iterator produce by the factory | |
394 | fn stop_stall(self, stall : Self::Output) -> StallIteratorFactory<Self::Output, Self> | |
395 | where Self : Sized, | |
396 | Self::Output : Num { | |
397 | StallIteratorFactory { base_options : self, stall : stall } | |
398 | } | |
399 | ||
400 | /// Is the iterator quiet, i.e., on-verbose? | |
401 | fn is_quiet(&self) -> bool { false } | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
402 | |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
403 | /// Returns an an [`std::iter::Iterator`] that can be used in a `for`-loop. |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
404 | fn iter(self) -> AlgIteratorIterator<Self::Iter> { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
405 | AlgIteratorIterator { |
41
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
406 | algi : Rc::new(RefCell::new(self.prepare())), |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
407 | } |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
408 | } |
0 | 409 | } |
410 | ||
5 | 411 | /// Options for [`BasicAlgIteratorFactory`]. |
412 | /// | |
413 | /// Use as: | |
0 | 414 | /// ``` |
415 | /// # use alg_tools::iterate::*; | |
416 | /// let iter = AlgIteratorOptions{ max_iter : 10000, .. Default::default() }; | |
417 | /// ``` | |
418 | #[derive(Clone, Copy, Debug, Serialize, Deserialize, Eq, PartialEq)] | |
419 | pub struct AlgIteratorOptions { | |
420 | /// Maximum number of iterations | |
421 | pub max_iter : usize, | |
422 | /// Number of iterations between verbose iterations that display state. | |
423 | pub verbose_iter : Verbose, | |
424 | /// Whether verbose iterations are displayed, or just passed onwards to a containing | |
425 | /// `AlgIterator`. | |
426 | pub quiet : bool, | |
427 | } | |
428 | ||
429 | #[derive(Clone, Copy, Debug, Serialize, Deserialize, Eq, PartialEq)] | |
430 | pub enum Verbose { | |
431 | /// Be verbose every $n$ iterations. | |
432 | Every(usize), | |
433 | /// Be verbose every $n$ iterations and initial $m$ iterations. | |
434 | EveryAndInitial{ every : usize, initial : usize }, | |
435 | /// Be verbose if iteration number $n$ divides by $b^{\text{floor}(\log_b(n))}$, where | |
436 | /// $b$ is indicated logarithmic base. So, with $b=10$, | |
437 | /// * every iteration for first 10 iterations, | |
438 | /// * every 10 iterations from there on until 100 iterations, | |
439 | /// * every 100 iteartinos frmo there on until 1000 iterations, etc. | |
440 | Logarithmic(usize), | |
441 | } | |
442 | ||
443 | impl Verbose { | |
444 | /// Indicates whether given iteration number is verbose | |
445 | pub fn is_verbose(&self, iter : usize) -> bool { | |
446 | match self { | |
447 | &Verbose::Every(every) => { | |
448 | every != 0 && iter % every == 0 | |
449 | }, | |
450 | &Verbose::EveryAndInitial{ every, initial } => { | |
451 | iter <= initial || (every != 0 && iter % every == 0) | |
452 | }, | |
453 | &Verbose::Logarithmic(base) => { | |
25
d14c877e14b7
Logarithmic logging base correction
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
454 | let every = base.pow((iter as float).log(base as float).floor() as u32); |
0 | 455 | iter % every == 0 |
456 | } | |
457 | } | |
458 | } | |
459 | } | |
460 | ||
461 | impl Default for AlgIteratorOptions { | |
462 | fn default() -> AlgIteratorOptions { | |
463 | AlgIteratorOptions{ | |
464 | max_iter : 1000, | |
465 | verbose_iter : Verbose::EveryAndInitial { every : 100, initial : 10 }, | |
466 | quiet : false | |
467 | } | |
468 | } | |
469 | } | |
470 | ||
471 | /// State of a `BasicAlgIterator` | |
472 | #[derive(Clone,Copy,Debug,Serialize,Eq,PartialEq)] | |
473 | pub struct BasicState { | |
5 | 474 | /// Current iteration |
0 | 475 | iter : usize, |
5 | 476 | /// Whether the iteration is verbose, i.e., results should be displayed. |
477 | /// Requires `calc` to be `true`. | |
0 | 478 | verbose : bool, |
5 | 479 | /// Whether results should be calculated. |
0 | 480 | calc : bool, |
31
50a77e4efcbb
Add is_quiet to AlgIteratorState as well.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
481 | /// Indicates whether the iteration is quiet |
50a77e4efcbb
Add is_quiet to AlgIteratorState as well.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
482 | quiet : bool, |
0 | 483 | } |
484 | ||
485 | /// [`AlgIteratorFactory`] for [`BasicAlgIterator`] | |
486 | #[derive(Clone,Debug)] | |
487 | pub struct BasicAlgIteratorFactory<V> { | |
488 | options : AlgIteratorOptions, | |
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
489 | _phantoms : PhantomData<V>, |
0 | 490 | } |
491 | ||
5 | 492 | /// The simplest [`AlgIterator`], created by [`BasicAlgIteratorFactory`] |
0 | 493 | #[derive(Clone,Debug)] |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
494 | pub struct BasicAlgIterator<V> { |
0 | 495 | options : AlgIteratorOptions, |
496 | iter : usize, | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
497 | _phantoms : PhantomData<V>, |
0 | 498 | } |
499 | ||
500 | impl AlgIteratorOptions { | |
501 | /// [`AlgIteratorOptions`] is directly a factory for [`BasicAlgIterator`], | |
502 | /// however, due to type inference issues, it may become convenient to instantiate | |
503 | /// it to a specific return type for the inner step function. This method does that. | |
504 | pub fn instantiate<V>(&self) -> BasicAlgIteratorFactory<V> { | |
505 | BasicAlgIteratorFactory { | |
506 | options : self.clone(), | |
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
507 | _phantoms : PhantomData |
0 | 508 | } |
509 | } | |
510 | } | |
511 | ||
512 | impl<V> AlgIteratorFactory<V> for AlgIteratorOptions | |
513 | where V : LogRepr { | |
514 | type State = BasicState; | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
515 | type Iter = BasicAlgIterator<V>; |
0 | 516 | type Output = V; |
517 | ||
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
518 | fn prepare(self) -> Self::Iter { |
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
519 | BasicAlgIterator{ |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
520 | options : self, |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
521 | iter : 0, |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
522 | _phantoms : PhantomData, |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
523 | } |
0 | 524 | } |
525 | ||
526 | #[inline] | |
527 | fn is_quiet(&self) -> bool { | |
528 | self.quiet | |
529 | } | |
530 | } | |
531 | ||
532 | impl<V> AlgIteratorFactory<V> for BasicAlgIteratorFactory<V> | |
533 | where V : LogRepr { | |
534 | type State = BasicState; | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
535 | type Iter = BasicAlgIterator<V>; |
0 | 536 | type Output = V; |
537 | ||
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
538 | fn prepare(self) -> Self::Iter { |
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
539 | BasicAlgIterator { |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
540 | options : self.options, |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
541 | iter : 0, |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
542 | _phantoms : PhantomData |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
543 | } |
0 | 544 | } |
545 | ||
546 | #[inline] | |
547 | fn is_quiet(&self) -> bool { | |
548 | self.options.quiet | |
549 | } | |
550 | } | |
551 | ||
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
552 | impl<V> AlgIterator for BasicAlgIterator<V> |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
553 | where V : LogRepr { |
0 | 554 | type State = BasicState; |
555 | type Output = V; | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
556 | type Input = V; |
0 | 557 | |
558 | #[inline] | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
559 | fn prestep(&mut self) -> Option<Self::State> { |
0 | 560 | if self.iter >= self.options.max_iter { |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
561 | None |
0 | 562 | } else { |
563 | self.iter += 1; | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
564 | Some(self.state()) |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
565 | } |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
566 | } |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
567 | |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
568 | fn poststep<E : Error>(&mut self, res : Step<V, Self::State, E>) -> Step<V, Self::State, E> { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
569 | if let Step::Result(ref val, ref state) = res { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
570 | if state.verbose && !self.options.quiet { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
571 | println!("{}{}/{} {}{}", "".dimmed(), |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
572 | state.iter, |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
573 | self.options.max_iter, |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
574 | val.logrepr(), |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
575 | "".clear()); |
0 | 576 | } |
577 | } | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
578 | res |
0 | 579 | } |
580 | ||
581 | #[inline] | |
582 | fn iteration(&self) -> usize { | |
583 | self.iter | |
584 | } | |
585 | ||
586 | #[inline] | |
587 | fn state(&self) -> BasicState { | |
588 | let iter = self.iter; | |
589 | let verbose = self.options.verbose_iter.is_verbose(iter); | |
31
50a77e4efcbb
Add is_quiet to AlgIteratorState as well.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
590 | BasicState { |
0 | 591 | iter : iter, |
592 | verbose : verbose, | |
593 | calc : verbose, | |
31
50a77e4efcbb
Add is_quiet to AlgIteratorState as well.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
594 | quiet : self.options.quiet |
0 | 595 | } |
596 | } | |
597 | } | |
598 | ||
599 | impl AlgIteratorState for BasicState { | |
600 | #[inline] | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
601 | fn if_verbose<V, E : Error>(self, mut calc_objective : impl FnMut() -> V) -> Step<V, Self, E> { |
0 | 602 | if self.calc { |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
603 | Step::Result(calc_objective(), self) |
0 | 604 | } else { |
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
605 | Step::Quiet |
0 | 606 | } |
607 | } | |
608 | ||
609 | #[inline] | |
610 | fn iteration(&self) -> usize { | |
31
50a77e4efcbb
Add is_quiet to AlgIteratorState as well.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
611 | self.iter |
50a77e4efcbb
Add is_quiet to AlgIteratorState as well.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
612 | } |
50a77e4efcbb
Add is_quiet to AlgIteratorState as well.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
613 | |
50a77e4efcbb
Add is_quiet to AlgIteratorState as well.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
614 | #[inline] |
50a77e4efcbb
Add is_quiet to AlgIteratorState as well.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
615 | fn is_quiet(&self) -> bool { |
50a77e4efcbb
Add is_quiet to AlgIteratorState as well.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
616 | self.quiet |
0 | 617 | } |
618 | } | |
619 | ||
620 | // | |
621 | // Stall detecting iteration function. | |
622 | // | |
623 | ||
5 | 624 | /// An [`AlgIteratorFactory`] for an [`AlgIterator`] that detects “stall”. |
625 | /// | |
626 | /// We define stall as $(v_{k+n}-v_k)/v_k ≤ θ$, where $n$ the distance between | |
627 | /// [`Step::Result`] iterations, and $θ$ is the provided `stall` parameter. | |
0 | 628 | #[derive(Clone,Copy,Debug,Serialize,Eq,PartialEq)] |
629 | pub struct StallIteratorFactory<U : Num, BaseFactory> { | |
5 | 630 | /// An [`AlgIteratorFactory`] on which to build on |
0 | 631 | pub base_options : BaseFactory, |
632 | /// Stalling threshold $θ$. | |
633 | pub stall : U, | |
634 | } | |
635 | ||
636 | /// Iterator produced by [`StallIteratorFactory`]. | |
637 | pub struct StallIterator<U : Num, BaseIterator> { | |
638 | base_iterator : BaseIterator, | |
639 | stall : U, | |
640 | previous_value : Option<U>, | |
641 | } | |
642 | ||
643 | impl<V, U, BaseFactory> AlgIteratorFactory<V> | |
644 | for StallIteratorFactory<U, BaseFactory> | |
645 | where BaseFactory : AlgIteratorFactory<V, Output=U>, | |
646 | U : SignedNum + PartialOrd { | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
647 | type Iter = StallIterator<U, BaseFactory::Iter>; |
0 | 648 | type State = BaseFactory::State; |
649 | type Output = BaseFactory::Output; | |
650 | ||
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
651 | fn prepare(self) -> Self::Iter { |
0 | 652 | StallIterator { |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
653 | base_iterator : self.base_options.prepare(), |
0 | 654 | stall : self.stall, |
655 | previous_value : None, | |
656 | } | |
657 | } | |
658 | ||
659 | fn is_quiet(&self) -> bool { | |
660 | self.base_options.is_quiet() | |
661 | } | |
662 | } | |
663 | ||
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
664 | impl<U, BaseIterator> AlgIterator |
0 | 665 | for StallIterator<U, BaseIterator> |
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
666 | where BaseIterator : AlgIterator<Output=U>, |
0 | 667 | U : SignedNum + PartialOrd { |
668 | type State = BaseIterator::State; | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
669 | type Output = U; |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
670 | type Input = BaseIterator::Input; |
0 | 671 | |
672 | #[inline] | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
673 | fn prestep(&mut self) -> Option<Self::State> { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
674 | self.base_iterator.prestep() |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
675 | } |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
676 | |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
677 | #[inline] |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
678 | fn poststep<E>(&mut self, res : Step<Self::Input, Self::State, E>) -> Step<U, Self::State, E> |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
679 | where E : Error { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
680 | match self.base_iterator.poststep(res) { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
681 | Step::Result(nv, state) => { |
0 | 682 | let previous_v = self.previous_value; |
683 | self.previous_value = Some(nv); | |
684 | match previous_v { | |
685 | Some(pv) if (nv - pv).abs() <= self.stall * pv.abs() => Step::Terminated, | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
686 | _ => Step::Result(nv, state), |
0 | 687 | } |
688 | }, | |
689 | val => val, | |
690 | } | |
691 | } | |
692 | ||
693 | #[inline] | |
694 | fn iteration(&self) -> usize { | |
695 | self.base_iterator.iteration() | |
696 | } | |
697 | ||
698 | #[inline] | |
699 | fn state(&self) -> Self::State { | |
700 | self.base_iterator.state() | |
701 | } | |
702 | } | |
703 | ||
704 | /// An [`AlgIteratorFactory`] for an [`AlgIterator`] that detect whether step function | |
705 | /// return value is less than `target`, and terminates if it is. | |
706 | #[derive(Clone,Copy,Debug,Serialize,Eq,PartialEq)] | |
707 | pub struct ValueIteratorFactory<U : Num, BaseFactory> { | |
5 | 708 | /// An [`AlgIteratorFactory`] on which to build on |
0 | 709 | pub base_options : BaseFactory, |
5 | 710 | /// Target value |
0 | 711 | pub target : U, |
712 | } | |
713 | ||
714 | /// Iterator produced by [`ValueIteratorFactory`]. | |
715 | pub struct ValueIterator<U : Num, BaseIterator> { | |
716 | base_iterator : BaseIterator, | |
717 | target : U, | |
718 | } | |
719 | ||
720 | impl<V, U, BaseFactory> AlgIteratorFactory<V> | |
721 | for ValueIteratorFactory<U, BaseFactory> | |
722 | where BaseFactory : AlgIteratorFactory<V, Output=U>, | |
723 | U : SignedNum + PartialOrd { | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
724 | type Iter = ValueIterator<U, BaseFactory::Iter>; |
0 | 725 | type State = BaseFactory::State; |
726 | type Output = BaseFactory::Output; | |
727 | ||
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
728 | fn prepare(self) -> Self::Iter { |
0 | 729 | ValueIterator { |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
730 | base_iterator : self.base_options.prepare(), |
0 | 731 | target : self.target |
732 | } | |
733 | } | |
734 | ||
735 | fn is_quiet(&self) -> bool { | |
736 | self.base_options.is_quiet() | |
737 | } | |
738 | } | |
739 | ||
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
740 | impl<U, BaseIterator> AlgIterator |
0 | 741 | for ValueIterator<U, BaseIterator> |
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
742 | where BaseIterator : AlgIterator<Output=U>, |
0 | 743 | U : SignedNum + PartialOrd { |
744 | type State = BaseIterator::State; | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
745 | type Output = U; |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
746 | type Input = BaseIterator::Input; |
0 | 747 | |
748 | #[inline] | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
749 | fn prestep(&mut self) -> Option<Self::State> { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
750 | self.base_iterator.prestep() |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
751 | } |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
752 | |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
753 | #[inline] |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
754 | fn poststep<E>(&mut self, res : Step<Self::Input, Self::State, E>) -> Step<U, Self::State, E> where E : Error{ |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
755 | match self.base_iterator.poststep(res) { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
756 | Step::Result(v, state) => { |
0 | 757 | if v <= self.target { |
758 | Step::Terminated | |
759 | } else { | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
760 | Step::Result(v, state) |
0 | 761 | } |
762 | }, | |
763 | val => val, | |
764 | } | |
765 | } | |
766 | ||
767 | #[inline] | |
768 | fn iteration(&self) -> usize { | |
769 | self.base_iterator.iteration() | |
770 | } | |
771 | ||
772 | #[inline] | |
773 | fn state(&self) -> Self::State { | |
774 | self.base_iterator.state() | |
775 | } | |
776 | } | |
777 | ||
778 | // | |
779 | // Logging iterator | |
780 | // | |
781 | ||
782 | /// [`AlgIteratorFactory`] for a logging [`AlgIterator`]. | |
5 | 783 | /// |
784 | /// Typically produced with [`AlgIteratorFactory::into_log`]. | |
0 | 785 | /// The `Output` of the corresponding [`LoggingIterator`] is `()`: |
786 | #[derive(Debug)] | |
787 | pub struct LoggingIteratorFactory<'log, U, BaseFactory> { | |
5 | 788 | /// Base [`AlgIteratorFactory`] on which to build |
789 | base_options : BaseFactory, | |
790 | /// The `Logger` to use. | |
791 | logger : &'log mut Logger<U>, | |
0 | 792 | } |
793 | ||
794 | /// Iterator produced by `LoggingIteratorFactory`. | |
795 | pub struct LoggingIterator<'log, U, BaseIterator> { | |
796 | base_iterator : BaseIterator, | |
797 | logger : &'log mut Logger<U>, | |
798 | } | |
799 | ||
800 | ||
801 | impl<'log, V, BaseFactory> AlgIteratorFactory<V> | |
802 | for LoggingIteratorFactory<'log, BaseFactory::Output, BaseFactory> | |
803 | where BaseFactory : AlgIteratorFactory<V>, | |
804 | BaseFactory::Output : 'log { | |
805 | type State = BaseFactory::State; | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
806 | type Iter = LoggingIterator<'log, BaseFactory::Output, BaseFactory::Iter>; |
0 | 807 | type Output = (); |
808 | ||
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
809 | fn prepare(self) -> Self::Iter { |
0 | 810 | LoggingIterator { |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
811 | base_iterator : self.base_options.prepare(), |
0 | 812 | logger : self.logger, |
813 | } | |
814 | } | |
815 | ||
816 | #[inline] | |
817 | fn is_quiet(&self) -> bool { | |
818 | self.base_options.is_quiet() | |
819 | } | |
820 | } | |
821 | ||
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
822 | impl<'log, BaseIterator> AlgIterator |
0 | 823 | for LoggingIterator<'log, BaseIterator::Output, BaseIterator> |
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
824 | where BaseIterator : AlgIterator, |
0 | 825 | BaseIterator::Output : 'log { |
826 | type State = BaseIterator::State; | |
827 | type Output = (); | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
828 | type Input = BaseIterator::Input; |
0 | 829 | |
830 | #[inline] | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
831 | fn prestep(&mut self) -> Option<Self::State> { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
832 | self.base_iterator.prestep() |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
833 | } |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
834 | |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
835 | #[inline] |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
836 | fn poststep<E>(&mut self, res : Step<Self::Input, Self::State, E>) -> Step<(), Self::State, E> where E : Error { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
837 | match self.base_iterator.poststep(res) { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
838 | Step::Result(v, _) => { |
0 | 839 | self.logger.log(v); |
840 | Step::Quiet | |
841 | }, | |
842 | Step::Quiet => Step::Quiet, | |
843 | Step::Terminated => Step::Terminated, | |
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
844 | Step::Failure(e) => Step::Failure(e), |
0 | 845 | } |
846 | } | |
847 | ||
848 | #[inline] | |
849 | fn iteration(&self) -> usize { | |
850 | self.base_iterator.iteration() | |
851 | } | |
852 | ||
853 | #[inline] | |
854 | fn state(&self) -> Self::State { | |
855 | self.base_iterator.state() | |
856 | } | |
857 | } | |
858 | ||
859 | /// This [`AlgIteratorFactory`] allows output mapping. | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
860 | /// |
5 | 861 | /// Typically produced with [`AlgIteratorFactory::mapped`]. |
0 | 862 | #[derive(Debug)] |
863 | pub struct MappingIteratorFactory<G, BaseFactory> { | |
5 | 864 | /// Base [`AlgIteratorFactory`] on which to build |
865 | base_options : BaseFactory, | |
866 | /// A closure `G : Fn(usize, BaseFactory::Output) -> U` that gets the current iteration | |
867 | /// and the output of the base factory as input, and produces a new output. | |
868 | map : G, | |
0 | 869 | } |
870 | ||
5 | 871 | /// [`AlgIterator`] produced by [`MappingIteratorFactory`]. |
0 | 872 | pub struct MappingIterator<G, BaseIterator> { |
873 | base_iterator : BaseIterator, | |
874 | map : G, | |
875 | } | |
876 | ||
877 | ||
878 | impl<V, U, G, BaseFactory> AlgIteratorFactory<V> | |
879 | for MappingIteratorFactory<G, BaseFactory> | |
880 | where BaseFactory : AlgIteratorFactory<V>, | |
881 | G : Fn(usize, BaseFactory::Output) -> U { | |
882 | type State = BaseFactory::State; | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
883 | type Iter = MappingIterator<G, BaseFactory::Iter>; |
0 | 884 | type Output = U; |
885 | ||
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
886 | fn prepare(self) -> Self::Iter { |
0 | 887 | MappingIterator { |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
888 | base_iterator : self.base_options.prepare(), |
0 | 889 | map : self.map |
890 | } | |
891 | } | |
892 | ||
893 | #[inline] | |
894 | fn is_quiet(&self) -> bool { | |
895 | self.base_options.is_quiet() | |
896 | } | |
897 | } | |
898 | ||
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
899 | impl<U, G, BaseIterator> AlgIterator |
0 | 900 | for MappingIterator<G, BaseIterator> |
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
901 | where BaseIterator : AlgIterator, |
0 | 902 | G : Fn(usize, BaseIterator::Output) -> U { |
903 | type State = BaseIterator::State; | |
904 | type Output = U; | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
905 | type Input = BaseIterator::Input; |
0 | 906 | |
907 | #[inline] | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
908 | fn prestep(&mut self) -> Option<Self::State> { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
909 | self.base_iterator.prestep() |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
910 | } |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
911 | |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
912 | #[inline] |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
913 | fn poststep<E>(&mut self, res : Step<Self::Input, Self::State, E>) -> Step<Self::Output, Self::State, E> where E : Error { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
914 | match self.base_iterator.poststep(res) { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
915 | Step::Result(v, state) => Step::Result((self.map)(self.iteration(), v), state), |
0 | 916 | Step::Quiet => Step::Quiet, |
917 | Step::Terminated => Step::Terminated, | |
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
918 | Step::Failure(e) => Step::Failure(e), |
0 | 919 | } |
920 | } | |
921 | ||
922 | #[inline] | |
923 | fn iteration(&self) -> usize { | |
924 | self.base_iterator.iteration() | |
925 | } | |
926 | ||
927 | #[inline] | |
928 | fn state(&self) -> Self::State { | |
929 | self.base_iterator.state() | |
930 | } | |
931 | } | |
932 | ||
933 | // | |
934 | // Timing iterator | |
935 | // | |
936 | ||
5 | 937 | /// An [`AlgIteratorFactory`] for an [`AlgIterator`] that adds spent CPU time to verbose events. |
0 | 938 | #[derive(Debug)] |
939 | pub struct TimingIteratorFactory<BaseFactory>(pub BaseFactory); | |
940 | ||
941 | /// Iterator produced by [`TimingIteratorFactory`] | |
942 | #[derive(Debug)] | |
943 | pub struct TimingIterator<BaseIterator> { | |
944 | base_iterator : BaseIterator, | |
945 | start_time : ProcessTime, | |
946 | } | |
947 | ||
948 | /// Data `U` with production time attached | |
949 | #[derive(Copy, Clone, Debug, Serialize)] | |
950 | pub struct Timed<U> { | |
951 | pub cpu_time : Duration, | |
952 | //#[serde(flatten)] | |
953 | pub data : U | |
954 | } | |
955 | ||
956 | impl<T> LogRepr for Timed<T> where T : LogRepr { | |
957 | fn logrepr(&self) -> ColoredString { | |
958 | format!("[{:.3}s] {}", self.cpu_time.as_secs_f64(), self.data.logrepr()).as_str().into() | |
959 | } | |
960 | } | |
961 | ||
962 | ||
963 | impl<V, BaseFactory> AlgIteratorFactory<V> | |
964 | for TimingIteratorFactory<BaseFactory> | |
965 | where BaseFactory : AlgIteratorFactory<V> { | |
966 | type State = BaseFactory::State; | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
967 | type Iter = TimingIterator<BaseFactory::Iter>; |
0 | 968 | type Output = Timed<BaseFactory::Output>; |
969 | ||
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
970 | fn prepare(self) -> Self::Iter { |
0 | 971 | TimingIterator { |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
972 | base_iterator : self.0.prepare(), |
0 | 973 | start_time : ProcessTime::now() |
974 | } | |
975 | } | |
976 | ||
977 | #[inline] | |
978 | fn is_quiet(&self) -> bool { | |
979 | self.0.is_quiet() | |
980 | } | |
981 | } | |
982 | ||
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
983 | impl<BaseIterator> AlgIterator |
0 | 984 | for TimingIterator<BaseIterator> |
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
985 | where BaseIterator : AlgIterator { |
0 | 986 | type State = BaseIterator::State; |
987 | type Output = Timed<BaseIterator::Output>; | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
988 | type Input = BaseIterator::Input; |
0 | 989 | |
990 | #[inline] | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
991 | fn prestep(&mut self) -> Option<Self::State> { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
992 | self.base_iterator.prestep() |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
993 | } |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
994 | |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
995 | #[inline] |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
996 | fn poststep<E>(&mut self, res : Step<Self::Input, Self::State, E>) -> Step<Self::Output, Self::State, E> where E : Error { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
997 | match self.base_iterator.poststep(res) { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
998 | Step::Result(data, state) => { |
0 | 999 | Step::Result(Timed{ |
1000 | cpu_time : self.start_time.elapsed(), | |
1001 | data | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1002 | }, state) |
0 | 1003 | }, |
1004 | Step::Quiet => Step::Quiet, | |
1005 | Step::Terminated => Step::Terminated, | |
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
1006 | Step::Failure(e) => Step::Failure(e), |
0 | 1007 | } |
1008 | } | |
1009 | ||
1010 | #[inline] | |
1011 | fn iteration(&self) -> usize { | |
1012 | self.base_iterator.iteration() | |
1013 | } | |
1014 | ||
1015 | #[inline] | |
1016 | fn state(&self) -> Self::State { | |
1017 | self.base_iterator.state() | |
1018 | } | |
1019 | } | |
1020 | ||
1021 | // | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1022 | // New for-loop interface |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1023 | // |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1024 | |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1025 | pub struct AlgIteratorIterator<I : AlgIterator> { |
41
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1026 | algi : Rc<RefCell<I>>, |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1027 | } |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1028 | |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1029 | pub struct AlgIteratorIteration<I : AlgIterator> { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1030 | state : I::State, |
41
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1031 | algi : Rc<RefCell<I>>, |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1032 | } |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1033 | |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1034 | impl<I : AlgIterator> std::iter::Iterator for AlgIteratorIterator<I> { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1035 | type Item = AlgIteratorIteration<I>; |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1036 | |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1037 | fn next(&mut self) -> Option<Self::Item> { |
41
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1038 | let algi = self.algi.clone(); |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1039 | RefCell::borrow_mut(&self.algi).prestep().map(|state| AlgIteratorIteration { |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1040 | state, |
41
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1041 | algi, |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1042 | }) |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1043 | } |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1044 | } |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1045 | |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1046 | /// Types of errors that may occur |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1047 | #[derive(Debug,PartialEq,Eq)] |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1048 | pub enum IterationError { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1049 | /// [`AlgIteratorIteration::if_verbose_check`] is not called in iteration order. |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1050 | ReportingOrderingError |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1051 | } |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1052 | |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1053 | impl<I : AlgIterator> AlgIteratorIteration<I> { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1054 | /// Call `call_objective` if this is a verbose iteration. |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1055 | /// |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1056 | /// Verbosity depends on the [`AlgIterator`] that produced this state. |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1057 | /// |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1058 | /// The closure `calc_objective` should return an arbitrary value of type `V`, to be inserted |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1059 | /// into the log, or whatever is deemed by the [`AlgIterator`]. For usage instructions see the |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1060 | /// [module documentation][self]. |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1061 | /// |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1062 | /// This function may panic if result reporting is not ordered correctly (an unlikely mistake |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1063 | /// if using this facility correctly). For a version that propagates errors, see |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1064 | /// [`Self::if_verbose_check`]. |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1065 | pub fn if_verbose(self, calc_objective : impl FnMut() -> I::Input) { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1066 | self.if_verbose_check(calc_objective).unwrap() |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1067 | } |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1068 | |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1069 | /// Version of [`Self::if_verbose`] that propagates errors instead of panicking. |
41
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1070 | pub fn if_verbose_check(self, calc_objective : impl FnMut() -> I::Input) |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1071 | -> Result<(), IterationError> { |
41
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1072 | let mut algi = match RefCell::try_borrow_mut(&self.algi) { |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1073 | Err(_) => return Err(IterationError::ReportingOrderingError), |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1074 | Ok(algi) => algi |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1075 | }; |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1076 | if self.state.iteration() != algi.iteration() { |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1077 | Err(IterationError::ReportingOrderingError) |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1078 | } else { |
41
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1079 | let res : Step<I::Input, I::State, std::convert::Infallible> |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1080 | = self.state.if_verbose(calc_objective); |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1081 | algi.poststep(res); |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1082 | Ok(()) |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1083 | } |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1084 | } |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1085 | |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1086 | /// Returns the current iteration count. |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1087 | pub fn iteration(&self) -> usize { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1088 | self.state.iteration() |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1089 | } |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1090 | |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1091 | /// Indicates whether the iterator is quiet |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1092 | pub fn is_quiet(&self) -> bool { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1093 | self.state.is_quiet() |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1094 | } |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1095 | } |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1096 | |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1097 | // |
0 | 1098 | // Tests |
1099 | // | |
1100 | ||
1101 | #[cfg(test)] | |
1102 | mod tests { | |
1103 | use super::*; | |
1104 | use crate::logger::Logger; | |
1105 | #[test] | |
1106 | fn iteration() { | |
1107 | let options = AlgIteratorOptions{ | |
1108 | max_iter : 10, | |
1109 | verbose_iter : Verbose::Every(3), | |
1110 | .. Default::default() | |
1111 | }; | |
1112 | ||
1113 | { | |
1114 | let mut start = 1 as int; | |
1115 | options.iterate(|state| { | |
1116 | start = start * 2; | |
1117 | state.if_verbose(|| start) | |
1118 | }); | |
1119 | assert_eq!(start, (2 as int).pow(10)); | |
1120 | } | |
1121 | ||
1122 | { | |
1123 | let mut start = 1 as int; | |
1124 | let mut log = Logger::new(); | |
1125 | let factory = options.instantiate() | |
1126 | .with_iteration_number() | |
1127 | .into_log(&mut log); | |
1128 | factory.iterate(|state| { | |
1129 | start = start * 2; | |
1130 | state.if_verbose(|| start) | |
1131 | }); | |
1132 | assert_eq!(start, (2 as int).pow(10)); | |
1133 | assert_eq!(log.data() | |
1134 | .iter() | |
1135 | .map(|LogItem{ data : v, iter : _ }| v.clone()) | |
1136 | .collect::<Vec<int>>(), | |
1
df3901ec2f5d
Fix some unit tests after fundamental changes that made them invalid
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
1137 | (1..10).map(|i| (2 as int).pow(i)) |
df3901ec2f5d
Fix some unit tests after fundamental changes that made them invalid
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
1138 | .skip(2) |
df3901ec2f5d
Fix some unit tests after fundamental changes that made them invalid
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
1139 | .step_by(3) |
0 | 1140 | .collect::<Vec<int>>()) |
1141 | } | |
1142 | } | |
41
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1143 | |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1144 | #[test] |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1145 | fn iteration_for_loop() { |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1146 | let options = AlgIteratorOptions{ |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1147 | max_iter : 10, |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1148 | verbose_iter : Verbose::Every(3), |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1149 | .. Default::default() |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1150 | }; |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1151 | |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1152 | { |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1153 | let mut start = 1 as int; |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1154 | for state in options.iter() { |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1155 | start = start * 2; |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1156 | state.if_verbose(|| start) |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1157 | } |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1158 | assert_eq!(start, (2 as int).pow(10)); |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1159 | } |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1160 | |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1161 | { |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1162 | let mut start = 1 as int; |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1163 | let mut log = Logger::new(); |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1164 | let factory = options.instantiate() |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1165 | .with_iteration_number() |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1166 | .into_log(&mut log); |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1167 | for state in factory.iter() { |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1168 | start = start * 2; |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1169 | state.if_verbose(|| start) |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1170 | } |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1171 | assert_eq!(start, (2 as int).pow(10)); |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1172 | assert_eq!(log.data() |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1173 | .iter() |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1174 | .map(|LogItem{ data : v, iter : _ }| v.clone()) |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1175 | .collect::<Vec<int>>(), |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1176 | (1..10).map(|i| (2 as int).pow(i)) |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1177 | .skip(2) |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1178 | .step_by(3) |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1179 | .collect::<Vec<int>>()) |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1180 | } |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1181 | } |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1182 | |
0 | 1183 | } |