Mon, 06 Jan 2025 20:29:25 -0500
More Serialize / Deserialize / Debug derives
| 5 | 1 | /*! | 
| 2 | Aggregation / summarisation of information in branches of bisection trees. | |
| 3 | */ | |
| 4 | ||
| 86 
d5b0e496b72f
More Serialize / Deserialize / Debug derives
 Tuomo Valkonen <tuomov@iki.fi> parents: 
63diff
changeset | 5 | use serde::{Serialize, Deserialize}; | 
| 0 | 6 | use crate::types::*; | 
| 7 | use crate::sets::Set; | |
| 63 
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
8diff
changeset | 8 | use crate::instance::Instance; | 
| 0 | 9 | |
| 5 | 10 | /// Trait for aggregating information about a branch of a [bisection tree][super::BT]. | 
| 11 | /// | |
| 12 | /// Currently [`Bounds`] is the only provided aggregator. | |
| 13 | /// It keeps track of upper and lower bounds of a function representeed by the `BT` by | |
| 14 | /// summing [`Bounds`] produced by [`LocalAnalysis`][super::support::LocalAnalysis] of the | |
| 15 | /// [`Support`][super::support::Support]s of the data stored in the tree. | |
| 16 | /// For the `Bounds` aggregator: | |
| 17 | /// * [`Self::aggregate`] sums input bounds to the current bound. This provides a conservative | |
| 18 | /// estimate of the upper and lower bounds of a sum of functions. | |
| 19 | /// * [`Self::summarise`] takes the maximum of the input bounds. This calculates the bounds | |
| 20 | /// of a function on a greater domain from bounds on subdomains | |
| 21 | /// (in practise [`Cube`][crate::sets::Cube]s). | |
| 22 | /// | |
| 8 
4e09b7829b51
Multithreaded bisection tree operations
 Tuomo Valkonen <tuomov@iki.fi> parents: 
5diff
changeset | 23 | pub trait Aggregator : Clone + Sync + Send + 'static + std::fmt::Debug { | 
| 5 | 24 | /// Aggregate a new data to current state. | 
| 0 | 25 | fn aggregate<I>(&mut self, aggregates : I) | 
| 26 | where I : Iterator<Item=Self>; | |
| 27 | ||
| 5 | 28 | /// Summarise several other aggregators, resetting current state. | 
| 0 | 29 | fn summarise<'a, I>(&'a mut self, aggregates : I) | 
| 30 | where I : Iterator<Item=&'a Self>; | |
| 31 | ||
| 5 | 32 | /// Create a new “empty” aggregate data. | 
| 0 | 33 | fn new() -> Self; | 
| 34 | } | |
| 35 | ||
| 36 | /// An [`Aggregator`] that doesn't aggregate anything. | |
| 86 
d5b0e496b72f
More Serialize / Deserialize / Debug derives
 Tuomo Valkonen <tuomov@iki.fi> parents: 
63diff
changeset | 37 | #[derive(Clone,Debug,Serialize,Deserialize)] | 
| 0 | 38 | pub struct NullAggregator; | 
| 39 | ||
| 40 | impl Aggregator for NullAggregator { | |
| 41 | fn aggregate<I>(&mut self, _aggregates : I) | |
| 42 | where I : Iterator<Item=Self> {} | |
| 43 | ||
| 44 | fn summarise<'a, I>(&'a mut self, _aggregates : I) | |
| 45 | where I : Iterator<Item=&'a Self> {} | |
| 46 | ||
| 47 | fn new() -> Self { NullAggregator } | |
| 48 | } | |
| 49 | ||
| 50 | /// Upper and lower bounds on an `F`-valued function. | |
| 86 
d5b0e496b72f
More Serialize / Deserialize / Debug derives
 Tuomo Valkonen <tuomov@iki.fi> parents: 
