From 8dad8eaf2d1a9992e2779053f306f6d0736886bf Mon Sep 17 00:00:00 2001 From: itsGarrin Date: Tue, 7 Nov 2023 10:40:06 -0500 Subject: Added a file without the use of route time difference minimization for reference --- ZestySalesmanNoMinimize.ipynb | 706 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 706 insertions(+) create mode 100644 ZestySalesmanNoMinimize.ipynb (limited to 'ZestySalesmanNoMinimize.ipynb') diff --git a/ZestySalesmanNoMinimize.ipynb b/ZestySalesmanNoMinimize.ipynb new file mode 100644 index 0000000..9632a4a --- /dev/null +++ b/ZestySalesmanNoMinimize.ipynb @@ -0,0 +1,706 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "initial_id", + "metadata": { + "collapsed": true, + "ExecuteTime": { + "end_time": "2023-11-07T15:38:40.404048Z", + "start_time": "2023-11-07T15:38:38.873539Z" + } + }, + "outputs": [], + "source": [ + "import pandas as pd\n", + "import folium\n", + "import utils" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "outputs": [], + "source": [ + "# Load the data\n", + "ListA = pd.read_csv('List A.csv')\n", + "ListB = pd.read_csv('List B.csv')\n", + "ListC = pd.read_csv('List C.csv')\n", + "ListD = pd.read_csv('List D.csv')" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2023-11-07T15:38:40.423960Z", + "start_time": "2023-11-07T15:38:40.404555Z" + } + }, + "id": "73b780e762c9de37" + }, + { + "cell_type": "code", + "execution_count": 3, + "outputs": [], + "source": [ + "# Create three centroids, one in the North End, one in the Financial District, and one in the Back Bay\n", + "centroids = [[42.364506, -71.054733], [42.358894, -71.056742], [42.3505, -71.0760]]\n", + "\n", + "northeastern_coordinate = \"-71.09033,42.33976\"" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2023-11-07T15:38:40.424218Z", + "start_time": "2023-11-07T15:38:40.420196Z" + } + }, + "id": "be4c8c1d77842ef7" + }, + { + "cell_type": "code", + "execution_count": 4, + "outputs": [], + "source": [ + "# Combine the two lists and add a column to indicate the list\n", + "ListA['list'] = 'A'\n", + "ListB['list'] = 'B'\n", + "ListC['list'] = 'C'\n", + "ListD['list'] = 'D'\n", + "\n", + "TotalList = pd.concat([ListA, ListB, ListC, ListD])" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2023-11-07T15:38:40.442314Z", + "start_time": "2023-11-07T15:38:40.425419Z" + } + }, + "id": "ffe4025e97a6c6b9" + }, + { + "cell_type": "code", + "execution_count": 5, + "outputs": [], + "source": [ + "# Remove all columns but name and gps\n", + "TotalList = TotalList[['name', 'gps', 'list']]" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2023-11-07T15:38:40.442798Z", + "start_time": "2023-11-07T15:38:40.430053Z" + } + }, + "id": "72657779b4484aae" + }, + { + "cell_type": "code", + "execution_count": 6, + "outputs": [], + "source": [ + "# Convert the gps column to a list of lists for k-means\n", + "TotalList['gps'] = TotalList['gps'].apply(lambda x: x.strip('[]').split(','))\n", + "TotalList['gps'] = TotalList['gps'].apply(lambda x: [float(i) for i in x])" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2023-11-07T15:38:40.442952Z", + "start_time": "2023-11-07T15:38:40.432847Z" + } + }, + "id": "a157ffaec020a29a" + }, + { + "cell_type": "code", + "execution_count": 7, + "outputs": [ + { + "data": { + "text/plain": " name gps list \\\n0 521 Commercial Street #525 [42.3688272, -71.0553792] A \n1 Acorn St [42.3576234, -71.0688746] A \n2 Arlington's Great Meadows [42.4299758, -71.2038948] A \n3 Arthur Fiedler Statue [42.3565057, -71.0754527] A \n4 BU Beach [42.3511927, -71.1060828] A \n.. ... ... ... \n33 The Quiet Few [42.3670906, -71.0359889] D \n34 The Tall Ship Boston [42.3649544, -71.0414523] D \n35 Toasted Flats [42.3711266, -71.0371343] D \n36 Vega Market [42.3891835, -71.033703] D \n37 Winthrop High School [42.3803348, -70.9799864] D \n\n normalized_gps \n0 [0.7251058917247415, 0.7797482353989729] \n1 [0.6747391031099019, 0.7451825969538083] \n2 [1.0, 0.3993566550776867] \n3 [0.6697144722136962, 0.7283341725828262] \n4 [0.6458298305822171, 0.6498815915448888] \n.. ... \n33 [0.717298990038831, 0.8294124246148072] \n34 [0.7076956827824702, 0.8154190706511427] \n35 [0.7354428661210094, 0.8264787225922622] \n36 [0.8166178304491644, 0.8352672783369615] \n37 [0.7768384161061446, 0.972851090162032] \n\n[169 rows x 4 columns]", + "text/html": "
\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
namegpslistnormalized_gps
0521 Commercial Street #525[42.3688272, -71.0553792]A[0.7251058917247415, 0.7797482353989729]
1Acorn St[42.3576234, -71.0688746]A[0.6747391031099019, 0.7451825969538083]
2Arlington's Great Meadows[42.4299758, -71.2038948]A[1.0, 0.3993566550776867]
3Arthur Fiedler Statue[42.3565057, -71.0754527]A[0.6697144722136962, 0.7283341725828262]
4BU Beach[42.3511927, -71.1060828]A[0.6458298305822171, 0.6498815915448888]
...............
33The Quiet Few[42.3670906, -71.0359889]D[0.717298990038831, 0.8294124246148072]
34The Tall Ship Boston[42.3649544, -71.0414523]D[0.7076956827824702, 0.8154190706511427]
35Toasted Flats[42.3711266, -71.0371343]D[0.7354428661210094, 0.8264787225922622]
36Vega Market[42.3891835, -71.033703]D[0.8166178304491644, 0.8352672783369615]
37Winthrop High School[42.3803348, -70.9799864]D[0.7768384161061446, 0.972851090162032]
\n

169 rows × 4 columns

\n
" + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# Create a new column with normalized gps coordinates and centroids\n", + "TotalList['normalized_gps'], norm_centroids = utils.normalize_gps(TotalList['gps'].values.tolist(), centroids)\n", + "display(TotalList)" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2023-11-07T15:38:40.443962Z", + "start_time": "2023-11-07T15:38:40.436165Z" + } + }, + "id": "a03ebde91b87fa3b" + }, + { + "cell_type": "markdown", + "source": [ + "# 2 Routes" + ], + "metadata": { + "collapsed": false + }, + "id": "4bd41be9aca5094b" + }, + { + "cell_type": "markdown", + "source": [ + "## Cluster and Minimize" + ], + "metadata": { + "collapsed": false + }, + "id": "90d1d2f1a931597f" + }, + { + "cell_type": "code", + "execution_count": 8, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/garrinshieh/anaconda3/lib/python3.11/site-packages/sklearn/cluster/_kmeans.py:1412: FutureWarning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress the warning\n", + " super()._check_params_vs_input(X, default_n_init=10)\n", + "/Users/garrinshieh/anaconda3/lib/python3.11/site-packages/sklearn/cluster/_kmeans.py:1412: RuntimeWarning: Explicit initial center position passed: performing only one init in KMeans instead of n_init=10.\n", + " super()._check_params_vs_input(X, default_n_init=10)\n" + ] + } + ], + "source": [ + "# Cluster and minimize the data\n", + "norm_centroids_2 = norm_centroids[:2]\n", + "_, route_1_coordinates, route_2_coordinates = utils.cluster_and_minimize_2(TotalList, centroids, norm_centroids_2,\n", + " northeastern_coordinate, 0.5, minimize=False)" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2023-11-07T15:38:43.183771Z", + "start_time": "2023-11-07T15:38:40.443836Z" + } + }, + "id": "ee9b3c1ecb360976" + }, + { + "cell_type": "markdown", + "source": [ + "## Create JSON" + ], + "metadata": { + "collapsed": false + }, + "id": "c85b8ef869e35006" + }, + { + "cell_type": "code", + "execution_count": 9, + "outputs": [], + "source": [ + "# Create a JSON request for the API\n", + "# This is the data we want to get from the API\n", + "route_1 = utils.list_to_string(route_1_coordinates)\n", + "route_2 = utils.list_to_string(route_2_coordinates)" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2023-11-07T15:38:43.192691Z", + "start_time": "2023-11-07T15:38:43.184382Z" + } + }, + "id": "aa618161182b5b07" + }, + { + "cell_type": "code", + "execution_count": 10, + "outputs": [], + "source": [ + "# Create a dataframe from the JSON\n", + "df1 = utils.create_json_df(route_1, utils.list_to_string([centroids[0]]), northeastern_coordinate)\n", + "df2 = utils.create_json_df(route_2, utils.list_to_string([centroids[1]]), northeastern_coordinate)" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2023-11-07T15:38:45.665442Z", + "start_time": "2023-11-07T15:38:43.186783Z" + } + }, + "id": "32c485788eedd94" + }, + { + "cell_type": "code", + "execution_count": 11, + "outputs": [], + "source": [ + "# Add columns for the route number\n", + "df1['route'] = 1\n", + "df2['route'] = 2\n", + "\n", + "# Concatenate the two dataframes\n", + "df = pd.concat([df1, df2], ignore_index=True)" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2023-11-07T15:38:45.678738Z", + "start_time": "2023-11-07T15:38:45.666570Z" + } + }, + "id": "49dba1f17ca8337e" + }, + { + "cell_type": "code", + "execution_count": 12, + "outputs": [ + { + "data": { + "text/plain": " waypoint_index trips_index \\\n0 0 0 \n1 1 0 \n2 2 0 \n3 3 0 \n4 4 0 \n.. ... ... \n168 84 0 \n169 85 0 \n170 86 0 \n171 87 0 \n172 88 0 \n\n hint distance \\\n0 t4YsgAGHLIAAAAAAVQEAAAAAAAAwAAAAAAAAAHV0F0IAAA... 19.432511 \n1 IzYEgGw1BIASAAAArwAAADMAAACUAwAAynkIQGUkmkEXlL... 6.024489 \n2 5IgsgAqJLID7AAAAHgAAAAwAAAAIAAAAz5ffQcMBVEDFYK... 5.871835 \n3 G4gsgDiILICSAwAA5gAAAOkAAAAAAAAAQljLQnyXy0Fhy8... 2.602121 \n4 gIosgLaKLIDOAAAArgAAAFwBAAAAAAAAp3O3QafxmUEQiR... 15.458439 \n.. ... ... \n168 7hAigPYQIoA2AgAAYwEAAAAAAAAAAAAAnsd7Qq9XHUIAAA... 7.478611 \n169 bwwigH0MIoAFAAAAEAAAAFUAAAArAAAAag0xP3921D-BFx... 8.340476 \n170 MQwigFwMIoAoAAAANQAAABwAAAB-AAAAoidqQSAYl0GvUh... 11.504463 \n171 k4chgBiIIYAKAAAAFwAAAPQDAAB_AgAAHn2aP-biHUBi6e... 36.240351 \n172 DoUhgBeFIYCcAAAAJgAAAAAAAAARAAAAm0CKQdkZiEAAAA... 0.236958 \n\n name location lat lon \\\n0 [-71.054865, 42.364361] -71.054865 42.364361 \n1 [-71.055569, 42.364032] -71.055569 42.364032 \n2 [-71.055582, 42.365251] -71.055582 42.365251 \n3 [-71.056164, 42.366918] -71.056164 42.366918 \n4 [-71.055561, 42.368861] -71.055561 42.368861 \n.. ... ... ... ... \n168 [-71.096959, 42.344689] -71.096959 42.344689 \n169 [-71.095003, 42.342001] -71.095003 42.342001 \n170 [-71.094327, 42.341231] -71.094327 42.341231 \n171 [-71.093834, 42.339096] -71.093834 42.339096 \n172 Northeastern (Inbound) [-71.090331, 42.339762] -71.090331 42.339762 \n\n route \n0 1 \n1 1 \n2 1 \n3 1 \n4 1 \n.. ... \n168 2 \n169 2 \n170 2 \n171 2 \n172 2 \n\n[173 rows x 9 columns]", + "text/html": "
\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
waypoint_indextrips_indexhintdistancenamelocationlatlonroute
000t4YsgAGHLIAAAAAAVQEAAAAAAAAwAAAAAAAAAHV0F0IAAA...19.432511[-71.054865, 42.364361]-71.05486542.3643611
110IzYEgGw1BIASAAAArwAAADMAAACUAwAAynkIQGUkmkEXlL...6.024489[-71.055569, 42.364032]-71.05556942.3640321
2205IgsgAqJLID7AAAAHgAAAAwAAAAIAAAAz5ffQcMBVEDFYK...5.871835[-71.055582, 42.365251]-71.05558242.3652511
330G4gsgDiILICSAwAA5gAAAOkAAAAAAAAAQljLQnyXy0Fhy8...2.602121[-71.056164, 42.366918]-71.05616442.3669181
440gIosgLaKLIDOAAAArgAAAFwBAAAAAAAAp3O3QafxmUEQiR...15.458439[-71.055561, 42.368861]-71.05556142.3688611
..............................
1688407hAigPYQIoA2AgAAYwEAAAAAAAAAAAAAnsd7Qq9XHUIAAA...7.478611[-71.096959, 42.344689]-71.09695942.3446892
169850bwwigH0MIoAFAAAAEAAAAFUAAAArAAAAag0xP3921D-BFx...8.340476[-71.095003, 42.342001]-71.09500342.3420012
170860MQwigFwMIoAoAAAANQAAABwAAAB-AAAAoidqQSAYl0GvUh...11.504463[-71.094327, 42.341231]-71.09432742.3412312
171870k4chgBiIIYAKAAAAFwAAAPQDAAB_AgAAHn2aP-biHUBi6e...36.240351[-71.093834, 42.339096]-71.09383442.3390962
172880DoUhgBeFIYCcAAAAJgAAAAAAAAARAAAAm0CKQdkZiEAAAA...0.236958Northeastern (Inbound)[-71.090331, 42.339762]-71.09033142.3397622
\n

173 rows × 9 columns

\n
" + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "display(df)" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2023-11-07T15:38:45.679592Z", + "start_time": "2023-11-07T15:38:45.669550Z" + } + }, + "id": "f231d9a35358988c" + }, + { + "cell_type": "markdown", + "source": [ + "## Map" + ], + "metadata": { + "collapsed": false + }, + "id": "75be92e34a36147f" + }, + { + "cell_type": "code", + "execution_count": 13, + "outputs": [ + { + "data": { + "text/plain": "", + "text/html": "
Make this Notebook Trusted to load map: File -> Trust Notebook
" + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Create a map\n", + "m = folium.Map(location=[df['lon'].mean(), df['lat'].mean()], zoom_start=11)\n", + "\n", + "# Add the points and lines for the two routes with different colors\n", + "colors = ['red', 'blue']\n", + "\n", + "for route in df['route'].unique():\n", + " df_route = df[df['route'] == route]\n", + " folium.PolyLine(df_route[['lon', 'lat']].values.tolist(), color=colors[route - 1]).add_to(m)\n", + " for i in range(len(df_route)):\n", + " folium.CircleMarker(df_route[['lon', 'lat']].iloc[i].values.tolist(), radius=3, color=colors[route - 1]).add_to(\n", + " m)\n", + "\n", + "# Display the map\n", + "m" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2023-11-07T15:38:45.781790Z", + "start_time": "2023-11-07T15:38:45.677705Z" + } + }, + "id": "80fd847da2833913" + }, + { + "cell_type": "markdown", + "source": [ + "## Results" + ], + "metadata": { + "collapsed": false + }, + "id": "a7b562f75f7e0813" + }, + { + "cell_type": "code", + "execution_count": 14, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Route 1 has 82 waypoints\n", + "Route 2 has 87 waypoints\n" + ] + } + ], + "source": [ + "# Get the number of waypoints for each route\n", + "route_1_waypoints = len(route_1_coordinates)\n", + "route_2_waypoints = len(route_2_coordinates)\n", + "print(\"Route 1 has {} waypoints\".format(route_1_waypoints))\n", + "print(\"Route 2 has {} waypoints\".format(route_2_waypoints))" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2023-11-07T15:38:45.782104Z", + "start_time": "2023-11-07T15:38:45.736109Z" + } + }, + "id": "f53c97acec1c2fc4" + }, + { + "cell_type": "code", + "execution_count": 15, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The trip will take 11.154166666666667 hours\n", + "The trip will take 14.8425 hours\n" + ] + } + ], + "source": [ + "trip_hrs_1 = utils.get_trip_time(route_1, route_1_waypoints, utils.list_to_string([centroids[0]]),\n", + " northeastern_coordinate)\n", + "print(\"The trip will take {} hours\".format(trip_hrs_1))\n", + "trip_hrs_2 = utils.get_trip_time(route_2, route_2_waypoints, utils.list_to_string([centroids[1]]),\n", + " northeastern_coordinate)\n", + "print(\"The trip will take {} hours\".format(trip_hrs_2))" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2023-11-07T15:38:48.049877Z", + "start_time": "2023-11-07T15:38:45.738475Z" + } + }, + "id": "a3ec09dfb5cbb5b3" + }, + { + "cell_type": "markdown", + "source": [ + "# 3 Routes" + ], + "metadata": { + "collapsed": false + }, + "id": "de7b5856172d213c" + }, + { + "cell_type": "code", + "execution_count": 16, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/garrinshieh/anaconda3/lib/python3.11/site-packages/sklearn/cluster/_kmeans.py:1412: FutureWarning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress the warning\n", + " super()._check_params_vs_input(X, default_n_init=10)\n", + "/Users/garrinshieh/anaconda3/lib/python3.11/site-packages/sklearn/cluster/_kmeans.py:1412: RuntimeWarning: Explicit initial center position passed: performing only one init in KMeans instead of n_init=10.\n", + " super()._check_params_vs_input(X, default_n_init=10)\n" + ] + } + ], + "source": [ + "# Cluster and minimize the data\n", + "_, route_1_coordinates, route_2_coordinates, route_3_coordinates = utils.cluster_and_minimize_3(TotalList, centroids,\n", + " norm_centroids,\n", + " northeastern_coordinate,\n", + " 0.2, minimize=False)" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2023-11-07T15:38:50.390169Z", + "start_time": "2023-11-07T15:38:48.048739Z" + } + }, + "id": "bb6e00857e8175c0" + }, + { + "cell_type": "markdown", + "source": [ + "## Create JSON" + ], + "metadata": { + "collapsed": false + }, + "id": "19afb4f687b37383" + }, + { + "cell_type": "code", + "execution_count": 17, + "outputs": [], + "source": [ + "# Create a JSON request for the API\n", + "# This is the data we want to get from the API\n", + "route_1 = utils.list_to_string(route_1_coordinates)\n", + "route_2 = utils.list_to_string(route_2_coordinates)\n", + "route_3 = utils.list_to_string(route_3_coordinates)" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2023-11-07T15:38:50.404500Z", + "start_time": "2023-11-07T15:38:50.392611Z" + } + }, + "id": "e886e061f86a2118" + }, + { + "cell_type": "code", + "execution_count": 18, + "outputs": [], + "source": [ + "# Create a dataframe from the JSON\n", + "df1 = utils.create_json_df(route_1, utils.list_to_string([centroids[0]]), northeastern_coordinate)\n", + "df2 = utils.create_json_df(route_2, utils.list_to_string([centroids[1]]), northeastern_coordinate)\n", + "df3 = utils.create_json_df(route_3, utils.list_to_string([centroids[2]]), northeastern_coordinate)" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2023-11-07T15:38:52.713204Z", + "start_time": "2023-11-07T15:38:50.400202Z" + } + }, + "id": "23e4682fe9e30631" + }, + { + "cell_type": "code", + "execution_count": 19, + "outputs": [], + "source": [ + "# Add columns for the route number\n", + "df1['route'] = 1\n", + "df2['route'] = 2\n", + "df3['route'] = 3\n", + "\n", + "# Concatenate the three dataframes\n", + "df = pd.concat([df1, df2, df3], ignore_index=True)" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2023-11-07T15:38:52.724854Z", + "start_time": "2023-11-07T15:38:52.718223Z" + } + }, + "id": "c3a5c5d6f3ac46c0" + }, + { + "cell_type": "code", + "execution_count": 20, + "outputs": [ + { + "data": { + "text/plain": " waypoint_index trips_index \\\n0 0 0 \n1 1 0 \n2 2 0 \n3 3 0 \n4 4 0 \n.. ... ... \n170 39 0 \n171 40 0 \n172 41 0 \n173 42 0 \n174 43 0 \n\n hint distance \\\n0 t4YsgAGHLIAAAAAAVQEAAAAAAAAwAAAAAAAAAHV0F0IAAA... 19.432511 \n1 e1kugJlZLoBmAAAA6QAAAAAAAAAAAAAAZ6M2QSewzkEAAA... 4.756158 \n2 tFkugHVaLoAOAAAAAAAAABgAAAAAAAAAwMG2QAAAAAB6ii... 4.525535 \n3 sJAugLOQLoBuAQAAlAEAAAAAAAAAAAAAHFcjQvEZM0IAAA... 7.844897 \n4 VREtgNlJBIBCAAAAYAAAAAAAAAARAAAAOOzeQU7vHkIAAA... 22.681980 \n.. ... ... \n170 cX8hgJF_IYA1AAAAMAAAAGcAAABOAAAATyWxQQ77nUEHMC... 22.776295 \n171 s9QhgLbUIYAwAAAAkAAAAAAAAAAAAAAA2XmpQNgrgEEAAA... 4.111715 \n172 5tYhgJHXIYAIAAAArQAAADwAAABCAQAAaRlbQD16mUGpAc... 17.374491 \n173 k4chgBiIIYAKAAAAFwAAAPQDAAB_AgAAHn2aP-biHUBi6e... 36.240351 \n174 DoUhgBeFIYCcAAAAJgAAAAAAAAARAAAAm0CKQdkZiEAAAA... 0.236958 \n\n name location lat lon \\\n0 [-71.054865, 42.364361] -71.054865 42.364361 \n1 [-71.060933, 42.376178] -71.060933 42.376178 \n2 [-71.060753, 42.376391] -71.060753 42.376391 \n3 [-71.060948, 42.380436] -71.060948 42.380436 \n4 Factory Street [-71.061206, 42.398809] -71.061206 42.398809 \n.. ... ... ... ... \n170 Alleghany Street [-71.099348, 42.33047] -71.099348 42.330470 \n171 [-71.09454, 42.325354] -71.094540 42.325354 \n172 Dudley Street [-71.090904, 42.329829] -71.090904 42.329829 \n173 [-71.093834, 42.339096] -71.093834 42.339096 \n174 Northeastern (Inbound) [-71.090331, 42.339762] -71.090331 42.339762 \n\n route \n0 1 \n1 1 \n2 1 \n3 1 \n4 1 \n.. ... \n170 3 \n171 3 \n172 3 \n173 3 \n174 3 \n\n[175 rows x 9 columns]", + "text/html": "
\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
waypoint_indextrips_indexhintdistancenamelocationlatlonroute
000t4YsgAGHLIAAAAAAVQEAAAAAAAAwAAAAAAAAAHV0F0IAAA...19.432511[-71.054865, 42.364361]-71.05486542.3643611
110e1kugJlZLoBmAAAA6QAAAAAAAAAAAAAAZ6M2QSewzkEAAA...4.756158[-71.060933, 42.376178]-71.06093342.3761781
220tFkugHVaLoAOAAAAAAAAABgAAAAAAAAAwMG2QAAAAAB6ii...4.525535[-71.060753, 42.376391]-71.06075342.3763911
330sJAugLOQLoBuAQAAlAEAAAAAAAAAAAAAHFcjQvEZM0IAAA...7.844897[-71.060948, 42.380436]-71.06094842.3804361
440VREtgNlJBIBCAAAAYAAAAAAAAAARAAAAOOzeQU7vHkIAAA...22.681980Factory Street[-71.061206, 42.398809]-71.06120642.3988091
..............................
170390cX8hgJF_IYA1AAAAMAAAAGcAAABOAAAATyWxQQ77nUEHMC...22.776295Alleghany Street[-71.099348, 42.33047]-71.09934842.3304703
171400s9QhgLbUIYAwAAAAkAAAAAAAAAAAAAAA2XmpQNgrgEEAAA...4.111715[-71.09454, 42.325354]-71.09454042.3253543
1724105tYhgJHXIYAIAAAArQAAADwAAABCAQAAaRlbQD16mUGpAc...17.374491Dudley Street[-71.090904, 42.329829]-71.09090442.3298293
173420k4chgBiIIYAKAAAAFwAAAPQDAAB_AgAAHn2aP-biHUBi6e...36.240351[-71.093834, 42.339096]-71.09383442.3390963
174430DoUhgBeFIYCcAAAAJgAAAAAAAAARAAAAm0CKQdkZiEAAAA...0.236958Northeastern (Inbound)[-71.090331, 42.339762]-71.09033142.3397623
\n

175 rows × 9 columns

\n
" + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "display(df)" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2023-11-07T15:38:52.735139Z", + "start_time": "2023-11-07T15:38:52.730797Z" + } + }, + "id": "17a8cc8fed5450a6" + }, + { + "cell_type": "markdown", + "source": [ + "## Map" + ], + "metadata": { + "collapsed": false + }, + "id": "b20a57aa09792c39" + }, + { + "cell_type": "code", + "execution_count": 21, + "outputs": [ + { + "data": { + "text/plain": "", + "text/html": "
Make this Notebook Trusted to load map: File -> Trust Notebook
" + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Create a map\n", + "m = folium.Map(location=[df['lon'].mean(), df['lat'].mean()], zoom_start=11)\n", + "\n", + "# Add the points and lines for the three routes with different colors\n", + "colors = ['red', 'blue', 'green']\n", + "\n", + "for route in df['route'].unique():\n", + " df_route = df[df['route'] == route]\n", + " folium.PolyLine(df_route[['lon', 'lat']].values.tolist(), color=colors[route - 1]).add_to(m)\n", + " for i in range(len(df_route)):\n", + " folium.CircleMarker(df_route[['lon', 'lat']].iloc[i].values.tolist(), radius=3, color=colors[route - 1]).add_to(\n", + " m)\n", + " \n", + "# Display the map\n", + "m" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2023-11-07T15:38:52.827726Z", + "start_time": "2023-11-07T15:38:52.736404Z" + } + }, + "id": "702adaec008a6ec8" + }, + { + "cell_type": "markdown", + "source": [ + "## Results" + ], + "metadata": { + "collapsed": false + }, + "id": "a947e49e27c734e9" + }, + { + "cell_type": "code", + "execution_count": 22, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Route 1 has 55 waypoints\n", + "Route 2 has 72 waypoints\n", + "Route 3 has 42 waypoints\n" + ] + } + ], + "source": [ + "# Get the number of waypoints for each route\n", + "route_1_waypoints = len(route_1_coordinates)\n", + "route_2_waypoints = len(route_2_coordinates)\n", + "route_3_waypoints = len(route_3_coordinates)\n", + "print(\"Route 1 has {} waypoints\".format(route_1_waypoints))\n", + "print(\"Route 2 has {} waypoints\".format(route_2_waypoints))\n", + "print(\"Route 3 has {} waypoints\".format(route_3_waypoints))" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2023-11-07T15:38:52.840867Z", + "start_time": "2023-11-07T15:38:52.802540Z" + } + }, + "id": "4106acf2adad01d7" + }, + { + "cell_type": "code", + "execution_count": 23, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The trip will take 8.883333333333333 hours\n", + "The trip will take 8.458055555555555 hours\n", + "The trip will take 10.230555555555556 hours\n" + ] + } + ], + "source": [ + "# Get the trip time for each route\n", + "trip_hrs_1 = utils.get_trip_time(route_1, route_1_waypoints, utils.list_to_string([centroids[0]]),\n", + " northeastern_coordinate)\n", + "print(\"The trip will take {} hours\".format(trip_hrs_1))\n", + "trip_hrs_2 = utils.get_trip_time(route_2, route_2_waypoints, utils.list_to_string([centroids[1]]),\n", + " northeastern_coordinate)\n", + "print(\"The trip will take {} hours\".format(trip_hrs_2))\n", + "trip_hrs_3 = utils.get_trip_time(route_3, route_3_waypoints, utils.list_to_string([centroids[2]]),\n", + " northeastern_coordinate)\n", + "print(\"The trip will take {} hours\".format(trip_hrs_3))" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2023-11-07T15:38:54.879411Z", + "start_time": "2023-11-07T15:38:52.805867Z" + } + }, + "id": "c58106faf0fc7f4e" + }, + { + "cell_type": "code", + "execution_count": 23, + "outputs": [], + "source": [], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2023-11-07T15:38:54.879657Z", + "start_time": "2023-11-07T15:38:54.867101Z" + } + }, + "id": "a2f10e3152b95a69" + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} -- cgit v1.2.3