def get_travel_time(self, destination): """Return the time it will take to arrive at the destination, rounded to the nearest integer. @type self: Driver @type destination: Location @rtype: int """ return self.speed * (manhattan_distance(self.location, destination))
def _average_ride_distance(self): """Return the average distance drivers have driven on rides. @type self: Monitor @rtype: float >>> m1 = Monitor() >>> m1.notify(0, DRIVER, REQUEST, 'David', Location(3,2)) >>> m1.notify(2, RIDER, REQUEST, 'Alice', Location(5,7)) >>> m1.notify(4, RIDER, CANCEL, 'Alice', Location(5,7)) >>> m1.notify(5, DRIVER, PICKUP, 'David', Location(5,7)) >>> m1.notify(5, DRIVER, REQUEST, 'David', Location(5,7)) >>> m1.report()['driver_ride_distance'] 0.0 >>> m2 = Monitor() >>> m2.notify(0, DRIVER, REQUEST, 'Tom', Location(1,1)) >>> m2.notify(1, RIDER, REQUEST, 'John', Location(2,3)) >>> m2.notify(2, DRIVER, PICKUP,'Tom', Location(2,3)) >>> m2.notify(3, DRIVER, REQUEST, 'Jacky', Location(14,16)) >>> m2.notify(4, DRIVER, DROPOFF,'Tom', Location(4,5)) >>> m2.notify(4, DRIVER, REQUEST, 'Tom', Location(4,5)) >>> m2.notify(5, RIDER, REQUEST, 'Ivan', Location(10, 11)) >>> m2.notify(7, RIDER, CANCEL, 'Ivan', Location(10, 11)) >>> m2.notify(9, DRIVER, REQUEST, 'Jacky', Location(10, 11)) >>> m2.report()['driver_ride_distance'] 2.0 """ # Accumulator total_ride_distance = 0 # Used for getting the location from the Pickup activity location = None for identifier in self._activities[DRIVER]: # Check whether the driver has more than 1 activity if len(self._activities[DRIVER][identifier]) > 1: # Check the activity for each driver for activity in self._activities[DRIVER][identifier]: # Get the location if the location is Pickup if activity.description == PICKUP: location = activity.location # Calculate the distance by the Dropoff location and # the Pickup location. if activity.description == DROPOFF: total_ride_distance += \ manhattan_distance(activity.location, location) # The average of the total ride distance is the quotient of the total # distance and the number of drivers return total_ride_distance / len(self._activities[DRIVER])
def request_driver(self, rider): """Return a driver for the rider, or None if no driver is available. Add the rider to the waiting list if there is no available driver. @type self: Dispatcher @type rider: Rider @rtype: Driver | None """ if len(self.req_driver) == 0: self.wait_rider.append(rider) return None else: index = 0 distance = manhattan_distance(self.req_driver[0].location, rider.origin) for num in range(1, len(self.req_driver)): m = manhattan_distance(self.req_driver[num].location, rider.origin) if m < distance: distance = m index = num return self.req_driver.pop(index)
def start_ride(self, rider): """Start a ride and return the time the ride will take. @type self: Driver @type rider: Rider @rtype: int >>> d = Driver('drvier1', Location(0, 0), 10) >>> d.start_ride(Rider('rider1', Location(0, 0), Location(10, 10), 100)) 2 """ self.destination = rider.destination self.is_idle = False return round(manhattan_distance(self.location, self.destination)/self.speed)
def get_travel_time(self, destination): """Return the time it will take to arrive at the destination, rounded to the nearest integer. @type self: Driver @type destination: Location @rtype: int >>> d = Driver('drvier1', Location(0, 0), 10) >>> d.get_travel_time(Location(5, 5)) 1 >>> d.get_travel_time(Location(5, 6)) 1 """ return round(manhattan_distance(self.location, destination)/self.speed)
def get_travel_time(self, destination): """Return the time it will take to arrive at the destination, rounded to the nearest integer. @type self: Driver @type destination: Location @rtype: int >>> a = Driver('Ben', Location(1,2), 15, True) >>> a.get_travel_time(Location(16,17)) 2 """ # TODO distance = manhattan_distance(self.location, destination) return round(distance / self.speed)
def start_ride(self, rider): """Start a ride and return the time the ride will take. @type self: Driver @type rider: Rider @rtype: int >>> d = Driver('drvier1', Location(0, 0), 10) >>> d.start_ride(Rider('rider1', Location(0, 0), Location(10, 10), 100)) 2 """ self.destination = rider.destination self.is_idle = False return round( manhattan_distance(self.location, self.destination) / self.speed)
def get_travel_time(self, destination): """Return the time it will take to arrive at the destination, rounded to the nearest integer. @type self: Driver @type destination: Location @rtype: int >>> new_driver = Driver('Donald Trump', Location(1,2), 2) >>> new_driver.get_travel_time(Location(3,4)) 2 """ return (manhattan_distance(self.location, destination) / self.speed).__round__()
def _average_total_distance(self): """Return the average distance drivers have driven. @type self: Monitor @rtype: float """ total_distance = 0 count = 0 from location import manhattan_distance for activities in self._activities[DRIVER].values(): i = 0 while i < len(activities) - 1: if activities[i + 1].description != REQUEST: total_distance += manhattan_distance( activities[i].location, activities[i+1].location) + \ manhattan_distance( activities[i+1].location, activities[i+2].location) i += 3 else: total_distance += manhattan_distance( activities[i].location, activities[i + 1].location) i += 1 count += 1 return total_distance / count
def _average_total_distance(self): """Return the average distance drivers have driven. @type self: Monitor @rtype: float """ from location import manhattan_distance total_distance = 0 count = len(self._activities[DRIVER]) for activities in self._activities[DRIVER].values(): if len(activities) >= 2: total_distance += ((manhattan_distance(activities[0].location, activities[1].location))) return total_distance / count
def get_travel_time(self, destination): """Return the time it will take to arrive at the destination, rounded to the nearest integer. @type self: Driver @type destination: Location @rtype: int >>> d = Driver('drvier1', Location(0, 0), 10) >>> d.get_travel_time(Location(5, 5)) 1 >>> d.get_travel_time(Location(5, 6)) 1 """ return round( manhattan_distance(self.location, destination) / self.speed)
def get_travel_time(self, destination): """Return the time it will take to arrive at the destination, rounded to the nearest integer. @type self: Driver @type destination: Location @rtype: int >>> driver1 = Driver("driver1",Location(5,10), 10) >>> driver1.location = Location(5,6) >>> destination = Location(8,3) >>> driver1.speed = 3 >>> driver1.get_travel_time(destination) 2 """ # TODO return manhattan_distance(self.location,destination) // self.speed
def start_drive(self, location): """Start driving to the location and return the time the drive will take. @type self: Driver @type location: Location @rtype: int >>> driver1 = Driver("driver1",Location(5,10), 10) >>> location = Location(4,8) >>> driver1.destination = Location(2,6) >>> driver1.speed = 2 >>> print(driver1.start_drive(location)) 1 """ # TODO self.destination = location return manhattan_distance(self.location,self.destination) // self.speed
def start_ride(self, rider): """Start a ride and return the time the ride will take. @type self: Driver @type rider: Rider @rtype: int >>> driver = Driver("driver",Location(5,10), 10) >>> driver.location = Location(20,5) >>> driver.speed = 5 >>> rider = Rider("rider","waiting",Location(5,15),Location(20,5),100) >>> driver.start_ride(rider) 5 """ # TODO self.destination = rider.destination return manhattan_distance(self.location,rider.destination) // self.speed
def get_travel_time(self, destination): """Return the time it will take to arrive at the destination, rounded to the nearest integer. @type self: Driver @type destination: Location @rtype: int >>> d1 = Driver('d1', Location(1, 1), 1) >>> d2 = Driver('d2', Location(1, 1), 2) >>> d1.get_travel_time(Location(2, 2)) 2 >>> d2.get_travel_time(Location(5, 5)) 4 """ return round(manhattan_distance(self.location, destination) / self.speed)
def _average_ride_distance(self): """Return the average distance drivers have driven on rides. @type self: Monitor @rtype: float """ distances = [] for activities in self._activities[RIDER].values(): if len(activities) >= 3: for activity in activities: if activity.description == PICKUP: pickup_location = activity.location elif activity.description == DROPOFF: dropoff_location = activity.location distances.append(manhattan_distance(pickup_location,dropoff_location)) # Record all the riders ride distance. return sum(distances)/len(distances)
def _average_ride_distance(self): """Return the average distance drivers have driven on rides. @type self: Monitor @rtype: float """ from location import manhattan_distance total_distance = 0 count = 0 for activities in self._activities[DRIVER].values(): if len(activities) >= 2: total_distance += manhattan_distance(activities[1].location, activities[2].location) count += 1 return total_distance / count
def _average_total_distance(self): """Return the average distance drivers have driven. @type self: Monitor @rtype: float >>> m1 = Monitor() >>> m1.notify(0, DRIVER, REQUEST, 'David', Location(3,2)) >>> m1.notify(2, RIDER, REQUEST, 'Alice', Location(5,7)) >>> m1.notify(4, RIDER, CANCEL, 'Alice', Location(5,7)) >>> m1.notify(5, DRIVER, PICKUP, 'David', Location(5,7)) >>> m1.notify(5, DRIVER, REQUEST, 'David', Location(5,7)) >>> m1.report()['driver_total_distance'] 7.0 >>> m2 = Monitor() >>> m2.notify(0, DRIVER, REQUEST, 'Tom', Location(1,1)) >>> m2.notify(1, RIDER, REQUEST, 'John', Location(2,3)) >>> m2.notify(2, DRIVER, PICKUP,'Tom', Location(2,3)) >>> m2.notify(3, DRIVER, REQUEST, 'Jacky', Location(14,16)) >>> m2.notify(4, DRIVER, DROPOFF,'Tom', Location(4,5)) >>> m2.notify(4, DRIVER, REQUEST, 'Tom', Location(4,5)) >>> m2.notify(5, RIDER, REQUEST, 'Ivan', Location(10, 11)) >>> m2.notify(7, RIDER, CANCEL, 'Ivan', Location(10, 11)) >>> m2.notify(9, DRIVER, REQUEST, 'Jacky', Location(10, 11)) >>> m2.report()['driver_total_distance'] 8.0 """ # Accumulator to get the total distance total_distance = 0 for identifier in self._activities[DRIVER]: # Check whether the driver has drove certain amount of distance if len(self._activities[DRIVER][identifier]) > 1: # Calculate the distance between locations of two nearby # activities of the driver and add it to the total distance for i in range(1, len(self._activities[DRIVER][identifier])): location1 =\ self._activities[DRIVER][identifier][i - 1].location location2 =\ self._activities[DRIVER][identifier][i].location total_distance += manhattan_distance(location2, location1) # The average of the total distance is the quotient of the total # distance and the number of drivers return total_distance / len(self._activities[DRIVER])
def _average_total_distance(self): """Return the average distance drivers have driven. @type self: Monitor @rtype: float """ total_dist = 0 count = 0 for activities in self._activities[DRIVER].values(): i = 0 while i < (len(activities) - 1): distance = manhattan_distance(activities[i].location, activities[i + 1].location) total_dist += distance i += 1 count += 1 return total_dist / count
def start_drive(self, location): """Start driving to the location and return the time the drive will take. @type self: Driver @type location: Location @rtype: int >>> d = Driver('drvier1', Location(0, 0), 10) >>> d.start_drive(Location(5, 5)) 1 >>> d2 = Driver('drvier2', Location(0, 0), 10) >>> d2.start_drive(Location(5, 6)) 1 """ self.destination = location self.is_idle = False r = round(manhattan_distance(self.location, location) / self.speed) self.location = location return r
def start_drive(self, location): """Start driving to the location and return the time the drive will take. @type self: Driver @type location: Location @rtype: int >>> d = Driver('drvier1', Location(0, 0), 10) >>> d.start_drive(Location(5, 5)) 1 >>> d2 = Driver('drvier2', Location(0, 0), 10) >>> d2.start_drive(Location(5, 6)) 1 """ self.destination = location self.is_idle = False r = round(manhattan_distance(self.location, location)/self.speed) self.location = location return r
def get_travel_time(self, destination): """ Return the time it will take to arrive at the destination, rounded to the nearest integer. @type self: Driver @type destination: Location @rtype: int >>> loc = Location(5,5) >>> d = Driver("Bob", Location(5,5), 10) >>> time = d.get_travel_time(Location(10,10)) >>> print(time) 1 >>> type(time) == int True """ return int( manhattan_distance(self.location, destination) / self._speed)
def _average_ride_distance(self): """Return the average distance drivers have driven on rides. @type self: Monitor @rtype: float """ # TODO average_ride_distance = 0 pickup = 0 for activities in self._activities[DRIVER].values(): for activity in activities: if activity.description == PICKUP: pickup = activity.location #location where driver picks up rider (begins ride) elif activity.description == DROPOFF: dropoff = activity.location #location where driver drops off rider (ends ride) average_ride_distance += manhattan_distance(dropoff, pickup) #distance between pickup and dropoff return average_ride_distance / len(self._activities[DRIVER])
def _average_ride_distance(self): """Return the average distance drivers have driven on rides. @type self: Monitor @rtype: float """ ride_dist = 0 count = 0 for activities in self._activities[DRIVER].values(): i = 0 while i < (len(activities) - 1): if activities[i].description == DROPOFF: distance = manhattan_distance(activities[i - 1].location, activities[i].location) ride_dist += distance i += 1 count += 1 return ride_dist / count
def get_travel_time(self, destination): """Return the time it will take to arrive at the destination, rounded to the nearest integer. @type self: Driver @type destination: Location @rtype: int >>> location1 = Location(1,2) >>> speed1 = 3 >>> id1 = 'John Doe' >>> driver = Driver(id1, location1, speed1) >>> destination1 = Location (2,4) >>> driver.get_travel_time(destination1) 1 """ return int(round((manhattan_distance(self.location, destination)) / self.speed, 0))
def _average_total_distance(self): """Return the average distance drivers have driven. @type self: Monitor @rtype: float >>> pickup = Activity(5,"pickup","driver",Location(5,15)) >>> dropoff = Activity(15,"dropoff","driver",Location(10,25)) >>> monitor1 = Monitor() >>> monitor1._activities[DRIVER] = {"driver":[pickup,dropoff]} >>> print(monitor1._average_total_distance()) 15.0 """ # TODO distance = 0 numberOfDriver = 0 for activities in self._activities[DRIVER].values():#The two loops looks through all the activities for each driver for i in range(len(activities) -1):#Then performs a manhaatan distance on the locations of each activity distance += manhattan_distance(activities[i].location,activities[i+1].location) numberOfDriver += 1 return distance / numberOfDriver
def _average_ride_distance(self): """Return the average distance drivers have driven on rides. @type self: Monitor @rtype: float """ ride_distance = 0 count = 0 for activities in self._activities[DRIVER].values(): # The first activity is REQUEST and the second activity is PICKUP. index = 2 # Sum all the distances in between PICKUP and DROPOFF. while index < len(activities): if activities[index].description == DROPOFF: ride_distance += manhattan_distance( activities[index].location, activities[index - 1].location) index += 1 if len(activities) > 0: count += 1 return ride_distance / count
def _average_total_distance(self): """Return the average distance drivers have driven. @type self: Monitor @rtype: float """ total_distance = 0 count = 0 for activities in self._activities[DRIVER].values(): # The driver must have at least two events in order for a distance # to be calculated. if len(activities) >= 2: index = 1 # Sum all the distances the driver has driven. while index < len(activities): total_distance += manhattan_distance( activities[index].location, activities[index - 1].location) index += 1 if len(activities) > 0: count += 1 return total_distance / count
def _average_ride_distance(self): """Return the average distance drivers have driven on rides. @type self: Monitor @rtype: float >>> request = Activity(5,"request","rider",Location(5,15)) >>> pickup = Activity(10,"pickup","rider",Location(5,10)) >>> dropoff = Activity(15,"dropoff","rider",Location(20,15)) >>> monitor1 = Monitor() >>> monitor1._activities[RIDER] = {"rider":[request,pickup,dropoff]} >>> monitor1._activities[DRIVER] = {1:2,2:3,3:4,5:6,7:6,8:9} >>> print(monitor1._average_ride_distance()) 3.3333333333333335 """ # TODO averageRideDistance = 0 count = len(self._activities[DRIVER]) for activity in self._activities[RIDER].values(): if len(activity) == 3:#If a rider has 3 events it means they were picked up so we should find this average. averageRideDistance += manhattan_distance(activity[1].location,activity[2].location) return averageRideDistance / count
def _average_total_distance(self): """Return the average distance drivers have driven. @type self: Monitor @rtype: float # this method is checked when simulation is tested # it is too complicated to write doctest separately as it will involve # creating a new list of events, putting them in the activities # dictionary and then checking avg total distance # >>> loc = Location(0,0) # >>> events = [] # >>> events.append(RiderRequest(2,Rider("r1", loc, Location(3,4), 4))) # >>> events.append(DriverRequest(3, Driver("d1", Location(0,4), 10))) # >>> sim = Simulation() # >>> sim.run(events) # >>> my_list = [] # >>> my_list.append(RiderRequest(1, r)) # >>> my_list.append(DriverRequest(10,d )) # >>> my_list.append(Pickup(12, r, d)) # >>> my_list.append(Cancellation(16, r)) # >>> my_list.append(Dropoff(10 + d.start_ride(r), r, d)) # >>> my_list.append(DriverRequest(17, r)) # >>> for x in my_list : # >>> x.do() """ distance = 0 for activities in self._activities[DRIVER].values(): # if the ith event is a request, that is when the driver starts a # ride the i+1th event must be a pickup, which will tell us when the # ride ended and where the drive started. i+2th event must be a # dropoff which will tell us where the ride ended for i in range(len(activities) - 2): if activities[i].description == REQUEST: start_drive = activities[i].location end_drive = activities[i + 1].location distance += manhattan_distance(start_drive, end_drive) # check if the dropoff actually did happen or if the # rider cancelled the ride before pickup if activities[i + 2].description == DROPOFF: start_ride = end_drive end_ride = activities[i + 2].location distance += manhattan_distance(start_ride, end_ride) return float((distance) / len(self._activities[DRIVER]))
def _average_ride_distance(self): """Return the average distance drivers have driven on rides. @type self: Monitor @rtype: float >>> m = Monitor() >>> m.notify(1, RIDER, REQUEST, 'Dan', Location(1,1)) >>> m.notify(10, DRIVER, REQUEST, 'Arnold', Location(3,3)) >>> m.notify(12, DRIVER, PICKUP, 'Arnold', Location(1,1)) >>> m.notify(12, RIDER, PICKUP, 'Dan', Location(1,1)) >>> m.notify(17, DRIVER, DROPOFF, 'Arnold', Location(6,6)) >>> m.notify(17, RIDER, DROPOFF, 'Dan', Location(6,6)) >>> m.notify(17, DRIVER, REQUEST, 'Arnold', Location(6,6)) >>> m._average_ride_distance() 10.0 """ distance = 0 for activities in self._activities[RIDER].values(): location = activities[0].location for e in activities: destination = e.location distance += manhattan_distance(location, destination) location = destination return distance / len(self._activities[DRIVER])
def _average_ride_distance(self): """Return the average distance drivers have driven on rides. @type self: Monitor @rtype: float # this method is checked when simulation is tested # it is too complicated to write doctest separately as it will involve # creating a new list of events, putting them in the activities # dictionary and then checking avg total distance """ distance = 0 for activities in self._activities[DRIVER].values(): # if the ith event is a pickup where the drive started. # i+1th event may or may not be a dropoff. if it is a dropoff, it # will tell us where the ride ended for i in range(len(activities) - 1): # check if the dropoff actually did happen or if the # rider cancelled the ride before pickup if (activities[i].description == PICKUP and activities[i + 1].description == DROPOFF): start_ride = activities[i].location end_ride = activities[i + 1].location distance += manhattan_distance(start_ride, end_ride) return float(distance / len(self._activities[DRIVER]))
def get_travel_time(self, destination: Location) -> int: """Return the time it will take to arrive at the destination, rounded to the nearest integer. """ dist = manhattan_distance(self.location, destination) time = dist / self.speed return round(time)
def start_ride(self, rider): """Start a ride and return the time the ride will take. @type self: Driver @type rider: Rider @rtype: int """ # TODO self.destination = rider.destination return manhattan_distance(self.location,rider.destination) // self.speed
def get_travel_time(self, destination): """Return the time it will take to arrive at the destination, rounded to the nearest integer. @type self: Driver @type destination: Location @rtype: int """ # TODO return manhattan_distance(self.location, destination) // self.speed
def start_drive(self, location): """Start driving to the location and return the time the drive will take. @type self: Driver @type location: Location @rtype: int """ # TODO self.destination = location return manhattan_distance(self.location,self.destination) // self.speed
def start_drive(self, location): """Start driving to the location and return the time the drive will take. @type self: Driver @type location: Location @rtype: int """ # TODO self.destination = location return manhattan_distance(self.location, self.destination) // self.speed
def start_drive(self, location): """Start driving to the location and return the time the drive will take. @type self: Driver @type location: Location @rtype: int """ self.is_idle = False self.destination = location return round(manhattan_distance(self.location, location) / self.speed)
def start_ride(self, rider): """Start a ride and return the time the ride will take. @type self: Driver @type rider: Rider @rtype: int """ # TODO self.destination = rider.destination return manhattan_distance(self.location, rider.destination) // self.speed
def start_ride(self, rider): """Start a ride and return the time the ride will take. @type self: Driver @type rider: Rider @rtype: int """ self.is_idle = False self.destination = rider.destination return round( manhattan_distance(self.location, rider.destination) / self.speed)
def _average_total_distance(self) -> float: """Return the average distance drivers have driven. """ dist = 0 count = 0 for activities in self._activities[DRIVER].values(): for i in range(len(activities) - 1): dist += (manhattan_distance(activities[i].location, activities[i + 1].location)) count += 1 return dist / count
def get_travel_time(self, destination): """Return the time it will take to arrive at the destination, rounded to the nearest integer. @type self: Driver @type destination: Location @rtype: int """ # travel time includes time to pick up rider and drop him off at his # destination? return round( manhattan_distance(self.location, destination) / self.speed)
def _average_ride_distance(self) -> float: """Return the average distance drivers have driven on rides. """ dist = 0 count = 0 for activities in self._activities[DRIVER].values(): for i in range(len(activities) - 1): if activities[i].description == PICKUP: dist += manhattan_distance(activities[i].location, activities[i + 1].location) count += 1 return dist / count
def get_travel_time(self, destination): """Return the time it will take to arrive at the destination, rounded to the nearest integer. @param self: Driver @param destination: Location @rtype: int >>> driver_Atom = Driver('Atom', Location(0,0), 1) >>> driver_Atom.get_travel_time(Location(5,8)) 13 >>> """ return round(manhattan_distance(self.location, destination) / self.speed)
def get_travel_time(self, destination): """Return the time it will take to arrive at the destination, rounded to the nearest integer. @type self: Driver @type destination: Location @rtype: int >>> d = Driver('Amaranth', Location(1, 1), 1) >>> pt = Location(2, 2) >>> print(d.get_travel_time(pt)) 2 """ return round( manhattan_distance(self.location, destination) / self.speed)
def get_travel_time(self, destination): """Return the time it will take to arrive at the destination, rounded to the nearest integer. @type self: Driver @type destination: Location @rtype: int >>> location1 = Location(1, 1) >>> destination1 = Location(2, 2) >>> driver1 = Driver("Bob", location1, 1) >>> print(driver1.get_travel_time(destination1)) 2 """ return manhattan_distance(self.location, destination) / self.speed
def _average_total_distance(self): """Return the average distance drivers have driven. @type self: Monitor @rtype: float """ total_distance = 0 distances = [] for activities in self._activities[DRIVER].values(): if len(activities) >= 2: temp_location = activities[0].location for activity in activities: total_distance += manhattan_distance(temp_location,activity.location) temp_location = activity.location distances.append(total_distance) # Record all the drivers driven distance. return sum(distances)/len(distances)