BlockScience / rideshare-model

0 stars 0 forks source link

Integrate RideRequest class into spec #52

Open SeanMcOwen opened 5 days ago

SeanMcOwen commented 5 days ago

I wonder if it is best to have this as a class in python that is actually a "type" in the spec.

I can explain this one more but basically we would be allowed to define this as a type and it still would make sense

SeanMcOwen commented 5 days ago

Code:

class RideRequest: states = ['requested', 'accepted', 'in_progress', 'completed', 'canceled']

  def __init__(self, ride_id, rider, origin_zone, destination_zone, grid):
      self.ride_id = ride_id
      self.rider = rider
      self.origin_zone = origin_zone
      self.destination_zone = destination_zone
      self.grid = grid
      self.metadata = self.generate_metadata()
      self.driver = None
      self.machine = Machine(model=self, states=RideRequest.states, initial='requested')
      self.machine.add_transition('accept', 'requested', 'accepted')
      self.machine.add_transition('start', 'accepted', 'in_progress')
      self.machine.add_transition('complete', 'in_progress', 'completed')
      self.machine.add_transition('cancel', '*', 'canceled')

  def generate_metadata(self):
      driver_to_pickup = self.calculate_travel_time(self.origin_zone, self.origin_zone)
      pickup_to_destination = self.calculate_travel_time(self.origin_zone, self.destination_zone)
      total_distance = driver_to_pickup + pickup_to_destination
      duration = total_distance
      cost = total_distance * 2.5
      return {
          "distance_km": total_distance,
          "duration_min": duration,
          "pickup_time": None,
          "dropoff_time": None,
          "cost_usd": round(cost, 2),
          "driver_rating": round(random.uniform(4.0, 5.0), 2),
          "rider_rating": round(random.uniform(4.0, 5.0), 2),
          "ride_type": random.choice(["economy", "premium", "pool"]),
          "traffic_conditions": random.choice(["normal", "heavy", "light"]),
          "weather_conditions": random.choice(["clear", "rainy", "stormy", "snowy"]),
      }

  def calculate_travel_time(self, zone1, zone2):
      try:
          return self.grid.shortest_paths[(zone1.x, zone1.y)][(zone2.x, zone2.y)]
      except KeyError:
          return float('inf')