RidePy Tutorial 3: Simulations on Graphs

This tutorial is based on the first two tutorials and will cover simulations on graphs. Here, we will use the cythonic components. However, using the Python components instead is straightforward as outlined in Tutorial 2.

To simulate on graphs we need the Graph TransportSpace. To create the actual graph, we will use the convenience wrapper make_nx_grid from the extras package:

from ridepy.util.spaces_cython import Graph
from ridepy.extras.spaces import make_nx_grid

space = Graph.from_nx(make_nx_grid())

We can now proceed as before:

from ridepy.util.request_generators import RandomRequestGenerator
from ridepy.data_structures_cython import TransportationRequest as CyTransportationRequest

rg = RandomRequestGenerator(
    rate=10,
    max_pickup_delay=3,
    max_delivery_delay_rel=1.9,
    space=space,
    seed=42,
    request_class=CyTransportationRequest
)

n_buses = 50

Slight change: as we now have integer node ids serving for coordinates we need to set the initial location accordingly:

initial_location = 0
from ridepy.fleet_state import SlowSimpleFleetState
from ridepy.vehicle_state_cython import VehicleState as CyVehicleState
from ridepy.util.dispatchers_cython import (
    BruteForceTotalTravelTimeMinimizingDispatcher as CyBruteForceTotalTravelTimeMinimizingDispatcher,
)

import itertools as it

fs = SlowSimpleFleetState(
    initial_locations={vehicle_id: initial_location for vehicle_id in range(n_buses)},
    seat_capacities=8,
    space=space,
    dispatcher=CyBruteForceTotalTravelTimeMinimizingDispatcher(space.loc_type),
    vehicle_state_class=CyVehicleState,
)
transportation_requests = it.islice(rg, 100)

With this slightly changed configuration, we can run the simulations as before:

events = list(fs.simulate(transportation_requests))

The resulting events looks as before, just containing integer locations instead of 2D coordinate pairs.

events[200:203]
[{'event_type': 'DeliveryEvent',
  'timestamp': 5.209283479680293,
  'request_id': 48,
  'vehicle_id': 9},
 {'event_type': 'RequestSubmissionEvent',
  'request_id': 53,
  'timestamp': 5.233952232301099,
  'origin': 8,
  'destination': 4,
  'pickup_timewindow_min': 5.233952232301099,
  'pickup_timewindow_max': 8.2339522323011,
  'delivery_timewindow_min': 5.233952232301099,
  'delivery_timewindow_max': 11.033952232301099},
 {'event_type': 'RequestAcceptanceEvent',
  'timestamp': 5.233952232301099,
  'request_id': 53,
  'origin': 8,
  'destination': 4,
  'pickup_timewindow_min': 5.233952232301099,
  'pickup_timewindow_max': 8.2339522323011,
  'delivery_timewindow_min': 5.233952232301099,
  'delivery_timewindow_max': 11.033952232301099}]