PyElevator is a sophisticated, terminal-based elevator simulation that brings classic computer science scheduling algorithms to life. This interactive tool allows users to visualize and compare different elevator control strategies in real-time, offering a hands-on understanding of their efficiency and behavior.
- Interactive Terminal Interface: A rich, dynamic interface built with the
richlibrary provides a real-time view of the elevator shaft, current status, and an event log. - Multiple Scheduling Algorithms: Switch between four well-known elevator scheduling algorithms to see how they perform under different scenarios.
- Real-time Control: Request floors on the fly and watch how the elevator responds according to the selected algorithm.
- Performance Metrics: The simulator tracks the total "cost" in terms of floors traveled, giving a clear metric for comparing algorithm efficiency.
- Customizable Simulation: Set the building's floor range and the elevator's starting position to create unique challenges.
PyElevator includes four distinct algorithms, each with its own strategy for handling floor requests.
- LOOK: Scans towards the furthest request in one direction, servicing all stops along the way, then reverses. A balanced and efficient choice.
- Circular LOOK (C-LOOK): Like LOOK, but after reaching the last stop in one direction, it jumps to the furthest request in the other direction without servicing stops on the return journey. This promotes more uniform wait times.
- Shortest Seek Time First (SSTF): Prioritizes the closest floor request to the elevator's current position to minimize immediate travel time. This can be very efficient but may lead to starvation for distant floors.
- First-Come, First-Served (FCFS): Processes requests in the exact order they are received. It's the simplest method but often the least efficient in terms of total travel distance.
To run the simulator, you will need Python 3 and the following libraries:
pynputpyfigletrich
You can install them using pip:
pip install -r requirements.txt- Save the provided code snippets as
elevator.pyandmain.py. - Run the main script from your terminal:
python main.py-
Configuration: Upon launching, you will be prompted to set:
- The lowest and highest floors of the building.
- The elevator's starting floor.
- The scheduling algorithm to use.
- You can press Enter to accept the default values for a quick start.
-
Requesting Floors: Once the simulation starts, simply type a floor number into the input prompt at the bottom and press
Enter. The request will be added to the queue and the elevator will begin servicing it based on the chosen algorithm's logic. -
Quitting: To exit the simulator, type
quitinto the input prompt and pressEnter.
This file contains the core logic for the elevator and the scheduling algorithms.
DirectionEnum: Defines the possible states of elevator movement (UP, DOWN, IDLE).AlgorithmStrategy(Abstract Base Class): Provides an interface for all scheduling algorithms, ensuring they implement thesort_requestsmethod.FCFS,SSTF,LOOK,C_LOOKClasses: Each class inherits fromAlgorithmStrategyand implements its specific logic for ordering therequest_floorslist.ElevatorClass: The main class that manages the elevator's state, including its current floor, direction, request queue, and total cost. Thestep()method moves the elevator one floor at a time, andadd_request()adds new user requests to the queue, re-sorting them according to the current algorithm.
This file handles the user interface and the main simulation loop.
richIntegration: The script uses therichlibrary'sLayout,Panel, andLivefeatures to create a responsive and visually appealing terminal UI.pynputfor Input: Thepynputlibrary is used to listen for keyboard input asynchronously, allowing the simulation to run smoothly while waiting for user commands.- Threading and Queues: A
threading.Lockand aqueue.Queueare used to safely handle user input from a separate thread without interrupting the main simulation and rendering loop. - Main Loop: The
while Trueloop continuously updates the UI, processes commands from the queue, and calls the elevator'sstep()method at a fixed interval to simulate movement. generate_elevator_view: This clever function creates the visual representation of the building shaft, dynamically centering the view around the elevator's current position.

