openmobisim.fleet ================= .. py:module:: openmobisim.fleet .. autoapi-nested-parse:: A fleet day without a run: shared-mobility services, a table of trips, one simulated day. ``service_bike_docked`` and ``service_taxi`` describe a service: its docks, or its vehicles and chargers, its options and its name. ``fleet_day`` runs every service's day together on the fleet engine, from a table of trips that use them and, optionally, the road times of an earlier run (``road_times_read``), and returns what became of each trip and of each dock. **A frozen day, an approximation:** the travellers' choices and the traffic are held fixed. Each trip's service is given; a shared-bike trip takes the dock nearest its origin on foot and returns at the dock nearest its destination on foot, whatever the docks hold; a taxi drives on the given road times and adds no traffic, and a request no taxi can pick up within the fleet's cancel limit is cancelled and ends there. What a full run adds — travellers choosing among modes and docks — is not here; a run's own service day is played the same way, once the run has stopped. The result says so (``FleetDay.fidelity``). **An operator** (``policy=``, see ``openmobisim.operator``) may steer the services during the day: move bikes between docks, set docks' target levels, relocate or charge taxis. Its decisions, accepted or refused with the reason, are in ``FleetDay.decisions()``. Attributes ---------- .. autoapisummary:: openmobisim.fleet.REWARD_WEIGHTS openmobisim.fleet.TAXI_STATES openmobisim.fleet.ZONE_GRID_M Classes ------- .. autoapisummary:: openmobisim.fleet.FleetDay openmobisim.fleet.FleetEnvironment openmobisim.fleet.FleetVectorEnvironment openmobisim.fleet.Service Functions --------- .. autoapisummary:: openmobisim.fleet.fleet_day openmobisim.fleet.fleet_environment openmobisim.fleet.road_times_read openmobisim.fleet.service_bike_docked openmobisim.fleet.service_taxi Module Contents --------------- .. py:class:: FleetDay What one fleet day did (``fleet_day``). .. py:method:: decisions() Every decision the policy took, in the order taken, accepted or refused. Columns: ``step`` (the policy step's index, 0 at the window's start), ``t_s`` (its second), ``service``, ``kind`` (``move``, ``target``, ``relocate``, ``charge``), the parameters by id (``from_dock``, ``to_dock``, ``n``, ``dock``, ``level``, ``vehicle``, ``node``, ``charger``, ``to_soc``; ``None`` or NaN where a kind has none), ``accepted``, ``reason`` (why it was refused, ``None`` if accepted), ``executed_s`` (the second it started), ``finished_s`` (the van's or vehicle's arrival, the charge's end; NaN if refused or not finished by the window's end), ``moved`` (a move's bikes in the van), ``metres`` (the drive's), ``from_target`` (a move the solver made for the step's targets). .. py:method:: trip_results() What became of each trip given, in the order given. Columns: ``trip_id``, ``service``, ``outcome`` (``completed``; ``cancelled``: no taxi within the cancel limit, or no bike at any dock tried; ``unfinished``: still waiting or riding when the window ended; ``not_requested``: asking after the window; ``not_planned``: no leg could be planned), ``requested_s``, ``pickup_s`` (a taxi at the pickup; the bike taken), ``dropoff_s`` (a taxi's drop-off at the destination; the bike returned), ``arrived_s`` (at the destination, a shared bike's walk on included; after a bike trip that found no bike, on foot from the last dock tried; NaN after a cancelled taxi request), ``waiting_s`` (a taxi's, from the request to the pickup, the dispatch delay included; NaN if never picked up; 0 for a bike), ``dock_from``, ``dock_to``, ``vehicle_id``, ``extra_walk_s``, ``redirections``, ``overflow``. .. py:attribute:: counts :type: dict[str, float] .. py:attribute:: decision_rows :type: openmobisim.table.Columns .. py:attribute:: dock_bins :type: dict[str, openmobisim.table.Columns] .. py:attribute:: fidelity :type: str .. py:attribute:: frozen :type: dict[str, Any] | None :value: None .. py:attribute:: plan :type: dict[str, int] .. py:attribute:: policy :type: dict[str, Any] | None .. py:attribute:: seconds :type: dict[str, float] .. py:attribute:: services :type: list[str] .. py:attribute:: trips :type: openmobisim.table.Columns .. py:class:: FleetEnvironment(inner, network = None, *, frozen = None) A fleet day to step through (``fleet_environment``). .. py:method:: fork() A copy of the day at its clock, to simulate ahead without touching this one. .. py:method:: reset(seed = 0) Start the day with ``seed``: ``(view, info)`` at the window's start. .. py:method:: step(decisions = None) Apply ``decisions`` at the clock and run one step. Returns ``(view, reward, terminated, truncated, info)``. .. py:attribute:: frozen :value: None .. py:property:: t :type: float The clock, in seconds from midnight. .. py:class:: FleetVectorEnvironment(inner, network = None, *, frozen = None) ``n`` fleet days stepped together, in parallel (``fleet_environment(..., n=...)``). One call steps every day; the engines run in parallel on the machine's cores. Seed ``k`` gives the same day here as in a single environment. ``observation`` and ``action`` encode one day's view or decisions, as for ``FleetEnvironment``. .. py:method:: reset(seeds) Start each day with its seed (one per day): ``(views, infos)``. .. py:method:: step(decisions) Apply one list of decisions per day and run each one step. Returns ``(views, rewards, terminated, truncated, infos)``: lists of one per day, ``rewards`` an array. :raises ValueError: Unless there is one list per day. .. py:attribute:: frozen :value: None .. py:property:: t :type: list[float] Each day's clock, in seconds from midnight. .. py:class:: Service One shared-mobility service: its kind, name, parts and options (see the makers). .. py:attribute:: chargers :type: Any :value: None .. py:attribute:: docks :type: Any :value: None .. py:attribute:: kind :type: str .. py:attribute:: name :type: str .. py:attribute:: options :type: collections.abc.Mapping[str, float] .. py:attribute:: vehicles :type: Any :value: None .. py:function:: fleet_day(network, services, trips = None, *, frozen = None, window = None, road_times = None, seed = None, policy = None, policy_options = None, zone_grid_m = ZONE_GRID_M) Run every service's day together on the fleet engine, from a table of trips. **Or from a run's frozen day** (``frozen``, a folder ``Run.freeze`` wrote): the legs of the run's service day as the run played them (its last loading's service trips: the docks the travellers chose, the second each asks for a bike or a taxi, its ride and walk), on the road times its taxis drove on, over its window. With no policy, the day replays the run's service day: every leg's outcome, seconds, docks and vehicle, and the docks' bins, bikes and taxis, are the run's own. The services must be the run's (the same names, docks, bikes and options) and the network too: either differing is refused, saying what differs. Each trip of the result is a leg: its ``trip_id`` the trip's index in the run (``Run.trip_results()``'s ``trip``); ``FleetDay.frozen`` holds the day's ``setup.json``. :param network: The road network (its walk and bike layers are made from it). :param services: The services (``service_bike_docked``, ``service_taxi``), names unique. :param trips: One row per trip: ``origin_lon, origin_lat, destination_lon, destination_lat``, ``departure`` (seconds from midnight, or ``"HH:MM"``), and optionally ``trip_id``, ``service`` (a service's name; required with more than one service) and ``weight`` (the people it stands for, 1 by default). A CSV/TSV path or text, or rows as dicts. ``None`` with ``frozen``. :param frozen: A folder ``Run.freeze`` wrote, in place of ``trips`` and ``road_times``. :param window: The day's start and end (seconds or ``"HH:MM"``); a trip asking before it opens asks at its start, one after it ends is never asked. ``None``: 07:30 to 09:30, or a frozen day's own (another window is refused with ``frozen``). :param road_times: An earlier run's road times (``road_times_read``); ``None``: free flow. :param seed: The day's seed (a policy's draws); ``None``: 0, or a frozen day's (the run's master seed). :param policy: An operator steering the services (``openmobisim.operator``): a policy object (``step(view)``), a built-in one (``policy_threshold(...)``), a name (built-in or registered with ``policy_register``), or ``None``: no operator. :param policy_options: A named policy's options (``{"low": 0.1}`` for ``"threshold"``). :param zone_grid_m: The side of the square zones a view groups places by, in metres (``ZONE_GRID_M``, 1 000: an assumption, uncalibrated). :raises ValueError: For a service, trip, option or policy that is not valid, road times of another network, or a decision naming an id that does not exist; with ``frozen``, for another network, other services, another window, or trips or road times given too. :raises FileNotFoundError: For a ``frozen`` folder without ``setup.json``. :raises Exception: A Python policy's own exception, which stops the day. .. py:function:: fleet_environment(network, services, trips = None, *, frozen = None, window = None, road_times = None, step_s = 900.0, reward_weights = None, legs_keep = 1.0, departure_jitter_s = 0.0, n = 1, zone_grid_m = ZONE_GRID_M) A fleet day to step through: ``reset(seed)``, then ``step(decisions)`` until the day ends. The day is ``fleet_day``'s (the same services, trips, window and road times; a frozen day: the travellers' choices and the traffic are held fixed), with no policy of its own: the caller decides at each step. ``reset(seed)`` returns ``(view, info)`` at the window's start; ``step(decisions)`` applies the decisions (as a policy returns them: ``operator.Move``, ``Target``, ``Relocate``, ``Charge``), runs ``step_s`` seconds and returns ``(view, reward, terminated, truncated, info)``; at the window's end ``info["outcome"]`` holds the day's tables. ``fork()`` copies the day at its clock, to simulate ahead without touching it. A view cannot be read after the next step. The reward of a step is minus ``w_cancelled`` × the people whose request was cancelled (a taxi request no taxi could pick up within the cancel limit, which ends there; a traveller who found no bike at any dock tried), ``w_wait`` × the minutes they waited, ``w_walk`` × the extra minutes they walked (between docks: shared bikes only), and ``w_km`` × the kilometres the policy's decisions drove (vans moving bikes, taxis relocating or driving to charge), each counted once, when its leg ends (``REWARD_WEIGHTS``, uncalibrated). ``info["refused"]`` holds, per decision given at the step, why the engine refused it (``None``: accepted). With ``n`` above 1 the result is a ``FleetVectorEnvironment``: ``n`` days stepped together, in parallel, ``reset(seeds)`` taking one seed per day and ``step`` one list of decisions per day. Seed ``k`` gives the same day there as here. ``observation(view, spec)`` and ``action(vector, spec)`` turn views and decisions into arrays of fixed shapes, for learning methods (``spec`` is described with ``observation``). With ``frozen`` (a folder ``Run.freeze`` wrote), the day is a run's last loading: its legs as the run played them and its road times, in place of trips; ``frozen`` on the environment holds its ``setup.json``. ``legs_keep`` and ``departure_jitter_s`` vary it by seed as they vary a day of trips; with neither, every seed replays the run's day. :param network: The road network. :param services: The services (``service_bike_docked``, ``service_taxi``). :param trips: The trips, as for ``fleet_day``; ``None`` with ``frozen``. :param frozen: A run's frozen day, as for ``fleet_day``. :param window: The day's start and end; ``None``: 07:30 to 09:30, or a frozen day's own. :param road_times: An earlier run's road times, or ``None``: free flow (a frozen day's own with ``frozen``). :param step_s: The seconds between steps. :param reward_weights: Weights by name in place of ``REWARD_WEIGHTS``'; a name not among them is refused with the list. :param legs_keep: The share of the trips' legs each day keeps, drawn by the day's seed (1: all). :param zone_grid_m: The side of the square zones a view groups places by, in metres (``ZONE_GRID_M``, 1 000: an assumption, uncalibrated). :param departure_jitter_s: How far each kept leg's request moves, drawn by the day's seed, in seconds either way (0: not at all). With these two, each seed of ``reset`` is another day; both thin and shift the same trips, they do not draw a new demand. :param n: How many days to step together (1: a ``FleetEnvironment``). :raises ValueError: As ``fleet_day``. .. py:function:: road_times_read(link_bins) The road times of an earlier run, from its ``link_bins.parquet``. Each link's mean time in each bin is ``pcu_seconds / pcu``; a link or bin without a row is taken at free flow. The run's network must be the network the fleet day is given (its fingerprint is checked). These times are by the bin a vehicle *left* the link, which the fleet day reads as the bin it enters: an approximation. :param link_bins: The path of a run's ``link_bins.parquet``. :raises ImportError: Without pyarrow — install ``openmobisim[all]``. .. py:function:: service_bike_docked(docks, *, name = 'bike_share', options = None) A docked bike-sharing service: bikes counted per dock. :param docks: Its docks (``dock_read_table`` or ``dock_read_gbfs``). :param name: Its name, unique among a day's services. :param options: Its options by name (``walk_max_s``, ``redirect_max``, ``redirect_max_m``, ``pickup_s``, ``return_s``, ``ride_factor``, ``start_share``, ``fail_s``, ``bin_s``, ``handling_s``, ``van_bikes``, ``candidates``, ``snap_m``, and ``log_events``: 1 to keep the day's log of events, the default, 0 not to, for a very large run, whose results are otherwise the same); a name not among them is refused with the list. A fleet day does not read ``candidates`` or ``fail_s``. .. py:function:: service_taxi(vehicles, chargers = None, *, name = 'taxi', options = None) An electric taxi fleet: vehicles tracked one by one, with a battery. :param vehicles: Its vehicles (``vehicle_read_table``). :param chargers: Its chargers (``charger_read_table``), or ``None``: its vehicles never charge. :param name: Its name, unique among a day's services. :param options: Its options by name (``battery_kwh``, ``kwh_per_km``, ``soc_start``, ``reserve_soc``, ``charge_below``, ``charge_to``, ``charger_kw``, ``request_cancel_s``, ``boarding_s``, ``dispatch_delay_s``, ``snap_m``, ``charger_reach_s``, and ``log_events``: 1 by default, 0 to keep no event log in a very large run, ``taxi_events.parquet`` then empty); a name not among them is refused with the list. ``request_cancel_s`` (600 s, an assumption): a request is cancelled if no taxi can pick it up within this time. ``dispatch_delay_s`` (120 s, an assumption): the time from a request being matched to the taxi setting off (the app's dispatch and the driver's acceptance), so the waiting time counts it, and a taxi is matched to a request only if the dispatch delay and its drive fit within ``request_cancel_s``; a ``dispatch_delay_s`` at or above ``request_cancel_s`` is refused. ``boarding_s`` (60 s, an assumption): the time to get in at the pickup. .. py:data:: REWARD_WEIGHTS :type: dict[str, float] .. py:data:: TAXI_STATES :type: tuple[str, ...] :value: ('idle', 'to_pickup', 'occupied', 'to_charger', 'queued_at_charger', 'charging', 'relocating') .. py:data:: ZONE_GRID_M :type: float :value: 1000.0