diff options
Diffstat (limited to 'dashboard_website/router.py')
| -rw-r--r-- | dashboard_website/router.py | 25 |
1 files changed, 15 insertions, 10 deletions
diff --git a/dashboard_website/router.py b/dashboard_website/router.py index b30dca7..122f767 100644 --- a/dashboard_website/router.py +++ b/dashboard_website/router.py @@ -129,7 +129,8 @@ def cluster_and_optimize( # return -1, [], [], [] # routes[i] = resp clue_pool = [] - routes = __balance_waypoints(routes, bikes, end, times, clue_pool, max_max_time, minimal) + routes_done = [False for bike in bikes if bike.status != "DISABLED"] + routes = __balance_waypoints(routes, bikes, end, times, clue_pool, routes_done, max_max_time, minimal) if routes == -1: return -1, [], [], [] @@ -285,7 +286,7 @@ def __minimize_route_time_diff( return routes, times def __balance_waypoints( - routes: [Clue], bikes: [Bike], end: Point, times, clue_pool: [Clue], max_time, minimal + routes: [Clue], bikes: [Bike], end: Point, times, clue_pool: [Clue], routes_done: [bool], max_time, minimal ): """ Takes a list of coordinates, a start point, an end point, and a maximum time and returns a list of coordinates @@ -303,8 +304,8 @@ def __balance_waypoints( time_balances = [ (max_times[i] - times[i]) for i in range(len(times))] print(time_balances) balanced = True - for balance in time_balances: - if balance < 0: + for i, balance in enumerate(time_balances): + if balance < 0 and (not routes_done[active_indices[i]]): balanced = False break if balanced: @@ -312,12 +313,14 @@ def __balance_waypoints( # shorten too-long routes for i, time_balance in enumerate(time_balances): print(i, time_balance) - if time_balance < 0: # route too long + if time_balance < 0 and (not routes_done[active_indices[i]]): # route too long route_mean = __mean_center(routes[active_indices[i]]) - farthest_coordinates = __find_farthest_coordinates(routes[active_indices[i]], route_mean, max(1,min(int(-1*time_balance/0.12),int(len(routes[active_indices[i]])*0.75)))) + s, farthest_coordinates = __find_farthest_coordinates(routes[active_indices[i]], route_mean, max(1,min(int(-1*time_balance/0.12),int(len(routes[active_indices[i]])*0.5)))) for farthest_coordinate in farthest_coordinates: routes[active_indices[i]].remove(farthest_coordinate) clue_pool.append(farthest_coordinate) + if s == -1: + routes_done[active_indices[i]] = True # lengthen routes with spare room for i, time_balance in enumerate(time_balances): if time_balance > 0.15: # route too short @@ -337,7 +340,7 @@ def __balance_waypoints( __clues_to_string([end])[:-1], ) * bikes[i].time_modifier - return __balance_waypoints(routes, bikes, end, times, clue_pool, max_time, minimal) + return __balance_waypoints(routes, bikes, end, times, clue_pool, routes_done, max_time, minimal) @@ -373,7 +376,7 @@ def __remove_longest_waypoints( if route_time > max_time: print(route_time) route_mean = __mean_center(route_coordinates) - farthest_coordinates = __find_farthest_coordinates(route_coordinates, route_mean) + _, farthest_coordinates = __find_farthest_coordinates(route_coordinates, route_mean) for farthest_coordinate in farthest_coordinates: route_coordinates.remove(farthest_coordinate) @@ -473,13 +476,15 @@ def __find_farthest_coordinates(clues: [Clue], centroid: Point, n:int=1): :param centroid: the centroid :return: the clue in the list that is farthest from the centroid """ - + print(len(clues),n) farthest_coordinates = [] for _ in range(min(len(clues),n)): farthest_coordinate = clues[0] # remove only unrequired clues i = 1 while farthest_coordinate.required or (farthest_coordinate in farthest_coordinates): + if i >= len(clues): + return -1, farthest_coordinates farthest_coordinate = clues[i] i += 1 @@ -492,7 +497,7 @@ def __find_farthest_coordinates(clues: [Clue], centroid: Point, n:int=1): farthest_coordinates.append(farthest_coordinate) - return farthest_coordinates + return 0, farthest_coordinates def __mean_center(clues: [Clue]): |
