summaryrefslogtreecommitdiff
path: root/utils.py
diff options
context:
space:
mode:
authoritsGarrin <garrin.shieh@gmail.com>2024-11-05 15:41:24 -0500
committeritsGarrin <garrin.shieh@gmail.com>2024-11-05 15:41:24 -0500
commitfeb047071ab7f7a688d2f3925d07d757b4b37d7d (patch)
treef3a9d00e6ea60de7e98e945cd083cd54a7cdba5e /utils.py
parent1cbe6a267628509c24d32b458363ddb74cb82838 (diff)
hi
Diffstat (limited to 'utils.py')
-rw-r--r--utils.py23
1 files changed, 14 insertions, 9 deletions
diff --git a/utils.py b/utils.py
index 9e632a2..62ccbe5 100644
--- a/utils.py
+++ b/utils.py
@@ -4,7 +4,7 @@ import requests
from sklearn.cluster import KMeans
-def cluster_and_optimize(df, centroids, end, time_diff=0.25, max_time=24, n=2):
+def cluster_and_optimize(df, centroids, end, time_diff=0.25, max_time=24, n=2, host="http://acetyl.net:5000"):
"""
Takes a dataframe of gps coordinates, a list of centroids, and an end point and returns a dataframe with a cluster
:param df: a dataframe of gps coordinates
@@ -22,7 +22,7 @@ def cluster_and_optimize(df, centroids, end, time_diff=0.25, max_time=24, n=2):
# Cluster the coordinates
kmeans = KMeans(n_clusters=len(norm_centroids), init=norm_centroids)
- kmeans.fit(df["normalized_gps"].values.tolist())
+ kmeans.fit_predict(df["normalized_gps"].values.tolist())
df["cluster"] = kmeans.labels_
@@ -32,7 +32,7 @@ def cluster_and_optimize(df, centroids, end, time_diff=0.25, max_time=24, n=2):
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)
+ routes = __minimize_route_time_diff(routes, starts, end, time_diff, n, host=host)
# Remove waypoints from the longest route until the trip time is less than the max time
for i in range(len(routes)):
@@ -55,16 +55,18 @@ def list_to_string(list_of_lists):
return string
-def create_json_df(coordinate_string, start, end):
+def create_json_df(coordinate_string, start, end, host="http://acetyl.net:5000"):
"""
Takes a string of coordinates and returns a dataframe of the coordinates in order of the waypoint index
:param coordinate_string: a string of coordinates
:param start: the start point of the trip
:param end: the end point of the trip
+ :param host: the host of the API
:return: a dataframe of the coordinates in order of the waypoint index
"""
coordinates = requests.get(
- "http://acetyl.net:5000/trip/v1/bike/"
+ host
+ + "/trip/v1/bike/"
+ start
+ coordinate_string
+ end
@@ -87,7 +89,7 @@ def create_json_df(coordinate_string, start, end):
return df
-def get_trip_time(coordinate_string, num_waypoints, start, end, time_per_waypoint=90):
+def get_trip_time(coordinate_string, num_waypoints, start, end, time_per_waypoint=90, host="http://acetyl.net:5000"):
"""
Takes a string of coordinates and returns the trip time in hours
:param coordinate_string: a string of coordinates
@@ -95,16 +97,19 @@ def get_trip_time(coordinate_string, num_waypoints, start, end, time_per_waypoin
:param start: the start point of the trip
:param end: the end point of the trip
:param time_per_waypoint: the time per waypoint in seconds
+ :param host: the host of the API
:return: the trip time in hours
"""
coordinates = requests.get(
- "http://acetyl.net:5000/trip/v1/bike/"
+ host
+ + "/trip/v1/bike/"
+ start
+ coordinate_string
+ end
+ "?roundtrip=false&source=first&destination=last"
)
coordinates = coordinates.json()
+ print(coordinates)
travel_time_seconds = int(coordinates["trips"][0]["duration"])
waypoint_time_seconds = num_waypoints * time_per_waypoint
@@ -114,7 +119,7 @@ def get_trip_time(coordinate_string, num_waypoints, start, end, time_per_waypoin
return total_time_hours
-def __minimize_route_time_diff(routes, starts, end, time_diff, n):
+def __minimize_route_time_diff(routes, starts, end, time_diff, n, host="http://acetyl.net:5000"):
"""
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
@@ -127,7 +132,7 @@ def __minimize_route_time_diff(routes, starts, end, time_diff, n):
times = []
for i, route in enumerate(routes):
- times.append(get_trip_time(list_to_string(route), len(route), starts[i], end))
+ times.append(get_trip_time(list_to_string(route), len(route), starts[i], end, host=host))
# Find the average trip time
average_time = np.mean(times)