Skip to content

PouyaBaniadam/py_elevator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PyElevator: The Ultimate Elevator Algorithm Simulator

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.

PyElevator Startup

Features

  • Interactive Terminal Interface: A rich, dynamic interface built with the rich library 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.

The Algorithms

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.

PyElevator in Action

Getting Started

Prerequisites

To run the simulator, you will need Python 3 and the following libraries:

  • pynput
  • pyfiglet
  • rich

You can install them using pip:

  pip install -r requirements.txt

Running the Simulator

  1. Save the provided code snippets as elevator.py and main.py.
  2. Run the main script from your terminal:
    python main.py

How to Use

  1. 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.
  2. 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.

  3. Quitting: To exit the simulator, type quit into the input prompt and press Enter.

Code Overview

elevator.py

This file contains the core logic for the elevator and the scheduling algorithms.

  • Direction Enum: Defines the possible states of elevator movement (UP, DOWN, IDLE).
  • AlgorithmStrategy (Abstract Base Class): Provides an interface for all scheduling algorithms, ensuring they implement the sort_requests method.
  • FCFS, SSTF, LOOK, C_LOOK Classes: Each class inherits from AlgorithmStrategy and implements its specific logic for ordering the request_floors list.
  • Elevator Class: The main class that manages the elevator's state, including its current floor, direction, request queue, and total cost. The step() method moves the elevator one floor at a time, and add_request() adds new user requests to the queue, re-sorting them according to the current algorithm.

main.py

This file handles the user interface and the main simulation loop.

  • rich Integration: The script uses the rich library's Layout, Panel, and Live features to create a responsive and visually appealing terminal UI.
  • pynput for Input: The pynput library is used to listen for keyboard input asynchronously, allowing the simulation to run smoothly while waiting for user commands.
  • Threading and Queues: A threading.Lock and a queue.Queue are used to safely handle user input from a separate thread without interrupting the main simulation and rendering loop.
  • Main Loop: The while True loop continuously updates the UI, processes commands from the queue, and calls the elevator's step() 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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages