BlockScience / rideshare-model

1 stars 0 forks source link

Add in new policy options for the handle_ride_request_event code #49

Open SeanMcOwen opened 4 months ago

SeanMcOwen commented 4 months ago
def handle_ride_request_event(self, event, event_queue, current_time, retry_queue):
    ride_id = event.spaces["ride_id"]
    ride_request = self.ride_requests[ride_id]
    print(f"Handling ride request event for ride {ride_id} at time {current_time}")

    for driver in self.drivers:
        if driver.state == 'idle' and driver.will_accept_ride(ride_request.metadata):
            travel_time = self.calculate_travel_time(driver.zone, ride_request.origin_zone)
            cost = self.calculate_cost(driver.zone, ride_request.origin_zone)
            print(f"Travel time: {travel_time}, Cost: {cost}")
            if travel_time < float('inf'):
                driver.zone = ride_request.origin_zone
                driver.time_to_next_zone = travel_time
                driver.current_rider = ride_request.rider
                driver.current_request = ride_request
                driver.accept_ride()
                ride_request.driver = driver
                ride_request.accept()
                ride_request.rider.match_with_driver()
                ride_request.rider.estimated_wait_time = travel_time
                ride_request.metadata["pickup_time"] = current_time + travel_time
                heapq.heappush(event_queue, Event(t=current_time + travel_time, action="Driver Assigned", spaces={"ride_id": ride_id, "driver_id": driver.name}))
                print(f"Driver {driver.name} assigned to ride {ride_id} at time {current_time + travel_time}")
                return

    print(f"No driver available for ride {ride_id} at time {current_time}, retrying...")
    retry_queue.append(Event(t=current_time + 1, action="Request Ride", spaces={"ride_id": ride_id}))

def handle_driver_assigned_event(self, event, event_queue, current_time):
    ride_id = event.spaces["ride_id"]
    driver_id = event.spaces["driver_id"]
    ride_request = self.ride_requests[ride_id]
    driver = next(d for d in self.drivers if d.name == driver_id)
    print(f"Driver {driver_id} arrived at pickup for ride {ride_id} at time {current_time}")

    driver.arrive_at_pickup()
    driver.pick_up_rider()
    ride_request.start()
    ride_request.rider.start_ride()
    travel_time = self.calculate_travel_time(driver.zone, ride_request.destination_zone)
    driver.time_to_next_zone = travel_time
    ride_request.metadata["dropoff_time"] = current_time + travel_time
    heapq.heappush(event_queue, Event(t=current_time + travel_time, action="Complete Ride", spaces={"ride_id": ride_id, "driver_id": driver.name}))
    print(f"Ride {ride_id} in progress, will complete at time {current_time + travel_time}")