Mon, 06 Jan 2025 20:29:25 -0500
More Serialize / Deserialize / Debug derives
0 | 1 | |
2 | use numeric_literals::replace_float_literals; | |
86
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
3 | use serde::{Serialize, Deserialize}; |
0 | 4 | use std::iter::Sum; |
5 | use std::marker::PhantomData; | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
6 | use std::sync::Arc; |
0 | 7 | use crate::types::Float; |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
8 | use crate::mapping::{ |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
9 | Instance, Mapping, DifferentiableImpl, DifferentiableMapping, Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
10 | BasicDecomposition, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
11 | }; |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
12 | //use crate::linops::{Apply, Linear}; |
0 | 13 | use crate::sets::Set; |
5 | 14 | use crate::sets::Cube; |
15 | use crate::loc::Loc; | |
0 | 16 | use super::support::*; |
17 | use super::bt::*; | |
18 | use super::refine::*; | |
19 | use super::aggregator::*; | |
20 | use super::either::*; | |
21 | use crate::fe_model::base::RealLocalModel; | |
22 | use crate::fe_model::p2_local_model::*; | |
23 | ||
5 | 24 | /// Presentation for (mathematical) functions constructed as a sum of components functions with |
25 | /// typically small support. | |
26 | /// | |
27 | /// The domain of the function is [`Loc`]`<F, N>`, where `F` is the type of floating point numbers, | |
28 | /// and `N` the dimension. | |
29 | /// | |
30 | /// The `generator` lists the component functions that have to implement [`Support`]. | |
31 | /// Identifiers of the components ([`SupportGenerator::Id`], usually `usize`) are stored stored | |
32 | /// in a [bisection tree][BTImpl], when one is provided as `bt`. However `bt` may also be `()` | |
33 | /// for a [`PreBTFN`] that is only useful for vector space operations with a full [`BTFN`]. | |
86
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
34 | #[derive(Clone,Debug,Serialize,Deserialize)] |
0 | 35 | pub struct BTFN< |
36 | F : Float, | |
37 | G : SupportGenerator<F, N>, | |
38 | BT /*: BTImpl<F, N>*/, | |
39 | const N : usize | |
40 | > /*where G::SupportType : LocalAnalysis<F, A, N>*/ { | |
41 | bt : BT, | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
42 | generator : Arc<G>, |
86
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
43 | #[serde(skip)] |
0 | 44 | _phantoms : PhantomData<F>, |
45 | } | |
46 | ||
47 | impl<F : Float, G, BT, const N : usize> | |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
48 | Space for BTFN<F, G, BT, N> |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
49 | where |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
50 | G : SupportGenerator<F, N, Id=BT::Data>, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
51 | G::SupportType : LocalAnalysis<F, BT::Agg, N>, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
52 | BT : BTImpl<F, N> |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
53 | { |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
54 | type Decomp = BasicDecomposition; |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
55 | } |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
56 | |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
57 | impl<F : Float, G, BT, const N : usize> |
0 | 58 | BTFN<F, G, BT, N> |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
59 | where |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
60 | G : SupportGenerator<F, N, Id=BT::Data>, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
61 | G::SupportType : LocalAnalysis<F, BT::Agg, N>, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
62 | BT : BTImpl<F, N> |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
63 | { |
0 | 64 | |
5 | 65 | /// Create a new BTFN from a support generator and a pre-initialised bisection tree. |
66 | /// | |
67 | /// The bisection tree `bt` should be pre-initialised to correspond to the `generator`. | |
68 | /// Use [`Self::construct`] if no preinitialised tree is available. Use [`Self::new_refresh`] | |
69 | /// when the aggregators of the tree may need updates. | |
70 | /// | |
71 | /// See the documentation for [`BTFN`] on the role of the `generator`. | |
0 | 72 | pub fn new(bt : BT, generator : G) -> Self { |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
73 | Self::new_arc(bt, Arc::new(generator)) |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
74 | } |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
75 | |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
76 | fn new_arc(bt : BT, generator : Arc<G>) -> Self { |
0 | 77 | BTFN { |
78 | bt : bt, | |
79 | generator : generator, | |
80 | _phantoms : std::marker::PhantomData, | |
81 | } | |
82 | } | |
83 | ||
5 | 84 | /// Create a new BTFN support generator and a pre-initialised bisection tree, |
85 | /// cloning the tree and refreshing aggregators. | |
86 | /// | |
87 | /// The bisection tree `bt` should be pre-initialised to correspond to the `generator`, but | |
88 | /// the aggregator may be out of date. | |
89 | /// | |
90 | /// See the documentation for [`BTFN`] on the role of the `generator`. | |
0 | 91 | pub fn new_refresh(bt : &BT, generator : G) -> Self { |
92 | // clone().refresh_aggregator(…) as opposed to convert_aggregator | |
93 | // ensures that type is maintained. Due to Rc-pointer copy-on-write, | |
94 | // the effort is not significantly different. | |
95 | let mut btnew = bt.clone(); | |
96 | btnew.refresh_aggregator(&generator); | |
97 | BTFN::new(btnew, generator) | |
98 | } | |
99 | ||
5 | 100 | /// Create a new BTFN from a support generator, domain, and depth for a new [`BT`]. |
101 | /// | |
102 | /// The top node of the created [`BT`] will have the given `domain`. | |
103 | /// | |
104 | /// See the documentation for [`BTFN`] on the role of the `generator`. | |
0 | 105 | pub fn construct(domain : Cube<F, N>, depth : BT::Depth, generator : G) -> Self { |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
106 | Self::construct_arc(domain, depth, Arc::new(generator)) |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
107 | } |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
108 | |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
109 | fn construct_arc(domain : Cube<F, N>, depth : BT::Depth, generator : Arc<G>) -> Self { |
0 | 110 | let mut bt = BT::new(domain, depth); |
111 | for (d, support) in generator.all_data() { | |
112 | bt.insert(d, &support); | |
113 | } | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
114 | Self::new_arc(bt, generator) |
0 | 115 | } |
116 | ||
5 | 117 | /// Convert the aggregator of the [`BTFN`] to a different one. |
118 | /// | |
119 | /// This will construct a [`BTFN`] with the same components and generator as the (consumed) | |
120 | /// `self`, but a new `BT` with [`Aggregator`]s of type `ANew`. | |
0 | 121 | pub fn convert_aggregator<ANew>(self) -> BTFN<F, G, BT::Converted<ANew>, N> |
122 | where ANew : Aggregator, | |
123 | G : SupportGenerator<F, N, Id=BT::Data>, | |
124 | G::SupportType : LocalAnalysis<F, ANew, N> { | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
125 | BTFN::new_arc(self.bt.convert_aggregator(&*self.generator), self.generator) |
0 | 126 | } |
127 | ||
128 | /// Change the generator (after, e.g., a scaling of the latter). | |
5 | 129 | fn new_generator(&self, generator : G) -> Self { |
0 | 130 | BTFN::new_refresh(&self.bt, generator) |
131 | } | |
132 | ||
133 | /// Refresh aggregator after updates to generator | |
134 | fn refresh_aggregator(&mut self) { | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
135 | self.bt.refresh_aggregator(&*self.generator); |
0 | 136 | } |
137 | ||
138 | } | |
139 | ||
5 | 140 | impl<F : Float, G, BT, const N : usize> |
141 | BTFN<F, G, BT, N> | |
142 | where G : SupportGenerator<F, N> { | |
143 | /// Change the [bisection tree][BTImpl] of the [`BTFN`] to a different one. | |
144 | /// | |
145 | /// This can be used to convert a [`PreBTFN`] to a full [`BTFN`], or the change | |
146 | /// the aggreagator; see also [`self.convert_aggregator`]. | |
147 | pub fn instantiate< | |
148 | BTNew : BTImpl<F, N, Data=G::Id>, | |
149 | > (self, domain : Cube<F, N>, depth : BTNew::Depth) -> BTFN<F, G, BTNew, N> | |
150 | where G::SupportType : LocalAnalysis<F, BTNew::Agg, N> { | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
151 | BTFN::construct_arc(domain, depth, self.generator) |
5 | 152 | } |
153 | } | |
154 | ||
155 | /// A BTFN with no bisection tree. | |
156 | /// | |
157 | /// Most BTFN methods are not available, but if a BTFN is going to be summed with another | |
158 | /// before other use, it will be more efficient to not construct an unnecessary bisection tree | |
159 | /// that would be shortly dropped. | |
0 | 160 | pub type PreBTFN<F, G, const N : usize> = BTFN<F, G, (), N>; |
161 | ||
162 | impl<F : Float, G, const N : usize> PreBTFN<F, G, N> where G : SupportGenerator<F, N> { | |
163 | ||
5 | 164 | /// Create a new [`PreBTFN`] with no bisection tree. |
0 | 165 | pub fn new_pre(generator : G) -> Self { |
166 | BTFN { | |
167 | bt : (), | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
168 | generator : Arc::new(generator), |
0 | 169 | _phantoms : std::marker::PhantomData, |
170 | } | |
171 | } | |
172 | } | |
173 | ||
174 | impl<F : Float, G, BT, const N : usize> | |
175 | BTFN<F, G, BT, N> | |
176 | where G : SupportGenerator<F, N, Id=usize>, | |
177 | G::SupportType : LocalAnalysis<F, BT::Agg, N>, | |
178 | BT : BTImpl<F, N, Data=usize> { | |
179 | ||
180 | /// Helper function for implementing [`std::ops::Add`]. | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
181 | fn add_another<G2>(&self, g2 : Arc<G2>) -> BTFN<F, BothGenerators<G, G2>, BT, N> |
0 | 182 | where G2 : SupportGenerator<F, N, Id=usize>, |
183 | G2::SupportType : LocalAnalysis<F, BT::Agg, N> { | |
184 | ||
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
185 | let mut bt = self.bt.clone(); |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
186 | let both = BothGenerators(Arc::clone(&self.generator), g2); |
0 | 187 | |
188 | for (d, support) in both.all_right_data() { | |
189 | bt.insert(d, &support); | |
190 | } | |
191 | ||
192 | BTFN { | |
193 | bt : bt, | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
194 | generator : Arc::new(both), |
0 | 195 | _phantoms : std::marker::PhantomData, |
196 | } | |
197 | } | |
198 | } | |
199 | ||
200 | macro_rules! make_btfn_add { | |
201 | ($lhs:ty, $preprocess:path, $($extra_trait:ident)?) => { | |
202 | impl<'a, F : Float, G1, G2, BT1, BT2, const N : usize> | |
203 | std::ops::Add<BTFN<F, G2, BT2, N>> for | |
204 | $lhs | |
205 | where BT1 : BTImpl<F, N, Data=usize>, | |
206 | G1 : SupportGenerator<F, N, Id=usize> + $($extra_trait)?, | |
207 | G2 : SupportGenerator<F, N, Id=usize>, | |
208 | G1::SupportType : LocalAnalysis<F, BT1::Agg, N>, | |
209 | G2::SupportType : LocalAnalysis<F, BT1::Agg, N> { | |
210 | type Output = BTFN<F, BothGenerators<G1, G2>, BT1, N>; | |
211 | #[inline] | |
212 | fn add(self, other : BTFN<F, G2, BT2, N>) -> Self::Output { | |
213 | $preprocess(self).add_another(other.generator) | |
214 | } | |
215 | } | |
216 | ||
217 | impl<'a, 'b, F : Float, G1, G2, BT1, BT2, const N : usize> | |
218 | std::ops::Add<&'b BTFN<F, G2, BT2, N>> for | |
219 | $lhs | |
220 | where BT1 : BTImpl<F, N, Data=usize>, | |
221 | G1 : SupportGenerator<F, N, Id=usize> + $($extra_trait)?, | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
222 | G2 : SupportGenerator<F, N, Id=usize>, |
0 | 223 | G1::SupportType : LocalAnalysis<F, BT1::Agg, N>, |
224 | G2::SupportType : LocalAnalysis<F, BT1::Agg, N> { | |
225 | ||
226 | type Output = BTFN<F, BothGenerators<G1, G2>, BT1, N>; | |
227 | #[inline] | |
228 | fn add(self, other : &'b BTFN<F, G2, BT2, N>) -> Self::Output { | |
229 | $preprocess(self).add_another(other.generator.clone()) | |
230 | } | |
231 | } | |
232 | } | |
233 | } | |
234 | ||
235 | make_btfn_add!(BTFN<F, G1, BT1, N>, std::convert::identity, ); | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
236 | make_btfn_add!(&'a BTFN<F, G1, BT1, N>, Clone::clone, ); |
0 | 237 | |
238 | macro_rules! make_btfn_sub { | |
239 | ($lhs:ty, $preprocess:path, $($extra_trait:ident)?) => { | |
240 | impl<'a, F : Float, G1, G2, BT1, BT2, const N : usize> | |
241 | std::ops::Sub<BTFN<F, G2, BT2, N>> for | |
242 | $lhs | |
243 | where BT1 : BTImpl<F, N, Data=usize>, | |
244 | G1 : SupportGenerator<F, N, Id=usize> + $($extra_trait)?, | |
245 | G2 : SupportGenerator<F, N, Id=usize>, | |
246 | G1::SupportType : LocalAnalysis<F, BT1::Agg, N>, | |
247 | G2::SupportType : LocalAnalysis<F, BT1::Agg, N> { | |
248 | type Output = BTFN<F, BothGenerators<G1, G2>, BT1, N>; | |
249 | #[inline] | |
250 | fn sub(self, other : BTFN<F, G2, BT2, N>) -> Self::Output { | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
251 | $preprocess(self).add_another(Arc::new( |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
252 | Arc::try_unwrap(other.generator) |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
253 | .unwrap_or_else(|arc| (*arc).clone()) |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
254 | .neg() |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
255 | )) |
0 | 256 | } |
257 | } | |
258 | ||
259 | impl<'a, 'b, F : Float, G1, G2, BT1, BT2, const N : usize> | |
260 | std::ops::Sub<&'b BTFN<F, G2, BT2, N>> for | |
261 | $lhs | |
262 | where BT1 : BTImpl<F, N, Data=usize>, | |
263 | G1 : SupportGenerator<F, N, Id=usize> + $($extra_trait)?, | |
264 | G2 : SupportGenerator<F, N, Id=usize> + Clone, | |
265 | G1::SupportType : LocalAnalysis<F, BT1::Agg, N>, | |
266 | G2::SupportType : LocalAnalysis<F, BT1::Agg, N>, | |
5 | 267 | &'b G2 : std::ops::Neg<Output=G2> { |
0 | 268 | |
269 | type Output = BTFN<F, BothGenerators<G1, G2>, BT1, N>; | |
270 | #[inline] | |
271 | fn sub(self, other : &'b BTFN<F, G2, BT2, N>) -> Self::Output { | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
272 | $preprocess(self).add_another(Arc::new((*other.generator).clone().neg())) |
0 | 273 | } |
274 | } | |
275 | } | |
276 | } | |
277 | ||
278 | make_btfn_sub!(BTFN<F, G1, BT1, N>, std::convert::identity, ); | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
279 | make_btfn_sub!(&'a BTFN<F, G1, BT1, N>, std::convert::identity, ); |
0 | 280 | |
281 | macro_rules! make_btfn_scalarop_rhs { | |
282 | ($trait:ident, $fn:ident, $trait_assign:ident, $fn_assign:ident) => { | |
283 | impl<F : Float, G, BT, const N : usize> | |
284 | std::ops::$trait_assign<F> | |
285 | for BTFN<F, G, BT, N> | |
286 | where BT : BTImpl<F, N>, | |
287 | G : SupportGenerator<F, N, Id=BT::Data>, | |
288 | G::SupportType : LocalAnalysis<F, BT::Agg, N> { | |
289 | #[inline] | |
290 | fn $fn_assign(&mut self, t : F) { | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
291 | Arc::make_mut(&mut self.generator).$fn_assign(t); |
0 | 292 | self.refresh_aggregator(); |
293 | } | |
294 | } | |
295 | ||
296 | impl<F : Float, G, BT, const N : usize> | |
297 | std::ops::$trait<F> | |
298 | for BTFN<F, G, BT, N> | |
299 | where BT : BTImpl<F, N>, | |
300 | G : SupportGenerator<F, N, Id=BT::Data>, | |
301 | G::SupportType : LocalAnalysis<F, BT::Agg, N> { | |
302 | type Output = Self; | |
303 | #[inline] | |
304 | fn $fn(mut self, t : F) -> Self::Output { | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
305 | Arc::make_mut(&mut self.generator).$fn_assign(t); |
0 | 306 | self.refresh_aggregator(); |
307 | self | |
308 | } | |
309 | } | |
310 | ||
311 | impl<'a, F : Float, G, BT, const N : usize> | |
312 | std::ops::$trait<F> | |
313 | for &'a BTFN<F, G, BT, N> | |
314 | where BT : BTImpl<F, N>, | |
315 | G : SupportGenerator<F, N, Id=BT::Data>, | |
316 | G::SupportType : LocalAnalysis<F, BT::Agg, N>, | |
317 | &'a G : std::ops::$trait<F,Output=G> { | |
318 | type Output = BTFN<F, G, BT, N>; | |
319 | #[inline] | |
320 | fn $fn(self, t : F) -> Self::Output { | |
321 | self.new_generator(self.generator.$fn(t)) | |
322 | } | |
323 | } | |
324 | } | |
325 | } | |
326 | ||
327 | make_btfn_scalarop_rhs!(Mul, mul, MulAssign, mul_assign); | |
328 | make_btfn_scalarop_rhs!(Div, div, DivAssign, div_assign); | |
329 | ||
330 | macro_rules! make_btfn_scalarop_lhs { | |
331 | ($trait:ident, $fn:ident, $fn_assign:ident, $($f:ident)+) => { $( | |
332 | impl<G, BT, const N : usize> | |
333 | std::ops::$trait<BTFN<$f, G, BT, N>> | |
334 | for $f | |
335 | where BT : BTImpl<$f, N>, | |
336 | G : SupportGenerator<$f, N, Id=BT::Data>, | |
337 | G::SupportType : LocalAnalysis<$f, BT::Agg, N> { | |
338 | type Output = BTFN<$f, G, BT, N>; | |
339 | #[inline] | |
340 | fn $fn(self, mut a : BTFN<$f, G, BT, N>) -> Self::Output { | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
341 | Arc::make_mut(&mut a.generator).$fn_assign(self); |
0 | 342 | a.refresh_aggregator(); |
343 | a | |
344 | } | |
345 | } | |
346 | ||
347 | impl<'a, G, BT, const N : usize> | |
348 | std::ops::$trait<&'a BTFN<$f, G, BT, N>> | |
349 | for $f | |
350 | where BT : BTImpl<$f, N>, | |
351 | G : SupportGenerator<$f, N, Id=BT::Data> + Clone, | |
352 | G::SupportType : LocalAnalysis<$f, BT::Agg, N>, | |
353 | // FIXME: This causes compiler overflow | |
354 | /*&'a G : std::ops::$trait<$f,Output=G>*/ { | |
355 | type Output = BTFN<$f, G, BT, N>; | |
356 | #[inline] | |
357 | fn $fn(self, a : &'a BTFN<$f, G, BT, N>) -> Self::Output { | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
358 | let mut tmp = (*a.generator).clone(); |
0 | 359 | tmp.$fn_assign(self); |
360 | a.new_generator(tmp) | |
361 | // FIXME: Prevented by the compiler overflow above. | |
362 | //a.new_generator(a.generator.$fn(a)) | |
363 | } | |
364 | } | |
365 | )+ } | |
366 | } | |
367 | ||
368 | make_btfn_scalarop_lhs!(Mul, mul, mul_assign, f32 f64); | |
369 | make_btfn_scalarop_lhs!(Div, div, div_assign, f32 f64); | |
370 | ||
371 | macro_rules! make_btfn_unaryop { | |
372 | ($trait:ident, $fn:ident) => { | |
373 | impl<F : Float, G, BT, const N : usize> | |
374 | std::ops::$trait | |
375 | for BTFN<F, G, BT, N> | |
376 | where BT : BTImpl<F, N>, | |
377 | G : SupportGenerator<F, N, Id=BT::Data>, | |
378 | G::SupportType : LocalAnalysis<F, BT::Agg, N> { | |
379 | type Output = Self; | |
380 | #[inline] | |
381 | fn $fn(mut self) -> Self::Output { | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
382 | self.generator = Arc::new(Arc::unwrap_or_clone(self.generator).$fn()); |
0 | 383 | self.refresh_aggregator(); |
384 | self | |
385 | } | |
386 | } | |
387 | ||
388 | /*impl<'a, F : Float, G, BT, const N : usize> | |
389 | std::ops::$trait | |
390 | for &'a BTFN<F, G, BT, N> | |
391 | where BT : BTImpl<F, N>, | |
392 | G : SupportGenerator<F, N, Id=BT::Data>, | |
393 | G::SupportType : LocalAnalysis<F, BT::Agg, N>, | |
394 | &'a G : std::ops::$trait<Output=G> { | |
395 | type Output = BTFN<F, G, BT, N>; | |
396 | #[inline] | |
397 | fn $fn(self) -> Self::Output { | |
398 | self.new_generator(std::ops::$trait::$fn(&self.generator)) | |
399 | } | |
400 | }*/ | |
401 | } | |
402 | } | |
403 | ||
404 | make_btfn_unaryop!(Neg, neg); | |
405 | ||
406 | // | |
27
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
407 | // Apply, Mapping, Differentiate |
0 | 408 | // |
409 | ||
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
410 | impl<F : Float, G, BT, V, const N : usize> Mapping<Loc<F, N>> |
0 | 411 | for BTFN<F, G, BT, N> |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
412 | where |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
413 | BT : BTImpl<F, N>, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
414 | G : SupportGenerator<F, N, Id=BT::Data>, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
415 | G::SupportType : LocalAnalysis<F, BT::Agg, N> + Mapping<Loc<F, N>, Codomain = V>, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
416 | V : Sum + Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
417 | { |
0 | 418 | |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
419 | type Codomain = V; |
0 | 420 | |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
421 | fn apply<I : Instance<Loc<F,N>>>(&self, x : I) -> Self::Codomain { |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
422 | let xc = x.cow(); |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
423 | self.bt.iter_at(&*xc) |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
424 | .map(|&d| self.generator.support_for(d).apply(&*xc)).sum() |
0 | 425 | } |
426 | } | |
427 | ||
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
428 | impl<F : Float, G, BT, V, const N : usize> DifferentiableImpl<Loc<F, N>> |
0 | 429 | for BTFN<F, G, BT, N> |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
430 | where |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
431 | BT : BTImpl<F, N>, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
432 | G : SupportGenerator<F, N, Id=BT::Data>, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
433 | G::SupportType : LocalAnalysis<F, BT::Agg, N> |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
434 | + DifferentiableMapping<Loc<F, N>, DerivativeDomain = V>, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
435 | V : Sum + Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
436 | { |
27
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
437 | |
47
a0db98c16ab5
Some Differentiable simplifications and clarifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
438 | type Derivative = V; |
27
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
439 | |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
440 | fn differential_impl<I : Instance<Loc<F, N>>>(&self, x :I) -> Self::Derivative { |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
441 | let xc = x.cow(); |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
442 | self.bt.iter_at(&*xc) |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
443 | .map(|&d| self.generator.support_for(d).differential(&*xc)) |
27
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
444 | .sum() |
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
445 | } |
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
446 | } |
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
447 | |
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
448 | // |
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
449 | // GlobalAnalysis |
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
450 | // |
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
451 | |
0 | 452 | impl<F : Float, G, BT, const N : usize> GlobalAnalysis<F, BT::Agg> |
453 | for BTFN<F, G, BT, N> | |
454 | where BT : BTImpl<F, N>, | |
455 | G : SupportGenerator<F, N, Id=BT::Data>, | |
456 | G::SupportType : LocalAnalysis<F, BT::Agg, N> { | |
457 | ||
458 | #[inline] | |
459 | fn global_analysis(&self) -> BT::Agg { | |
460 | self.bt.global_analysis() | |
461 | } | |
462 | } | |
463 | ||
464 | // | |
465 | // Blanket implementation of BTFN as a linear functional over objects | |
466 | // that are linear functionals over BTFN. | |
467 | // | |
468 | ||
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
469 | /* |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
470 | impl<'b, X, F : Float, G, BT, const N : usize> Apply<&'b X, F> |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
471 | for BTFN<F, G, BT, N> |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
472 | where BT : BTImpl<F, N>, |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
473 | G : SupportGenerator<F, N, Id=BT::Data>, |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
474 | G::SupportType : LocalAnalysis<F, BT::Agg, N>, |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
475 | X : for<'a> Apply<&'a BTFN<F, G, BT, N>, F> { |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
476 | |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
477 | #[inline] |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
478 | fn apply(&self, x : &'b X) -> F { |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
479 | x.apply(&self) |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
480 | } |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
481 | } |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
482 | |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
483 | impl<X, F : Float, G, BT, const N : usize> Apply<X, F> |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
484 | for BTFN<F, G, BT, N> |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
485 | where BT : BTImpl<F, N>, |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
486 | G : SupportGenerator<F, N, Id=BT::Data>, |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
487 | G::SupportType : LocalAnalysis<F, BT::Agg, N>, |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
488 | X : for<'a> Apply<&'a BTFN<F, G, BT, N>, F> { |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
489 | |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
490 | #[inline] |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
491 | fn apply(&self, x : X) -> F { |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
492 | x.apply(&self) |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
493 | } |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
494 | } |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
495 | |
0 | 496 | impl<X, F : Float, G, BT, const N : usize> Linear<X> |
497 | for BTFN<F, G, BT, N> | |
498 | where BT : BTImpl<F, N>, | |
499 | G : SupportGenerator<F, N, Id=BT::Data>, | |
500 | G::SupportType : LocalAnalysis<F, BT::Agg, N>, | |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
501 | X : for<'a> Apply<&'a BTFN<F, G, BT, N>, F> { |
0 | 502 | type Codomain = F; |
503 | } | |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
504 | */ |
0 | 505 | |
506 | /// Helper trait for performing approximate minimisation using P2 elements. | |
5 | 507 | /// |
508 | /// `U` is the domain, generally [`Loc`]`<F, N>`, and `F` the type of floating point numbers. | |
509 | /// `Self` is generally a set of `U`, for example, [`Cube`]`<F, N>`. | |
63
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
510 | pub trait P2Minimise<U : Space, F : Float> : Set<U> { |
5 | 511 | /// Minimise `g` over the set presented by `Self`. |
512 | /// | |
513 | /// The function returns `(x, v)` where `x` is the minimiser `v` an approximation of `g(x)`. | |
0 | 514 | fn p2_minimise<G : Fn(&U) -> F>(&self, g : G) -> (U, F); |
515 | ||
516 | } | |
517 | ||
518 | impl<F : Float> P2Minimise<Loc<F, 1>, F> for Cube<F, 1> { | |
519 | fn p2_minimise<G : Fn(&Loc<F, 1>) -> F>(&self, g : G) -> (Loc<F, 1>, F) { | |
520 | let interval = Simplex(self.corners()); | |
521 | interval.p2_model(&g).minimise(&interval) | |
522 | } | |
523 | } | |
524 | ||
525 | #[replace_float_literals(F::cast_from(literal))] | |
526 | impl<F : Float> P2Minimise<Loc<F, 2>, F> for Cube<F, 2> { | |
527 | fn p2_minimise<G : Fn(&Loc<F, 2>) -> F>(&self, g : G) -> (Loc<F, 2>, F) { | |
528 | if false { | |
529 | // Split into two triangle (simplex) with separate P2 model in each. | |
530 | // The six nodes of each triangle are the corners and the edges. | |
531 | let [a, b, c, d] = self.corners(); | |
532 | let [va, vb, vc, vd] = [g(&a), g(&b), g(&c), g(&d)]; | |
533 | ||
534 | let ab = midpoint(&a, &b); | |
535 | let bc = midpoint(&b, &c); | |
536 | let ca = midpoint(&c, &a); | |
537 | let cd = midpoint(&c, &d); | |
538 | let da = midpoint(&d, &a); | |
539 | let [vab, vbc, vca, vcd, vda] = [g(&ab), g(&bc), g(&ca), g(&cd), g(&da)]; | |
540 | ||
541 | let s1 = Simplex([a, b, c]); | |
5 | 542 | let m1 = P2LocalModel::<F, 2, 3>::new( |
0 | 543 | &[a, b, c, ab, bc, ca], |
544 | &[va, vb, vc, vab, vbc, vca] | |
545 | ); | |
546 | ||
547 | let r1@(_, v1) = m1.minimise(&s1); | |
548 | ||
549 | let s2 = Simplex([c, d, a]); | |
5 | 550 | let m2 = P2LocalModel::<F, 2, 3>::new( |
0 | 551 | &[c, d, a, cd, da, ca], |
552 | &[vc, vd, va, vcd, vda, vca] | |
553 | ); | |
554 | ||
555 | let r2@(_, v2) = m2.minimise(&s2); | |
556 | ||
557 | if v1 < v2 { r1 } else { r2 } | |
558 | } else { | |
559 | // Single P2 model for the entire cube. | |
560 | let [a, b, c, d] = self.corners(); | |
561 | let [va, vb, vc, vd] = [g(&a), g(&b), g(&c), g(&d)]; | |
562 | let [e, f] = match 'r' { | |
563 | 'm' => [(&a + &b + &c) / 3.0, (&c + &d + &a) / 3.0], | |
564 | 'c' => [midpoint(&a, &b), midpoint(&a, &d)], | |
565 | 'w' => [(&a + &b * 2.0) / 3.0, (&a + &d * 2.0) / 3.0], | |
566 | 'r' => { | |
567 | // Pseudo-randomise edge midpoints | |
568 | let Loc([x, y]) = a; | |
569 | let tmp : f64 = (x+y).as_(); | |
570 | match tmp.to_bits() % 4 { | |
571 | 0 => [midpoint(&a, &b), midpoint(&a, &d)], | |
572 | 1 => [midpoint(&c, &d), midpoint(&a, &d)], | |
573 | 2 => [midpoint(&a, &b), midpoint(&b, &c)], | |
574 | _ => [midpoint(&c, &d), midpoint(&b, &c)], | |
575 | } | |
576 | }, | |
577 | _ => [self.center(), (&a + &b) / 2.0], | |
578 | }; | |
579 | let [ve, vf] = [g(&e), g(&f)]; | |
580 | ||
5 | 581 | let m1 = P2LocalModel::<F, 2, 3>::new( |
0 | 582 | &[a, b, c, d, e, f], |
583 | &[va, vb, vc, vd, ve, vf], | |
584 | ); | |
585 | ||
586 | m1.minimise(self) | |
587 | } | |
588 | } | |
589 | } | |
590 | ||
5 | 591 | /// Helper type to use [`P2Refiner`] for maximisation. |
86
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
592 | #[derive(Debug, Clone, Serialize, Deserialize)] |
0 | 593 | struct RefineMax; |
5 | 594 | |
595 | /// Helper type to use [`P2Refiner`] for minimisation. | |
86
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
596 | #[derive(Debug, Clone, Serialize, Deserialize)] |
0 | 597 | struct RefineMin; |
598 | ||
5 | 599 | /// A bisection tree [`Refiner`] for maximising or minimising a [`BTFN`]. |
600 | /// | |
0 | 601 | /// The type parameter `T` should be either [`RefineMax`] or [`RefineMin`]. |
86
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
602 | #[derive(Debug, Clone, Serialize, Deserialize)] |
0 | 603 | struct P2Refiner<F : Float, T> { |
5 | 604 | /// The maximum / minimum should be above / below this threshold. |
605 | /// If the threshold cannot be satisfied, the refiner will return `None`. | |
0 | 606 | bound : Option<F>, |
5 | 607 | /// Tolerance for function value estimation. |
0 | 608 | tolerance : F, |
5 | 609 | /// Maximum number of steps to execute the refiner for |
0 | 610 | max_steps : usize, |
5 | 611 | /// Either [`RefineMax`] or [`RefineMin`]. Used only for type system purposes. |
0 | 612 | #[allow(dead_code)] // `how` is just for type system purposes. |
613 | how : T, | |
614 | } | |
615 | ||
616 | impl<F : Float, G, const N : usize> Refiner<F, Bounds<F>, G, N> | |
617 | for P2Refiner<F, RefineMax> | |
618 | where Cube<F, N> : P2Minimise<Loc<F, N>, F>, | |
619 | G : SupportGenerator<F, N>, | |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
620 | G::SupportType : Mapping<Loc<F, N>, Codomain=F> |
0 | 621 | + LocalAnalysis<F, Bounds<F>, N> { |
622 | type Result = Option<(Loc<F, N>, F)>; | |
623 | type Sorting = UpperBoundSorting<F>; | |
624 | ||
625 | fn refine( | |
626 | &self, | |
627 | aggregator : &Bounds<F>, | |
628 | cube : &Cube<F, N>, | |
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
629 | data : &[G::Id], |
0 | 630 | generator : &G, |
631 | step : usize | |
632 | ) -> RefinerResult<Bounds<F>, Self::Result> { | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
633 | |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
634 | if self.bound.map_or(false, |b| aggregator.upper() <= b + self.tolerance) { |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
635 | // The upper bound is below the maximisation threshold. Don't bother with this cube. |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
636 | return RefinerResult::Uncertain(*aggregator, None) |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
637 | } |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
638 | |
0 | 639 | // g gives the negative of the value of the function presented by `data` and `generator`. |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
640 | let g = move |x : &Loc<F, N>| { |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
641 | let f = move |&d| generator.support_for(d).apply(x); |
0 | 642 | -data.iter().map(f).sum::<F>() |
643 | }; | |
644 | // … so the negative of the minimum is the maximm we want. | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
645 | let (x, _neg_v) = cube.p2_minimise(g); |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
646 | //let v = -neg_v; |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
647 | let v = -g(&x); |
0 | 648 | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
649 | if step < self.max_steps && (aggregator.upper() > v + self.tolerance |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
650 | /*|| aggregator.lower() > v - self.tolerance*/) { |
0 | 651 | // The function isn't refined enough in `cube`, so return None |
652 | // to indicate that further subdivision is required. | |
653 | RefinerResult::NeedRefinement | |
654 | } else { | |
655 | // The data is refined enough, so return new hopefully better bounds | |
656 | // and the maximiser. | |
657 | let res = (x, v); | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
658 | let bounds = Bounds(v, v); |
0 | 659 | RefinerResult::Uncertain(bounds, Some(res)) |
660 | } | |
661 | } | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
662 | |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
663 | fn fuse_results(r1 : &mut Self::Result, r2 : Self::Result) { |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
664 | match (*r1, r2) { |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
665 | (Some((_, v1)), Some((_, v2))) => if v1 < v2 { *r1 = r2 } |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
666 | (None, Some(_)) => *r1 = r2, |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
667 | (_, _) => {}, |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
668 | } |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
669 | } |
0 | 670 | } |
671 | ||
672 | ||
673 | impl<F : Float, G, const N : usize> Refiner<F, Bounds<F>, G, N> | |
674 | for P2Refiner<F, RefineMin> | |
675 | where Cube<F, N> : P2Minimise<Loc<F, N>, F>, | |
676 | G : SupportGenerator<F, N>, | |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
677 | G::SupportType : Mapping<Loc<F, N>, Codomain=F> |
0 | 678 | + LocalAnalysis<F, Bounds<F>, N> { |
679 | type Result = Option<(Loc<F, N>, F)>; | |
680 | type Sorting = LowerBoundSorting<F>; | |
681 | ||
682 | fn refine( | |
683 | &self, | |
684 | aggregator : &Bounds<F>, | |
685 | cube : &Cube<F, N>, | |
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
686 | data : &[G::Id], |
0 | 687 | generator : &G, |
688 | step : usize | |
689 | ) -> RefinerResult<Bounds<F>, Self::Result> { | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
690 | |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
691 | if self.bound.map_or(false, |b| aggregator.lower() >= b - self.tolerance) { |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
692 | // The lower bound is above the minimisation threshold. Don't bother with this cube. |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
693 | return RefinerResult::Uncertain(*aggregator, None) |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
694 | } |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
695 | |
0 | 696 | // g gives the value of the function presented by `data` and `generator`. |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
697 | let g = move |x : &Loc<F, N>| { |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
698 | let f = move |&d| generator.support_for(d).apply(x); |
0 | 699 | data.iter().map(f).sum::<F>() |
700 | }; | |
701 | // Minimise it. | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
702 | let (x, _v) = cube.p2_minimise(g); |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
703 | let v = g(&x); |
0 | 704 | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
705 | if step < self.max_steps && (aggregator.lower() < v - self.tolerance |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
706 | /*|| aggregator.upper() < v + self.tolerance*/) { |
0 | 707 | // The function isn't refined enough in `cube`, so return None |
708 | // to indicate that further subdivision is required. | |
709 | RefinerResult::NeedRefinement | |
710 | } else { | |
711 | // The data is refined enough, so return new hopefully better bounds | |
712 | // and the minimiser. | |
713 | let res = (x, v); | |
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
714 | let l = aggregator.lower(); |
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
715 | let bounds = if l > v { |
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
716 | eprintln!("imprecision!"); |
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
717 | Bounds(l, l) |
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
718 | } else { |
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
719 | Bounds(v, v) |
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
720 | }; |
0 | 721 | RefinerResult::Uncertain(bounds, Some(res)) |
722 | } | |
723 | } | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
724 | |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
725 | fn fuse_results(r1 : &mut Self::Result, r2 : Self::Result) { |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
726 | match (*r1, r2) { |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
727 | (Some((_, v1)), Some((_, v2))) => if v1 > v2 { *r1 = r2 } |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
728 | (_, Some(_)) => *r1 = r2, |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
729 | (_, _) => {}, |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
730 | } |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
731 | } |
0 | 732 | } |
733 | ||
734 | ||
5 | 735 | /// A bisection tree [`Refiner`] for checking that a [`BTFN`] is within a stated |
736 | //// upper or lower bound. | |
737 | /// | |
738 | /// The type parameter `T` should be either [`RefineMax`] for upper bound or [`RefineMin`] | |
739 | /// for lower bound. | |
0 | 740 | |
86
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
741 | #[derive(Debug, Clone, Serialize, Deserialize)] |
0 | 742 | struct BoundRefiner<F : Float, T> { |
5 | 743 | /// The upper/lower bound to check for |
0 | 744 | bound : F, |
5 | 745 | /// Tolerance for function value estimation. |
0 | 746 | tolerance : F, |
5 | 747 | /// Maximum number of steps to execute the refiner for |
0 | 748 | max_steps : usize, |
749 | #[allow(dead_code)] // `how` is just for type system purposes. | |
5 | 750 | /// Either [`RefineMax`] or [`RefineMin`]. Used only for type system purposes. |
0 | 751 | how : T, |
752 | } | |
753 | ||
754 | impl<F : Float, G, const N : usize> Refiner<F, Bounds<F>, G, N> | |
755 | for BoundRefiner<F, RefineMax> | |
756 | where G : SupportGenerator<F, N> { | |
757 | type Result = bool; | |
758 | type Sorting = UpperBoundSorting<F>; | |
759 | ||
760 | fn refine( | |
761 | &self, | |
762 | aggregator : &Bounds<F>, | |
763 | _cube : &Cube<F, N>, | |
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
764 | _data : &[G::Id], |
0 | 765 | _generator : &G, |
766 | step : usize | |
767 | ) -> RefinerResult<Bounds<F>, Self::Result> { | |
768 | if aggregator.upper() <= self.bound + self.tolerance { | |
769 | // Below upper bound within tolerances. Indicate uncertain success. | |
770 | RefinerResult::Uncertain(*aggregator, true) | |
771 | } else if aggregator.lower() >= self.bound - self.tolerance { | |
772 | // Above upper bound within tolerances. Indicate certain failure. | |
773 | RefinerResult::Certain(false) | |
774 | } else if step < self.max_steps { | |
775 | // No decision possible, but within step bounds - further subdivision is required. | |
776 | RefinerResult::NeedRefinement | |
777 | } else { | |
778 | // No decision possible, but past step bounds | |
779 | RefinerResult::Uncertain(*aggregator, false) | |
780 | } | |
781 | } | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
782 | |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
783 | fn fuse_results(r1 : &mut Self::Result, r2 : Self::Result) { |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
784 | *r1 = *r1 && r2; |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
785 | } |
0 | 786 | } |
787 | ||
5 | 788 | impl<F : Float, G, const N : usize> Refiner<F, Bounds<F>, G, N> |
789 | for BoundRefiner<F, RefineMin> | |
790 | where G : SupportGenerator<F, N> { | |
791 | type Result = bool; | |
792 | type Sorting = UpperBoundSorting<F>; | |
793 | ||
794 | fn refine( | |
795 | &self, | |
796 | aggregator : &Bounds<F>, | |
797 | _cube : &Cube<F, N>, | |
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
798 | _data : &[G::Id], |
5 | 799 | _generator : &G, |
800 | step : usize | |
801 | ) -> RefinerResult<Bounds<F>, Self::Result> { | |
802 | if aggregator.lower() >= self.bound - self.tolerance { | |
803 | // Above lower bound within tolerances. Indicate uncertain success. | |
804 | RefinerResult::Uncertain(*aggregator, true) | |
805 | } else if aggregator.upper() <= self.bound + self.tolerance { | |
806 | // Below lower bound within tolerances. Indicate certain failure. | |
807 | RefinerResult::Certain(false) | |
808 | } else if step < self.max_steps { | |
809 | // No decision possible, but within step bounds - further subdivision is required. | |
810 | RefinerResult::NeedRefinement | |
811 | } else { | |
812 | // No decision possible, but past step bounds | |
813 | RefinerResult::Uncertain(*aggregator, false) | |
814 | } | |
815 | } | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
816 | |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
817 | fn fuse_results(r1 : &mut Self::Result, r2 : Self::Result) { |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
818 | *r1 = *r1 && r2; |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
819 | } |
5 | 820 | } |
0 | 821 | |
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
822 | // FIXME: The most likely reason for the “Refiner failure” expectation in the methods below |
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
823 | // is numerical inaccuracy: the `glb` maintained in `HeapContainer` (`refine.rs`) becomes bigger |
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
824 | // than the *upper bound* of nodes attempted to be inserted into the `heap` in the container. |
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
825 | // But the `glb` is there exactly to prevent that. Due to numerical inaccuracy, however, a |
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
826 | // newly subdivided node may have lower upper bound than the original lower bound that should |
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
827 | // have been above the `glb` since the node was picked from the queue. Due to the subdivision |
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
828 | // process, if a node whose lower bound is at the `glb` is picked, all of its refined subnodes |
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
829 | // should have lower bound at least the old `glb`, so in a single-threaded situation there should |
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
830 | // always be nodes above the `glb` in the queue. In a multi-threaded situation a node below the |
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
831 | // `glb` may be picked by some thread. When that happens, that thread inserts no new nodes into |
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
832 | // the queue. If the queue empties as a result of that, the thread goes to wait for other threads |
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
833 | // to produce results. Since some node had a node whose lower bound was above the `glb`, eventually |
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
834 | // there should be a result, or new nodes above the `glb` inserted into the queue. Then the waiting |
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
835 | // threads can also continue processing. If, however, numerical inaccuracy destroyes the `glb`, |
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
836 | // the queue may run out, and we get “Refiner failure”. |
0 | 837 | impl<F : Float, G, BT, const N : usize> BTFN<F, G, BT, N> |
838 | where BT : BTSearch<F, N, Agg=Bounds<F>>, | |
839 | G : SupportGenerator<F, N, Id=BT::Data>, | |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
840 | G::SupportType : Mapping<Loc<F, N>, Codomain=F> |
0 | 841 | + LocalAnalysis<F, Bounds<F>, N>, |
842 | Cube<F, N> : P2Minimise<Loc<F, N>, F> { | |
843 | ||
5 | 844 | /// Maximise the `BTFN` within stated value `tolerance`. |
845 | /// | |
846 | /// At most `max_steps` refinement steps are taken. | |
847 | /// Returns the approximate maximiser and the corresponding function value. | |
0 | 848 | pub fn maximise(&mut self, tolerance : F, max_steps : usize) -> (Loc<F, N>, F) { |
849 | let refiner = P2Refiner{ tolerance, max_steps, how : RefineMax, bound : None }; | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
850 | self.bt.search_and_refine(refiner, &self.generator).expect("Refiner failure.").unwrap() |
0 | 851 | } |
852 | ||
5 | 853 | /// Maximise the `BTFN` within stated value `tolerance` subject to a lower bound. |
854 | /// | |
855 | /// At most `max_steps` refinement steps are taken. | |
856 | /// Returns the approximate maximiser and the corresponding function value when one is found | |
857 | /// above the `bound` threshold, otherwise `None`. | |
0 | 858 | pub fn maximise_above(&mut self, bound : F, tolerance : F, max_steps : usize) |
859 | -> Option<(Loc<F, N>, F)> { | |
860 | let refiner = P2Refiner{ tolerance, max_steps, how : RefineMax, bound : Some(bound) }; | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
861 | self.bt.search_and_refine(refiner, &self.generator).expect("Refiner failure.") |
0 | 862 | } |
863 | ||
5 | 864 | /// Minimise the `BTFN` within stated value `tolerance`. |
865 | /// | |
866 | /// At most `max_steps` refinement steps are taken. | |
867 | /// Returns the approximate minimiser and the corresponding function value. | |
0 | 868 | pub fn minimise(&mut self, tolerance : F, max_steps : usize) -> (Loc<F, N>, F) { |
869 | let refiner = P2Refiner{ tolerance, max_steps, how : RefineMin, bound : None }; | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
870 | self.bt.search_and_refine(refiner, &self.generator).expect("Refiner failure.").unwrap() |
0 | 871 | } |
872 | ||
5 | 873 | /// Minimise the `BTFN` within stated value `tolerance` subject to a lower bound. |
874 | /// | |
875 | /// At most `max_steps` refinement steps are taken. | |
876 | /// Returns the approximate minimiser and the corresponding function value when one is found | |
877 | /// above the `bound` threshold, otherwise `None`. | |
0 | 878 | pub fn minimise_below(&mut self, bound : F, tolerance : F, max_steps : usize) |
879 | -> Option<(Loc<F, N>, F)> { | |
880 | let refiner = P2Refiner{ tolerance, max_steps, how : RefineMin, bound : Some(bound) }; | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
881 | self.bt.search_and_refine(refiner, &self.generator).expect("Refiner failure.") |
0 | 882 | } |
5 | 883 | |
884 | /// Verify that the `BTFN` has a given upper `bound` within indicated `tolerance`. | |
885 | /// | |
886 | /// At most `max_steps` refinement steps are taken. | |
0 | 887 | pub fn has_upper_bound(&mut self, bound : F, tolerance : F, max_steps : usize) -> bool { |
888 | let refiner = BoundRefiner{ bound, tolerance, max_steps, how : RefineMax }; | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
889 | self.bt.search_and_refine(refiner, &self.generator).expect("Refiner failure.") |
0 | 890 | } |
5 | 891 | |
892 | /// Verify that the `BTFN` has a given lower `bound` within indicated `tolerance`. | |
893 | /// | |
894 | /// At most `max_steps` refinement steps are taken. | |
895 | pub fn has_lower_bound(&mut self, bound : F, tolerance : F, max_steps : usize) -> bool { | |
896 | let refiner = BoundRefiner{ bound, tolerance, max_steps, how : RefineMin }; | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
897 | self.bt.search_and_refine(refiner, &self.generator).expect("Refiner failure.") |
5 | 898 | } |
0 | 899 | } |