def test_route_destination(): origin = Position(-73.99780, 40.71205) destination = Position(-73.99567, 40.71689) clock = Clock(time_step=10, time_unit="s") router = LinearRouter(clock=clock) assert router.calculate_route(origin, destination).destination == destination
def test_map_match(): clock = Clock(time_step=1, time_unit="m") router = LinearRouter(speed=12, clock=clock) pos1 = Position(-0.39376, 39.5145) pos2 = Position(-0.38874, 39.503) for pos in [pos1, pos2]: pos_m = router.map_match(pos) assert pos == pos_m assert pos.id != pos_m.id
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()
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()
def test_router2d_2(): speed_kmph = 17 nyc_pos = Position(-73.935242, 40.730610) nyc_pos_shift = Position(-73.935, 40.730610) clock = Clock(time_step=1, time_unit="s") router = LinearRouter(speed=speed_kmph, clock=clock) assert router.estimate_duration(nyc_pos, nyc_pos_shift) == 5 route = router.calculate_route(nyc_pos, nyc_pos_shift) assert len(route.coordinates) == 6 for p in route.coordinates: assert p.lat == nyc_pos.lat
def test_end_to_end(): init_pos = Position(13.3764, 52.5461) dest1 = Position(13.4014, 52.5478) dest2 = Position(13.3393, 52.5053) clock = Clock() # to start not from the beginning clock.tick() clock.tick() router = LinearRouter(clock) engine = VehicleEngine(init_pos, router, clock) engine.start_move(dest1) assert engine.eta == 8 assert engine.current_position == init_pos clock.tick() clock.tick() assert engine.current_position != init_pos for _ in range(engine.eta - clock.clock_time): clock.tick() assert engine.current_position == dest1 assert not engine.is_moving() assert engine.eta == clock.clock_time engine.end_move() assert not engine.is_moving() assert engine.eta == clock.clock_time
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
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()
def test_router2d(): speed_kmph = 25 nyc_pos = GeographicPosition(-73.935242, 40.730610) nyc_pos_shift = GeographicPosition(-73.935, 40.7306) # monutes clock = Clock(time_step=1, time_unit="m") router = LinearRouter(speed=speed_kmph, clock=clock) assert router.estimate_duration(nyc_pos, nyc_pos) == 0 route = router.calculate_route(nyc_pos, nyc_pos) assert route.duration == 0 assert route.distance == 0 assert len(route.coordinates) == 1 assert route.approximate_position(clock.clock_time) == nyc_pos assert route.approximate_position(clock.clock_time + 1) == nyc_pos assert router.estimate_duration(nyc_pos, nyc_pos_shift) == 1 for i in range(10): clock.tick() assert router.estimate_duration(nyc_pos, nyc_pos) == 0 # seconds clock = Clock(time_step=1, time_unit="s") router = LinearRouter(speed=speed_kmph, clock=clock) assert router.estimate_duration(nyc_pos, nyc_pos_shift) == 3 route = router.calculate_route(nyc_pos, nyc_pos_shift) assert route.duration == 3 assert pytest.approx(route.distance, 3) == 0.02 assert len(route.coordinates) == 4 assert route.approximate_position(clock.clock_time) == nyc_pos assert route.approximate_position(clock.clock_time + 1) != nyc_pos assert route.approximate_position(clock.clock_time + 1) == route.coordinates[1] assert route.approximate_position(clock.clock_time + 3) == nyc_pos_shift assert route.approximate_position(clock.clock_time + 3) == route.coordinates[-1]
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
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
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