diff --git a/notebooks/MC_RAPTOR.ipynb b/notebooks/MC_RAPTOR.ipynb index 1bc8d11..ceca5a8 100644 --- a/notebooks/MC_RAPTOR.ipynb +++ b/notebooks/MC_RAPTOR.ipynb @@ -1,1060 +1,1060 @@ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# McRAPTOR for latest departure problem \n", "\n", "*Authors: Felix Grimberg, Cyril Pulver*\n", "\n", "Multiple-criteria adaptation of RAPTOR (Round bAsed Public Transit Optimized Router) algorithm (cf. [README](../README.md)).\n", "\n", "## Left out at this stage:\n", "\n", "- Realistic time to get out of one transport and walk to the platform of the next. Instead, we just set it to 2 minutes, no matter what.\n", "We also reserve 2 minutes for walking from the entrance of a station to the correct platform, and vice versa, when walking between stations (total: `time_to_walk_distance + 4 minutes`)\n", "\n", "# Run the algorithm\n", "\n", "Please make sure that you follow these steps:\n", "1. Execute `git lfs pull` in a terminal to download our data files (if you haven't already).\n", - "2. **Execute the code cells in the other sections of this notebook (\"Load the data and onwards\") to load the data and define the classes and functions that make up the algorithm.**\n", - "3. Execute all three cells below\n", + "2. **Execute the code cells in the other sections of this notebook (\"Load the data\") and onwards to load the data and define the classes and functions that make up the algorithm.**\n", + "3. Execute all three cells below (grid, log and output)\n", "4. Plan journeys !" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "After executing the two cells below, please enter:\n", "- The departure stop (only BPUIC-like stop ID are currently supported, with a minimum of 7 characters. Platform information etc. is tolerated, but ignored)\n", "- The arrival (target) stop (same format)\n", "- The arrival time (format HH:mm)\n", "- The minimum probability of success for the journey (slider)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Execute to set input\n", "grid" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "log" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# execute to get output\n", "output" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load the data\n", "### General considerations\n", "We adhere to the data structures proposed by Delling et al. These structures aim to minimize read times in memory by making use of consecutive in-memory adresses. Thus, structures with varying dimensions (e.g dataframes, python lists) are excluded. We illustrate the difficulty with an example. \n", "\n", "Each route has a potentially unique number of stops. Therefore, we cannot store stops in a 2D array of routes by stops, as the number of stops is not the same for each route. We adress this problem by storing stops consecutively by route, and keeping track of the index of the first stop for each route.\n", "\n", "This general strategy is applied to all the required data structures, where possible.\n", "\n", "### routes\n", "The `routes` array will contain arrays `[n_trips, n_stops, pt_1st_stop, pt_1st_trip]` where all four values are `int`. To avoid overcomplicating things and try to mimic pointers in python, `pt_1st_stop` and `pt_1st_trip` contain integer indices." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "lines_to_next_cell": 0 }, "outputs": [], "source": [ "import numpy as np\n", "import pickle\n", "\n", "def pkload(path):\n", " with open(path, 'rb') as f:\n", " obj = pickle.load(f)\n", " return obj" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "lines_to_next_cell": 0 }, "outputs": [], "source": [ "routes = pkload(\"../data/routes_array_cyril.pkl\").astype(np.uint32)\n", "print(routes.shape)\n", "routes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### routeStops\n", "`routeStops` is an array that contains the ordered lists of stops for each route. `pt_1st_stop` in `routes` is required to get to the first stop of the route. is itself an array that contains the sequence of stops for route $r_i$." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "routeStops = pkload(\"../data/route_stops_array_cyril.pkl\").astype(np.uint16)\n", "print(routeStops.shape)\n", "print(routeStops.max())\n", "routeStops" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### stopTimes\n", "\n", "The i-th entry in the `stopTimes` array is itself an array which contains the arrival and departure time at a particular stop for a particular trip. `stopTimes` is sorted by routes, and then by trips. We retrieve the index of the first (earliest) trip of the route with the pointer `pt_1st_trip` stored in `routes`. We may use the built-in `numpy` [date and time data structures](https://blog.finxter.com/how-to-work-with-dates-and-times-in-python/). In short, declaring dates and times is done like this: `np.datetime64('YYYY-MM-DDThh:mm')`. Entries with a `NaT` arrival or departure times correspond to beginning and end of trips respectively.\n", "\n", "Note that trips are indexed implicitely in stopTimes, but we decided to change a little bit from the paper and index them according to their parent route instead of giving them an absolute index. It makes things a bit easier when coding the algorithm." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "stopTimes = pkload(\"../data/stop_times_array_cyril.pkl\")\n", "print(stopTimes.shape)\n", "stopTimes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`NaT` is the `None` equivalent for `numpy datetime64`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(np.isnat(stopTimes[0]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### stopRoutes\n", "\n", "`stopRoutes` contains the routes (as `int`s representing an index in `routes`) associated with each stop. We need the pointer in `stops` to index `stopRoutes` correctly." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "stopRoutes = pkload(\"../data/stop_routes_array_cyril.pkl\").flatten().astype(np.uint32)\n", "print(stopRoutes.shape)\n", "stopRoutes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### transfers\n", "`transfers` is a 2D `np.ndarray` where each entry `[p_j, time]` represents (in seconds) the time it takes to walk from stop p_j to the implicitely given stop p_i.\n", "p_i is given implicitely by the indexing, in conjunction with `stops`. In other words:\n", "`transfers[stops[p_i][2]:stops[p_i][3]]` returns all the footpaths arriving at stop p_i.\n", "\n", "As we cannot store different data types in numpy arras, `time` will have to be converted to `np.timedelta64`, the format used to make differences between `np.datetime.64` variables. We will consider all `time` values as **positive values in seconds**." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "transfers = pkload(\"../data/transfer_array_cyril.pkl\").astype(np.uint16)\n", "print(transfers.shape)\n", "transfers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### stops\n", "\n", "`stops` stores the indices in `stopRoutes` and in `transfers` corresponding to each stop.\n", "\n", "`stopRoutes[stops[p][0]:stops[p][1]]` returns the routes serving stop p.\n", "\n", "`transfers[stops[p][2]:stops[p][3]]` returns the footpaths arriving at stop p." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "stops = pkload(\"../data/stops_array_cyril.pkl\")\n", "stopRoutes = pkload(\"../data/stop_routes_array_cyril.pkl\")\n", "print(np.isnan(stops.astype(np.float64)).sum(axis=0))\n", "print(np.equal(stops, None).sum(axis=0))\n", "print(stops.shape)\n", "stops = stops[:,[0,0,1,1]]\n", "# Make column 1 contain the start_index of the next stop in stopRoutes\n", "stops[:-1,1] = stops[1:,0]\n", "stops[-1, 1] = stopRoutes.shape[0]\n", "# Deal with NaN's in column 2 (for stops with 0 foot transfers within 500m)\n", "if np.isnan(stops[-1,2]).item():\n", " stops[-1,2] = transfers.shape[0]\n", "for i in np.isnan(stops[:-1,2].astype(np.float64)).nonzero()[0][::-1]:\n", " stops[i,2] = stops[i+1,2]\n", "print(np.isnan(stops.astype(np.float64)).sum(axis=0))\n", "# Make column 3 contain the start_index of the next stop in stopRoutes\n", "stops[:-1,3] = stops[1:,2]\n", "stops[-1, 3] = transfers.shape[0]\n", "# Convert to int\n", "stops = stops.astype(np.uint32)\n", "stops" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p = 0\n", "routes_serving_p = stopRoutes[stops[p][0]:stops[p][1]]\n", "print(\"routes serving stop 0:\", routes_serving_p)\n", "for r in routes_serving_p:\n", " print(\"stops of route {}:\".format(r), routeStops[routes[r][2]:routes[r][2]+routes[r][1]])\n", "for pPrime, walking_seconds in transfers[stops[p][2]:stops[p][3]]:\n", " print(\"stop {} can be reached from stop {} by walking for {} seconds.\".format(p, pPrime, walking_seconds))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Distribution of delays" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import gzip \n", "with gzip.open(\"../data/transfer_probabilities.pkl.gz\") as distrib_pkl:\n", " distrib_delays = pickle.load(distrib_pkl)\n", " \n", "distrib_delays[0:2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Relate `stop_id`s and `trip_headsign`s to the integer indices used in the algorithm" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "stop_times_df = pkload(\"../data/stop_times_df_cyril.pkl\")[['route_id', 'stop_id_general', 'stop_name', 'trip_headsign', 'route_int', 'stop_int', 'route_desc']]\n", "stop_times_df.head()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "stop_ids_names = stop_times_df[['stop_id_general', 'stop_int', 'stop_name']].drop_duplicates()\n", "assert np.all(stop_ids_names == stop_ids_names.drop_duplicates(subset='stop_int'))\n", "assert np.all(stop_ids_names == stop_ids_names.drop_duplicates(subset='stop_id_general'))\n", "assert np.all(stop_ids_names == stop_ids_names.drop_duplicates(subset='stop_name'))\n", "stop_ids_names = stop_ids_names.sort_values(by='stop_int')\n", "stop_ids = stop_ids_names['stop_id_general'].to_numpy()\n", "stop_names = stop_ids_names['stop_name'].to_numpy()\n", "print(stop_ids, stop_names)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p = np.random.randint(stops.shape[0])\n", "print(p, stop_ids[p], stop_names[p])\n", "stop_times_df[stop_times_df['stop_int'] == p].head(2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Define the classes and functions making up the journey planner\n", "\n", "Based on a modified version of RAPTOR (reversed RAPTOR), we implement a multiple criteria RAPTOR algorithm.\n", "The optimization criteria are:\n", "- Latest departure\n", "- Highest probability of success of the entire trip\n", "- Lowest number of connections (implicit with the round-based approach)\n", "\n", "We specify the time to change platforms as an absolute constant:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tau_change_platform = np.timedelta64(2, 'm')\n", "np.datetime64('2020-05-11T15:30') - tau_change_platform" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, we define some shorthand functions to help with the indexing in the data structures:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def gen_random_stop_id():\n", " \"\"\"Generate a random stop_id to test the journey planner.\"\"\"\n", " return str(stop_ids[np.random.randint(stops.shape[0])])+':0:5DE'\n", "\n", "def stop_id_to_int(p_id):\n", " \"\"\"Given a stop id, returns the corresponding stop_int\"\"\"\n", " return np.asarray(stop_ids == int(p_id[:7])).nonzero()[0].item()\n", "\n", "def get_stops(r):\n", " \"\"\"Returns the stops of route r\"\"\"\n", " idx_first_stop = routes[r][2]\n", " return routeStops[idx_first_stop:idx_first_stop+routes[r][1]] # n_stops = routes[r][1]\n", "\n", "def calc_stopTimes_idx(r, t, offset_p):\n", " \"\"\"Returns the index of the entry in stopTimes\n", " corresponding to the offset_p-th stop of the t-th trip\n", " of route r.\n", " \"\"\"\n", " return (routes[r][3] # 1st trip of route\n", " + t * routes[r][1] # offset for the right trip\n", " + offset_p # offset for the right stop\n", " )\n", "\n", "def get_arrival_time(r, t, offset_p):\n", " \"\"\"Returns 2000 (instead of 0) if t is None.\n", " Otherwise, returns the arrival time of the t-th trip of route r\n", " at the offset_p-th stop of route r.\n", " trips and stops of route r start at t=0, offset_p=0.\n", " \"\"\"\n", " if t is None:\n", " return np.datetime64('2000-01-01T01:00')\n", " \n", " return stopTimes[calc_stopTimes_idx(r,t,offset_p)][0] # 0 for arrival time\n", "\n", "def get_departure_time(r, t, offset_p):\n", " \"\"\"Throws TypeError if t is None.\n", " Otherwise, returns the departure time of the t-th trip of route r\n", " at the offset_p-th stop of route r.\n", " trips and stops of route r start at t=0 & offset_p=0.\n", " \"\"\"\n", " if t is None:\n", " raise TypeError(\"Requested departure time of None trip!\")\n", " \n", " return stopTimes[calc_stopTimes_idx(r,t,offset_p)][1] # 1 for departure time\n", "\n", "# This one isn't indexing related. Forgive us.\n", "def time2str(t):\n", " \"\"\"Prints the hour and minutes of np.datetime64 t.\"\"\"\n", " return str(t.astype('datetime64[m]')).split('T')[1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the cell below, we define the following class hierarchy of label types:\n", "```\n", "BaseLabel\n", " +-- RouteLabel\n", " +-- ImmutableLabel\n", " +-- WalkLabel\n", " +-- TargetLabel\n", "```\n", "Labels come together recursively to represent a (partial) solution, going from the `stop` attribute of the first label to the target stop (that stop will be referred to as \"here\").\n", "Each partial solution is associated with a departure time `tau_dep`, an overall probability of success (all the way to the target stop) `Pr`, and a number of trips necessary to get from here to the target stop.\n", "Any two labels can be compared via their `dominates` method, to retain only non-dominated labels.\n", "Further, all types of labels implement a `print_instructions` method, used to recursively print instructions for the entire journey.\n", "Each `RouteLabel` corresponds to the portion of a journey that begins with standing on the correct platform to board a vehicle, and ends with exiting from that vehicle at another stop.\n", "Each `WalkLabel` corresponds to the portion of a journey that begins with walking away from the main hall of one stop and ends with having walked all the way to the main hall of another stop." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "class InstantiationException(Exception):\n", " pass\n", "\n", "class BaseLabel:\n", " \"\"\"An abstract base class for Labels. Do not instantiate.\n", " A label corresponds to a recursive (partial) solution, going\n", " to the target stop from the stop currently under consideration.\n", " \"\"\"\n", " indent = \" \"*4\n", " def __init__(self, stop, tau_dep, Pr, n_trips):\n", " self.stop = stop\n", " self.tau_dep = tau_dep\n", " self.Pr = Pr\n", " self.n_trips = n_trips\n", " \n", " def dominates(self, other):\n", " \"\"\"Returns True if self dominates other, else returns False.\n", " other: another Label instance.\n", " \"\"\"\n", " if self.tau_dep >= other.tau_dep \\\n", " and self.Pr >= other.Pr \\\n", " and self.n_trips <= other.n_trips :\n", " return True\n", " return False\n", " \n", " def stop2str(self):\n", " \"\"\"Returns a printable str representing self.stop\"\"\"\n", " return \"{stopN} (stop {stopI})\".format(\n", " stopN = stop_names[self.stop],\n", " stopI = stop_ids[self.stop]\n", " )\n", " \n", " def print_journey(self):\n", " print(\"Departure stop: \", self.stop2str())\n", " print(\"Departure time: \", time2str(self.tau_dep))\n", " print(\"Number of trips used: \", self.n_trips)\n", " print(\"Probability of success: {:.4f}\".format(self.Pr))\n", " \n", " self.print_instructions()\n", " \n", " def to_str(self):\n", " s = \"Departure at {0} from stop {1} (id: {2}, int: {3}).\".format(\n", " self.tau_dep,\n", " stop_names[self.stop],\n", " stop_ids[self.stop],\n", " self.stop\n", " )\n", " return repr(type(self)) + s\n", " \n", " def pprint(self, indent=''):\n", " print(indent, self.to_str())\n", " \n", " def copy(self):\n", " raise InstantiationException(\"class BaseLabel should never \"\n", " \"be instantiated.\"\n", " )\n", "\n", "class ImmutableLabel(BaseLabel):\n", " \"\"\"Base class for immutable Labels\"\"\"\n", " def copy(self):\n", " return self\n", "\n", "class TargetLabel(ImmutableLabel):\n", " \"\"\"A special type of label reserved for the target stop.\"\"\"\n", " def __init__(self, stop, tau_dep):\n", " BaseLabel.__init__(self, stop, tau_dep, 1., 0)\n", " \n", " def print_instructions(self):\n", " \"\"\"Finish printing instructions for the journey.\"\"\"\n", " print(\"Target stop: \", self.stop2str())\n", " print(\"Requested arrival time:\", time2str(self.tau_dep))\n", "\n", "class WalkLabel(ImmutableLabel):\n", " \"\"\"A special type of label for walking connections.\"\"\"\n", " def __init__(self, stop, tau_walk, next_label):\n", " \"\"\"Create a new WalkLabel instance.\n", " stop: stop where you start walking.\n", " tau_walk: (np.timedelta64) duration of the walk.\n", " next_label: label describing the rest of the trip after walking.\n", " \"\"\"\n", " if isinstance(next_label, WalkLabel):\n", " raise ValueError(\"Cannot chain two consecutive WalkLabels!\")\n", " tau_dep = next_label.tau_dep - tau_walk - tau_change_platform\n", " BaseLabel.__init__(self, stop, tau_dep, next_label.Pr, next_label.n_trips)\n", " self.tau_walk = tau_walk\n", " self.next_label = next_label\n", " \n", " def print_instructions(self):\n", " \"\"\"Recursively print instructions for the whole journey.\"\"\"\n", " print(self.indent + \"Walk {:.1f} minutes from\".format(\n", " self.tau_walk / np.timedelta64(1,'m')\n", " ),\n", " self.stop2str()\n", " )\n", " print(self.indent*2 + \"to\", self.next_label.stop2str())\n", " self.next_label.print_instructions()\n", "\n", "class RouteLabel(BaseLabel):\n", " \"\"\"A type of label for regular transports.\"\"\"\n", " def __init__(self,\n", " tau_dep,\n", " r,\n", " t,\n", " offset_p,\n", " next_label,\n", " Pr_connection_success\n", " ):\n", " \n", " self.tau_dep = tau_dep\n", " self.r = r\n", " self.t = t\n", " self.offset_p_in = offset_p\n", " self.offset_p_out = offset_p\n", " self.next_label = next_label\n", " # Store Pr_connection_success for self.copy()\n", " self.Pr_connection_success = Pr_connection_success\n", " \n", " self.route_stops = get_stops(self.r)\n", " self.stop = self.route_stops[self.offset_p_in]\n", " self.Pr = self.Pr_connection_success * self.next_label.Pr\n", " self.n_trips = self.next_label.n_trips + 1\n", " \n", " def update_stop(self, stop):\n", " self.stop = stop\n", " self.offset_p_in = self.offset_p_in - 1\n", " # Sanity check:\n", " assert self.offset_p_in >= 0\n", " assert self.route_stops[self.offset_p_in] == stop\n", " self.tau_dep = get_departure_time(self.r, self.t, self.offset_p_in)\n", " \n", " def print_instructions(self):\n", " \"\"\"Recursively print instructions for the whole journey.\"\"\"\n", " stopTimes_idx = calc_stopTimes_idx(self.r, self.t,\n", " self.offset_p_in)\n", " \n", " print(self.indent + \"At\", self.stop2str())\n", " print(self.indent*2 + \"take the\",\n", " stop_times_df['route_desc'][stopTimes_idx], \"to\",\n", " stop_times_df['trip_headsign'][stopTimes_idx]\n", " )\n", " print(self.indent*2 + \"leaving at\", time2str(self.tau_dep),\n", " \"(route id: {}).\".format(\n", " stop_times_df['route_id'][stopTimes_idx]\n", " )\n", " )\n", " \n", " tau_arr = get_arrival_time(\n", " self.r,\n", " self.t,\n", " self.offset_p_out\n", " )\n", " assert self.next_label.stop == self.route_stops[self.offset_p_out]\n", " \n", " print(self.indent + \"Get out at\", self.next_label.stop2str())\n", " print(self.indent*2 + \"at\", time2str(tau_arr)+\".\")\n", "\n", " self.next_label.print_instructions()\n", " \n", " def copy(self):\n", " \"\"\"When RouteLabels are merged into the bag of a stop,\n", " they must be copied (because they will subsequently\n", " be changed with self.update_stop()).\n", " \"\"\"\n", " l = RouteLabel(self.tau_dep,\n", " self.r,\n", " self.t,\n", " self.offset_p_in,\n", " self.next_label,\n", " self.Pr_connection_success\n", " )\n", " l.offset_p_out = self.offset_p_out\n", " return l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The functions below define the bag merge operation and implement the journey planner:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def run_mc_raptor(stop_id_start,\n", " stop_id_destination,\n", " arrival_time_max,\n", " Pr_min\n", " ):\n", " \"\"\"Run MC RAPTOR, using the data defined in cells above (stopRoutes etc.).\n", " Inputs:\n", " stop_id_start: source stop id\n", " str (format: '1234567')\n", " stop_id_destination: target stop id\n", " str (format: '1234567')\n", " arrival_time_max: latest acceptable arrival time\n", " str (format: '14:56')\n", " Pr_min: minimum acceptable probability of success\n", " float in open interval (0,1)\n", " Outputs:\n", " bags_p_s: bags_p_s[k] contains the Pareto set of non-dominated journeys\n", " from stop_id_start to stop_id_destination that use at most\n", " k different trips (i.e, getting in at most k different vehicles),\n", " under the given constraints:\n", " 1. Each journey must succeed with a probability\n", " greater or equal to Pr_min.\n", " 2. The journey is a success if and only if all individual\n", " connections succeed, including the user's appointment\n", " in p_t at tau_0.\n", " 3. A connection succeeds if, and only if, the user reaches\n", " the platform before or on the scheduled departure time\n", " (allowing some time to change platforms)\n", " Non-dominated:\n", " A journey J1 is *dominated* by another journey J2, if\n", " J2 departs no earlier than J1 AND the probability of\n", " success of J2 is no less than that of J1.\n", " Pareto set:\n", " Each bag in bags_p_s contains only journeys that are not\n", " dominated by any other possible journey. Such a collection\n", " of non-dominated solutions is called a *Pareto set*.\n", " bags: bags[k][stop_id_to_int(stop_id)] contains the Pareto set of\n", " non-dominated journeys from stop_id to stop_id_destination\n", " that use at most k different trips under the given constraints.\n", " \n", " Each journey is represented as a Label that forms the start of a chain.\n", " The journey can be reconstructed by calling label.print_journey().\n", " \"\"\"\n", "# Input sanitization:\n", " try:\n", " tau_0 = np.datetime64('2020-05-24T'+arrival_time_max)\n", " p_s = stop_id_to_int(stop_id_start)\n", " p_t = stop_id_to_int(stop_id_destination)\n", " except Exception as e:\n", " print(\"ERROR parsing input! Please make sure that:\")\n", " print(\"- stop_id_start and stop_id_destination are strings \"\n", " \"beginning with 7 or more digits.\")\n", " print(\"- arrival_time_max is in the format 'hh:mm'. Example: '09:42'.\")\n", " raise e\n", " \n", "# initialization\n", " k_max = 10 # Maximum number of rounds\n", " \n", " # For each route and for each label at each stop p, we will look at the n latest\n", " # trips until we find a trip for which the individual connection at stop p\n", " # succeeds with a probability at least equal to Pr_threshold.\n", " # Under some reasonable assumptions, setting Pr_threshold = Pr_min**(1/k)\n", " # guarantees that we will find a solution, if a solution exists involving at\n", " # most k connections (including the user's appointment in p_t at tau_0).\n", " Pr_threshold = Pr_min**(0.1)\n", " \n", " # Initialize empty bags for each stop for round 0:\n", " n_stops = stops.shape[0]\n", " bags = [\n", " [\n", " [] # an empty bag\n", " for _ in range(n_stops)] # one empty bag per stop\n", " ]\n", "\n", " # Create a TargetLabel for p_t, and mark p_t\n", " bags[0][p_t].append(TargetLabel(p_t, tau_0))\n", " marked = {p_t}\n", "\n", " print(\"Searching for journeys from\",\n", " stop_names[p_s],\n", " \"(stop {})\".format(stop_ids[p_s]),\n", " \"to\", bags[0][p_t][0].stop2str())\n", "\n", "# Define bag operations (they depend on p_s and Pr_min for target pruning):\n", " def update_bag(bag, label, k, target_pruning=True):\n", " \"\"\"Add label to bag and remove dominated labels.\n", " bag is altered in-place.\n", "\n", " k: Round number, used for target pruning.\n", "\n", " returns: Boolean indicating whether bag was altered.\n", " \"\"\"\n", " if target_pruning:\n", " # Apply the Pr_min constraint to label:\n", " if label.Pr < Pr_min:\n", " return False\n", "\n", " # Prune label if it is dominated by bags[k][p_s]:\n", " for L_star in bags[k][p_s]:\n", " if L_star.dominates(label):\n", " return False\n", "\n", " # Otherwise, merge label into bag1\n", " changed = False\n", " for L_old in bag:\n", " if L_old.dominates(label):\n", " return changed\n", " if label.dominates(L_old):\n", " bag.remove(L_old)\n", " changed = True\n", " bag.append(label.copy())\n", " return True\n", "\n", " def merge_bags(bag1, bag2, k, target_pruning=True):\n", " \"\"\"Merge bag2 into bag1 in-place.\n", " k: Round number, used for target pruning.\n", " returns: Boolean indicating whether bag was altered.\n", " \"\"\"\n", " changed = False\n", " for label in bag2:\n", " changed = update_bag(bag1, label, k, target_pruning) or changed\n", " return changed\n", " \n", " globals().update({'merge_bags': merge_bags, 'Pr_min':Pr_min})\n", " \n", "# Define the footpaths-checking function (depends on update_bag)\n", " def check_footpaths(bags, marked, k):\n", " \"\"\"Modify bags and marked in-place to account for foot-paths.\"\"\"\n", " q = []\n", " for p in marked:\n", " for pPrime, delta_seconds in transfers[stops[p][2]:stops[p][3]]:\n", " q.append((p, pPrime, delta_seconds))\n", " for p, pPrime, delta_seconds in q:\n", " for L_k in bags[k][p]:\n", " # We do not allow two consecutive walking trips\n", " if not isinstance(L_k, WalkLabel):\n", " L_new = WalkLabel(pPrime, np.timedelta64(delta_seconds, 's'), L_k)\n", " if update_bag(bags[k][pPrime], L_new, k):\n", " marked.add(pPrime)\n", "\n", "# main loop\n", " indent= ' '*4\n", "\n", " k = 0\n", " # Check footpaths leading to p_t at k=0:\n", " check_footpaths(bags, marked, k)\n", " while k < k_max:\n", " k += 1 # k=1 at fist round, as it should.\n", "\n", " # Instead of using best bags, carry over the bags from last round.\n", " # if len(bags <= k):\n", "\n", " bags.append([bags[-1][p].copy() for p in range(n_stops)])\n", "\n", " print('\\n', ' '*30, 'STARTING round k =', k)\n", " if len(marked) < 50:\n", " print('Marked stops at the start of the round:', [stop_ids[p] for p in marked])\n", " else:\n", " print('Number of marked stops at the start of the round:', len(marked))\n", " \n", " # accumulate routes serving marked stops from previous rounds\n", " q = []\n", " for p in marked:\n", " for r in stopRoutes[stops[p][0]:stops[p][1]]: # foreach route r serving p\n", " append_r_p = True\n", " for idx, (rPrime, pPrime) in enumerate(q): # is there already a stop from the same route in q ?\n", " if rPrime == r:\n", " append_r_p = False\n", " p_pos_in_r = np.where(get_stops(r) == p)[0][-1]\n", " pPrime_pos_in_r = np.where(get_stops(r) == pPrime)[0][-1]\n", " if p_pos_in_r > pPrime_pos_in_r:\n", " q[idx] = (r, p) # substituting (rPrime, pPrime) by (r, p)\n", " if append_r_p:\n", " q.append((r, p))\n", " marked.clear() # unmarking all stops\n", "# print(\"Queue:\", q)\n", "\n", "# print('Queue before traversing each route: {}'.format(q))\n", " # traverse each route\n", " for (r, p) in q:\n", "# print('\\n****TRAVERSING ROUTE r={0} from stop p={1}****'.format(r, p))\n", " B_route = [] # new empty route bag\n", "\n", " # we traverse the route backwards (starting at p, not from the end of the route)\n", " stops_of_current_route = get_stops(r)\n", "# print('Stops of current route:', stops_of_current_route)\n", " offset_p = np.asarray(stops_of_current_route == p).nonzero()[0]\n", " if offset_p.size < 1:\n", " print(\"WARNING: route {r} is said to serve stop {p} in stopRoutes, but stop {p} \"\n", " \"is not included as a stop of route {r} in routeStops...\".format(p=p, r=r))\n", " offset_p = -1\n", " else:\n", " offset_p = offset_p[-1]\n", " for offset_p_i in range(offset_p, -1, -1):\n", " p_i = stops_of_current_route[offset_p_i]\n", "# print('\\n\\n'+indent+\"p_i: {}\".format(p_i))\n", "\n", " # Update the labels of the route bag:\n", " for L in B_route:\n", " L.update_stop(p_i)\n", "\n", " # Merge B_route into bags[k][p_i]\n", " if merge_bags(bags[k][p_i], B_route, k):\n", "# print(\"marking stop\", p_i)\n", " marked.add(p_i)\n", "\n", " # Can we step out of a later trip at p_i ?\n", " # This is only possible if we already know a way to get from p_i to p_t in < k vehicles\n", " # (i.e., if there is at least one label in bags[k-1][p_i])\n", " for L_k in bags[k-1][p_i]:\n", " # Note that k starts at 1 and bags[0][p_t] contains a TargetLabel.\n", "# print('\\n'+indent+'----scanning arrival times for route r={0} at stop p_i={1}----'.format(r, p_i))\n", "\n", " # We check the trips from latest to earliest\n", " for t in range(routes[r][0]-1, -1, -1): # n_trips = routes[r][0]\n", " # Does t_r arrive early enough for us to make the rest \n", " # of the journey from here (tau[k-1][p_i])?\n", " tau_arr = get_arrival_time(r, t, offset_p_i)\n", "# print(indent+'arrival time: ', tau_arr)\n", " if tau_arr <= L_k.tau_dep - tau_change_platform:\n", "\n", " max_delay = L_k.tau_dep - tau_arr - tau_change_platform\n", " max_delay_int = min(max_delay.astype('timedelta64[m]').astype('int'), 30)\n", " \n", " Pr_connection = distrib_delays[calc_stopTimes_idx(r, t, offset_p_i),\n", " max_delay_int + 1]\n", "# print(Pr_connection)\n", " L_new = RouteLabel(get_departure_time(r, t, offset_p_i),\n", " r,\n", " t,\n", " offset_p_i,\n", " L_k,\n", " Pr_connection\n", " )\n", " update_bag(B_route, L_new, k)#:\n", "# print(indent+\"Explored connection from\")\n", "# L_new.pprint(indent*2)\n", "# print(indent+\"to\")\n", "# L_k.pprint(indent*2)\n", "\n", " # We don't want to add a label for every trip that's earlier than tau_dep.\n", " # Instead, we stop once we've found a trip that's safe enough.\n", " if Pr_connection > Pr_threshold:\n", " break\n", " \n", "# print(marked)\n", " # Look at foot-paths (bags and marked are altered in-place):\n", " check_footpaths(bags, marked, k)\n", "# print(marked)\n", " \n", " # Report the number of new journeys found this round:\n", " n_new_journeys = len(bags[k][p_s]) - len(bags[k-1][p_s])\n", " if n_new_journeys:\n", " print(\"Found\", n_new_journeys, \"non-dominated journeys with\", k, \"trips.\")\n", " # If there's a journey with X trips, we won't look for a solution with X+3 trips:\n", " if not bags[k-1][p_s]:\n", " # The condition above means we found a journey for the first time.\n", " print(\"Will search for journeys with up to\", k+1, \"trips.\")\n", " k_max = k+1\n", "\n", " # Additional stopping criterion: reached equilibrium\n", " if not marked:\n", " print(\"\\n\", ' '*15, \"*\"*15, \" THE END \", \"*\"*15)\n", " print(\"Equilibrium reached with\" + \"out\"*(not bags[k][p_s]),\n", " \"finding a solution. The end.\")\n", " return [bags[K][p_s] for K in range(len(bags))], bags\n", " \n", " # We have exited the while-loop because k==kmax:\n", " print(\"\\n\" + \"*\"*15 + \" THE END \" + \"*\"*15)\n", " if bags[k][p_s]:\n", " print(\"There is a solution with\", k-1, \"trips.\")\n", " else:\n", " print(\"There are no solutions with up to\", k, \"trips.\")\n", " print(\"We shall not search for journeys with\", k+1, \"or more trips.\")\n", "\n", " return [bags[K][p_s] for K in range(len(bags))], bags\n", "\n", "def time_sorted_pareto(bags_p):\n", " \"\"\"Input: list of bags, e.g. for one stop and various k.\n", " It is assumed that Pr >= Pr_min for each label.\n", " Output: A Pareto set of non-dominated labels (np.array),\n", " sorted by decreasing departure time.\n", " \"\"\"\n", " res_bag = []\n", " for bag in bags_p:\n", " globals()['merge_bags'](res_bag, bag, 0, target_pruning=False)\n", " res = np.array(res_bag)\n", " return res[np.argsort([label.tau_dep for label in res])[::-1]]\n", "\n", "def print_solutions(bags_p):\n", " print(\"Showing Pareto set of non-dominated journeys\")\n", " if not bags_p[-1]:\n", " print(\"There are no journeys to print.\")\n", " return\n", " L_s = bags_p[-1][0]\n", " L_t = L_s\n", " while not isinstance(L_t, TargetLabel):\n", " L_t = L_t.next_label\n", " print(\"from\", L_s.stop2str(), \"to\", L_t.stop2str())\n", " print(\"with criteria:\")\n", " print(\" - maximize departure time\")\n", " print(\" - maximize probability of success\")\n", " print(\" - minimize number of individual trips\")\n", " print(\"and constraints:\")\n", " print(\" - arrival time at least\", tau_change_platform,\n", " \"minutes before\", time2str(L_t.tau_dep), \"(2 minutes to walk from the platform to the extraction point).\")\n", " print(\" - probability of success at least {Pr_min:.4f}.\".format(**globals()))\n", " print(\"\\nJourneys are sorted by descending departure time.\")\n", " \n", " for i, label in enumerate(time_sorted_pareto(bags_p)):\n", " print('\\n'*2, '-'*10, 'OPTION', i+1, '-'*10)\n", " label.print_journey()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# basic user interface\n", "style_wi_desc = {'description_width': 'initial'}\n", "import ipywidgets as widgets\n", "from ipywidgets import HBox, Label\n", "departure_stop_input = widgets.Text(description='departure stop ID',\n", " style = style_wi_desc\n", " )\n", "arrival_stop_input = widgets.Text(description='arrival stop ID',\n", " style = style_wi_desc\n", " )\n", "arrival_time_input = widgets.Text(description = 'arrival time (HH:mm)',\n", " value='17:30',\n", " style = style_wi_desc\n", " )\n", "\n", "probability_min_input = widgets.FloatSlider(\n", " value=0.9,\n", " min=0,\n", " max=1.0,\n", " step=0.01,\n", " disabled=False,\n", " continuous_update=False,\n", " orientation='horizontal',\n", " readout=True,\n", " readout_format='.2f',\n", " style = style_wi_desc\n", ")\n", "\n", "from IPython.display import display\n", "find_journey_button = widgets.Button(description=\"Find journey\")\n", "random_stops_button = widgets.Button(description=\"Pick random stops\")\n", "log = widgets.Output()\n", "output = widgets.Output()\n", "\n", "def on_find_journey_button_clicked(b):\n", " log.clear_output()\n", " output.clear_output()\n", " with log:\n", " bags_p_s, bags = run_mc_raptor(departure_stop_input.value,\n", " arrival_stop_input.value,\n", " arrival_time_input.value,\n", " probability_min_input.value,\n", " )\n", " with output:\n", " print_solutions(bags_p_s)\n", " \n", "find_journey_button.on_click(on_find_journey_button_clicked)\n", "\n", "def on_random_stops_clicked(b):\n", " departure_stop_input.value = gen_random_stop_id()[:7]\n", " arrival_stop_input.value = gen_random_stop_id()\n", "\n", "random_stops_button.on_click(on_random_stops_clicked)\n", "\n", "from ipywidgets import GridspecLayout\n", "grid = GridspecLayout(4, 2)\n", "grid[0, 0] = departure_stop_input\n", "grid[0, 1] = arrival_stop_input\n", "grid[1, 0] = arrival_time_input\n", "grid[2, :] = HBox([Label('Minimum probability of success'), probability_min_input])\n", "grid[3, 0] = find_journey_button\n", "grid[3, 1] = random_stops_button" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " " ] } ], "metadata": { "jupytext": { "formats": "ipynb,md,py:percent" }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.6" } }, "nbformat": 4, "nbformat_minor": 4 }