Skip to content

Commit

Permalink
feat: sorting traits
Browse files Browse the repository at this point in the history
  • Loading branch information
TurtIeSocks committed Nov 23, 2023
1 parent 269f34e commit f8e974a
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 149 deletions.
160 changes: 96 additions & 64 deletions server/algorithms/src/routing/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,91 +2,123 @@ use geo::Coord;
use geohash::{decode, encode};
use model::api::{args::SortBy, single_vec::SingleVec};
use rand::rngs::mock::StepRng;
use rayon::slice::ParallelSliceMut;
use rayon::{
iter::{IntoParallelIterator, ParallelIterator},
slice::ParallelSliceMut,
};
use s2::{cell::Cell, latlng::LatLng};
use shuffle::{irs::Irs, shuffler::Shuffler};

use crate::rtree::{self, cluster::Cluster, point};

fn random(clusters: SingleVec) -> SingleVec {
let mut clusters = clusters;
let mut rng = StepRng::new(2, 13);
let mut irs = Irs::default();
match irs.shuffle(&mut clusters, &mut rng) {
Ok(_) => {}
Err(e) => {
log::warn!("Error while shuffling: {}", e);
}
}
clusters
pub trait ClusterSorting {
fn sort_random(self) -> Self;
fn sort_random_mut(&mut self);
fn sort_geohash(&self) -> Self;
fn sort_geohash_mut(&mut self);
fn sort_s2(&self) -> Self;
fn sort_s2_mut(&mut self);
fn sort_point_count(&self, points: &SingleVec, radius: f64) -> Self;
fn sort_point_count_mut(&mut self, points: &SingleVec, radius: f64);
}

pub fn cluster_count(points: &SingleVec, clusters: SingleVec, radius: f64) -> SingleVec {
let tree = rtree::spawn(radius, points);
let clusters: Vec<point::Point> = clusters
.into_iter()
.map(|c| point::Point::new(radius, 20, c))
.collect();
impl ClusterSorting for SingleVec {
fn sort_point_count(&self, points: &SingleVec, radius: f64) -> Self {
let tree = rtree::spawn(radius, points);
let clusters: Vec<point::Point> = self
.into_par_iter()
.map(|c| point::Point::new(radius, 20, *c))
.collect();

let mut clusters: Vec<Cluster<'_>> = rtree::cluster_info(&tree, &clusters);
let mut clusters: Vec<Cluster<'_>> = rtree::cluster_info(&tree, &clusters);

clusters.par_sort_by(|a, b| b.all.len().cmp(&a.all.len()));
clusters.par_sort_by(|a, b| b.all.len().cmp(&a.all.len()));

clusters.into_iter().map(|c| c.point.center).collect()
}
clusters.into_iter().map(|c| c.point.center).collect()
}

fn sort_point_count_mut(&mut self, points: &SingleVec, radius: f64) {
*self = self.sort_point_count(points, radius)
}

fn geohash(clusters: SingleVec) -> SingleVec {
let mut points: Vec<String> = clusters
.into_iter()
.filter_map(|p| match encode(Coord { x: p[1], y: p[0] }, 12) {
Ok(geohash) => Some(geohash),
fn sort_random(self) -> Self {
let mut clusters = self;
clusters.sort_random_mut();
clusters
}

fn sort_random_mut(&mut self) {
let mut rng = StepRng::new(2, 13);
let mut irs = Irs::default();
match irs.shuffle(self, &mut rng) {
Ok(_) => {}
Err(e) => {
log::warn!("Error while encoding geohash: {}", e);
None
log::warn!("Error while shuffling: {}", e);
}
})
.collect();

points.par_sort();
}
}

points
.into_iter()
.map(|p| {
let coord = decode(&p);
match coord {
Ok(coord) => [coord.0.y, coord.0.x],
fn sort_geohash(&self) -> Self {
let mut points: Vec<String> = self
.into_iter()
.filter_map(|p| match encode(Coord { x: p[1], y: p[0] }, 12) {
Ok(geohash) => Some(geohash),
Err(e) => {
log::warn!("Error while decoding geohash: {}", e);
[0., 0.]
log::warn!("Error while encoding geohash: {}", e);
None
}
}
})
.collect()
}
})
.collect();

points.par_sort();

points
.into_iter()
.map(|p| {
let coord = decode(&p);
match coord {
Ok(coord) => [coord.0.y, coord.0.x],
Err(e) => {
log::warn!("Error while decoding geohash: {}", e);
[0., 0.]
}
}
})
.collect()
}

fn sort_geohash_mut(&mut self) {
*self = self.sort_geohash()
}

fn sort_s2(&self) -> Self {
let mut points: Vec<Cell> = self
.into_iter()
.map(|p| LatLng::from_degrees(p[0], p[1]).into())
.collect();

points.par_sort_by(|a, b| a.id.cmp(&b.id));

fn s2cell(clusters: SingleVec) -> SingleVec {
let mut points: Vec<Cell> = clusters
.into_iter()
.map(|p| LatLng::from_degrees(p[0], p[1]).into())
.collect();

points.par_sort_by(|a, b| a.id.cmp(&b.id));

points
.into_iter()
.map(|p| {
let center = p.center();
[center.latitude().deg(), center.longitude().deg()]
})
.collect()
points
.into_iter()
.map(|p| {
let center = p.center();
[center.latitude().deg(), center.longitude().deg()]
})
.collect()
}

fn sort_s2_mut(&mut self) {
*self = self.sort_s2()
}
}

pub fn sort(points: &SingleVec, clusters: SingleVec, radius: f64, sort_by: &SortBy) -> SingleVec {
match sort_by {
SortBy::Random => random(clusters),
SortBy::GeoHash => geohash(clusters),
SortBy::S2Cell => s2cell(clusters),
SortBy::ClusterCount => cluster_count(&points, clusters, radius),
SortBy::Random => clusters.sort_random(),
SortBy::GeoHash => clusters.sort_geohash(),
SortBy::S2Cell => clusters.sort_s2(),
SortBy::ClusterCount => clusters.sort_point_count(points, radius),
_ => clusters,
}
}
2 changes: 1 addition & 1 deletion server/algorithms/src/routing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn main(
) -> SingleVec {
let route_time = Instant::now();
let clusters = if sort_by == &SortBy::TSP && !clusters.is_empty() {
let tour = tsp::multi(&clusters, route_split_level);
let tour = tsp::multi(clusters, route_split_level);
let mut final_clusters = VecDeque::<PointArray>::new();

let mut rotate_count = 0;
Expand Down
Loading

0 comments on commit f8e974a

Please sign in to comment.