Mon, 30 Dec 2024 11:00:12 -0500
Fix RowOp apply_mut.
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]. |
46 | 138 | fn if_verbose<V, E : Error>(self, calc_objective : impl FnOnce() -> 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 | |
46 | 263 | /// Iterate the closure `step`. |
3
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 | } |
46 | 409 | |
410 | /// Returns an an [`std::iter::Iterator`] that can be used in a `for`-loop, | |
411 | /// also inputting an initial iteration status calculated by `f` if needed. | |
412 | fn iter_init(self, f : impl FnOnce() -> <Self::Iter as AlgIterator>::Input) | |
413 | -> AlgIteratorIterator<Self::Iter> { | |
414 | let mut i = self.prepare(); | |
415 | let st = i.state(); | |
416 | let step : Step<<Self::Iter as AlgIterator>::Input, Self::State> = st.if_verbose(f); | |
417 | i.poststep(step); | |
418 | AlgIteratorIterator { | |
419 | algi : Rc::new(RefCell::new(i)), | |
420 | } | |
421 | } | |
0 | 422 | } |
423 | ||
5 | 424 | /// Options for [`BasicAlgIteratorFactory`]. |
425 | /// | |
426 | /// Use as: | |
0 | 427 | /// ``` |
428 | /// # use alg_tools::iterate::*; | |
429 | /// let iter = AlgIteratorOptions{ max_iter : 10000, .. Default::default() }; | |
430 | /// ``` | |
431 | #[derive(Clone, Copy, Debug, Serialize, Deserialize, Eq, PartialEq)] | |
432 | pub struct AlgIteratorOptions { | |
433 | /// Maximum number of iterations | |
434 | pub max_iter : usize, | |
435 | /// Number of iterations between verbose iterations that display state. | |
436 | pub verbose_iter : Verbose, | |
437 | /// Whether verbose iterations are displayed, or just passed onwards to a containing | |
438 | /// `AlgIterator`. | |
439 | pub quiet : bool, | |
440 | } | |
441 | ||
442 | #[derive(Clone, Copy, Debug, Serialize, Deserialize, Eq, PartialEq)] | |
443 | pub enum Verbose { | |
444 | /// Be verbose every $n$ iterations. | |
445 | Every(usize), | |
446 | /// Be verbose every $n$ iterations and initial $m$ iterations. | |
447 | EveryAndInitial{ every : usize, initial : usize }, | |
448 | /// Be verbose if iteration number $n$ divides by $b^{\text{floor}(\log_b(n))}$, where | |
449 | /// $b$ is indicated logarithmic base. So, with $b=10$, | |
450 | /// * every iteration for first 10 iterations, | |
451 | /// * every 10 iterations from there on until 100 iterations, | |
452 | /// * every 100 iteartinos frmo there on until 1000 iterations, etc. | |
453 | Logarithmic(usize), | |
454 | } | |
455 | ||
456 | impl Verbose { | |
457 | /// Indicates whether given iteration number is verbose | |
458 | pub fn is_verbose(&self, iter : usize) -> bool { | |
459 | match self { | |
460 | &Verbose::Every(every) => { | |
461 | every != 0 && iter % every == 0 | |
462 | }, | |
463 | &Verbose::EveryAndInitial{ every, initial } => { | |
464 | iter <= initial || (every != 0 && iter % every == 0) | |
465 | }, | |
466 | &Verbose::Logarithmic(base) => { | |
25
d14c877e14b7
Logarithmic logging base correction
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
467 | let every = base.pow((iter as float).log(base as float).floor() as u32); |
0 | 468 | iter % every == 0 |
469 | } | |
470 | } | |
471 | } | |
472 | } | |
473 | ||
474 | impl Default for AlgIteratorOptions { | |
475 | fn default() -> AlgIteratorOptions { | |
476 | AlgIteratorOptions{ | |
477 | max_iter : 1000, | |
478 | verbose_iter : Verbose::EveryAndInitial { every : 100, initial : 10 }, | |
479 | quiet : false | |
480 | } | |
481 | } | |
482 | } | |
483 | ||
484 | /// State of a `BasicAlgIterator` | |
485 | #[derive(Clone,Copy,Debug,Serialize,Eq,PartialEq)] | |
486 | pub struct BasicState { | |
5 | 487 | /// Current iteration |
0 | 488 | iter : usize, |
5 | 489 | /// Whether the iteration is verbose, i.e., results should be displayed. |
490 | /// Requires `calc` to be `true`. | |
0 | 491 | verbose : bool, |
5 | 492 | /// Whether results should be calculated. |
0 | 493 | calc : bool, |
31
50a77e4efcbb
Add is_quiet to AlgIteratorState as well.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
494 | /// Indicates whether the iteration is quiet |
50a77e4efcbb
Add is_quiet to AlgIteratorState as well.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
495 | quiet : bool, |
0 | 496 | } |
497 | ||
498 | /// [`AlgIteratorFactory`] for [`BasicAlgIterator`] | |
499 | #[derive(Clone,Debug)] | |
500 | pub struct BasicAlgIteratorFactory<V> { | |
501 | options : AlgIteratorOptions, | |
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
502 | _phantoms : PhantomData<V>, |
0 | 503 | } |
504 | ||
5 | 505 | /// The simplest [`AlgIterator`], created by [`BasicAlgIteratorFactory`] |
0 | 506 | #[derive(Clone,Debug)] |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
507 | pub struct BasicAlgIterator<V> { |
0 | 508 | options : AlgIteratorOptions, |
509 | iter : usize, | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
510 | _phantoms : PhantomData<V>, |
0 | 511 | } |
512 | ||
513 | impl AlgIteratorOptions { | |
514 | /// [`AlgIteratorOptions`] is directly a factory for [`BasicAlgIterator`], | |
515 | /// however, due to type inference issues, it may become convenient to instantiate | |
516 | /// it to a specific return type for the inner step function. This method does that. | |
517 | pub fn instantiate<V>(&self) -> BasicAlgIteratorFactory<V> { | |
518 | BasicAlgIteratorFactory { | |
519 | 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
|
520 | _phantoms : PhantomData |
0 | 521 | } |
522 | } | |
523 | } | |
524 | ||
525 | impl<V> AlgIteratorFactory<V> for AlgIteratorOptions | |
526 | where V : LogRepr { | |
527 | type State = BasicState; | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
528 | type Iter = BasicAlgIterator<V>; |
0 | 529 | type Output = V; |
530 | ||
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
531 | 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
|
532 | BasicAlgIterator{ |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
533 | options : self, |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
534 | iter : 0, |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
535 | _phantoms : PhantomData, |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
536 | } |
0 | 537 | } |
538 | ||
539 | #[inline] | |
540 | fn is_quiet(&self) -> bool { | |
541 | self.quiet | |
542 | } | |
543 | } | |
544 | ||
545 | impl<V> AlgIteratorFactory<V> for BasicAlgIteratorFactory<V> | |
546 | where V : LogRepr { | |
547 | type State = BasicState; | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
548 | type Iter = BasicAlgIterator<V>; |
0 | 549 | type Output = V; |
550 | ||
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
551 | 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
|
552 | BasicAlgIterator { |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
553 | options : self.options, |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
554 | iter : 0, |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
555 | _phantoms : PhantomData |
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
556 | } |
0 | 557 | } |
558 | ||
559 | #[inline] | |
560 | fn is_quiet(&self) -> bool { | |
561 | self.options.quiet | |
562 | } | |
563 | } | |
564 | ||
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
565 | impl<V> AlgIterator for BasicAlgIterator<V> |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
566 | where V : LogRepr { |
0 | 567 | type State = BasicState; |
568 | type Output = V; | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
569 | type Input = V; |
0 | 570 | |
571 | #[inline] | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
572 | fn prestep(&mut self) -> Option<Self::State> { |
0 | 573 | if self.iter >= self.options.max_iter { |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
574 | None |
0 | 575 | } else { |
576 | self.iter += 1; | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
577 | Some(self.state()) |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
578 | } |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
579 | } |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
580 | |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
581 | 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
|
582 | 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
|
583 | if state.verbose && !self.options.quiet { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
584 | println!("{}{}/{} {}{}", "".dimmed(), |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
585 | state.iter, |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
586 | self.options.max_iter, |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
587 | val.logrepr(), |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
588 | "".clear()); |
0 | 589 | } |
590 | } | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
591 | res |
0 | 592 | } |
593 | ||
594 | #[inline] | |
595 | fn iteration(&self) -> usize { | |
596 | self.iter | |
597 | } | |
598 | ||
599 | #[inline] | |
600 | fn state(&self) -> BasicState { | |
601 | let iter = self.iter; | |
602 | 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
|
603 | BasicState { |
0 | 604 | iter : iter, |
605 | verbose : verbose, | |
606 | calc : verbose, | |
31
50a77e4efcbb
Add is_quiet to AlgIteratorState as well.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
607 | quiet : self.options.quiet |
0 | 608 | } |
609 | } | |
610 | } | |
611 | ||
612 | impl AlgIteratorState for BasicState { | |
613 | #[inline] | |
46 | 614 | fn if_verbose<V, E : Error>(self, calc_objective : impl FnOnce() -> V) -> Step<V, Self, E> { |
0 | 615 | if self.calc { |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
616 | Step::Result(calc_objective(), self) |
0 | 617 | } else { |
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
618 | Step::Quiet |
0 | 619 | } |
620 | } | |
621 | ||
622 | #[inline] | |
623 | fn iteration(&self) -> usize { | |
31
50a77e4efcbb
Add is_quiet to AlgIteratorState as well.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
624 | self.iter |
50a77e4efcbb
Add is_quiet to AlgIteratorState as well.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
625 | } |
50a77e4efcbb
Add is_quiet to AlgIteratorState as well.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
626 | |
50a77e4efcbb
Add is_quiet to AlgIteratorState as well.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
627 | #[inline] |
50a77e4efcbb
Add is_quiet to AlgIteratorState as well.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
628 | fn is_quiet(&self) -> bool { |
50a77e4efcbb
Add is_quiet to AlgIteratorState as well.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
629 | self.quiet |
0 | 630 | } |
631 | } | |
632 | ||
633 | // | |
634 | // Stall detecting iteration function. | |
635 | // | |
636 | ||
5 | 637 | /// An [`AlgIteratorFactory`] for an [`AlgIterator`] that detects “stall”. |
638 | /// | |
639 | /// We define stall as $(v_{k+n}-v_k)/v_k ≤ θ$, where $n$ the distance between | |
640 | /// [`Step::Result`] iterations, and $θ$ is the provided `stall` parameter. | |
0 | 641 | #[derive(Clone,Copy,Debug,Serialize,Eq,PartialEq)] |
642 | pub struct StallIteratorFactory<U : Num, BaseFactory> { | |
5 | 643 | /// An [`AlgIteratorFactory`] on which to build on |
0 | 644 | pub base_options : BaseFactory, |
645 | /// Stalling threshold $θ$. | |
646 | pub stall : U, | |
647 | } | |
648 | ||
649 | /// Iterator produced by [`StallIteratorFactory`]. | |
650 | pub struct StallIterator<U : Num, BaseIterator> { | |
651 | base_iterator : BaseIterator, | |
652 | stall : U, | |
653 | previous_value : Option<U>, | |
654 | } | |
655 | ||
656 | impl<V, U, BaseFactory> AlgIteratorFactory<V> | |
657 | for StallIteratorFactory<U, BaseFactory> | |
658 | where BaseFactory : AlgIteratorFactory<V, Output=U>, | |
659 | U : SignedNum + PartialOrd { | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
660 | type Iter = StallIterator<U, BaseFactory::Iter>; |
0 | 661 | type State = BaseFactory::State; |
662 | type Output = BaseFactory::Output; | |
663 | ||
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
664 | fn prepare(self) -> Self::Iter { |
0 | 665 | StallIterator { |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
666 | base_iterator : self.base_options.prepare(), |
0 | 667 | stall : self.stall, |
668 | previous_value : None, | |
669 | } | |
670 | } | |
671 | ||
672 | fn is_quiet(&self) -> bool { | |
673 | self.base_options.is_quiet() | |
674 | } | |
675 | } | |
676 | ||
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
677 | impl<U, BaseIterator> AlgIterator |
0 | 678 | 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
|
679 | where BaseIterator : AlgIterator<Output=U>, |
0 | 680 | U : SignedNum + PartialOrd { |
681 | type State = BaseIterator::State; | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
682 | type Output = U; |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
683 | type Input = BaseIterator::Input; |
0 | 684 | |
685 | #[inline] | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
686 | fn prestep(&mut self) -> Option<Self::State> { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
687 | self.base_iterator.prestep() |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
688 | } |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
689 | |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
690 | #[inline] |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
691 | 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
|
692 | where E : Error { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
693 | match self.base_iterator.poststep(res) { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
694 | Step::Result(nv, state) => { |
0 | 695 | let previous_v = self.previous_value; |
696 | self.previous_value = Some(nv); | |
697 | match previous_v { | |
698 | 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
|
699 | _ => Step::Result(nv, state), |
0 | 700 | } |
701 | }, | |
702 | val => val, | |
703 | } | |
704 | } | |
705 | ||
706 | #[inline] | |
707 | fn iteration(&self) -> usize { | |
708 | self.base_iterator.iteration() | |
709 | } | |
710 | ||
711 | #[inline] | |
712 | fn state(&self) -> Self::State { | |
713 | self.base_iterator.state() | |
714 | } | |
715 | } | |
716 | ||
717 | /// An [`AlgIteratorFactory`] for an [`AlgIterator`] that detect whether step function | |
718 | /// return value is less than `target`, and terminates if it is. | |
719 | #[derive(Clone,Copy,Debug,Serialize,Eq,PartialEq)] | |
720 | pub struct ValueIteratorFactory<U : Num, BaseFactory> { | |
5 | 721 | /// An [`AlgIteratorFactory`] on which to build on |
0 | 722 | pub base_options : BaseFactory, |
5 | 723 | /// Target value |
0 | 724 | pub target : U, |
725 | } | |
726 | ||
727 | /// Iterator produced by [`ValueIteratorFactory`]. | |
728 | pub struct ValueIterator<U : Num, BaseIterator> { | |
729 | base_iterator : BaseIterator, | |
730 | target : U, | |
731 | } | |
732 | ||
733 | impl<V, U, BaseFactory> AlgIteratorFactory<V> | |
734 | for ValueIteratorFactory<U, BaseFactory> | |
735 | where BaseFactory : AlgIteratorFactory<V, Output=U>, | |
736 | U : SignedNum + PartialOrd { | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
737 | type Iter = ValueIterator<U, BaseFactory::Iter>; |
0 | 738 | type State = BaseFactory::State; |
739 | type Output = BaseFactory::Output; | |
740 | ||
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
741 | fn prepare(self) -> Self::Iter { |
0 | 742 | ValueIterator { |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
743 | base_iterator : self.base_options.prepare(), |
0 | 744 | target : self.target |
745 | } | |
746 | } | |
747 | ||
748 | fn is_quiet(&self) -> bool { | |
749 | self.base_options.is_quiet() | |
750 | } | |
751 | } | |
752 | ||
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
753 | impl<U, BaseIterator> AlgIterator |
0 | 754 | 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
|
755 | where BaseIterator : AlgIterator<Output=U>, |
0 | 756 | U : SignedNum + PartialOrd { |
757 | type State = BaseIterator::State; | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
758 | type Output = U; |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
759 | type Input = BaseIterator::Input; |
0 | 760 | |
761 | #[inline] | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
762 | fn prestep(&mut self) -> Option<Self::State> { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
763 | self.base_iterator.prestep() |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
764 | } |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
765 | |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
766 | #[inline] |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
767 | 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
|
768 | match self.base_iterator.poststep(res) { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
769 | Step::Result(v, state) => { |
0 | 770 | if v <= self.target { |
771 | Step::Terminated | |
772 | } else { | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
773 | Step::Result(v, state) |
0 | 774 | } |
775 | }, | |
776 | val => val, | |
777 | } | |
778 | } | |
779 | ||
780 | #[inline] | |
781 | fn iteration(&self) -> usize { | |
782 | self.base_iterator.iteration() | |
783 | } | |
784 | ||
785 | #[inline] | |
786 | fn state(&self) -> Self::State { | |
787 | self.base_iterator.state() | |
788 | } | |
789 | } | |
790 | ||
791 | // | |
792 | // Logging iterator | |
793 | // | |
794 | ||
795 | /// [`AlgIteratorFactory`] for a logging [`AlgIterator`]. | |
5 | 796 | /// |
797 | /// Typically produced with [`AlgIteratorFactory::into_log`]. | |
0 | 798 | /// The `Output` of the corresponding [`LoggingIterator`] is `()`: |
799 | #[derive(Debug)] | |
800 | pub struct LoggingIteratorFactory<'log, U, BaseFactory> { | |
5 | 801 | /// Base [`AlgIteratorFactory`] on which to build |
802 | base_options : BaseFactory, | |
803 | /// The `Logger` to use. | |
804 | logger : &'log mut Logger<U>, | |
0 | 805 | } |
806 | ||
807 | /// Iterator produced by `LoggingIteratorFactory`. | |
808 | pub struct LoggingIterator<'log, U, BaseIterator> { | |
809 | base_iterator : BaseIterator, | |
810 | logger : &'log mut Logger<U>, | |
811 | } | |
812 | ||
813 | ||
814 | impl<'log, V, BaseFactory> AlgIteratorFactory<V> | |
815 | for LoggingIteratorFactory<'log, BaseFactory::Output, BaseFactory> | |
816 | where BaseFactory : AlgIteratorFactory<V>, | |
817 | BaseFactory::Output : 'log { | |
818 | type State = BaseFactory::State; | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
819 | type Iter = LoggingIterator<'log, BaseFactory::Output, BaseFactory::Iter>; |
0 | 820 | type Output = (); |
821 | ||
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
822 | fn prepare(self) -> Self::Iter { |
0 | 823 | LoggingIterator { |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
824 | base_iterator : self.base_options.prepare(), |
0 | 825 | logger : self.logger, |
826 | } | |
827 | } | |
828 | ||
829 | #[inline] | |
830 | fn is_quiet(&self) -> bool { | |
831 | self.base_options.is_quiet() | |
832 | } | |
833 | } | |
834 | ||
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
835 | impl<'log, BaseIterator> AlgIterator |
0 | 836 | 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
|
837 | where BaseIterator : AlgIterator, |
0 | 838 | BaseIterator::Output : 'log { |
839 | type State = BaseIterator::State; | |
840 | type Output = (); | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
841 | type Input = BaseIterator::Input; |
0 | 842 | |
843 | #[inline] | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
844 | fn prestep(&mut self) -> Option<Self::State> { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
845 | self.base_iterator.prestep() |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
846 | } |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
847 | |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
848 | #[inline] |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
849 | 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
|
850 | match self.base_iterator.poststep(res) { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
851 | Step::Result(v, _) => { |
0 | 852 | self.logger.log(v); |
853 | Step::Quiet | |
854 | }, | |
855 | Step::Quiet => Step::Quiet, | |
856 | 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
|
857 | Step::Failure(e) => Step::Failure(e), |
0 | 858 | } |
859 | } | |
860 | ||
861 | #[inline] | |
862 | fn iteration(&self) -> usize { | |
863 | self.base_iterator.iteration() | |
864 | } | |
865 | ||
866 | #[inline] | |
867 | fn state(&self) -> Self::State { | |
868 | self.base_iterator.state() | |
869 | } | |
870 | } | |
871 | ||
872 | /// This [`AlgIteratorFactory`] allows output mapping. | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
873 | /// |
5 | 874 | /// Typically produced with [`AlgIteratorFactory::mapped`]. |
0 | 875 | #[derive(Debug)] |
876 | pub struct MappingIteratorFactory<G, BaseFactory> { | |
5 | 877 | /// Base [`AlgIteratorFactory`] on which to build |
878 | base_options : BaseFactory, | |
879 | /// A closure `G : Fn(usize, BaseFactory::Output) -> U` that gets the current iteration | |
880 | /// and the output of the base factory as input, and produces a new output. | |
881 | map : G, | |
0 | 882 | } |
883 | ||
5 | 884 | /// [`AlgIterator`] produced by [`MappingIteratorFactory`]. |
0 | 885 | pub struct MappingIterator<G, BaseIterator> { |
886 | base_iterator : BaseIterator, | |
887 | map : G, | |
888 | } | |
889 | ||
890 | ||
891 | impl<V, U, G, BaseFactory> AlgIteratorFactory<V> | |
892 | for MappingIteratorFactory<G, BaseFactory> | |
893 | where BaseFactory : AlgIteratorFactory<V>, | |
894 | G : Fn(usize, BaseFactory::Output) -> U { | |
895 | type State = BaseFactory::State; | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
896 | type Iter = MappingIterator<G, BaseFactory::Iter>; |
0 | 897 | type Output = U; |
898 | ||
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
899 | fn prepare(self) -> Self::Iter { |
0 | 900 | MappingIterator { |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
901 | base_iterator : self.base_options.prepare(), |
0 | 902 | map : self.map |
903 | } | |
904 | } | |
905 | ||
906 | #[inline] | |
907 | fn is_quiet(&self) -> bool { | |
908 | self.base_options.is_quiet() | |
909 | } | |
910 | } | |
911 | ||
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
912 | impl<U, G, BaseIterator> AlgIterator |
0 | 913 | 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
|
914 | where BaseIterator : AlgIterator, |
0 | 915 | G : Fn(usize, BaseIterator::Output) -> U { |
916 | type State = BaseIterator::State; | |
917 | type Output = U; | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
918 | type Input = BaseIterator::Input; |
0 | 919 | |
920 | #[inline] | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
921 | fn prestep(&mut self) -> Option<Self::State> { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
922 | self.base_iterator.prestep() |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
923 | } |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
924 | |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
925 | #[inline] |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
926 | 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
|
927 | match self.base_iterator.poststep(res) { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
928 | Step::Result(v, state) => Step::Result((self.map)(self.iteration(), v), state), |
0 | 929 | Step::Quiet => Step::Quiet, |
930 | 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
|
931 | Step::Failure(e) => Step::Failure(e), |
0 | 932 | } |
933 | } | |
934 | ||
935 | #[inline] | |
936 | fn iteration(&self) -> usize { | |
937 | self.base_iterator.iteration() | |
938 | } | |
939 | ||
940 | #[inline] | |
941 | fn state(&self) -> Self::State { | |
942 | self.base_iterator.state() | |
943 | } | |
944 | } | |
945 | ||
946 | // | |
947 | // Timing iterator | |
948 | // | |
949 | ||
5 | 950 | /// An [`AlgIteratorFactory`] for an [`AlgIterator`] that adds spent CPU time to verbose events. |
0 | 951 | #[derive(Debug)] |
952 | pub struct TimingIteratorFactory<BaseFactory>(pub BaseFactory); | |
953 | ||
954 | /// Iterator produced by [`TimingIteratorFactory`] | |
955 | #[derive(Debug)] | |
956 | pub struct TimingIterator<BaseIterator> { | |
957 | base_iterator : BaseIterator, | |
958 | start_time : ProcessTime, | |
959 | } | |
960 | ||
961 | /// Data `U` with production time attached | |
962 | #[derive(Copy, Clone, Debug, Serialize)] | |
963 | pub struct Timed<U> { | |
51 | 964 | /// CPU time taken |
0 | 965 | pub cpu_time : Duration, |
51 | 966 | /// Iteration number |
967 | pub iter : usize, | |
968 | /// User data | |
0 | 969 | //#[serde(flatten)] |
970 | pub data : U | |
971 | } | |
972 | ||
973 | impl<T> LogRepr for Timed<T> where T : LogRepr { | |
974 | fn logrepr(&self) -> ColoredString { | |
975 | format!("[{:.3}s] {}", self.cpu_time.as_secs_f64(), self.data.logrepr()).as_str().into() | |
976 | } | |
977 | } | |
978 | ||
979 | ||
980 | impl<V, BaseFactory> AlgIteratorFactory<V> | |
981 | for TimingIteratorFactory<BaseFactory> | |
982 | where BaseFactory : AlgIteratorFactory<V> { | |
983 | type State = BaseFactory::State; | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
984 | type Iter = TimingIterator<BaseFactory::Iter>; |
0 | 985 | type Output = Timed<BaseFactory::Output>; |
986 | ||
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
987 | fn prepare(self) -> Self::Iter { |
0 | 988 | TimingIterator { |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
989 | base_iterator : self.0.prepare(), |
0 | 990 | start_time : ProcessTime::now() |
991 | } | |
992 | } | |
993 | ||
994 | #[inline] | |
995 | fn is_quiet(&self) -> bool { | |
996 | self.0.is_quiet() | |
997 | } | |
998 | } | |
999 | ||
3
20db884b7028
Allow step closure of AlgIterators to indicate succesfull termination or failure.
Tuomo Valkonen <tuomov@iki.fi>
parents:
1
diff
changeset
|
1000 | impl<BaseIterator> AlgIterator |
0 | 1001 | 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
|
1002 | where BaseIterator : AlgIterator { |
0 | 1003 | type State = BaseIterator::State; |
1004 | type Output = Timed<BaseIterator::Output>; | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1005 | type Input = BaseIterator::Input; |
0 | 1006 | |
1007 | #[inline] | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1008 | fn prestep(&mut self) -> Option<Self::State> { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1009 | self.base_iterator.prestep() |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1010 | } |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1011 | |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1012 | #[inline] |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1013 | 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
|
1014 | match self.base_iterator.poststep(res) { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1015 | Step::Result(data, state) => { |
0 | 1016 | Step::Result(Timed{ |
1017 | cpu_time : self.start_time.elapsed(), | |
51 | 1018 | iter : self.iteration(), |
0 | 1019 | data |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1020 | }, state) |
0 | 1021 | }, |
1022 | Step::Quiet => Step::Quiet, | |
1023 | 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
|
1024 | Step::Failure(e) => Step::Failure(e), |
0 | 1025 | } |
1026 | } | |
1027 | ||
1028 | #[inline] | |
1029 | fn iteration(&self) -> usize { | |
1030 | self.base_iterator.iteration() | |
1031 | } | |
1032 | ||
1033 | #[inline] | |
1034 | fn state(&self) -> Self::State { | |
1035 | self.base_iterator.state() | |
1036 | } | |
1037 | } | |
1038 | ||
1039 | // | |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1040 | // New for-loop interface |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1041 | // |
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 | pub struct AlgIteratorIterator<I : AlgIterator> { |
41
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1044 | algi : Rc<RefCell<I>>, |
40
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 | |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1047 | pub struct AlgIteratorIteration<I : AlgIterator> { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1048 | state : I::State, |
41
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1049 | algi : Rc<RefCell<I>>, |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1050 | } |
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 | 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
|
1053 | type Item = AlgIteratorIteration<I>; |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1054 | |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1055 | fn next(&mut self) -> Option<Self::Item> { |
41
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1056 | let algi = self.algi.clone(); |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1057 | 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
|
1058 | state, |
41
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1059 | algi, |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1060 | }) |
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 | } |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1063 | |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1064 | /// Types of errors that may occur |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1065 | #[derive(Debug,PartialEq,Eq)] |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1066 | pub enum IterationError { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1067 | /// [`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
|
1068 | ReportingOrderingError |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1069 | } |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1070 | |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1071 | impl<I : AlgIterator> AlgIteratorIteration<I> { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1072 | /// 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
|
1073 | /// |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1074 | /// 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
|
1075 | /// |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1076 | /// 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
|
1077 | /// 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
|
1078 | /// [module documentation][self]. |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1079 | /// |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1080 | /// 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
|
1081 | /// 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
|
1082 | /// [`Self::if_verbose_check`]. |
46 | 1083 | pub fn if_verbose(self, calc_objective : impl FnOnce() -> I::Input) { |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1084 | self.if_verbose_check(calc_objective).unwrap() |
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 | |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1087 | /// Version of [`Self::if_verbose`] that propagates errors instead of panicking. |
46 | 1088 | pub fn if_verbose_check(self, calc_objective : impl FnOnce() -> I::Input) |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1089 | -> Result<(), IterationError> { |
41
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1090 | 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
|
1091 | Err(_) => return Err(IterationError::ReportingOrderingError), |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1092 | Ok(algi) => algi |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1093 | }; |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1094 | if self.state.iteration() != algi.iteration() { |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1095 | Err(IterationError::ReportingOrderingError) |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1096 | } else { |
41
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1097 | 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
|
1098 | = self.state.if_verbose(calc_objective); |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1099 | algi.poststep(res); |
40
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1100 | Ok(()) |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1101 | } |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1102 | } |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1103 | |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1104 | /// Returns the current iteration count. |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1105 | pub fn iteration(&self) -> usize { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1106 | self.state.iteration() |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1107 | } |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1108 | |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1109 | /// Indicates whether the iterator is quiet |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1110 | pub fn is_quiet(&self) -> bool { |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1111 | self.state.is_quiet() |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1112 | } |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1113 | } |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1114 | |
daf0e3a70c79
New iteration interface, allowing for loops.
Tuomo Valkonen <tuomov@iki.fi>
parents:
38
diff
changeset
|
1115 | // |
0 | 1116 | // Tests |
1117 | // | |
1118 | ||
1119 | #[cfg(test)] | |
1120 | mod tests { | |
1121 | use super::*; | |
1122 | use crate::logger::Logger; | |
1123 | #[test] | |
1124 | fn iteration() { | |
1125 | let options = AlgIteratorOptions{ | |
1126 | max_iter : 10, | |
1127 | verbose_iter : Verbose::Every(3), | |
1128 | .. Default::default() | |
1129 | }; | |
1130 | ||
1131 | { | |
1132 | let mut start = 1 as int; | |
1133 | options.iterate(|state| { | |
1134 | start = start * 2; | |
1135 | state.if_verbose(|| start) | |
1136 | }); | |
1137 | assert_eq!(start, (2 as int).pow(10)); | |
1138 | } | |
1139 | ||
1140 | { | |
1141 | let mut start = 1 as int; | |
1142 | let mut log = Logger::new(); | |
1143 | let factory = options.instantiate() | |
1144 | .with_iteration_number() | |
1145 | .into_log(&mut log); | |
1146 | factory.iterate(|state| { | |
1147 | start = start * 2; | |
1148 | state.if_verbose(|| start) | |
1149 | }); | |
1150 | assert_eq!(start, (2 as int).pow(10)); | |
1151 | assert_eq!(log.data() | |
1152 | .iter() | |
1153 | .map(|LogItem{ data : v, iter : _ }| v.clone()) | |
1154 | .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
|
1155 | (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
|
1156 | .skip(2) |
df3901ec2f5d
Fix some unit tests after fundamental changes that made them invalid
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
1157 | .step_by(3) |
0 | 1158 | .collect::<Vec<int>>()) |
1159 | } | |
1160 | } | |
41
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 | #[test] |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1163 | fn iteration_for_loop() { |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1164 | let options = AlgIteratorOptions{ |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1165 | max_iter : 10, |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1166 | verbose_iter : Verbose::Every(3), |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1167 | .. Default::default() |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1168 | }; |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1169 | |
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 | let mut start = 1 as int; |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1172 | for state in options.iter() { |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1173 | start = start * 2; |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1174 | state.if_verbose(|| start) |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1175 | } |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1176 | assert_eq!(start, (2 as int).pow(10)); |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1177 | } |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1178 | |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1179 | { |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1180 | let mut start = 1 as int; |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1181 | let mut log = Logger::new(); |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1182 | let factory = options.instantiate() |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1183 | .with_iteration_number() |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1184 | .into_log(&mut log); |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1185 | for state in factory.iter() { |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1186 | start = start * 2; |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1187 | state.if_verbose(|| start) |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1188 | } |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1189 | assert_eq!(start, (2 as int).pow(10)); |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1190 | assert_eq!(log.data() |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1191 | .iter() |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1192 | .map(|LogItem{ data : v, iter : _ }| v.clone()) |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1193 | .collect::<Vec<int>>(), |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1194 | (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
|
1195 | .skip(2) |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1196 | .step_by(3) |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1197 | .collect::<Vec<int>>()) |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1198 | } |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1199 | } |
121cf065e9ed
Simplify iterate facility for-loop mechanism
Tuomo Valkonen <tuomov@iki.fi>
parents:
40
diff
changeset
|
1200 | |
0 | 1201 | } |