0
0
LldHow-ToIntermediate ยท 4 min read

How to Design an Elevator System: Architecture and Flow

To design an elevator system, model elevators as independent units with states like current floor and direction, and implement a scheduler to assign requests efficiently. Use request queues and state machines to manage elevator movements and optimize wait times.
๐Ÿ“

Syntax

An elevator system design typically includes these parts:

  • Elevator: Tracks current floor, direction, and door status.
  • Request: Represents a call from a floor or inside the elevator.
  • Scheduler: Assigns requests to elevators based on their state.
  • State Machine: Controls elevator movement and door operations.

This structure helps organize the system clearly and handle requests efficiently.

java
class Elevator {
    int id;
    int currentFloor;
    String direction; // "UP", "DOWN", or "IDLE"
    Queue<Integer> destinationFloors;

    void move() {
        // Move elevator one floor towards next destination
    }

    void openDoor() {
        // Open elevator door
    }

    void closeDoor() {
        // Close elevator door
    }
}

class Scheduler {
    List<Elevator> elevators;

    void assignRequest(int floor, String direction) {
        // Find best elevator and add floor to its destination queue
    }
}
๐Ÿ’ป

Example

This example shows a simple elevator system with two elevators handling floor requests. The scheduler assigns requests to the closest idle or moving elevator going in the right direction.

java
import java.util.*;

class Elevator {
    int id;
    int currentFloor = 0;
    String direction = "IDLE";
    TreeSet<Integer> destinations = new TreeSet<>();

    Elevator(int id) {
        this.id = id;
    }

    void addDestination(int floor) {
        destinations.add(floor);
        updateDirection();
    }

    void updateDirection() {
        if (destinations.isEmpty()) {
            direction = "IDLE";
        } else if (currentFloor < destinations.first()) {
            direction = "UP";
        } else if (currentFloor > destinations.last()) {
            direction = "DOWN";
        }
    }

    void step() {
        if (direction.equals("UP")) {
            currentFloor++;
            if (destinations.contains(currentFloor)) {
                destinations.remove(currentFloor);
                System.out.println("Elevator " + id + " stopped at floor " + currentFloor);
            }
        } else if (direction.equals("DOWN")) {
            currentFloor--;
            if (destinations.contains(currentFloor)) {
                destinations.remove(currentFloor);
                System.out.println("Elevator " + id + " stopped at floor " + currentFloor);
            }
        }
        updateDirection();
    }
}

class Scheduler {
    List<Elevator> elevators;

    Scheduler(List<Elevator> elevators) {
        this.elevators = elevators;
    }

    void requestFloor(int floor) {
        Elevator bestElevator = null;
        int minDistance = Integer.MAX_VALUE;
        for (Elevator e : elevators) {
            int distance = Math.abs(e.currentFloor - floor);
            if (e.direction.equals("IDLE") && distance < minDistance) {
                bestElevator = e;
                minDistance = distance;
            }
        }
        if (bestElevator == null) {
            bestElevator = elevators.get(0); // fallback
        }
        bestElevator.addDestination(floor);
        System.out.println("Assigned floor " + floor + " to elevator " + bestElevator.id);
    }
}

public class ElevatorSystemDemo {
    public static void main(String[] args) {
        Elevator e1 = new Elevator(1);
        Elevator e2 = new Elevator(2);
        Scheduler scheduler = new Scheduler(Arrays.asList(e1, e2));

        scheduler.requestFloor(5);
        scheduler.requestFloor(3);

        for (int i = 0; i < 10; i++) {
            e1.step();
            e2.step();
        }
    }
}
Output
Assigned floor 5 to elevator 1 Assigned floor 3 to elevator 2 Elevator 2 stopped at floor 3 Elevator 1 stopped at floor 5
โš ๏ธ

Common Pitfalls

Common mistakes when designing elevator systems include:

  • Ignoring concurrent requests causing inefficient scheduling.
  • Not handling elevator state transitions properly, leading to stuck elevators.
  • Assigning requests to elevators moving away from the requested floor.
  • Failing to update elevator direction after servicing requests.

Proper state management and request assignment logic are key to avoid these issues.

java
/* Wrong: Assign request to any elevator without checking direction */
void assignRequestWrong(int floor) {
    elevators.get(0).addDestination(floor); // always first elevator
}

/* Right: Assign request to closest elevator moving towards the floor or idle */
void assignRequestRight(int floor) {
    Elevator best = null;
    int minDist = Integer.MAX_VALUE;
    for (Elevator e : elevators) {
        if ((e.direction.equals("UP") && floor >= e.currentFloor) ||
            (e.direction.equals("DOWN") && floor <= e.currentFloor) ||
            e.direction.equals("IDLE")) {
            int dist = Math.abs(e.currentFloor - floor);
            if (dist < minDist) {
                best = e;
                minDist = dist;
            }
        }
    }
    if (best != null) best.addDestination(floor);
}
๐Ÿ“Š

Quick Reference

  • Elevator: Track floor, direction, and destinations.
  • Scheduler: Assign requests to best elevator based on state.
  • State Machine: Manage elevator movement and door operations.
  • Request Handling: Queue requests and assign efficiently.
  • Optimization: Minimize wait and travel time by smart scheduling.
โœ…

Key Takeaways

Model elevators with clear states and destination queues for easy management.
Use a scheduler to assign requests to the best elevator based on current position and direction.
Implement a state machine to control elevator movement and door operations reliably.
Avoid assigning requests to elevators moving away from the requested floor.
Optimize for minimal wait and travel time by updating elevator directions dynamically.