From 6b9d2b694821e63da8a9db47a97ce9cc6d807a4b Mon Sep 17 00:00:00 2001 From: itsGarrin Date: Tue, 7 Nov 2023 18:36:29 -0500 Subject: pleb. --- ZestySalesman.ipynb | 4956 ++++++++++++++++++++++++++++++++++++++--- ZestySalesman.py | 368 +++ ZestySalesmanNoMinimize.ipynb | 706 ------ utils.py | 3 +- 4 files changed, 4969 insertions(+), 1064 deletions(-) create mode 100644 ZestySalesman.py delete mode 100644 ZestySalesmanNoMinimize.ipynb diff --git a/ZestySalesman.ipynb b/ZestySalesman.ipynb index 4e14da2..60dd19e 100644 --- a/ZestySalesman.ipynb +++ b/ZestySalesman.ipynb @@ -5,11 +5,11 @@ "execution_count": 1, "id": "initial_id", "metadata": { - "collapsed": true, "ExecuteTime": { "end_time": "2023-11-07T23:05:35.179983Z", "start_time": "2023-11-07T23:05:34.039783Z" - } + }, + "collapsed": true }, "outputs": [], "source": [ @@ -21,6 +21,13 @@ { "cell_type": "code", "execution_count": 2, + "id": "73b780e762c9de37", + "metadata": { + "ExecuteTime": { + "end_time": "2023-11-07T23:05:35.194166Z", + "start_time": "2023-11-07T23:05:35.181233Z" + } + }, "outputs": [], "source": [ "# Load the data\n", @@ -28,38 +35,36 @@ "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-07T23:05:35.194166Z", - "start_time": "2023-11-07T23:05:35.181233Z" - } - }, - "id": "73b780e762c9de37" + ] }, { "cell_type": "code", "execution_count": 3, - "outputs": [], - "source": [ - "# Create two centroids, one in the North End and one in the Seaport District\n", - "centroids = [[42.365, -71.054], [42.351, -71.045]]\n", - "\n", - "northeastern_coordinate = \"-71.09033,42.33976\"" - ], + "id": "be4c8c1d77842ef7", "metadata": { - "collapsed": false, "ExecuteTime": { "end_time": "2023-11-07T23:05:35.195314Z", "start_time": "2023-11-07T23:05:35.193156Z" } }, - "id": "be4c8c1d77842ef7" + "outputs": [], + "source": [ + "# Create two centroids, one in the North End and one in the Seaport District\n", + "centroids = [[42.365, -71.054], [42.351, -71.045]]\n", + "\n", + "northeastern_coordinate = \"-71.09033,42.33976\"" + ] }, { "cell_type": "code", "execution_count": 4, + "id": "ffe4025e97a6c6b9", + "metadata": { + "ExecuteTime": { + "end_time": "2023-11-07T23:05:35.201800Z", + "start_time": "2023-11-07T23:05:35.197747Z" + } + }, "outputs": [], "source": [ "# Combine the two lists and add a column to indicate the list\n", @@ -69,59 +74,166 @@ "ListD['list'] = 'D'\n", "\n", "TotalList = pd.concat([ListA, ListB, ListC, ListD])" - ], - "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2023-11-07T23:05:35.201800Z", - "start_time": "2023-11-07T23:05:35.197747Z" - } - }, - "id": "ffe4025e97a6c6b9" + ] }, { "cell_type": "code", "execution_count": 5, - "outputs": [], - "source": [ - "# Remove all columns but name and gps\n", - "TotalList = TotalList[['name', 'gps', 'list']]" - ], + "id": "72657779b4484aae", "metadata": { - "collapsed": false, "ExecuteTime": { "end_time": "2023-11-07T23:05:35.215762Z", "start_time": "2023-11-07T23:05:35.200811Z" } }, - "id": "72657779b4484aae" + "outputs": [], + "source": [ + "# Remove all columns but name and gps\n", + "TotalList = TotalList[['name', 'gps', 'list']]" + ] }, { "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])" - ], + "id": "a157ffaec020a29a", "metadata": { - "collapsed": false, "ExecuteTime": { "end_time": "2023-11-07T23:05:35.215916Z", "start_time": "2023-11-07T23:05:35.204173Z" } }, - "id": "a157ffaec020a29a" + "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])" + ] }, { "cell_type": "code", "execution_count": 7, + "id": "a03ebde91b87fa3b", + "metadata": { + "ExecuteTime": { + "end_time": "2023-11-07T23:05:35.216384Z", + "start_time": "2023-11-07T23:05:35.206794Z" + } + }, "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[169 rows x 3 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
namegpslist
0521 Commercial Street #525[42.3688272, -71.0553792]A
1Acorn St[42.3576234, -71.0688746]A
2Arlington's Great Meadows[42.4299758, -71.2038948]A
3Arthur Fiedler Statue[42.3565057, -71.0754527]A
4BU Beach[42.3511927, -71.1060828]A
............
33The Quiet Few[42.3670906, -71.0359889]D
34The Tall Ship Boston[42.3649544, -71.0414523]D
35Toasted Flats[42.3711266, -71.0371343]D
36Vega Market[42.3891835, -71.033703]D
37Winthrop High School[42.3803348, -70.9799864]D
\n

169 rows × 3 columns

\n
" + "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", + "
namegpslist
0521 Commercial Street #525[42.3688272, -71.0553792]A
1Acorn St[42.3576234, -71.0688746]A
2Arlington's Great Meadows[42.4299758, -71.2038948]A
3Arthur Fiedler Statue[42.3565057, -71.0754527]A
4BU Beach[42.3511927, -71.1060828]A
............
33The Quiet Few[42.3670906, -71.0359889]D
34The Tall Ship Boston[42.3649544, -71.0414523]D
35Toasted Flats[42.3711266, -71.0371343]D
36Vega Market[42.3891835, -71.033703]D
37Winthrop High School[42.3803348, -70.9799864]D
\n", + "

169 rows × 3 columns

\n", + "
" + ], + "text/plain": [ + " name gps list\n", + "0 521 Commercial Street #525 [42.3688272, -71.0553792] A\n", + "1 Acorn St [42.3576234, -71.0688746] A\n", + "2 Arlington's Great Meadows [42.4299758, -71.2038948] A\n", + "3 Arthur Fiedler Statue [42.3565057, -71.0754527] A\n", + "4 BU Beach [42.3511927, -71.1060828] A\n", + ".. ... ... ...\n", + "33 The Quiet Few [42.3670906, -71.0359889] D\n", + "34 The Tall Ship Boston [42.3649544, -71.0414523] D\n", + "35 Toasted Flats [42.3711266, -71.0371343] D\n", + "36 Vega Market [42.3891835, -71.033703] D\n", + "37 Winthrop High School [42.3803348, -70.9799864] D\n", + "\n", + "[169 rows x 3 columns]" + ] }, "metadata": {}, "output_type": "display_data" @@ -129,39 +241,34 @@ ], "source": [ "display(TotalList)" - ], - "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2023-11-07T23:05:35.216384Z", - "start_time": "2023-11-07T23:05:35.206794Z" - } - }, - "id": "a03ebde91b87fa3b" + ] }, { "cell_type": "markdown", + "id": "4bd41be9aca5094b", + "metadata": {}, "source": [ "# 2 Routes" - ], - "metadata": { - "collapsed": false - }, - "id": "4bd41be9aca5094b" + ] }, { "cell_type": "markdown", + "id": "90d1d2f1a931597f", + "metadata": {}, "source": [ "## Cluster and Minimize" - ], - "metadata": { - "collapsed": false - }, - "id": "90d1d2f1a931597f" + ] }, { "cell_type": "code", "execution_count": 8, + "id": "ee9b3c1ecb360976", + "metadata": { + "ExecuteTime": { + "end_time": "2023-11-07T23:06:04.963804Z", + "start_time": "2023-11-07T23:05:35.213646Z" + } + }, "outputs": [ { "name": "stderr", @@ -181,70 +288,66 @@ "\n", "route_1_coordinates = routes[0]\n", "route_2_coordinates = routes[1]" - ], - "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2023-11-07T23:06:04.963804Z", - "start_time": "2023-11-07T23:05:35.213646Z" - } - }, - "id": "ee9b3c1ecb360976" + ] }, { "cell_type": "markdown", - "source": [ - "## Create JSON" - ], + "id": "c85b8ef869e35006", "metadata": { - "collapsed": false, "ExecuteTime": { "end_time": "2023-11-07T17:33:02.697755Z", "start_time": "2023-11-07T17:33:02.687460Z" } }, - "id": "c85b8ef869e35006" + "source": [ + "## Create JSON" + ] }, { "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)" - ], + "id": "aa618161182b5b07", "metadata": { - "collapsed": false, "ExecuteTime": { "end_time": "2023-11-07T23:06:04.981278Z", "start_time": "2023-11-07T23:06:04.967948Z" } }, - "id": "aa618161182b5b07" + "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)" + ] }, { "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)" - ], + "id": "32c485788eedd94", "metadata": { - "collapsed": false, "ExecuteTime": { "end_time": "2023-11-07T23:06:07.185941Z", "start_time": "2023-11-07T23:06:04.976840Z" } }, - "id": "32c485788eedd94" + "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)" + ] }, { "cell_type": "code", "execution_count": 11, + "id": "49dba1f17ca8337e", + "metadata": { + "ExecuteTime": { + "end_time": "2023-11-07T23:06:07.206851Z", + "start_time": "2023-11-07T23:06:07.193514Z" + } + }, "outputs": [], "source": [ "# Add columns for the route number\n", @@ -253,24 +356,244 @@ "\n", "# Concatenate the two dataframes\n", "df = pd.concat([df1, df2], ignore_index=True)" - ], - "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2023-11-07T23:06:07.206851Z", - "start_time": "2023-11-07T23:06:07.193514Z" - } - }, - "id": "49dba1f17ca8337e" + ] }, { "cell_type": "code", "execution_count": 12, + "id": "f231d9a35358988c", + "metadata": { + "ExecuteTime": { + "end_time": "2023-11-07T23:06:07.219423Z", + "start_time": "2023-11-07T23:06:07.214064Z" + } + }, "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 61 0 \n169 62 0 \n170 63 0 \n171 64 0 \n172 65 0 \n\n hint distance \\\n0 1IwsgDuNLIBFAAAAWgEAAA8AAAAAAAAAFQP1QGa9GUI7qN... 8.262982 \n1 G4gsgDiILICSAwAA5gAAAOkAAAAAAAAAQljLQnyXy0Fhy8... 2.602121 \n2 gIosgLaKLIDOAAAArgAAAFwBAAAAAAAAp3O3QafxmUEQiR... 15.458439 \n3 HpwsgCKcLIAAAAAAEgAAAAAAAAAAAAAAAAAAACg870AAAA... 39.201677 \n4 qn8sgKt_LIAfAAAAAAAAAAAAAAAAAAAA2ElcQAAAAAAAAA... 39.331841 \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.053931, 42.365054] -71.053931 42.365054 \n1 [-71.056164, 42.366918] -71.056164 42.366918 \n2 [-71.055561, 42.368861] -71.055561 42.368861 \n3 [-71.062507, 42.365968] -71.062507 42.365968 \n4 [-71.064277, 42.358851] -71.064277 42.358851 \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
0001IwsgDuNLIBFAAAAWgEAAA8AAAAAAAAAFQP1QGa9GUI7qN...8.262982[-71.053931, 42.365054]-71.05393142.3650541
110G4gsgDiILICSAwAA5gAAAOkAAAAAAAAAQljLQnyXy0Fhy8...2.602121[-71.056164, 42.366918]-71.05616442.3669181
220gIosgLaKLIDOAAAArgAAAFwBAAAAAAAAp3O3QafxmUEQiR...15.458439[-71.055561, 42.368861]-71.05556142.3688611
330HpwsgCKcLIAAAAAAEgAAAAAAAAAAAAAAAAAAACg870AAAA...39.201677[-71.062507, 42.365968]-71.06250742.3659681
440qn8sgKt_LIAfAAAAAAAAAAAAAAAAAAAA2ElcQAAAAAAAAA...39.331841[-71.064277, 42.358851]-71.06427742.3588511
..............................
1686107hAigPYQIoA2AgAAYwEAAAAAAAAAAAAAnsd7Qq9XHUIAAA...7.478611[-71.096959, 42.344689]-71.09695942.3446892
169620bwwigH0MIoAFAAAAEAAAAFUAAAArAAAAag0xP3921D-BFx...8.340476[-71.095003, 42.342001]-71.09500342.3420012
170630MQwigFwMIoAoAAAANQAAABwAAAB-AAAAoidqQSAYl0GvUh...11.504463[-71.094327, 42.341231]-71.09432742.3412312
171640k4chgBiIIYAKAAAAFwAAAPQDAAB_AgAAHn2aP-biHUBi6e...36.240351[-71.093834, 42.339096]-71.09383442.3390962
172650DoUhgBeFIYCcAAAAJgAAAAAAAAARAAAAm0CKQdkZiEAAAA...0.236958Northeastern (Inbound)[-71.090331, 42.339762]-71.09033142.3397622
\n

173 rows × 9 columns

\n
" + "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
0001IwsgDuNLIBFAAAAWgEAAA8AAAAAAAAAFQP1QGa9GUI7qN...8.262982[-71.053931, 42.365054]-71.05393142.3650541
110G4gsgDiILICSAwAA5gAAAOkAAAAAAAAAQljLQnyXy0Fhy8...2.602121[-71.056164, 42.366918]-71.05616442.3669181
220gIosgLaKLIDOAAAArgAAAFwBAAAAAAAAp3O3QafxmUEQiR...15.458439[-71.055561, 42.368861]-71.05556142.3688611
330HpwsgCKcLIAAAAAAEgAAAAAAAAAAAAAAAAAAACg870AAAA...39.201677[-71.062507, 42.365968]-71.06250742.3659681
440qn8sgKt_LIAfAAAAAAAAAAAAAAAAAAAA2ElcQAAAAAAAAA...39.331841[-71.064277, 42.358851]-71.06427742.3588511
..............................
1686107hAigPYQIoA2AgAAYwEAAAAAAAAAAAAAnsd7Qq9XHUIAAA...7.478611[-71.096959, 42.344689]-71.09695942.3446892
169620bwwigH0MIoAFAAAAEAAAAFUAAAArAAAAag0xP3921D-BFx...8.340476[-71.095003, 42.342001]-71.09500342.3420012
170630MQwigFwMIoAoAAAANQAAABwAAAB-AAAAoidqQSAYl0GvUh...11.504463[-71.094327, 42.341231]-71.09432742.3412312
171640k4chgBiIIYAKAAAAFwAAAPQDAAB_AgAAHn2aP-biHUBi6e...36.240351[-71.093834, 42.339096]-71.09383442.3390962
172650DoUhgBeFIYCcAAAAJgAAAAAAAAARAAAAm0CKQdkZiEAAAA...0.236958Northeastern (Inbound)[-71.090331, 42.339762]-71.09033142.3397622
\n", + "

173 rows × 9 columns

\n", + "
" + ], + "text/plain": [ + " waypoint_index trips_index \\\n", + "0 0 0 \n", + "1 1 0 \n", + "2 2 0 \n", + "3 3 0 \n", + "4 4 0 \n", + ".. ... ... \n", + "168 61 0 \n", + "169 62 0 \n", + "170 63 0 \n", + "171 64 0 \n", + "172 65 0 \n", + "\n", + " hint distance \\\n", + "0 1IwsgDuNLIBFAAAAWgEAAA8AAAAAAAAAFQP1QGa9GUI7qN... 8.262982 \n", + "1 G4gsgDiILICSAwAA5gAAAOkAAAAAAAAAQljLQnyXy0Fhy8... 2.602121 \n", + "2 gIosgLaKLIDOAAAArgAAAFwBAAAAAAAAp3O3QafxmUEQiR... 15.458439 \n", + "3 HpwsgCKcLIAAAAAAEgAAAAAAAAAAAAAAAAAAACg870AAAA... 39.201677 \n", + "4 qn8sgKt_LIAfAAAAAAAAAAAAAAAAAAAA2ElcQAAAAAAAAA... 39.331841 \n", + ".. ... ... \n", + "168 7hAigPYQIoA2AgAAYwEAAAAAAAAAAAAAnsd7Qq9XHUIAAA... 7.478611 \n", + "169 bwwigH0MIoAFAAAAEAAAAFUAAAArAAAAag0xP3921D-BFx... 8.340476 \n", + "170 MQwigFwMIoAoAAAANQAAABwAAAB-AAAAoidqQSAYl0GvUh... 11.504463 \n", + "171 k4chgBiIIYAKAAAAFwAAAPQDAAB_AgAAHn2aP-biHUBi6e... 36.240351 \n", + "172 DoUhgBeFIYCcAAAAJgAAAAAAAAARAAAAm0CKQdkZiEAAAA... 0.236958 \n", + "\n", + " name location lat lon \\\n", + "0 [-71.053931, 42.365054] -71.053931 42.365054 \n", + "1 [-71.056164, 42.366918] -71.056164 42.366918 \n", + "2 [-71.055561, 42.368861] -71.055561 42.368861 \n", + "3 [-71.062507, 42.365968] -71.062507 42.365968 \n", + "4 [-71.064277, 42.358851] -71.064277 42.358851 \n", + ".. ... ... ... ... \n", + "168 [-71.096959, 42.344689] -71.096959 42.344689 \n", + "169 [-71.095003, 42.342001] -71.095003 42.342001 \n", + "170 [-71.094327, 42.341231] -71.094327 42.341231 \n", + "171 [-71.093834, 42.339096] -71.093834 42.339096 \n", + "172 Northeastern (Inbound) [-71.090331, 42.339762] -71.090331 42.339762 \n", + "\n", + " route \n", + "0 1 \n", + "1 1 \n", + "2 1 \n", + "3 1 \n", + "4 1 \n", + ".. ... \n", + "168 2 \n", + "169 2 \n", + "170 2 \n", + "171 2 \n", + "172 2 \n", + "\n", + "[173 rows x 9 columns]" + ] }, "metadata": {}, "output_type": "display_data" @@ -278,34 +601,1153 @@ ], "source": [ "display(df)" - ], - "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2023-11-07T23:06:07.219423Z", - "start_time": "2023-11-07T23:06:07.214064Z" - } - }, - "id": "f231d9a35358988c" + ] }, { "cell_type": "markdown", + "id": "75be92e34a36147f", + "metadata": {}, "source": [ "## Map" - ], - "metadata": { - "collapsed": false - }, - "id": "75be92e34a36147f" + ] }, { "cell_type": "code", "execution_count": 13, + "id": "80fd847da2833913", + "metadata": { + "ExecuteTime": { + "end_time": "2023-11-07T23:06:07.316817Z", + "start_time": "2023-11-07T23:06:07.221200Z" + } + }, "outputs": [ { "data": { - "text/plain": "", - "text/html": "
Make this Notebook Trusted to load map: File -> Trust Notebook
" + "text/html": [ + "
Make this Notebook Trusted to load map: File -> Trust Notebook
" + ], + "text/plain": [ + "" + ] }, "execution_count": 13, "metadata": {}, @@ -328,29 +1770,26 @@ "\n", "# Display the map\n", "m" - ], - "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2023-11-07T23:06:07.316817Z", - "start_time": "2023-11-07T23:06:07.221200Z" - } - }, - "id": "80fd847da2833913" + ] }, { "cell_type": "markdown", + "id": "a7b562f75f7e0813", + "metadata": {}, "source": [ "## Results" - ], - "metadata": { - "collapsed": false - }, - "id": "a7b562f75f7e0813" + ] }, { "cell_type": "code", "execution_count": 14, + "id": "f53c97acec1c2fc4", + "metadata": { + "ExecuteTime": { + "end_time": "2023-11-07T23:06:07.318988Z", + "start_time": "2023-11-07T23:06:07.297230Z" + } + }, "outputs": [ { "name": "stdout", @@ -367,19 +1806,18 @@ "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-07T23:06:07.318988Z", - "start_time": "2023-11-07T23:06:07.297230Z" - } - }, - "id": "f53c97acec1c2fc4" + ] }, { "cell_type": "code", "execution_count": 15, + "id": "a3ec09dfb5cbb5b3", + "metadata": { + "ExecuteTime": { + "end_time": "2023-11-07T23:06:09.735945Z", + "start_time": "2023-11-07T23:06:07.299647Z" + } + }, "outputs": [ { "name": "stdout", @@ -397,29 +1835,26 @@ "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-07T23:06:09.735945Z", - "start_time": "2023-11-07T23:06:07.299647Z" - } - }, - "id": "a3ec09dfb5cbb5b3" + ] }, { "cell_type": "markdown", + "id": "de7b5856172d213c", + "metadata": {}, "source": [ "# 3 Routes" - ], - "metadata": { - "collapsed": false - }, - "id": "de7b5856172d213c" + ] }, { "cell_type": "code", "execution_count": 16, + "id": "bb6e00857e8175c0", + "metadata": { + "ExecuteTime": { + "end_time": "2023-11-07T23:07:36.195528Z", + "start_time": "2023-11-07T23:06:09.732162Z" + } + }, "outputs": [ { "name": "stderr", @@ -441,29 +1876,26 @@ "route_1_coordinates = routes[0]\n", "route_2_coordinates = routes[1]\n", "route_3_coordinates = routes[2]" - ], - "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2023-11-07T23:07:36.195528Z", - "start_time": "2023-11-07T23:06:09.732162Z" - } - }, - "id": "bb6e00857e8175c0" + ] }, { "cell_type": "markdown", + "id": "19afb4f687b37383", + "metadata": {}, "source": [ "## Create JSON" - ], - "metadata": { - "collapsed": false - }, - "id": "19afb4f687b37383" + ] }, { "cell_type": "code", "execution_count": 17, + "id": "e886e061f86a2118", + "metadata": { + "ExecuteTime": { + "end_time": "2023-11-07T23:07:36.198703Z", + "start_time": "2023-11-07T23:07:36.194798Z" + } + }, "outputs": [], "source": [ "# Create a JSON request for the API\n", @@ -471,38 +1903,36 @@ "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-07T23:07:36.198703Z", - "start_time": "2023-11-07T23:07:36.194798Z" - } - }, - "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)" - ], + "id": "23e4682fe9e30631", "metadata": { - "collapsed": false, "ExecuteTime": { "end_time": "2023-11-07T23:07:38.315154Z", "start_time": "2023-11-07T23:07:36.199174Z" } }, - "id": "23e4682fe9e30631" + "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)" + ] }, { "cell_type": "code", "execution_count": 19, + "id": "c3a5c5d6f3ac46c0", + "metadata": { + "ExecuteTime": { + "end_time": "2023-11-07T23:07:38.325567Z", + "start_time": "2023-11-07T23:07:38.320173Z" + } + }, "outputs": [], "source": [ "# Add columns for the route number\n", @@ -512,24 +1942,244 @@ "\n", "# Concatenate the three dataframes\n", "df = pd.concat([df1, df2, df3], ignore_index=True)" - ], - "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2023-11-07T23:07:38.325567Z", - "start_time": "2023-11-07T23:07:38.320173Z" - } - }, - "id": "c3a5c5d6f3ac46c0" + ] }, { "cell_type": "code", "execution_count": 20, + "id": "17a8cc8fed5450a6", + "metadata": { + "ExecuteTime": { + "end_time": "2023-11-07T23:07:38.333653Z", + "start_time": "2023-11-07T23:07:38.322616Z" + } + }, "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 49 0 \n171 50 0 \n172 51 0 \n173 52 0 \n174 53 0 \n\n hint distance \\\n0 1IwsgDuNLIBFAAAAWgEAAA8AAAAAAAAAFQP1QGa9GUI7qN... 8.262982 \n1 G4gsgDiILICSAwAA5gAAAOkAAAAAAAAAQljLQnyXy0Fhy8... 2.602121 \n2 gIosgLaKLIDOAAAArgAAAFwBAAAAAAAAp3O3QafxmUEQiR... 15.458439 \n3 HpwsgCKcLIAAAAAAEgAAAAAAAAAAAAAAAAAAACg870AAAA... 39.201677 \n4 LRUugHAVLoA1AAAA7wEAAKAAAADqAAAAYZa9QBEBXEIOWo... 1.865658 \n.. ... ... \n170 7hAigPYQIoA2AgAAYwEAAAAAAAAAAAAAnsd7Qq9XHUIAAA... 7.478611 \n171 bwwigH0MIoAFAAAAEAAAAFUAAAArAAAAag0xP3921D-BFx... 8.340476 \n172 MQwigFwMIoAoAAAANQAAABwAAAB-AAAAoidqQSAYl0GvUh... 11.504463 \n173 k4chgBiIIYAKAAAAFwAAAPQDAAB_AgAAHn2aP-biHUBi6e... 36.240351 \n174 DoUhgBeFIYCcAAAAJgAAAAAAAAARAAAAm0CKQdkZiEAAAA... 0.236958 \n\n name location lat lon \\\n0 [-71.053931, 42.365054] -71.053931 42.365054 \n1 [-71.056164, 42.366918] -71.056164 42.366918 \n2 [-71.055561, 42.368861] -71.055561 42.368861 \n3 [-71.062507, 42.365968] -71.062507 42.365968 \n4 [-71.061735, 42.369195] -71.061735 42.369195 \n.. ... ... ... ... \n170 [-71.096959, 42.344689] -71.096959 42.344689 \n171 [-71.095003, 42.342001] -71.095003 42.342001 \n172 [-71.094327, 42.341231] -71.094327 42.341231 \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
0001IwsgDuNLIBFAAAAWgEAAA8AAAAAAAAAFQP1QGa9GUI7qN...8.262982[-71.053931, 42.365054]-71.05393142.3650541
110G4gsgDiILICSAwAA5gAAAOkAAAAAAAAAQljLQnyXy0Fhy8...2.602121[-71.056164, 42.366918]-71.05616442.3669181
220gIosgLaKLIDOAAAArgAAAFwBAAAAAAAAp3O3QafxmUEQiR...15.458439[-71.055561, 42.368861]-71.05556142.3688611
330HpwsgCKcLIAAAAAAEgAAAAAAAAAAAAAAAAAAACg870AAAA...39.201677[-71.062507, 42.365968]-71.06250742.3659681
440LRUugHAVLoA1AAAA7wEAAKAAAADqAAAAYZa9QBEBXEIOWo...1.865658[-71.061735, 42.369195]-71.06173542.3691951
..............................
1704907hAigPYQIoA2AgAAYwEAAAAAAAAAAAAAnsd7Qq9XHUIAAA...7.478611[-71.096959, 42.344689]-71.09695942.3446893
171500bwwigH0MIoAFAAAAEAAAAFUAAAArAAAAag0xP3921D-BFx...8.340476[-71.095003, 42.342001]-71.09500342.3420013
172510MQwigFwMIoAoAAAANQAAABwAAAB-AAAAoidqQSAYl0GvUh...11.504463[-71.094327, 42.341231]-71.09432742.3412313
173520k4chgBiIIYAKAAAAFwAAAPQDAAB_AgAAHn2aP-biHUBi6e...36.240351[-71.093834, 42.339096]-71.09383442.3390963
174530DoUhgBeFIYCcAAAAJgAAAAAAAAARAAAAm0CKQdkZiEAAAA...0.236958Northeastern (Inbound)[-71.090331, 42.339762]-71.09033142.3397623
\n

175 rows × 9 columns

\n
" + "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
0001IwsgDuNLIBFAAAAWgEAAA8AAAAAAAAAFQP1QGa9GUI7qN...8.262982[-71.053931, 42.365054]-71.05393142.3650541
110G4gsgDiILICSAwAA5gAAAOkAAAAAAAAAQljLQnyXy0Fhy8...2.602121[-71.056164, 42.366918]-71.05616442.3669181
220gIosgLaKLIDOAAAArgAAAFwBAAAAAAAAp3O3QafxmUEQiR...15.458439[-71.055561, 42.368861]-71.05556142.3688611
330HpwsgCKcLIAAAAAAEgAAAAAAAAAAAAAAAAAAACg870AAAA...39.201677[-71.062507, 42.365968]-71.06250742.3659681
440LRUugHAVLoA1AAAA7wEAAKAAAADqAAAAYZa9QBEBXEIOWo...1.865658[-71.061735, 42.369195]-71.06173542.3691951
..............................
1704907hAigPYQIoA2AgAAYwEAAAAAAAAAAAAAnsd7Qq9XHUIAAA...7.478611[-71.096959, 42.344689]-71.09695942.3446893
171500bwwigH0MIoAFAAAAEAAAAFUAAAArAAAAag0xP3921D-BFx...8.340476[-71.095003, 42.342001]-71.09500342.3420013
172510MQwigFwMIoAoAAAANQAAABwAAAB-AAAAoidqQSAYl0GvUh...11.504463[-71.094327, 42.341231]-71.09432742.3412313
173520k4chgBiIIYAKAAAAFwAAAPQDAAB_AgAAHn2aP-biHUBi6e...36.240351[-71.093834, 42.339096]-71.09383442.3390963
174530DoUhgBeFIYCcAAAAJgAAAAAAAAARAAAAm0CKQdkZiEAAAA...0.236958Northeastern (Inbound)[-71.090331, 42.339762]-71.09033142.3397623
\n", + "

175 rows × 9 columns

\n", + "
" + ], + "text/plain": [ + " waypoint_index trips_index \\\n", + "0 0 0 \n", + "1 1 0 \n", + "2 2 0 \n", + "3 3 0 \n", + "4 4 0 \n", + ".. ... ... \n", + "170 49 0 \n", + "171 50 0 \n", + "172 51 0 \n", + "173 52 0 \n", + "174 53 0 \n", + "\n", + " hint distance \\\n", + "0 1IwsgDuNLIBFAAAAWgEAAA8AAAAAAAAAFQP1QGa9GUI7qN... 8.262982 \n", + "1 G4gsgDiILICSAwAA5gAAAOkAAAAAAAAAQljLQnyXy0Fhy8... 2.602121 \n", + "2 gIosgLaKLIDOAAAArgAAAFwBAAAAAAAAp3O3QafxmUEQiR... 15.458439 \n", + "3 HpwsgCKcLIAAAAAAEgAAAAAAAAAAAAAAAAAAACg870AAAA... 39.201677 \n", + "4 LRUugHAVLoA1AAAA7wEAAKAAAADqAAAAYZa9QBEBXEIOWo... 1.865658 \n", + ".. ... ... \n", + "170 7hAigPYQIoA2AgAAYwEAAAAAAAAAAAAAnsd7Qq9XHUIAAA... 7.478611 \n", + "171 bwwigH0MIoAFAAAAEAAAAFUAAAArAAAAag0xP3921D-BFx... 8.340476 \n", + "172 MQwigFwMIoAoAAAANQAAABwAAAB-AAAAoidqQSAYl0GvUh... 11.504463 \n", + "173 k4chgBiIIYAKAAAAFwAAAPQDAAB_AgAAHn2aP-biHUBi6e... 36.240351 \n", + "174 DoUhgBeFIYCcAAAAJgAAAAAAAAARAAAAm0CKQdkZiEAAAA... 0.236958 \n", + "\n", + " name location lat lon \\\n", + "0 [-71.053931, 42.365054] -71.053931 42.365054 \n", + "1 [-71.056164, 42.366918] -71.056164 42.366918 \n", + "2 [-71.055561, 42.368861] -71.055561 42.368861 \n", + "3 [-71.062507, 42.365968] -71.062507 42.365968 \n", + "4 [-71.061735, 42.369195] -71.061735 42.369195 \n", + ".. ... ... ... ... \n", + "170 [-71.096959, 42.344689] -71.096959 42.344689 \n", + "171 [-71.095003, 42.342001] -71.095003 42.342001 \n", + "172 [-71.094327, 42.341231] -71.094327 42.341231 \n", + "173 [-71.093834, 42.339096] -71.093834 42.339096 \n", + "174 Northeastern (Inbound) [-71.090331, 42.339762] -71.090331 42.339762 \n", + "\n", + " route \n", + "0 1 \n", + "1 1 \n", + "2 1 \n", + "3 1 \n", + "4 1 \n", + ".. ... \n", + "170 3 \n", + "171 3 \n", + "172 3 \n", + "173 3 \n", + "174 3 \n", + "\n", + "[175 rows x 9 columns]" + ] }, "metadata": {}, "output_type": "display_data" @@ -537,34 +2187,1171 @@ ], "source": [ "display(df)" - ], - "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2023-11-07T23:07:38.333653Z", - "start_time": "2023-11-07T23:07:38.322616Z" - } - }, - "id": "17a8cc8fed5450a6" + ] }, { "cell_type": "markdown", + "id": "b20a57aa09792c39", + "metadata": {}, "source": [ "## Map" - ], - "metadata": { - "collapsed": false - }, - "id": "b20a57aa09792c39" + ] }, { "cell_type": "code", "execution_count": 21, + "id": "702adaec008a6ec8", + "metadata": { + "ExecuteTime": { + "end_time": "2023-11-07T23:07:38.444061Z", + "start_time": "2023-11-07T23:07:38.336503Z" + } + }, "outputs": [ { "data": { - "text/plain": "", - "text/html": "
Make this Notebook Trusted to load map: File -> Trust Notebook
" + "text/html": [ + "
Make this Notebook Trusted to load map: File -> Trust Notebook
" + ], + "text/plain": [ + "" + ] }, "execution_count": 21, "metadata": {}, @@ -587,29 +3374,26 @@ "\n", "# Display the map\n", "m" - ], - "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2023-11-07T23:07:38.444061Z", - "start_time": "2023-11-07T23:07:38.336503Z" - } - }, - "id": "702adaec008a6ec8" + ] }, { "cell_type": "markdown", + "id": "a947e49e27c734e9", + "metadata": {}, "source": [ "## Results" - ], - "metadata": { - "collapsed": false - }, - "id": "a947e49e27c734e9" + ] }, { "cell_type": "code", "execution_count": 22, + "id": "4106acf2adad01d7", + "metadata": { + "ExecuteTime": { + "end_time": "2023-11-07T23:07:38.445284Z", + "start_time": "2023-11-07T23:07:38.401884Z" + } + }, "outputs": [ { "name": "stdout", @@ -629,19 +3413,18 @@ "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-07T23:07:38.445284Z", - "start_time": "2023-11-07T23:07:38.401884Z" - } - }, - "id": "4106acf2adad01d7" + ] }, { "cell_type": "code", "execution_count": 23, + "id": "c58106faf0fc7f4e", + "metadata": { + "ExecuteTime": { + "end_time": "2023-11-07T23:07:40.521741Z", + "start_time": "2023-11-07T23:07:38.405889Z" + } + }, "outputs": [ { "name": "stdout", @@ -664,29 +3447,26 @@ "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-07T23:07:40.521741Z", - "start_time": "2023-11-07T23:07:38.405889Z" - } - }, - "id": "c58106faf0fc7f4e" + ] }, { "cell_type": "markdown", + "id": "4068a0b6460f19ab", + "metadata": {}, "source": [ "# 10 ROUTES (because I can)" - ], - "metadata": { - "collapsed": false - }, - "id": "4068a0b6460f19ab" + ] }, { "cell_type": "code", "execution_count": 24, + "id": "5995d6556f940e67", + "metadata": { + "ExecuteTime": { + "end_time": "2023-11-07T23:09:18.376367Z", + "start_time": "2023-11-07T23:07:40.529888Z" + } + }, "outputs": [ { "name": "stderr", @@ -706,29 +3486,26 @@ " centroids.append([42.365 + i * 0.01, -71.054 + i * 0.01])\n", "\n", "_, routes = utils.cluster_and_optimize(TotalList, centroids, northeastern_coordinate, time_diff=0.5, max_time=24)" - ], - "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2023-11-07T23:09:18.376367Z", - "start_time": "2023-11-07T23:07:40.529888Z" - } - }, - "id": "5995d6556f940e67" + ] }, { "cell_type": "markdown", + "id": "8c6f5aeb5e6c2832", + "metadata": {}, "source": [ "## Create JSON" - ], - "metadata": { - "collapsed": false - }, - "id": "8c6f5aeb5e6c2832" + ] }, { "cell_type": "code", "execution_count": 25, + "id": "375b090921cab03e", + "metadata": { + "ExecuteTime": { + "end_time": "2023-11-07T23:09:18.395941Z", + "start_time": "2023-11-07T23:09:18.378652Z" + } + }, "outputs": [], "source": [ "# Create a JSON request for the API\n", @@ -736,19 +3513,18 @@ "route_strings = []\n", "for route in routes:\n", " route_strings.append(utils.list_to_string(route))" - ], - "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2023-11-07T23:09:18.395941Z", - "start_time": "2023-11-07T23:09:18.378652Z" - } - }, - "id": "375b090921cab03e" + ] }, { "cell_type": "code", "execution_count": 26, + "id": "74f619c6df3bd6c4", + "metadata": { + "ExecuteTime": { + "end_time": "2023-11-07T23:09:21.149586Z", + "start_time": "2023-11-07T23:09:18.384347Z" + } + }, "outputs": [], "source": [ "# Create a dataframe from the JSON\n", @@ -758,19 +3534,18 @@ " \n", "# Concatenate the dataframes\n", "df = pd.concat(dfs, ignore_index=True)" - ], - "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2023-11-07T23:09:21.149586Z", - "start_time": "2023-11-07T23:09:18.384347Z" - } - }, - "id": "74f619c6df3bd6c4" + ] }, { "cell_type": "code", "execution_count": 30, + "id": "488924ebe78c61aa", + "metadata": { + "ExecuteTime": { + "end_time": "2023-11-07T23:10:17.312994Z", + "start_time": "2023-11-07T23:10:17.307208Z" + } + }, "outputs": [ { "name": "stderr", @@ -788,24 +3563,244 @@ "# Add columns for the route number\n", "for i in range(len(routes)):\n", " df['route'].iloc[i * len(routes[i]):(i + 1) * len(routes[i])] = i + 1" - ], - "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2023-11-07T23:10:17.312994Z", - "start_time": "2023-11-07T23:10:17.307208Z" - } - }, - "id": "488924ebe78c61aa" + ] }, { "cell_type": "code", "execution_count": 31, + "id": "8e436ba5d3949420", + "metadata": { + "ExecuteTime": { + "end_time": "2023-11-07T23:10:19.393773Z", + "start_time": "2023-11-07T23:10:19.390813Z" + } + }, "outputs": [ { "data": { - "text/plain": " waypoint_index trips_index \\\n0 0 0 \n1 1 0 \n2 2 0 \n3 3 0 \n4 4 0 \n.. ... ... \n184 11 0 \n185 12 0 \n186 13 0 \n187 14 0 \n188 15 0 \n\n hint distance \\\n0 1IwsgDuNLIBFAAAAWgEAAA8AAAAAAAAAFQP1QGa9GUI7qN... 8.262982 \n1 LRUugHAVLoA1AAAA7wEAAKAAAADqAAAAYZa9QBEBXEIOWo... 1.865658 \n2 lM4AgM3LAIAEAAAAHAAAAJEAAAC_AgAAyLv6PxJ7NEGyPn... 2.242639 \n3 ZQ0fgPINH4AgAAAAEQAAAFEAAAAqAAAArYRYQRHu20BfWQ... 48.627645 \n4 HR8ugIJiBICVAQAARwAAAAAAAACLAAAAQ1M0Qu3l-EAAAA... 0.645763 \n.. ... ... \n184 -2EugABiLoCcAQAAigAAAAAAAAAAAAAAMQI3QqZ0dUEAAA... 7.363621 \n185 VSIfgAYjH4AUAAAAAAAAACUBAADDAAAAaIcPQAAAAADYBw... 18.888832 \n186 0OEhgPvhIYADAAAABgAAAA8AAAA0AAAA2lq-PipQFD-Y-N... 2.009578 \n187 C-AhgGbgIYBZAAAAMQAAAAAAAABqAAAAj5QfQS1zq0AAAA... 4.887502 \n188 DoUhgBeFIYCcAAAAJgAAAAAAAAARAAAAm0CKQdkZiEAAAA... 0.236958 \n\n name location lat \\\n0 [-71.053931, 42.365054] -71.053931 \n1 [-71.061735, 42.369195] -71.061735 \n2 Miller's River Littoral Way [-71.065634, 42.371832] -71.065634 \n3 [-71.06828, 42.369868] -71.068280 \n4 [-71.094764, 42.377355] -71.094764 \n.. ... ... ... \n184 [-71.102659, 42.382131] -71.102659 \n185 [-71.110851, 42.374259] -71.110851 \n186 [-71.085166, 42.349997] -71.085166 \n187 [-71.091358, 42.348977] -71.091358 \n188 Northeastern (Inbound) [-71.090331, 42.339762] -71.090331 \n\n lon route \n0 42.365054 1 \n1 42.369195 1 \n2 42.371832 1 \n3 42.369868 4 \n4 42.377355 1 \n.. ... ... \n184 42.382131 10 \n185 42.374259 6 \n186 42.349997 6 \n187 42.348977 6 \n188 42.339762 6 \n\n[189 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
0001IwsgDuNLIBFAAAAWgEAAA8AAAAAAAAAFQP1QGa9GUI7qN...8.262982[-71.053931, 42.365054]-71.05393142.3650541
110LRUugHAVLoA1AAAA7wEAAKAAAADqAAAAYZa9QBEBXEIOWo...1.865658[-71.061735, 42.369195]-71.06173542.3691951
220lM4AgM3LAIAEAAAAHAAAAJEAAAC_AgAAyLv6PxJ7NEGyPn...2.242639Miller's River Littoral Way[-71.065634, 42.371832]-71.06563442.3718321
330ZQ0fgPINH4AgAAAAEQAAAFEAAAAqAAAArYRYQRHu20BfWQ...48.627645[-71.06828, 42.369868]-71.06828042.3698684
440HR8ugIJiBICVAQAARwAAAAAAAACLAAAAQ1M0Qu3l-EAAAA...0.645763[-71.094764, 42.377355]-71.09476442.3773551
..............................
184110-2EugABiLoCcAQAAigAAAAAAAAAAAAAAMQI3QqZ0dUEAAA...7.363621[-71.102659, 42.382131]-71.10265942.38213110
185120VSIfgAYjH4AUAAAAAAAAACUBAADDAAAAaIcPQAAAAADYBw...18.888832[-71.110851, 42.374259]-71.11085142.3742596
1861300OEhgPvhIYADAAAABgAAAA8AAAA0AAAA2lq-PipQFD-Y-N...2.009578[-71.085166, 42.349997]-71.08516642.3499976
187140C-AhgGbgIYBZAAAAMQAAAAAAAABqAAAAj5QfQS1zq0AAAA...4.887502[-71.091358, 42.348977]-71.09135842.3489776
188150DoUhgBeFIYCcAAAAJgAAAAAAAAARAAAAm0CKQdkZiEAAAA...0.236958Northeastern (Inbound)[-71.090331, 42.339762]-71.09033142.3397626
\n

189 rows × 9 columns

\n
" + "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
0001IwsgDuNLIBFAAAAWgEAAA8AAAAAAAAAFQP1QGa9GUI7qN...8.262982[-71.053931, 42.365054]-71.05393142.3650541
110LRUugHAVLoA1AAAA7wEAAKAAAADqAAAAYZa9QBEBXEIOWo...1.865658[-71.061735, 42.369195]-71.06173542.3691951
220lM4AgM3LAIAEAAAAHAAAAJEAAAC_AgAAyLv6PxJ7NEGyPn...2.242639Miller's River Littoral Way[-71.065634, 42.371832]-71.06563442.3718321
330ZQ0fgPINH4AgAAAAEQAAAFEAAAAqAAAArYRYQRHu20BfWQ...48.627645[-71.06828, 42.369868]-71.06828042.3698684
440HR8ugIJiBICVAQAARwAAAAAAAACLAAAAQ1M0Qu3l-EAAAA...0.645763[-71.094764, 42.377355]-71.09476442.3773551
..............................
184110-2EugABiLoCcAQAAigAAAAAAAAAAAAAAMQI3QqZ0dUEAAA...7.363621[-71.102659, 42.382131]-71.10265942.38213110
185120VSIfgAYjH4AUAAAAAAAAACUBAADDAAAAaIcPQAAAAADYBw...18.888832[-71.110851, 42.374259]-71.11085142.3742596
1861300OEhgPvhIYADAAAABgAAAA8AAAA0AAAA2lq-PipQFD-Y-N...2.009578[-71.085166, 42.349997]-71.08516642.3499976
187140C-AhgGbgIYBZAAAAMQAAAAAAAABqAAAAj5QfQS1zq0AAAA...4.887502[-71.091358, 42.348977]-71.09135842.3489776
188150DoUhgBeFIYCcAAAAJgAAAAAAAAARAAAAm0CKQdkZiEAAAA...0.236958Northeastern (Inbound)[-71.090331, 42.339762]-71.09033142.3397626
\n", + "

189 rows × 9 columns

\n", + "
" + ], + "text/plain": [ + " waypoint_index trips_index \\\n", + "0 0 0 \n", + "1 1 0 \n", + "2 2 0 \n", + "3 3 0 \n", + "4 4 0 \n", + ".. ... ... \n", + "184 11 0 \n", + "185 12 0 \n", + "186 13 0 \n", + "187 14 0 \n", + "188 15 0 \n", + "\n", + " hint distance \\\n", + "0 1IwsgDuNLIBFAAAAWgEAAA8AAAAAAAAAFQP1QGa9GUI7qN... 8.262982 \n", + "1 LRUugHAVLoA1AAAA7wEAAKAAAADqAAAAYZa9QBEBXEIOWo... 1.865658 \n", + "2 lM4AgM3LAIAEAAAAHAAAAJEAAAC_AgAAyLv6PxJ7NEGyPn... 2.242639 \n", + "3 ZQ0fgPINH4AgAAAAEQAAAFEAAAAqAAAArYRYQRHu20BfWQ... 48.627645 \n", + "4 HR8ugIJiBICVAQAARwAAAAAAAACLAAAAQ1M0Qu3l-EAAAA... 0.645763 \n", + ".. ... ... \n", + "184 -2EugABiLoCcAQAAigAAAAAAAAAAAAAAMQI3QqZ0dUEAAA... 7.363621 \n", + "185 VSIfgAYjH4AUAAAAAAAAACUBAADDAAAAaIcPQAAAAADYBw... 18.888832 \n", + "186 0OEhgPvhIYADAAAABgAAAA8AAAA0AAAA2lq-PipQFD-Y-N... 2.009578 \n", + "187 C-AhgGbgIYBZAAAAMQAAAAAAAABqAAAAj5QfQS1zq0AAAA... 4.887502 \n", + "188 DoUhgBeFIYCcAAAAJgAAAAAAAAARAAAAm0CKQdkZiEAAAA... 0.236958 \n", + "\n", + " name location lat \\\n", + "0 [-71.053931, 42.365054] -71.053931 \n", + "1 [-71.061735, 42.369195] -71.061735 \n", + "2 Miller's River Littoral Way [-71.065634, 42.371832] -71.065634 \n", + "3 [-71.06828, 42.369868] -71.068280 \n", + "4 [-71.094764, 42.377355] -71.094764 \n", + ".. ... ... ... \n", + "184 [-71.102659, 42.382131] -71.102659 \n", + "185 [-71.110851, 42.374259] -71.110851 \n", + "186 [-71.085166, 42.349997] -71.085166 \n", + "187 [-71.091358, 42.348977] -71.091358 \n", + "188 Northeastern (Inbound) [-71.090331, 42.339762] -71.090331 \n", + "\n", + " lon route \n", + "0 42.365054 1 \n", + "1 42.369195 1 \n", + "2 42.371832 1 \n", + "3 42.369868 4 \n", + "4 42.377355 1 \n", + ".. ... ... \n", + "184 42.382131 10 \n", + "185 42.374259 6 \n", + "186 42.349997 6 \n", + "187 42.348977 6 \n", + "188 42.339762 6 \n", + "\n", + "[189 rows x 9 columns]" + ] }, "metadata": {}, "output_type": "display_data" @@ -814,34 +3809,1297 @@ "source": [ "# Display the dataframe\n", "display(df)" - ], - "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2023-11-07T23:10:19.393773Z", - "start_time": "2023-11-07T23:10:19.390813Z" - } - }, - "id": "8e436ba5d3949420" + ] }, { "cell_type": "markdown", + "id": "1552586cb84a48c5", + "metadata": {}, "source": [ "## Map" - ], - "metadata": { - "collapsed": false - }, - "id": "1552586cb84a48c5" + ] }, { "cell_type": "code", "execution_count": 37, + "id": "4305c6981e48e87f", + "metadata": { + "ExecuteTime": { + "end_time": "2023-11-07T23:13:22.619199Z", + "start_time": "2023-11-07T23:13:22.527566Z" + } + }, "outputs": [ { "data": { - "text/plain": "", - "text/html": "
Make this Notebook Trusted to load map: File -> Trust Notebook
" + "text/html": [ + "
Make this Notebook Trusted to load map: File -> Trust Notebook
" + ], + "text/plain": [ + "" + ] }, "execution_count": 37, "metadata": {}, @@ -864,29 +5122,26 @@ " \n", "# Display the map\n", "m" - ], - "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2023-11-07T23:13:22.619199Z", - "start_time": "2023-11-07T23:13:22.527566Z" - } - }, - "id": "4305c6981e48e87f" + ] }, { "cell_type": "markdown", + "id": "4723f2f26efe49d3", + "metadata": {}, "source": [ "## Results" - ], - "metadata": { - "collapsed": false - }, - "id": "4723f2f26efe49d3" + ] }, { "cell_type": "code", "execution_count": 36, + "id": "5887f93dd890bc77", + "metadata": { + "ExecuteTime": { + "end_time": "2023-11-07T23:12:32.990935Z", + "start_time": "2023-11-07T23:12:32.987254Z" + } + }, "outputs": [ { "name": "stdout", @@ -912,19 +5167,18 @@ " route_waypoints.append(len(route))\n", "for i in range(len(route_waypoints)):\n", " print(\"Route {} has {} waypoints\".format(i + 1, route_waypoints[i]))" - ], - "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2023-11-07T23:12:32.990935Z", - "start_time": "2023-11-07T23:12:32.987254Z" - } - }, - "id": "5887f93dd890bc77" + ] }, { "cell_type": "code", "execution_count": 34, + "id": "3a4b529fc3f2b336", + "metadata": { + "ExecuteTime": { + "end_time": "2023-11-07T23:11:11.477373Z", + "start_time": "2023-11-07T23:11:08.172393Z" + } + }, "outputs": [ { "name": "stdout", @@ -951,44 +5205,34 @@ " northeastern_coordinate))\n", "for i in range(len(trip_hrs)):\n", " print(\"The trip will take {} hours\".format(trip_hrs[i]))" - ], - "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2023-11-07T23:11:11.477373Z", - "start_time": "2023-11-07T23:11:08.172393Z" - } - }, - "id": "3a4b529fc3f2b336" + ] }, { "cell_type": "code", "execution_count": null, + "id": "a4cf5509f890423a", + "metadata": {}, "outputs": [], - "source": [], - "metadata": { - "collapsed": false - }, - "id": "a4cf5509f890423a" + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.6" + "pygments_lexer": "ipython3", + "version": "3.11.4" } }, "nbformat": 4, diff --git a/ZestySalesman.py b/ZestySalesman.py new file mode 100644 index 0000000..1901a0e --- /dev/null +++ b/ZestySalesman.py @@ -0,0 +1,368 @@ +#!/usr/bin/env python +# coding: utf-8 + +# In[1]: + + +import pandas as pd +import folium +import utils + + +# In[2]: + + +# Load the data +ListA = pd.read_csv('List A.csv') +ListB = pd.read_csv('List B.csv') +ListC = pd.read_csv('List C.csv') +ListD = pd.read_csv('List D.csv') + + +# In[3]: + + +# Create two centroids, one in the North End and one in the Seaport District +centroids = [[42.365, -71.054], [42.351, -71.045]] + +northeastern_coordinate = "-71.09033,42.33976" + + +# In[4]: + + +# Combine the two lists and add a column to indicate the list +ListA['list'] = 'A' +ListB['list'] = 'B' +ListC['list'] = 'C' +ListD['list'] = 'D' + +TotalList = pd.concat([ListA, ListB, ListC, ListD]) + + +# In[5]: + + +# Remove all columns but name and gps +TotalList = TotalList[['name', 'gps', 'list']] + + +# In[6]: + + +# Convert the gps column to a list of lists for k-means +TotalList['gps'] = TotalList['gps'].apply(lambda x: x.strip('[]').split(',')) +TotalList['gps'] = TotalList['gps'].apply(lambda x: [float(i) for i in x]) + + +# In[7]: + + +display(TotalList) + + +# # 2 Routes + +# ## Cluster and Minimize + +# In[8]: + + +# Cluster and minimize the data +_, routes = utils.cluster_and_optimize(TotalList, centroids, northeastern_coordinate, + time_diff=0.25, max_time=24) + +route_1_coordinates = routes[0] +route_2_coordinates = routes[1] + + +# ## Create JSON + +# In[9]: + + +# Create a JSON request for the API +# This is the data we want to get from the API +route_1 = utils.list_to_string(route_1_coordinates) +route_2 = utils.list_to_string(route_2_coordinates) + + +# In[10]: + + +# Create a dataframe from the JSON +df1 = utils.create_json_df(route_1, utils.list_to_string([centroids[0]]), northeastern_coordinate) +df2 = utils.create_json_df(route_2, utils.list_to_string([centroids[1]]), northeastern_coordinate) + + +# In[11]: + + +# Add columns for the route number +df1['route'] = 1 +df2['route'] = 2 + +# Concatenate the two dataframes +df = pd.concat([df1, df2], ignore_index=True) + + +# In[12]: + + +display(df) + + +# ## Map + +# In[13]: + + +# Create a map +m = folium.Map(location=[df['lon'].mean(), df['lat'].mean()], zoom_start=11) + +# Add the points and lines for the two routes with different colors +colors = ['red', 'blue'] + +for route in df['route'].unique(): + df_route = df[df['route'] == route] + folium.PolyLine(df_route[['lon', 'lat']].values.tolist(), color=colors[route - 1]).add_to(m) + for i in range(len(df_route)): + folium.CircleMarker(df_route[['lon', 'lat']].iloc[i].values.tolist(), radius=3, color=colors[route - 1]).add_to( + m) + +# Display the map +m + + +# ## Results + +# In[14]: + + +# Get the number of waypoints for each route +route_1_waypoints = len(route_1_coordinates) +route_2_waypoints = len(route_2_coordinates) +print("Route 1 has {} waypoints".format(route_1_waypoints)) +print("Route 2 has {} waypoints".format(route_2_waypoints)) + + +# In[15]: + + +trip_hrs_1 = utils.get_trip_time(route_1, route_1_waypoints, utils.list_to_string([centroids[0]]), + northeastern_coordinate) +print("The trip will take {} hours".format(trip_hrs_1)) +trip_hrs_2 = utils.get_trip_time(route_2, route_2_waypoints, utils.list_to_string([centroids[1]]), + northeastern_coordinate) +print("The trip will take {} hours".format(trip_hrs_2)) + + +# # 3 Routes + +# In[16]: + + +# Cluster and minimize the data +# Add a third centroid in the Financial District +centroids.append([42.356, -71.055]) +_, routes = utils.cluster_and_optimize(TotalList, centroids, northeastern_coordinate, time_diff=0.3, max_time=24) + +route_1_coordinates = routes[0] +route_2_coordinates = routes[1] +route_3_coordinates = routes[2] + + +# ## Create JSON + +# In[17]: + + +# Create a JSON request for the API +# This is the data we want to get from the API +route_1 = utils.list_to_string(route_1_coordinates) +route_2 = utils.list_to_string(route_2_coordinates) +route_3 = utils.list_to_string(route_3_coordinates) + + +# In[18]: + + +# Create a dataframe from the JSON +df1 = utils.create_json_df(route_1, utils.list_to_string([centroids[0]]), northeastern_coordinate) +df2 = utils.create_json_df(route_2, utils.list_to_string([centroids[1]]), northeastern_coordinate) +df3 = utils.create_json_df(route_3, utils.list_to_string([centroids[2]]), northeastern_coordinate) + + +# In[19]: + + +# Add columns for the route number +df1['route'] = 1 +df2['route'] = 2 +df3['route'] = 3 + +# Concatenate the three dataframes +df = pd.concat([df1, df2, df3], ignore_index=True) + + +# In[20]: + + +display(df) + + +# ## Map + +# In[21]: + + +# Create a map +m = folium.Map(location=[df['lon'].mean(), df['lat'].mean()], zoom_start=11) + +# Add the points and lines for the three routes with different colors +colors = ['red', 'blue', 'green'] + +for route in df['route'].unique(): + df_route = df[df['route'] == route] + folium.PolyLine(df_route[['lon', 'lat']].values.tolist(), color=colors[route - 1]).add_to(m) + for i in range(len(df_route)): + folium.CircleMarker(df_route[['lon', 'lat']].iloc[i].values.tolist(), radius=3, color=colors[route - 1]).add_to( + m) + +# Display the map +m + + +# ## Results + +# In[22]: + + +# Get the number of waypoints for each route +route_1_waypoints = len(route_1_coordinates) +route_2_waypoints = len(route_2_coordinates) +route_3_waypoints = len(route_3_coordinates) +print("Route 1 has {} waypoints".format(route_1_waypoints)) +print("Route 2 has {} waypoints".format(route_2_waypoints)) +print("Route 3 has {} waypoints".format(route_3_waypoints)) + + +# In[23]: + + +# Get the trip time for each route +trip_hrs_1 = utils.get_trip_time(route_1, route_1_waypoints, utils.list_to_string([centroids[0]]), + northeastern_coordinate) +print("The trip will take {} hours".format(trip_hrs_1)) +trip_hrs_2 = utils.get_trip_time(route_2, route_2_waypoints, utils.list_to_string([centroids[1]]), + northeastern_coordinate) +print("The trip will take {} hours".format(trip_hrs_2)) +trip_hrs_3 = utils.get_trip_time(route_3, route_3_waypoints, utils.list_to_string([centroids[2]]), + northeastern_coordinate) +print("The trip will take {} hours".format(trip_hrs_3)) + + +# # 10 ROUTES (because I can) + +# In[24]: + + +# Cluster and minimize the data +# Add seven more centroids around Boston with different latitudes and longitudes +for i in range(7): + centroids.append([42.365 + i * 0.01, -71.054 + i * 0.01]) + +_, routes = utils.cluster_and_optimize(TotalList, centroids, northeastern_coordinate, time_diff=0.5, max_time=24) + + +# ## Create JSON + +# In[25]: + + +# Create a JSON request for the API +# This is the data we want to get from the API +route_strings = [] +for route in routes: + route_strings.append(utils.list_to_string(route)) + + +# In[26]: + + +# Create a dataframe from the JSON +dfs = [] +for i in range(len(routes)): + dfs.append(utils.create_json_df(route_strings[i], utils.list_to_string([centroids[i]]), northeastern_coordinate)) + +# Concatenate the dataframes +df = pd.concat(dfs, ignore_index=True) + + +# In[30]: + + +# Add columns for the route number +for i in range(len(routes)): + df['route'].iloc[i * len(routes[i]):(i + 1) * len(routes[i])] = i + 1 + + +# In[31]: + + +# Display the dataframe +display(df) + + +# ## Map + +# In[37]: + + +# Create a map +m = folium.Map(location=[df['lon'].mean(), df['lat'].mean()], zoom_start=11) + +# Add the points and lines for the three routes with different colors +colors = ['red', 'blue', 'green', 'orange', 'purple', 'pink', 'black', 'gray', 'brown', 'yellow'] + +for route in df['route'].unique(): + df_route = df[df['route'] == route] + folium.PolyLine(df_route[['lon', 'lat']].values.tolist(), color=colors[route - 1]).add_to(m) + for i in range(len(df_route)): + folium.CircleMarker(df_route[['lon', 'lat']].iloc[i].values.tolist(), radius=3, color=colors[route - 1]).add_to( + m) + +# Display the map +m + + +# ## Results + +# In[36]: + + +# Get the number of waypoints for each route +route_waypoints = [] +for route in routes: + route_waypoints.append(len(route)) +for i in range(len(route_waypoints)): + print("Route {} has {} waypoints".format(i + 1, route_waypoints[i])) + + +# In[34]: + + +# Get the trip time for each route +trip_hrs = [] +for i in range(len(routes)): + trip_hrs.append(utils.get_trip_time(route_strings[i], route_waypoints[i], utils.list_to_string([centroids[i]]), + northeastern_coordinate)) +for i in range(len(trip_hrs)): + print("The trip will take {} hours".format(trip_hrs[i])) + + +# In[ ]: + + + + diff --git a/ZestySalesmanNoMinimize.ipynb b/ZestySalesmanNoMinimize.ipynb deleted file mode 100644 index 9632a4a..0000000 --- a/ZestySalesmanNoMinimize.ipynb +++ /dev/null @@ -1,706 +0,0 @@ -{ - "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 -} diff --git a/utils.py b/utils.py index 3709724..c37c64a 100644 --- a/utils.py +++ b/utils.py @@ -1,4 +1,3 @@ -import folium import numpy as np import pandas as pd import requests @@ -261,4 +260,4 @@ 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 \ No newline at end of file + return ((coordinate1[0] - coordinate2[0]) ** 2 + (coordinate1[1] - coordinate2[1]) ** 2) ** 0.5 -- cgit v1.2.3