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