summaryrefslogtreecommitdiff
path: root/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'utils.py')
-rw-r--r--utils.py72
1 files changed, 50 insertions, 22 deletions
diff --git a/utils.py b/utils.py
index c37c64a..9e632a2 100644
--- a/utils.py
+++ b/utils.py
@@ -16,18 +16,20 @@ def cluster_and_optimize(df, centroids, end, time_diff=0.25, max_time=24, n=2):
:return: a dataframe with a cluster column
"""
# Create a new column with normalized gps coordinates and centroids
- df['normalized_gps'], norm_centroids = __normalize_gps(df['gps'].values.tolist(), centroids)
+ df["normalized_gps"], norm_centroids = __normalize_gps(
+ df["gps"].values.tolist(), centroids
+ )
# Cluster the coordinates
kmeans = KMeans(n_clusters=len(norm_centroids), init=norm_centroids)
- kmeans.fit(df['normalized_gps'].values.tolist())
+ kmeans.fit(df["normalized_gps"].values.tolist())
- df['cluster'] = kmeans.labels_
+ df["cluster"] = kmeans.labels_
routes = []
starts = []
for i in range(len(centroids)):
- routes.append(df[df['cluster'] == i]['gps'].values.tolist())
+ routes.append(df[df["cluster"] == i]["gps"].values.tolist())
starts.append(list_to_string([centroids[i]]))
routes = __minimize_route_time_diff(routes, starts, end, time_diff, n)
@@ -35,7 +37,7 @@ def cluster_and_optimize(df, centroids, end, time_diff=0.25, max_time=24, n=2):
# 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], starts[i], end, max_time)
- df.loc[df['gps'].astype(str).isin(map(str, routes[i])), 'cluster'] = i
+ df.loc[df["gps"].astype(str).isin(map(str, routes[i])), "cluster"] = i
return df, routes
@@ -46,9 +48,9 @@ def list_to_string(list_of_lists):
:param list_of_lists: a list of lists
:return: a string of the list of lists
"""
- string = ''
+ string = ""
for i in list_of_lists:
- string += str(i[1]) + ',' + str(i[0]) + ';'
+ string += str(i[1]) + "," + str(i[0]) + ";"
return string
@@ -62,20 +64,25 @@ def create_json_df(coordinate_string, start, end):
:return: a dataframe of the coordinates in order of the waypoint index
"""
coordinates = requests.get(
- 'http://acetyl.net:5000/trip/v1/bike/' + start + coordinate_string + end + '?roundtrip=false&source=first&destination=last')
+ "http://acetyl.net:5000/trip/v1/bike/"
+ + start
+ + coordinate_string
+ + end
+ + "?roundtrip=false&source=first&destination=last"
+ )
coordinates = coordinates.json()
# Create a dataframe from the JSON
- df = pd.DataFrame(coordinates['waypoints'])
+ df = pd.DataFrame(coordinates["waypoints"])
# Separate the location column into lon and lat columns
- df['lat'] = df['location'].apply(lambda x: x[0])
- df['lon'] = df['location'].apply(lambda x: x[1])
+ df["lat"] = df["location"].apply(lambda x: x[0])
+ df["lon"] = df["location"].apply(lambda x: x[1])
- df['waypoint_index'] = df['waypoint_index'].astype(int)
+ df["waypoint_index"] = df["waypoint_index"].astype(int)
# Map out the waypoints in order of the waypoint index
- df = df.sort_values(by=['waypoint_index'])
+ df = df.sort_values(by=["waypoint_index"])
return df
@@ -91,10 +98,15 @@ def get_trip_time(coordinate_string, num_waypoints, start, end, time_per_waypoin
:return: the trip time in hours
"""
coordinates = requests.get(
- 'http://acetyl.net:5000/trip/v1/bike/' + start + coordinate_string + end + '?roundtrip=false&source=first&destination=last')
+ "http://acetyl.net:5000/trip/v1/bike/"
+ + start
+ + coordinate_string
+ + end
+ + "?roundtrip=false&source=first&destination=last"
+ )
coordinates = coordinates.json()
- travel_time_seconds = int(coordinates['trips'][0]['duration'])
+ travel_time_seconds = int(coordinates["trips"][0]["duration"])
waypoint_time_seconds = num_waypoints * time_per_waypoint
total_time_hours = (travel_time_seconds + waypoint_time_seconds) / 3600
@@ -129,8 +141,9 @@ def __minimize_route_time_diff(routes, starts, end, time_diff, n):
# 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]]))
+ closest_coordinate = __find_closest_coordinate(
+ routes[sorted_indices[-1]], __mean_center(routes[sorted_indices[0]])
+ )
routes[sorted_indices[0]].append(closest_coordinate)
routes[sorted_indices[-1]].remove(closest_coordinate)
@@ -151,7 +164,9 @@ def __remove_longest_waypoints(route_coordinates, start, end, max_time):
:return: a list of coordinates
"""
# Find the trip time for the route
- route_time = get_trip_time(list_to_string(route_coordinates), len(route_coordinates), start, end)
+ route_time = get_trip_time(
+ list_to_string(route_coordinates), len(route_coordinates), start, end
+ )
# 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:
@@ -189,10 +204,18 @@ def __normalize_gps(coordinates, centroids):
for i in coordinates:
normalized_coordinates.append(
- [__min_max_normalize(i[0], min_lat, max_lat), __min_max_normalize(i[1], min_lon, max_lon)])
+ [
+ __min_max_normalize(i[0], min_lat, max_lat),
+ __min_max_normalize(i[1], min_lon, max_lon),
+ ]
+ )
for i in centroids:
normalized_centroids.append(
- [__min_max_normalize(i[0], min_lat, max_lat), __min_max_normalize(i[1], min_lon, max_lon)])
+ [
+ __min_max_normalize(i[0], min_lat, max_lat),
+ __min_max_normalize(i[1], min_lon, max_lon),
+ ]
+ )
return normalized_coordinates, normalized_centroids
@@ -250,7 +273,10 @@ def __mean_center(coordinates):
:param coordinates: the list of coordinates
:return: the mean center of the coordinates
"""
- return [sum([i[0] for i in coordinates]) / len(coordinates), sum([i[1] for i in coordinates]) / len(coordinates)]
+ return [
+ sum([i[0] for i in coordinates]) / len(coordinates),
+ sum([i[1] for i in coordinates]) / len(coordinates),
+ ]
def __distance(coordinate1, coordinate2):
@@ -260,4 +286,6 @@ def __distance(coordinate1, coordinate2):
:param coordinate2: the second coordinate
:return: the distance between the two coordinates
"""
- return ((coordinate1[0] - coordinate2[0]) ** 2 + (coordinate1[1] - coordinate2[1]) ** 2) ** 0.5
+ return (
+ (coordinate1[0] - coordinate2[0]) ** 2 + (coordinate1[1] - coordinate2[1]) ** 2
+ ) ** 0.5