1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
//! As Close As Possible — [nearest neighbor search] in Rust. //! //! # Overview //! //! The notion of distances between points is captured by the [`Proximity`] trait. Its //! [`distance()`] method returns a [`Distance`], from which the actual numerical distance may be //! retrieved with [`value()`]. These layers of abstraction allow `acap` to work with generically //! with different distance functions over different types. //! //! There are no restrictions on the distances computed by a [`Proximity`]. For example, they don't //! have to be symmetric, subadditive, or even positive. Implementations that do have these //! desirable properties will additionally implement the [`Metric`] marker trait. This distinction //! allows `acap` to support a wide variety of useful metric and non-metric distances. //! //! As a concrete example, consider `Euclidean<[i32; 2]>`. The [`Euclidean`] wrapper equips any //! type that has [coordinates] with the [Euclidean distance] function as its [`Proximity`] //! implementation: //! //! use acap::distance::Proximity; //! use acap::euclid::Euclidean; //! //! let a = Euclidean([3, 4]); //! let b = Euclidean([7, 7]); //! assert_eq!(a.distance(&b), 5); //! //! In this case, `distance()` doesn't return a number directly; as an optimization, it returns a //! [`EuclideanDistance`] wrapper. This wrapper stores the squared value of the distance, to avoid //! computing square roots until absolutely necessary. Still, it transparently supports comparisons //! with numerical values: //! //! # use acap::distance::Proximity; //! # use acap::euclid::Euclidean; //! # let a = Euclidean([3, 4]); //! # let b = Euclidean([7, 7]); //! use acap::distance::Distance; //! //! let d = a.distance(&b); //! assert!(d > 4 && d < 6); //! assert_eq!(d, 5); //! assert_eq!(d.value(), 5.0f32); //! //! For finding the nearest neighbors to a point from a set of other points, the //! [`NearestNeighbors`] trait provides a uniform interface to [many different similarity search //! data structures]. One such structure is the [vantage-point tree], available in `acap` as //! [`VpTree`]: //! //! # use acap::euclid::Euclidean; //! use acap::vp::VpTree; //! use acap::NearestNeighbors; //! //! let tree = VpTree::balanced(vec![ //! Euclidean([3, 4]), //! Euclidean([5, 12]), //! Euclidean([8, 15]), //! Euclidean([7, 24]), //! ]); //! //! [`VpTree`] implements [`NearestNeighbors`], which has a [`nearest()`] method that returns an //! optional [`Neighbor`]. The [`Neighbor`] struct holds the actual neighbor it found, and the //! distance it was from the target: //! //! # use acap::euclid::Euclidean; //! # use acap::vp::VpTree; //! # use acap::NearestNeighbors; //! # let tree = VpTree::balanced( //! # vec![Euclidean([3, 4]), Euclidean([5, 12]), Euclidean([8, 15]), Euclidean([7, 24])] //! # ); //! let nearest = tree.nearest(&[7, 7]).unwrap(); //! assert_eq!(nearest.item, &Euclidean([3, 4])); //! assert_eq!(nearest.distance, 5); //! //! [`NearestNeighbors`] also provides the [`nearest_within()`], [`k_nearest()`], and //! [`k_nearest_within()`] methods which find up to `k` neighbors within a possible threshold. //! //! It can be expensive to compute nearest neighbors exactly, especially in high dimensions. //! For performance reasons, [`NearestNeighbors`] implementations are allowed to return approximate //! results. Many implementations have a speed/accuracy tradeoff which can be tuned. Those //! implementations which always return exact results will also implement the [`ExactNeighbors`] //! marker trait. For example, a [`VpTree`] will be exact when the [`Proximity`] function is a //! [`Metric`]. //! //! [nearest neighbor search]: https://en.wikipedia.org/wiki/Nearest_neighbor_search //! [`distance()`]: Proximity#tymethod.distance //! [`value()`]: Distance#method.value //! [coordinates]: Coordinates //! [Euclidean distance]: https://en.wikipedia.org/wiki/Euclidean_distance //! [many different similarity search data structures]: NearestNeighbors#implementors //! [vantage-point tree]: https://en.wikipedia.org/wiki/Vantage-point_tree //! [`VpTree`]: vp::VpTree //! [`nearest()`]: NearestNeighbors#method.nearest //! [`k_nearest()`]: NearestNeighbors#method.k_nearest //! [`nearest_within()`]: NearestNeighbors#method.nearest_within //! [`k_nearest_within()`]: NearestNeighbors#method.k_nearest_within pub mod chebyshev; pub mod coords; pub mod cos; pub mod distance; pub mod euclid; pub mod exhaustive; pub mod hamming; pub mod kd; pub mod lp; pub mod taxi; pub mod vp; mod util; pub use coords::Coordinates; pub use distance::{Distance, Metric, Proximity}; pub use euclid::{euclidean_distance, Euclidean, EuclideanDistance}; use std::cmp::Ordering; use std::collections::BinaryHeap; use std::convert::TryInto; /// A nearest neighbor. #[derive(Clone, Copy, Debug)] pub struct Neighbor<V, D> { /// The neighbor itself. pub item: V, /// The distance from the target to this neighbor. pub distance: D, } impl<V, D> Neighbor<V, D> { /// Create a new Neighbor. pub fn new(item: V, distance: D) -> Self { Self { item, distance } } } impl<V1, D1, V2, D2> PartialEq<Neighbor<V2, D2>> for Neighbor<V1, D1> where V1: PartialEq<V2>, D1: PartialEq<D2>, { fn eq(&self, other: &Neighbor<V2, D2>) -> bool { self.item == other.item && self.distance == other.distance } } /// Accumulates nearest neighbor search results. /// /// Type parameters: /// /// * `K`: The type of the search target (the "key" type) /// * `V`: The type of neighbors this contains (the "value" type) /// /// Neighborhood implementations keep track of the current search radius and accumulate the results, /// work which would otherwise have to be duplicated for every nearest neighbor search algorithm. /// They also serve as a customization point, allowing for functionality to be injected into any /// [NearestNeighbors] implementation (for example, filtering the result set or limiting the number /// of neighbors considered). pub trait Neighborhood<K: Proximity<V>, V> { /// Returns the target of the nearest neighbor search. fn target(&self) -> K; /// Check whether a distance is within the current search radius. fn contains<D>(&self, distance: D) -> bool where D: PartialOrd<K::Distance>; /// Consider a new candidate neighbor. /// /// Returns `self.target().distance(item)`. fn consider(&mut self, item: V) -> K::Distance; } /// A candidate nearest neighbor found during a search. #[derive(Debug)] struct Candidate<V, D>(Neighbor<V, D>); impl<V, D: Distance> Candidate<V, D> { fn new<K>(target: K, item: V) -> Self where K: Proximity<V, Distance = D>, { let distance = target.distance(&item); Self(Neighbor::new(item, distance)) } } impl<V, D: PartialOrd> PartialOrd for Candidate<V, D> { fn partial_cmp(&self, other: &Self) -> Option<Ordering> { self.0.distance.partial_cmp(&other.0.distance) } } impl<V, D: PartialOrd> Ord for Candidate<V, D> { fn cmp(&self, other: &Self) -> Ordering { self.partial_cmp(other) .expect("Unordered distances found during nearest neighbor search") } } impl<V, D: PartialEq> PartialEq for Candidate<V, D> { fn eq(&self, other: &Self) -> bool { self.0.distance == other.0.distance } } impl<V, D: PartialEq> Eq for Candidate<V, D> {} /// A [Neighborhood] with at most one result. #[derive(Debug)] struct SingletonNeighborhood<K, V, D> { /// The search target. target: K, /// The current threshold distance. threshold: Option<D>, /// The current nearest neighbor, if any. candidate: Option<Candidate<V, D>>, } impl<K, V, D> SingletonNeighborhood<K, V, D> { /// Create a new singleton neighborhood. /// /// * `target`: The search target. /// * `threshold`: The maximum allowable distance. fn new(target: K, threshold: Option<D>) -> Self { Self { target, threshold, candidate: None, } } /// Convert this result into an optional neighbor. fn into_option(self) -> Option<Neighbor<V, D>> { self.candidate.map(|c| c.0) } } impl<K, V> Neighborhood<K, V> for SingletonNeighborhood<K, V, K::Distance> where K: Copy + Proximity<V>, { fn target(&self) -> K { self.target } fn contains<D>(&self, distance: D) -> bool where D: PartialOrd<K::Distance>, { self.threshold.map_or(true, |t| distance <= t) } fn consider(&mut self, item: V) -> K::Distance { let candidate = Candidate::new(self.target, item); let distance = candidate.0.distance; if self.contains(distance) { self.threshold = Some(distance); self.candidate = Some(candidate); } distance } } /// A [Neighborhood] of up to `k` results, using a binary heap. #[derive(Debug)] struct HeapNeighborhood<K, V, D> { /// The target of the nearest neighbor search. target: K, /// The number of nearest neighbors to find. k: usize, /// The current threshold distance to the farthest result. threshold: Option<D>, /// A max-heap of the best candidates found so far. heap: BinaryHeap<Candidate<V, D>>, } impl<K, V, D: PartialOrd> HeapNeighborhood<K, V, D> { /// Create a new singleton neighborhood. /// /// * `target`: The search target. /// * `k`: The number of nearest neighbors to find. /// * `threshold`: The maximum allowable distance. fn new(target: K, k: usize, threshold: Option<D>) -> Self { Self { target, k, threshold, heap: BinaryHeap::new(), } } /// Convert this result into an optional neighbor. fn into_vec(self) -> Vec<Neighbor<V, D>> { self.heap .into_sorted_vec() .into_iter() .map(|c| c.0) .collect() } } impl<K, V> Neighborhood<K, V> for HeapNeighborhood<K, V, K::Distance> where K: Copy + Proximity<V>, { fn target(&self) -> K { self.target } fn contains<D>(&self, distance: D) -> bool where D: PartialOrd<K::Distance>, { self.k > 0 && self.threshold.map_or(true, |t| distance <= t) } fn consider(&mut self, item: V) -> K::Distance { let candidate = Candidate::new(self.target, item); let distance = candidate.0.distance; if self.contains(distance) { let heap = &mut self.heap; if heap.len() == self.k { heap.pop(); } heap.push(candidate); if heap.len() == self.k { self.threshold = heap.peek().map(|c| c.0.distance) } } distance } } /// A [nearest neighbor search] index. /// /// Type parameters: /// /// * `K`: The type of the search target (the "key" type) /// * `V`: The type of the returned neighbors (the "value" type) /// /// In general, exact nearest neighbor searches may be prohibitively expensive due to the [curse of /// dimensionality]. Therefore, NearestNeighbor implementations are allowed to give approximate /// results. The marker trait [ExactNeighbors] denotes implementations which are guaranteed to give /// exact results. /// /// [nearest neighbor search]: https://en.wikipedia.org/wiki/Nearest_neighbor_search /// [curse of dimensionality]: https://en.wikipedia.org/wiki/Curse_of_dimensionality pub trait NearestNeighbors<K: Proximity<V>, V = K> { /// Returns the nearest neighbor to `target` (or `None` if this index is empty). fn nearest(&self, target: &K) -> Option<Neighbor<&V, K::Distance>> { self.search(SingletonNeighborhood::new(target, None)) .into_option() } /// Returns the nearest neighbor to `target` within the distance `threshold`, if one exists. fn nearest_within<D>(&self, target: &K, threshold: D) -> Option<Neighbor<&V, K::Distance>> where D: TryInto<K::Distance>, { if let Ok(distance) = threshold.try_into() { self.search(SingletonNeighborhood::new(target, Some(distance))) .into_option() } else { None } } /// Returns the up to `k` nearest neighbors to `target`. fn k_nearest(&self, target: &K, k: usize) -> Vec<Neighbor<&V, K::Distance>> { self.search(HeapNeighborhood::new(target, k, None)) .into_vec() } /// Returns the up to `k` nearest neighbors to `target` within the distance `threshold`. fn k_nearest_within<D>(&self, target: &K, k: usize, threshold: D) -> Vec<Neighbor<&V, K::Distance>> where D: TryInto<K::Distance>, { if let Ok(distance) = threshold.try_into() { self.search(HeapNeighborhood::new(target, k, Some(distance))) .into_vec() } else { Vec::new() } } /// Search for nearest neighbors and add them to a neighborhood. fn search<'k, 'v, N>(&'v self, neighborhood: N) -> N where K: 'k, V: 'v, N: Neighborhood<&'k K, &'v V>; } /// Marker trait for [NearestNeighbors] implementations that always return exact results. pub trait ExactNeighbors<K: Proximity<V>, V = K>: NearestNeighbors<K, V> {} #[cfg(test)] pub mod tests { use super::*; use crate::exhaustive::ExhaustiveSearch; use rand::prelude::*; use std::iter::FromIterator; type Point = Euclidean<[f32; 3]>; /// Test a [NearestNeighbors] implementation. pub fn test_nearest_neighbors<T, F>(from_iter: F) where T: NearestNeighbors<Point>, F: Fn(Vec<Point>) -> T, { test_empty(&from_iter); test_pythagorean(&from_iter); test_random_points(&from_iter); } fn test_empty<T, F>(from_iter: &F) where T: NearestNeighbors<Point>, F: Fn(Vec<Point>) -> T, { let points = Vec::new(); let index = from_iter(points); let target = Euclidean([0.0, 0.0, 0.0]); assert_eq!(index.nearest(&target), None); assert_eq!(index.nearest_within(&target, 1.0), None); assert!(index.k_nearest(&target, 0).is_empty()); assert!(index.k_nearest(&target, 3).is_empty()); assert!(index.k_nearest_within(&target, 0, 1.0).is_empty()); assert!(index.k_nearest_within(&target, 3, 1.0).is_empty()); } fn test_pythagorean<T, F>(from_iter: &F) where T: NearestNeighbors<Point>, F: Fn(Vec<Point>) -> T, { let points = vec![ Euclidean([3.0, 4.0, 0.0]), Euclidean([5.0, 0.0, 12.0]), Euclidean([0.0, 8.0, 15.0]), Euclidean([1.0, 2.0, 2.0]), Euclidean([2.0, 3.0, 6.0]), Euclidean([4.0, 4.0, 7.0]), ]; let index = from_iter(points); let target = Euclidean([0.0, 0.0, 0.0]); assert_eq!( index.nearest(&target).expect("No nearest neighbor found"), Neighbor::new(&Euclidean([1.0, 2.0, 2.0]), 3.0) ); assert_eq!(index.nearest_within(&target, 2.0), None); assert_eq!( index.nearest_within(&target, 4.0).expect("No nearest neighbor found within 4.0"), Neighbor::new(&Euclidean([1.0, 2.0, 2.0]), 3.0) ); assert!(index.k_nearest(&target, 0).is_empty()); assert_eq!( index.k_nearest(&target, 3), vec![ Neighbor::new(&Euclidean([1.0, 2.0, 2.0]), 3.0), Neighbor::new(&Euclidean([3.0, 4.0, 0.0]), 5.0), Neighbor::new(&Euclidean([2.0, 3.0, 6.0]), 7.0), ] ); assert!(index.k_nearest(&target, 0).is_empty()); assert_eq!( index.k_nearest_within(&target, 3, 6.0), vec![ Neighbor::new(&Euclidean([1.0, 2.0, 2.0]), 3.0), Neighbor::new(&Euclidean([3.0, 4.0, 0.0]), 5.0), ] ); assert_eq!( index.k_nearest_within(&target, 3, 8.0), vec![ Neighbor::new(&Euclidean([1.0, 2.0, 2.0]), 3.0), Neighbor::new(&Euclidean([3.0, 4.0, 0.0]), 5.0), Neighbor::new(&Euclidean([2.0, 3.0, 6.0]), 7.0), ] ); } fn test_random_points<T, F>(from_iter: &F) where T: NearestNeighbors<Point>, F: Fn(Vec<Point>) -> T, { let mut points = Vec::new(); for _ in 0..256 { points.push(Euclidean([random(), random(), random()])); } let index = from_iter(points.clone()); let eindex = ExhaustiveSearch::from_iter(points.clone()); let target = Euclidean([random(), random(), random()]); assert_eq!( index.k_nearest(&target, 3), eindex.k_nearest(&target, 3), "target: {:?}, points: {:#?}", target, points, ); } }