Skip to content

Commit

Permalink
refactor: move rotating code to its own fn
Browse files Browse the repository at this point in the history
  • Loading branch information
TurtIeSocks committed Dec 2, 2023
1 parent 3421925 commit 4c9c85c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 23 deletions.
2 changes: 1 addition & 1 deletion server/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "koji"
version = "1.4.0"
version = "1.4.1"
edition = "2021"

[workspace]
Expand Down
26 changes: 6 additions & 20 deletions server/algorithms/src/routing/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::{collections::VecDeque, time::Instant};
use std::time::Instant;

use model::api::{args::SortBy, point_array::PointArray, single_vec::SingleVec};
use model::api::{args::SortBy, single_vec::SingleVec};

use crate::stats::Stats;
use crate::{stats::Stats, utils::rotate_to_best};

pub mod basic;
pub mod tsp;
Expand All @@ -18,26 +18,12 @@ pub fn main(
) -> SingleVec {
let route_time = Instant::now();
let clusters = if sort_by == &SortBy::TSP && !clusters.is_empty() {
let tour = tsp::run(clusters, route_split_level);
let mut final_clusters = VecDeque::<PointArray>::new();

let mut rotate_count = 0;
for (i, [lat, lon]) in tour.into_iter().enumerate() {
if stats.best_clusters.len() > 0
&& lat == stats.best_clusters[0][0]
&& lon == stats.best_clusters[0][1]
{
rotate_count = i;
log::debug!("Found Best! {}, {} - {}", lat, lon, i);
}
final_clusters.push_back([lat, lon]);
}
final_clusters.rotate_left(rotate_count);

final_clusters.into()
tsp::run(clusters, route_split_level)
} else {
basic::sort(&data_points, clusters, radius, sort_by)
};
let clusters = rotate_to_best(clusters, stats);

stats.set_route_time(route_time);
stats.distance_stats(&clusters);

Expand Down
26 changes: 25 additions & 1 deletion server/algorithms/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use std::collections::HashMap;
use std::collections::{HashMap, VecDeque};
use std::fmt::Debug;
use std::fs::{create_dir_all, File};
use std::io::{Result, Write};

use colored::Colorize;
use geo::Coord;
use geohash::encode;
use hashbrown::HashSet;
use model::api::{point_array::PointArray, single_vec::SingleVec};

use crate::rtree::cluster::Cluster;
use crate::stats::Stats;

pub fn debug_hashmap<T, U>(file_name: &str, input: &T) -> Result<()>
where
Expand Down Expand Up @@ -116,3 +119,24 @@ pub fn _debug_clusters(clusters: &HashSet<Cluster>, file_suffix: &str) {
debug_hashmap(&format!("{}_cluster.txt", file_suffix), &cluster_map).unwrap();
debug_hashmap(&format!("{}_unique.txt", file_suffix), &unique_map).unwrap();
}

pub fn rotate_to_best(clusters: SingleVec, stats: &Stats) -> SingleVec {
let mut final_clusters = VecDeque::<PointArray>::new();

let best_cluster_set = stats
.best_clusters
.iter()
.map(|x| encode(Coord { x: x[1], y: x[0] }, 12).unwrap())
.collect::<HashSet<String>>();
let mut rotate_count = 0;
for (i, [lat, lon]) in clusters.into_iter().enumerate() {
if best_cluster_set.contains(&encode(Coord { x: lon, y: lat }, 12).unwrap()) {
rotate_count = i;
log::debug!("Found Best! {}, {} - {}", lat, lon, i);
}
final_clusters.push_back([lat, lon]);
}
final_clusters.rotate_left(rotate_count);

final_clusters.into()
}

1 comment on commit 4c9c85c

@vercel
Copy link

@vercel vercel bot commented on 4c9c85c Dec 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

koji – ./

koji-turtiesocks.vercel.app
koji.vercel.app
koji-git-main-turtiesocks.vercel.app

Please sign in to comment.