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