Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event Queue Handler #39

Open
2 tasks
SeanMcOwen opened this issue Jul 3, 2024 · 1 comment
Open
2 tasks

Event Queue Handler #39

SeanMcOwen opened this issue Jul 3, 2024 · 1 comment
Labels

Comments

@SeanMcOwen
Copy link
Collaborator

This should be done after some of the other wirings are first complete, but eventually we need a controller wiring that does the routing of an event to the wiring that it should take on. When we get here I can help out a bunch because there are a LOT of design considerations to work through first

  • Add the following text to the wiring or control action (from the notebook):
  • The simulation is event-driven, handling various events such as ride requests, driver assignments, and ride completions.
  • An event queue manages the sequence and timing of events, ensuring that the system evolves over time.
  • Scaffold the wiring and write issues for all the actions that might need to come after
@SeanMcOwen SeanMcOwen added the DIPR label Jul 3, 2024
@SeanMcOwen
Copy link
Collaborator Author

Relevant Code:

def initialize_event_queue(ride_requests) -> List[Event]:

  initial_events = []
  start_time_counter = Counter()

  for ride_id in ride_requests:
      start_time = random.randint(1, len(ride_requests))
      start_time_counter[start_time] += 1
      initial_events.append(Event(t=start_time, action="Request Ride", spaces={"ride_id": ride_id}))
  for _, count in list(start_time_counter.items()):
      if count > 10:
          start_time_counter[start_time] = 10
          
  heapq.heapify(initial_events)

  return initial_events

def process_events(event_queue, simulation):
current_time = 0
retry_queue = deque()

  while event_queue or retry_queue:
      while event_queue and event_queue[0].t == current_time:
          current_event = heapq.heappop(event_queue)
          handle_event(current_event, event_queue, simulation, retry_queue, current_time)
      while retry_queue and retry_queue[0].t <= current_time:
          current_event = retry_queue.popleft()
          handle_event(current_event, event_queue, simulation, retry_queue, current_time)
      
      # Record the state at each time step
      simulation.record_state(current_time)
      print("State recorded")
      current_time += 1
      simulation.grid.update_priorities()
  
  print(f"The event queue is now empty. Simulation complete! Time: {current_time}")
  simulation.state_map.print_state_map()

def handle_event(event, event_queue, simulation, retry_queue, current_time):
print(f"Processing event: {event} at time {current_time}")

  if event.action == "Request Ride":
      simulation.handle_ride_request_event(event, event_queue, current_time, retry_queue)
  elif event.action == "Driver Assigned":
      simulation.handle_driver_assigned_event(event, event_queue, current_time)
  elif event.action == "Complete Ride":
      simulation.handle_complete_ride_event(event, event_queue, current_time)
  else:
      print(f"Unknown event action: {event.action}")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant