Esempio n. 1
0
def test_create():
    vehicle = "111"
    clock = Clock()
    clock.tick()
    clock.tick()
    itinerary = Itinerary(clock.now, vehicle)

    assert itinerary.created_at == clock.now
    assert itinerary.next_jobs == []
    assert itinerary.current_job is None
    assert itinerary.jobs_to_complete == []
    # itinerary without jobs
    assert itinerary.is_completed()
    assert itinerary.vehicle == vehicle
Esempio n. 2
0
def test_change_flow():
    clock = Clock()
    booking = Booking(clock, Position(13.4014, 52.5478), Position(13.3393, 52.5053), 3)

    booking.set_matched()
    assert booking.is_matched() is True
    with pytest.raises(Exception):
        booking._set_pending()

    booking.set_waiting_pickup()
    assert booking.is_waiting_pickup() is True
    with pytest.raises(Exception):
        booking.set_matched()

    booking.set_pickup()
    assert booking.is_pickup() is True
    with pytest.raises(Exception):
        booking.set_waiting_pickup()

    booking.set_waiting_dropoff()
    assert booking.is_waiting_dropoff() is True
    with pytest.raises(Exception):
        booking.set_pickup()

    booking.set_dropoff()
    assert booking.is_dropoff() is True
    with pytest.raises(Exception):
        booking.set_waiting_dropoff()
Esempio n. 3
0
def test_Dispatcher_3():
    clock = Clock()
    router = LinearRouter(clock)

    fleet = Fleet(clock, router)
    vehicle = Vehicle(clock)
    fleet.infleet(vehicle, Position(13.3764, 52.5461))

    cnt = Dispatcher()

    booking = Booking(clock, Position(13.4014, 52.5478),
                      Position(13.3764, 52.5461))

    itinerary = Itinerary(101, vehicle)
    job1 = itinerary.pickup(booking)
    job2 = itinerary.move_to(Position(13.4014, 52.5478))
    job3 = itinerary.dropoff(booking)

    cnt.dispatch(itinerary)
    cnt.step()

    # finish 2 jobs in 1 step
    assert itinerary.current_job is job2

    assert booking.is_waiting_dropoff()
Esempio n. 4
0
def test_create_vehicle():
    v = Vehicle(Clock())

    assert v.state == States.offline
    assert v.position is None
    assert v.destination is None

    # TODO: check particular type of exception
    with pytest.raises(Exception):
        v.stop()

    with pytest.raises(Exception):
        v.move_to(111)

    with pytest.raises(Exception):
        v.step()

    engine = MagicMock()
    engine.route = MagicMock()
    engine.route.duration = 23
    engine.route.distance = 2.34
    engine.route.traveled_distance = MagicMock(return_value=43)
    engine.route.traveled_time = MagicMock(return_value=21.11)

    engine.position = MagicMock()
    engine.position.to_dict = MagicMock(return_value={111: 22})

    v.install_engine(engine)
    assert v.state == States.idling

    with pytest.raises(Exception):
        v.install_engine(engine)
Esempio n. 5
0
def test_Dispatcher():
    clock = Clock()
    router = LinearRouter(clock)

    fleet = Fleet(clock, router)
    vehicle = Vehicle(clock)
    fleet.infleet(vehicle, Position(13.3764, 52.5461))

    cnt = Dispatcher()

    cnt.step()

    booking = Booking(clock, Position(13.4014, 52.5478),
                      Position(13.3764, 52.5461))

    itinerary = Itinerary(101, vehicle)
    itinerary.move_to(Position(13.4014, 52.5478))
    itinerary.pickup(booking)
    itinerary.dropoff(booking)

    cnt.dispatch(itinerary)
    cnt.step()

    assert vehicle.is_moving
    assert booking.is_waiting_pickup()

    booking.set_pickup()

    cnt.dispatch(itinerary)
    cnt.step()

    assert booking.is_waiting_dropoff()
Esempio n. 6
0
def test_dont_move_vehicle():
    init_pos = Position(13.3764, 52.5461)
    dest1 = Position(13.4014, 52.5478)

    clock = Clock()
    router = LinearRouter(clock)

    engine = VehicleEngine(init_pos, router, clock)

    v = Vehicle(clock)
    v.install_engine(engine)

    assert v.position == init_pos
    assert v.destination is None

    v.move_to(init_pos)
    assert not v.engine.is_moving()
    assert v.engine.current_position == init_pos
    assert v.state == States.idling

    assert v.position == init_pos

    v.step()
    assert v.state == States.idling
    assert not v.engine.is_moving()
    assert v.engine.destination is None

    assert v.position == init_pos

    assert v.state == States.idling
    assert not v.engine.is_moving()
    assert v.engine.current_position == init_pos
    assert not v.is_moving
Esempio n. 7
0
def test_Dispatcher_2():
    clock = Clock()
    router = LinearRouter(clock)

    fleet = Fleet(clock, router)
    vehicle = Vehicle(clock)
    fleet.infleet(vehicle, GeographicPosition(13.3764, 52.5461))

    cnt = Dispatcher()

    booking = Booking(
        clock,
        GeographicPosition(13.4014, 52.5478),
        GeographicPosition(13.3764, 52.5461),
    )

    itinerary = Itinerary(101, vehicle)
    job1 = itinerary.pickup(booking)
    job2 = itinerary.dropoff(booking)

    cnt.dispatch(itinerary)
    cnt.step()

    # finish 2 jobs in 1 step
    assert itinerary.current_job is None

    assert booking.is_complete()
Esempio n. 8
0
def test_update_next_bookings():
    v = create_vehicle()

    clock = Clock()
    b1 = Booking(
        clock,
        GeographicPosition(13.4014, 52.5478),
        GeographicPosition(13.3393, 52.5053),
    )
    b2 = Booking(
        clock,
        GeographicPosition(13.4014, 52.5478),
        GeographicPosition(13.3393, 52.5053),
    )

    itinerary = Itinerary(3434, v)
    itinerary.move_to(111)
    itinerary.pickup(b1)
    itinerary.dropoff(b1)
    itinerary.pickup(b2)
    itinerary.dropoff(b2)

    update_next_bookings(itinerary)

    assert b1.is_waiting_pickup()
    assert b2.is_waiting_pickup()

    b1.set_pickup()
    update_next_bookings(itinerary)
    assert b1.is_waiting_dropoff()
    assert b2.is_waiting_pickup()
Esempio n. 9
0
def create_scenario(config):
    clock = Clock(
        time_step=config["simulation"]["clock_step"],
        time_unit="s",
        starting_time=config["simulation"]["starting_time"],
    )

    if config["fleet"]["router"] == "linear":
        fleet_router = routers.LinearRouter(
            clock=clock, speed=config["routers"]["linear"]["speed"]
        )

    elif config["fleet"]["router"] == "osrm":
        fleet_router = routers.OSRMRouter(
            clock=clock, server=config["routers"]["osrm"]["server"]
        )

    else:
        logging.warning("Unknown router. Fallback to linear")
        fleet_router = routers.LinearRouter(
            clock=clock, speed=config["routers"]["linear"]["speed"]
        )

    logging.info(f"Fleet router {fleet_router}")

    fleet_router = routers.CachingRouter(fleet_router)

    fleet = Fleet(clock, fleet_router)
    fleet.infleet_from_geojson(
        config["fleet"]["vehicles"],
        config["fleet"]["stations"],
        geofence_file=config.get("geofence"),
        seed=config["simulation"].get("fleet_seed"),
    )

    max_pending_time = clock.time_to_clock_time(
        config["bookings"]["max_pending_time"], "m"
    )
    booking_service = BookingService(clock, max_pending_time)

    demand = create_demand_model(config, clock=clock, map_matcher=fleet_router)

    dispatcher = Dispatcher()

    context = Context(clock, fleet, booking_service, dispatcher)

    return context, demand
Esempio n. 10
0
def test_pending_state():
    clock = Clock()
    booking = Booking(clock, Position(13.4014, 52.5478), Position(13.3393, 52.5053), 3)

    assert booking.is_pending() is True

    with pytest.raises(Exception):
        booking._set_pending()
Esempio n. 11
0
def test_expire():
    clock = Clock()
    booking = Booking(clock, Position(13.4014, 52.5478), Position(13.3393, 52.5053), 3)

    booking.set_expired()

    with pytest.raises(Exception):
        booking.set_matched()
Esempio n. 12
0
def create_state_machine():
    transitions = [
        ["set_state1", [TstStates.STATE3], TstStates.STATE1],
        ["set_state2", [TstStates.STATE1], TstStates.STATE2],
        ["set_state3", [TstStates.STATE1, TstStates.STATE2], TstStates.STATE3],
    ]

    states = [s for s in TstStates]

    return StateMachine(Clock(), transitions, states, TstStates.STATE1)
Esempio n. 13
0
def test_create():
    pickup = Position(13.3393, 52.5053)
    dropoff = Position(13.4014, 52.5478)
    seats = 30
    preferences = {"cat": "AV"}

    clock = Clock()
    booking = Booking(clock, pickup, dropoff, seats, preferences)

    assert booking.seats == seats
    assert booking.pickup == pickup
    assert booking.dropoff == dropoff
    assert booking.preferences == preferences
Esempio n. 14
0
def test_do_current_job_2():
    init_pos = Position(13.3764, 52.5461)
    dest = Position(13.4014, 52.5478)

    clock = Clock()
    router = LinearRouter(clock)

    engine = VehicleEngine(init_pos, router, clock)

    v = Vehicle(clock)
    v.install_engine(engine)

    itinerary = Itinerary(2, v)
    itinerary.move_to(dest)
    do_job(itinerary)

    assert v.is_moving
Esempio n. 15
0
def test_on_state_changed_pending():
    pickup = Position(13.4014, 52.5478)
    dropoff = Position(13.3393, 52.5053)

    clock = Clock()
    booking = Booking(clock, pickup, dropoff, 3)

    event_data = MagicMock()
    event_data.transition = MagicMock()

    # test to pending
    event_data.transition.dest = States.pending.value
    event_data.kwargs = {}

    booking.on_state_changed(event_data)
    assert event_data.kwargs["position"] == pickup.to_dict()
    assert event_data.kwargs["dropoff"] == dropoff.to_dict()
Esempio n. 16
0
def test_on_state_changed_pickup_position():
    pickup = Position(13.4014, 52.5478)
    dropoff = Position(13.3393, 52.5053)

    clock = Clock()
    booking = Booking(clock, pickup, dropoff, 3)

    event_data = MagicMock()
    event_data.transition = MagicMock()

    for state in (States.dropoff.value, States.complete.value):

        event_data.transition.dest = state
        event_data.kwargs = {}
        booking.on_state_changed(event_data)

        assert event_data.kwargs["position"] == dropoff.to_dict()
Esempio n. 17
0
def test_do_job():
    vehicle = MagicMock()
    itinerary = Itinerary(Clock(), vehicle)

    booking = MagicMock()
    booking.is_pending = MagicMock(return_value=False)
    booking.is_matched = MagicMock(return_value=True)

    job = MagicMock()
    job.is_pickup = MagicMock(return_value=True)
    job.is_dropoff = MagicMock(return_value=False)
    job.is_move_to = MagicMock(return_value=False)
    job.booking = booking

    itinerary.current_job = job

    do_job(itinerary)

    booking.set_waiting_pickup.assert_called_once_with(itinerary=itinerary)
    vehicle.move_to.assert_not_called()

    job.is_pickup = MagicMock(return_value=False)
    job.is_dropoff = MagicMock(return_value=True)
    itinerary.current_job = job

    booking.is_waiting_dropoff = MagicMock(return_value=True)
    do_job(itinerary)
    booking.set_dropoff.assert_called_once_with(itinerary=itinerary)
    vehicle.move_to.assert_not_called()

    job.is_pickup = MagicMock(return_value=False)
    job.is_dropoff = MagicMock(return_value=False)
    job.is_move_to = MagicMock(return_value=True)
    job.destination = "aaa"

    vehicle.is_moving = False
    vehicle.position = "bb"
    itinerary.current_job = job

    do_job(itinerary)

    vehicle.move_to.assert_called_once_with(job.destination,
                                            itinerary=itinerary)
Esempio n. 18
0
def test_real_vehicle():
    init_pos = Position(13.3764, 52.5461)
    dest = Position(13.4014, 52.5478)

    clock = Clock()
    router = LinearRouter(clock)

    engine = VehicleEngine(init_pos, router, clock)

    v = Vehicle(clock)
    v.install_engine(engine)

    itinerary = Itinerary(111, v)
    itinerary.move_to(dest)

    assert not v.is_moving

    move_vehicle(itinerary)

    assert v.is_moving
Esempio n. 19
0
def test_on_state_changed_pickup_position():
    pickup = Position(13.4014, 52.5478)
    dropoff = Position(13.3393, 52.5053)

    clock = Clock()
    booking = Booking(clock, pickup, dropoff, 3)

    event_data = MagicMock()
    event_data.transition = MagicMock()

    for state in (
        States.pending.value,
        States.matched.value,
        States.pickup.value,
        States.expired.value,
        States.waiting_dropoff.value,
        States.waiting_pickup.value,
        States.expired.value,
    ):

        event_data.transition.dest = state
        event_data.kwargs = {}
        booking.on_state_changed(event_data)
        assert event_data.kwargs["position"] == pickup.to_dict()
Esempio n. 20
0
def test_dropoff_booking():
    vehicle = create_vehicle()
    itinerary = Itinerary(Clock(), vehicle)

    booking = MagicMock()
    booking.is_waiting_dropoff = MagicMock(return_value=True)
    dropoff_booking(booking, itinerary)
    booking.set_dropoff.assert_called_once_with(itinerary=itinerary)
    booking.set_complete.assert_called_once_with(itinerary=itinerary)

    booking = MagicMock()
    booking.is_waiting_dropoff = MagicMock(return_value=False)
    booking.is_pickup = MagicMock(return_value=True)
    dropoff_booking(booking, itinerary)
    booking.set_waiting_dropoff.assert_called_once_with(itinerary=itinerary)
    booking.set_dropoff.assert_called_once_with(itinerary=itinerary)
    booking.set_complete.assert_called_once_with(itinerary=itinerary)

    booking = MagicMock()
    booking.is_waiting_dropoff = MagicMock(return_value=False)
    booking.is_pickup = MagicMock(return_value=False)

    with pytest.raises(Exception):
        dropoff_booking(booking, itinerary)
Esempio n. 21
0
def test_pickup_booking():
    vehicle = create_vehicle()
    itinerary = Itinerary(Clock(), vehicle)

    booking = MagicMock()
    booking.is_pending = MagicMock(return_value=True)
    booking.is_matched = MagicMock(return_value=True)

    context = {"vehicle_id": vehicle.id}
    pickup_booking(booking, itinerary)

    booking.set_matched.assert_called_once_with(itinerary=itinerary)
    booking.set_waiting_pickup.assert_called_once_with(itinerary=itinerary)
    booking.set_pickup.assert_called_once_with(itinerary=itinerary)

    booking = MagicMock()
    booking.is_pending = MagicMock(return_value=False)
    booking.is_matched = MagicMock(return_value=True)
    pickup_booking(booking, itinerary)
    booking.set_waiting_pickup.assert_called_once_with(itinerary=itinerary)
    booking.set_pickup.assert_called_once_with(itinerary=itinerary)

    booking = MagicMock()
    booking.is_pending = MagicMock(return_value=False)
    booking.is_matched = MagicMock(return_value=False)
    booking.is_waiting_pickup = MagicMock(return_value=True)
    pickup_booking(booking, itinerary)
    booking.set_pickup.assert_called_once_with(itinerary=itinerary)

    booking = MagicMock()
    booking.is_pending = MagicMock(return_value=False)
    booking.is_matched = MagicMock(return_value=False)
    booking.is_waiting_pickup = MagicMock(return_value=False)

    with pytest.raises(NotImplementedError):
        pickup_booking(booking, itinerary)
Esempio n. 22
0
def test_move_vehicle():
    init_pos = Position(13.3764, 52.5461)
    dest1 = Position(13.4014, 52.5478)

    clock = Clock()
    router = LinearRouter(clock)

    engine = VehicleEngine(init_pos, router, clock)

    v = Vehicle(clock)
    v.install_engine(engine)

    v.move_to(dest1)
    assert v.engine.is_moving()
    assert v.state == States.moving_to

    v.step()
    assert v.state == States.moving_to
    assert v.engine.is_moving()

    clock.tick()
    v.step()

    assert v.engine.is_moving()

    for _ in range(1000):
        clock.tick()
        v.step()

        if not v.engine.is_moving():
            assert v.position == dest1
            assert v.state == States.idling
        else:
            assert v.position != dest1
            assert v.destination == dest1
            assert v.state == States.moving_to
Esempio n. 23
0
    print(f"Dropoff in {round(clock.clock_time_to_seconds(eta) / 60)} minutes")
    print(f"Distance to dropoff {route.distance:.2f} km")


if __name__ == "__main__":

    loggers.configure_root_logger()

    # create in-memory log handler to be able to access to all
    # state changes after the simulation is over
    simulation_logs = loggers.InMemoryLogHandler()
    _ = loggers.get_simobility_logger(simulation_logs)

    clock = Clock(time_step=10,
                  time_unit="s",
                  starting_time="2020-02-05 12:35:01",
                  initial_time=5)
    print(f"Current time {clock.to_datetime()} ({clock.now} clock time)")

    fleet = create_fleet(clock)
    vehicle = fleet.get_online_vehicles()[0]

    booking = create_booking(clock)

    print_estimates(vehicle, booking, clock)

    print("\nObject states before simulation:")
    print(f'Booking state is "{booking.state.value}"')
    print(f'Vehicle state is "{vehicle.state.value}"')

    # explain vehicle what to do
Esempio n. 24
0
        if origin == destination:
            return []

        return [Booking(self.clock, Cell(*origin), Cell(*destination))]


if __name__ == "__main__":
    loggers.configure_root_logger()

    # create in-memory log handler to be able to access to all
    # state changes after the simulation is over
    simulation_logs = loggers.InMemoryLogHandler()
    _ = loggers.get_simobility_logger(simulation_logs)

    clock = Clock(time_step=30,
                  time_unit="s",
                  starting_time="2020-11-11 13:00:01",
                  initial_time=1)
    print(f"Current time {clock.to_datetime()} ({clock.now} clock time)")

    world_width = 10
    world_height = 10
    num_vehicles = 2
    max_pending_time = 5

    random_cell_fn = partial(random_cell, world_width, world_height)

    dispatcher = Dispatcher()
    booking_service = BookingService(clock, max_pending_time)

    fleet = create_fleet(num_vehicles, clock, random_cell_fn)