summaryrefslogtreecommitdiff
path: root/dashboard_website/router.py
diff options
context:
space:
mode:
authorAnson Bridges <bridges.anson@gmail.com>2023-11-10 00:00:10 -0500
committerAnson Bridges <bridges.anson@gmail.com>2023-11-10 00:00:10 -0500
commit0e5d5ea7b26e55a6b83a10c53eada788d87428c2 (patch)
tree4c32407e1e09c1f1063836461f305e41c427bccf /dashboard_website/router.py
parent04f7d71f25078d38a267d8e848bf556e8b6d3a0b (diff)
routes begin to show .
Diffstat (limited to 'dashboard_website/router.py')
-rw-r--r--dashboard_website/router.py53
1 files changed, 30 insertions, 23 deletions
diff --git a/dashboard_website/router.py b/dashboard_website/router.py
index 99c83d2..5bafd1e 100644
--- a/dashboard_website/router.py
+++ b/dashboard_website/router.py
@@ -2,7 +2,7 @@ import numpy as np
import requests
from sklearn.cluster import KMeans
-from dashboard_website import db
+from datastructs import *
host = "http://acetyl.net:5000" # queries acetyl.net:5000, the OSRM engine
@@ -37,21 +37,27 @@ def getZSP(bike, home, clue_cluster):
# determines clusters based on current bikes and clues
def getClusters(bikes, clues, endpoint):
- clusters = [[] for bike in bikes]
+
+ clusters = [[] for bike in bikes ]
+ route_geos = [[] for bike in bikes ]
+ active_indices = [i for i in range(len(bikes)) if bikes[i].status == "ACTIVE"]
active_bikes = [bike for bike in bikes if bike.status == "ACTIVE"]
+ if len(active_bikes) == 0:
+ return clusters, route_geos
active_clues = [clue for clue in clues if clue.status == "UNVISITED"]
# select only active bikes
# select only unvisited clues
- clusters_t, route_geo = cluster_and_optimize(active_clues, active_bikes, endpoint)
- for cluster in clusters_t:
- clusters[i] = cluster
+ clusters_t, route_geos_t = cluster_and_optimize(active_clues, active_bikes, endpoint)
+ for i in range(len(active_indices)):
+ route_geos[active_indices[i]] = route_geos_t[i]
+ clusters[active_indices[i]] = clusters_t[i]
# return list of clue clusters corresponding to bikes
- pass
+ return clusters, route_geos
# utility functions (internal)
-def cluster_and_optimize(clues: [db.Clue], bikes: [db.Bike], end: db.Point, time_diff=0.25, max_time=24, n=2):
+def cluster_and_optimize(clues: [Clue], bikes: [Bike], end: Point, time_diff=0.25, max_time=24, n=2):
"""
Takes a dataframe of gps coordinates, a list of centroids, and an end point and returns a dataframe with a cluster
:param clues: a list of clues
@@ -65,7 +71,7 @@ def cluster_and_optimize(clues: [db.Clue], bikes: [db.Bike], end: db.Point, time
# Create a new column with normalized gps coordinates and centroids
normalized_points, norm_centroids = __normalize_points(clues, bikes)
-
+ print(norm_centroids)
# Cluster the coordinates
kmeans = KMeans(n_clusters=len(norm_centroids), init=norm_centroids)
kmeans.fit(normalized_points)
@@ -82,21 +88,22 @@ def cluster_and_optimize(clues: [db.Clue], bikes: [db.Bike], end: db.Point, time
routes[i] = __remove_longest_waypoints(routes[i], bikes[i], end, max_time)
# Get the json of the routes
- route_geo = []
+ route_waypoints = []
+ geometries = []
for i, route in enumerate(routes):
- route_geo.append(
- __get_json(__clues_to_string(route), __clues_to_string([bikes[i]]), __clues_to_string([end])[:-1])[
- 'waypoints'])
+ route_json = __get_json(__clues_to_string(route), __clues_to_string([bikes[i]]), __clues_to_string([end])[:-1])
+ geometries.append(route_json['trips'][0]['geometry']['coordinates'])
+ route_waypoints.append(route_json['waypoints'])
# Use the waypoint_index to reorder each route
for i, route in enumerate(routes):
- route = [route[j] for j in route_geo[i][0]['waypoint_index']]
+ route = [ route[ j['waypoint_index']-1 ] for j in route_waypoints[i] if route_waypoints[i].index(j) < (len(route_waypoints[i])-1) ]
routes[i] = route
- return routes
+ return routes, geometries
-def __clues_to_string(points: [db.Clue]):
+def __clues_to_string(points: [Clue]):
"""
Takes a list of points and returns a string of the list of points
:param points: a list of points
@@ -146,7 +153,7 @@ def __get_trip_time(coordinate_string, num_waypoints, start, end, time_per_waypo
return total_time_hours
-def __minimize_route_time_diff(routes: [db.Clue], starts: [db.Point], end: db.Point, time_diff, n):
+def __minimize_route_time_diff(routes: [Clue], starts: [Point], end: Point, time_diff, n):
"""
Takes a list of lists of coordinates, a list of start points, an end point, a time difference, and a number of routes
:param routes: the list of lists of coordinates
@@ -186,7 +193,7 @@ def __minimize_route_time_diff(routes: [db.Clue], starts: [db.Point], end: db.Po
return routes
-def __remove_longest_waypoints(route_coordinates: [db.Clue], start: db.Bike, end: db.Point, max_time):
+def __remove_longest_waypoints(route_coordinates: [Clue], start: Bike, end: Point, max_time):
"""
Takes a list of coordinates, a start point, an end point, and a maximum time and returns a list of coordinates
:param route_coordinates: the list of coordinates
@@ -210,7 +217,7 @@ def __remove_longest_waypoints(route_coordinates: [db.Clue], start: db.Bike, end
return route_coordinates
-def __normalize_points(clues: [db.Clue], bikes: [db.Bike]):
+def __normalize_points(clues: [Clue], bikes: [Bike]):
"""
Takes a list of coordinates and a list of centroids and returns a list of normalized coordinates and a list of
normalized centroids
@@ -254,7 +261,7 @@ def __min_max_normalize(value, min_value, max_value):
return (value - min_value) / (max_value - min_value)
-def __find_closest_coordinate(clues: [db.Clue], centroid: db.Point):
+def __find_closest_coordinate(clues: [Clue], centroid: Point):
"""
Takes a list of coordinates and a centroid and returns the clue in the list that is closest to the centroid
:param clues: the list of coordinates
@@ -273,7 +280,7 @@ def __find_closest_coordinate(clues: [db.Clue], centroid: db.Point):
return closest_coordinate
-def __find_farthest_coordinate(clues: [db.Clue], centroid: db.Point):
+def __find_farthest_coordinate(clues: [Clue], centroid: Point):
"""
Takes a list of coordinates and a centroid and returns the clue in the list that is farthest from the centroid
:param clues: the list of coordinates
@@ -291,17 +298,17 @@ def __find_farthest_coordinate(clues: [db.Clue], centroid: db.Point):
return farthest_coordinate
-def __mean_center(clues: [db.Clue]):
+def __mean_center(clues: [Clue]):
"""
Takes a list of coordinates and returns the mean center of the coordinates
:param clues: the list of coordinates
:return: the mean center of the coordinates
"""
- return db.Point(np.mean([coordinate.latitude for coordinate in clues]),
+ return Point(np.mean([coordinate.latitude for coordinate in clues]),
np.mean([coordinate.longitude for coordinate in clues]))
-def __distance(coordinate1: db.Clue, coordinate2: db.Point):
+def __distance(coordinate1: Clue, coordinate2: Point):
"""
Takes two coordinates and returns the distance between them
:param coordinate1: the first coordinate