63diff
changeset | 51 | #[derive(Copy,Clone,Debug,Serialize,Deserialize)] | 
| 5 | 52 | pub struct Bounds<F>( | 
| 53 | /// Lower bound | |
| 54 | pub F, | |
| 55 | /// Upper bound | |
| 56 | pub F | |
| 57 | ); | |
| 0 | 58 | |
| 5 | 59 | impl<F : Copy> Bounds<F> { | 
| 0 | 60 | /// Returns the lower bound | 
| 61 | #[inline] | |
| 62 | pub fn lower(&self) -> F { self.0 } | |
| 63 | ||
| 64 | /// Returns the upper bound | |
| 65 | #[inline] | |
| 66 | pub fn upper(&self) -> F { self.1 } | |
| 67 | } | |
| 68 | ||
| 69 | impl<F : Float> Bounds<F> { | |
| 5 | 70 | /// Returns a uniform bound. | 
| 71 | /// | |
| 72 | /// This is maximum over the absolute values of the upper and lower bound. | |
| 0 | 73 | #[inline] | 
| 74 | pub fn uniform(&self) -> F { | |
| 75 | let &Bounds(lower, upper) = self; | |
| 76 | lower.abs().max(upper.abs()) | |
| 77 | } | |
| 8 
4e09b7829b51
Multithreaded bisection tree operations
 Tuomo Valkonen <tuomov@iki.fi> parents: 
5diff
changeset | 78 | |
| 
4e09b7829b51
Multithreaded bisection tree operations
 Tuomo Valkonen <tuomov@iki.fi> parents: 
5diff
changeset | 79 | /// Construct a bounds, making sure `lower` bound is less than `upper` | 
| 
4e09b7829b51
Multithreaded bisection tree operations
 Tuomo Valkonen <tuomov@iki.fi> parents: 
5diff
changeset | 80 | #[inline] | 
| 
4e09b7829b51
Multithreaded bisection tree operations
 Tuomo Valkonen <tuomov@iki.fi> parents: 
5diff
changeset | 81 | pub fn corrected(lower : F, upper : F) -> Self { | 
| 
4e09b7829b51
Multithreaded bisection tree operations
 Tuomo Valkonen <tuomov@iki.fi> parents: 
5diff
changeset | 82 | if lower <= upper { | 
| 
4e09b7829b51
Multithreaded bisection tree operations
 Tuomo Valkonen <tuomov@iki.fi> parents: 
5diff
changeset | 83 | Bounds(lower, upper) | 
| 
4e09b7829b51
Multithreaded bisection tree operations
 Tuomo Valkonen <tuomov@iki.fi> parents: 
5diff
changeset | 84 | } else { | 
| 
4e09b7829b51
Multithreaded bisection tree operations
 Tuomo Valkonen <tuomov@iki.fi> parents: 
5diff
changeset | 85 | Bounds(upper, lower) | 
| 
4e09b7829b51
Multithreaded bisection tree operations
 Tuomo Valkonen <tuomov@iki.fi> parents: 
5diff
changeset | 86 | } | 
| 
4e09b7829b51
Multithreaded bisection tree operations
 Tuomo Valkonen <tuomov@iki.fi> parents: 
5diff
changeset | 87 | } | 
| 
4e09b7829b51
Multithreaded bisection tree operations
 Tuomo Valkonen <tuomov@iki.fi> parents: 
5diff
changeset | 88 | |
| 
4e09b7829b51
Multithreaded bisection tree operations
 Tuomo Valkonen <tuomov@iki.fi> parents: 
5diff
changeset | 89 | /// Refine the lower bound | 
| 
4e09b7829b51
Multithreaded bisection tree operations
 Tuomo Valkonen <tuomov@iki.fi> parents: 
5diff
changeset | 90 | #[inline] | 
| 
4e09b7829b51
Multithreaded bisection tree operations
 Tuomo Valkonen <tuomov@iki.fi> parents: 
5diff
changeset | 91 | pub fn refine_lower(&self, lower : F) -> Self { | 
| 
4e09b7829b51
Multithreaded bisection tree operations
 Tuomo Valkonen <tuomov@iki.fi> parents: 
5diff
changeset | 92 | let &Bounds(l, u) = self; | 
| 
4e09b7829b51
Multithreaded bisection tree operations
 Tuomo Valkonen <tuomov@iki.fi> parents: 
5diff
changeset | 93 | debug_assert!(l <= u); | 
| 
4e09b7829b51
Multithreaded bisection tree operations
 Tuomo Valkonen <tuomov@iki.fi> parents: 
5diff
changeset | 94 | Bounds(l.max(lower), u.max(lower)) | 
| 
4e09b7829b51
Multithreaded bisection tree operations
 Tuomo Valkonen <tuomov@iki.fi> parents: 
5diff
changeset | 95 | } | 
| 
4e09b7829b51
Multithreaded bisection tree operations
 Tuomo Valkonen <tuomov@iki.fi> parents: 
5diff
changeset | 96 | |
| 
4e09b7829b51
Multithreaded bisection tree operations
 Tuomo Valkonen <tuomov@iki.fi> parents: 
5diff
changeset | 97 | /// Refine the lower bound | 
| 
4e09b7829b51
Multithreaded bisection tree operations
 Tuomo Valkonen <tuomov@iki.fi> parents: 
5diff
changeset | 98 | #[inline] | 
| 
4e09b7829b51
Multithreaded bisection tree operations
 Tuomo Valkonen <tuomov@iki.fi> parents: 
5diff
changeset | 99 | pub fn refine_upper(&self, upper : F) -> Self { | 
| 
4e09b7829b51
Multithreaded bisection tree operations
 Tuomo Valkonen <tuomov@iki.fi> parents: 
5diff
changeset | 100 | let &Bounds(l, u) = self; | 
| 
4e09b7829b51
Multithreaded bisection tree operations
 Tuomo Valkonen <tuomov@iki.fi> parents: 
5diff
changeset | 101 | debug_assert!(l <= u); | 
| 
4e09b7829b51
Multithreaded bisection tree operations
 Tuomo Valkonen <tuomov@iki.fi> parents: 
5diff
changeset | 102 | Bounds(l.min(upper), u.min(upper)) | 
| 
4e09b7829b51
Multithreaded bisection tree operations
 Tuomo Valkonen <tuomov@iki.fi> parents: 
5diff
changeset | 103 | } | 
| 0 | 104 | } | 
| 105 | ||
| 106 | impl<'a, F : Float> std::ops::Add<Self> for Bounds<F> { | |
| 107 | type Output = Self; | |
| 108 | #[inline] | |
| 109 | fn add(self, Bounds(l2, u2) : Self) -> Self::Output { | |
| 110 | let Bounds(l1, u1) = self; | |
| 111 | debug_assert!(l1 <= u1 && l2 <= u2); | |
| 112 | Bounds(l1 + l2, u1 + u2) | |
| 113 | } | |
| 114 | } | |
| 115 | ||
| 116 | impl<'a, F : Float> std::ops::Mul<Self> for Bounds<F> { | |
| 117 | type Output = Self; | |
| 118 | #[inline] | |
| 119 | fn mul(self, Bounds(l2, u2) : Self) -> Self::Output { | |
| 120 | let Bounds(l1, u1) = self; | |
| 121 | debug_assert!(l1 <= u1 && l2 <= u2); | |
| 122 | let a = l1 * l2; | |
| 123 | let b = u1 * u2; | |
| 124 | // The order may flip when negative numbers are involved, so need min/max | |
| 125 | Bounds(a.min(b), a.max(b)) | |
| 126 | } | |
| 127 | } | |
| 128 | ||
| 129 | impl<F : Float> std::iter::Product for Bounds<F> { | |
| 130 | #[inline] | |
| 131 | fn product<I>(mut iter: I) -> Self | |
| 132 | where I: Iterator<Item = Self> { | |
| 133 | match iter.next() { | |
| 134 | None => Bounds(F::ZERO, F::ZERO), | |
| 135 | Some(init) => iter.fold(init, |a, b| a*b) | |
| 136 | } | |
| 137 | } | |
| 138 | } | |
| 139 | ||
| 140 | impl<F : Float> Set<F> for Bounds<F> { | |
| 63 
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
8diff
changeset | 141 | fn contains<I : Instance<F>>(&self, item : I) -> bool { | 
| 
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
8diff
changeset | 142 | let v = item.own(); | 
| 0 | 143 | let &Bounds(l, u) = self; | 
| 144 | debug_assert!(l <= u); | |
| 63 
f7b87d84864d
Extra reflexivity and hilbert-like requirements for Euclidean. Fuse Dot into Euclidean.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
8diff
changeset | 145 | l <= v && v <= u | 
| 0 | 146 | } | 
| 147 | } | |
| 148 | ||
| 149 | impl<F : Float> Bounds<F> { | |
| 150 | /// Calculate a common bound (glb, lub) for two bounds. | |
| 151 | #[inline] | |
| 152 | pub fn common(&self, &Bounds(l2, u2) : &Self) -> Self { | |
| 153 | let &Bounds(l1, u1) = self; | |
| 154 | debug_assert!(l1 <= u1 && l2 <= u2); | |
| 155 | Bounds(l1.min(l2), u1.max(u2)) | |
| 156 | } | |
| 157 | ||
| 5 | 158 | /// Indicates whether `Self` is a superset of the argument bound. | 
| 0 | 159 | #[inline] | 
| 160 | pub fn superset(&self, &Bounds(l2, u2) : &Self) -> bool { | |
| 161 | let &Bounds(l1, u1) = self; | |
| 162 | debug_assert!(l1 <= u1 && l2 <= u2); | |
| 163 | l1 <= l2 && u2 <= u1 | |
| 164 | } | |
| 165 | ||
| 5 | 166 | /// Returns the greatest bound contained by both argument bounds, if one exists. | 
| 0 | 167 | #[inline] | 
| 168 | pub fn glb(&self, &Bounds(l2, u2) : &Self) -> Option<Self> { | |
| 169 | let &Bounds(l1, u1) = self; | |
| 170 | debug_assert!(l1 <= u1 && l2 <= u2); | |
| 171 | let l = l1.max(l2); | |
| 172 | let u = u1.min(u2); | |
| 8 
4e09b7829b51
Multithreaded bisection tree operations
 Tuomo Valkonen <tuomov@iki.fi> parents: 
5diff
changeset | 173 | debug_assert!(l <= u); | 
| 0 | 174 | if l < u { | 
| 175 | Some(Bounds(l, u)) | |
| 176 | } else { | |
| 177 | None | |
| 178 | } | |
| 179 | } | |
| 180 | } | |
| 181 | ||
| 182 | impl<F : Float> Aggregator for Bounds<F> { | |
| 183 | #[inline] | |
| 184 | fn aggregate<I>(&mut self, aggregates : I) | |
| 185 | where I : Iterator<Item=Self> { | |
| 186 | *self = aggregates.fold(*self, |a, b| a + b); | |
| 187 | } | |
| 188 | ||
| 189 | #[inline] | |
| 190 | fn summarise<'a, I>(&'a mut self, mut aggregates : I) | |
| 191 | where I : Iterator<Item=&'a Self> { | |
| 192 | *self = match aggregates.next() { | |
| 193 | None => Bounds(F::ZERO, F::ZERO), // No parts in this cube; the function is zero | |
| 194 | Some(&bounds) => { | |
| 195 | aggregates.fold(bounds, |a, b| a.common(b)) | |
| 196 | } | |
| 197 | } | |
| 198 | } | |
| 199 | ||
| 200 | #[inline] | |
| 201 | fn new() -> Self { | |
| 202 | Bounds(F::ZERO, F::ZERO) | |
| 203 | } | |
| 204 | } |