summaryrefslogtreecommitdiff
path: root/dashboard_website/router.py
diff options
context:
space:
mode:
Diffstat (limited to 'dashboard_website/router.py')
-rw-r--r--dashboard_website/router.py32
1 files changed, 21 insertions, 11 deletions
diff --git a/dashboard_website/router.py b/dashboard_website/router.py
index 4840c8d..70af4c6 100644
--- a/dashboard_website/router.py
+++ b/dashboard_website/router.py
@@ -72,7 +72,7 @@ def getClusters(bikes, clues, endpoint, minimal=True):
# utility functions (internal)
def cluster_and_optimize(
- clues: [Clue], bikes: [Bike], end: Point, minimal : bool, time_diff=0.25, max_time=24
+ clues: [Clue], bikes: [Bike], end: Point, minimal : bool, time_diff=0.25
):
"""
Takes a dataframe of gps coordinates, a list of centroids, and an end point and returns a dataframe with a cluster
@@ -86,8 +86,8 @@ def cluster_and_optimize(
"""
status = 0
# OVERRIDE MAX TIME
- max_time = datetime.datetime.now() - endtime
- max_time = max_time.seconds / 3600
+ max_max_time = endtime - datetime.datetime.now()
+ max_max_time = max_max_time.seconds / 3600
# Create a new column with normalized gps coordinates and centroids
active_indices = [i for i in range(len(bikes)) if bikes[i].status != "DISABLED"]
@@ -124,13 +124,17 @@ def cluster_and_optimize(
# Remove waypoints from the longest route until the trip time is less than the max time
for i in range(len(routes)):
- routes[i] = __remove_longest_waypoints(routes[i], bikes[i], end, max_time)
-
+ resp = __remove_longest_waypoints(routes[i], bikes[i], end, max(bikes[i].timeTilDeadline()/3600, max_max_time))
+ if resp == -1:
+ return -1, [], [], []
+ routes[i] = resp
+
# Get the json of the routes
route_waypoints = []
geometries = []
times = []
for i, route in enumerate(routes):
+ if db.should_cancel_routing(): return -1, [], [], []
route_json = __get_json(
__clues_to_string(route),
__clues_to_string([bikes[i].target_clue if (minimal and (bikes[i].target_clue != None)) else bikes[i]]),
@@ -257,15 +261,16 @@ def __minimize_route_time_diff(
# Find the difference between the longest trip time and the average trip time
time_difference = times[sorted_indices[-1]] - average_time
-
+ print(times)
# If the difference is greater than the time difference, move a coordinate from the longest route to the shortest route
if time_difference > time_diff:
# Move a coordinate from the longest route to the shortest route
- closest_coordinate = __find_closest_coordinate(
- routes[sorted_indices[-1]], __mean_center(routes[sorted_indices[0]])
- )
- routes[active_indices[sorted_indices[0]]].append(closest_coordinate)
- routes[active_indices[sorted_indices[-1]]].remove(closest_coordinate)
+ for _ in range(max(1,int(time_difference/0.12))): # roughly estimate how many points must be moved to equalize
+ closest_coordinate = __find_closest_coordinate(
+ routes[active_indices[sorted_indices[-1]]], __mean_center(routes[sorted_indices[0]])
+ )
+ routes[active_indices[sorted_indices[0]]].append(closest_coordinate)
+ routes[active_indices[sorted_indices[-1]]].remove(closest_coordinate)
# Recursively minimize the time difference between the routes
return __minimize_route_time_diff(routes, bikes, end, minimal, time_diff)
@@ -288,6 +293,10 @@ def __remove_longest_waypoints(
if len(route_coordinates) < 1:
return []
+ if db.should_cancel_routing(): return -1
+
+ print(max_time)
+
if start.status != "DISABLED" and start.target_clue != None: # already assigned a clue
start = start.target_clue
# Find the trip time for the route
@@ -300,6 +309,7 @@ def __remove_longest_waypoints(
# If the trip time is greater than the max time, remove the waypoint with the longest distance from the mean
if route_time > max_time:
+ print(route_time)
route_mean = __mean_center(route_coordinates)
farthest_coordinates = __find_farthest_coordinates(route_coordinates, route_mean)
for farthest_coordinate in farthest_coordinates: