Site icon R-bloggers

When will that elevator arrive? Elevator dispatching

[This article was first published on Online College Math Teacher, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
< size = 3>
      I was recently at a high-rise building with 23 floors and 6 elevators. There always seemed to be a long wait for an elevator. This made me think: there must be elevator algorithms where the set of elevators decides which one will answer your call. And of course there are. Elevator dispatching falls under operations research, optimization, queuing theory, etc.

      Suppose I am on floor 11 and I press “Down.” Some of the inputs to the algorithm might be:

  1. Which elevators are currently moving down?
  2. Which are closest?
  3. How many stops are already scheduled?
  4. How full is each elevator?
  5. How long would I wait?

      The simplest modeling approach would be a nearest-elevator algorithm: when someone presses the down button, the elevator system assigns the passenger to the elevator that is physically closest in floors. This is an illustrative algorithm, not a standard industry algorithm.

      Let’s do some simulations in R. For a first approximation, we can make the following assumptions:

  1. The building has 23 floors, 6 elevators, and 5 passengers waiting to board.
  2. The elevators are initially distributed randomly throughout the building.
  3. Each elevator is stationary when the requests arrive.
  4. Each elevator travels at the same speed.
  5. The system assigns each passenger to the elevator closest to them in terms of floors, ignoring direction, waiting passengers, capacity, and existing assignments.
  6. Each elevator can be assigned multiple passengers.
  7. The passenger assignments are calculated independently. The code does not update elevator routes after assigning a passenger.

 
# nearest-elevator algorithm
set.seed(123)

# Building and elevator information
num_floors <- 23
num_elevators <- 6
num_passengers <- 5

# Randomly position the six elevators
elevators <- sample(1:num_floors, num_elevators,
                    replace = TRUE)

# Passenger floors
# Passenger 1 is you, on floor 11
passengers <- c(11, sample(1:num_floors, 4,
                           replace = TRUE))

# Calculate assignments
assignments <- data.frame(
  Passenger = 1:num_passengers,
  Floor = passengers,
  Elevator = NA,
  Distance = NA
)

for (i in 1:num_passengers) {

  # Distance from each elevator to passenger
  distances <- abs(elevators - passengers[i])

  # Find closest elevator
  assignments$Elevator[i] <- which.min(distances)   # If there is a tie, choose the first elevator.

  # Record distance
  assignments$Distance[i] <- min(distances)
}

# Display elevator locations
cat("Elevator locations:\n")
print(elevators)

# Display passenger assignments
cat("\nPassenger assignments:\n")
print(assignments)
                             


Elevator locations:
[1] 15 19 14  3 10 18

Passenger assignments:
  Passenger Floor Elevator Distance
1         1    11        5        1
2         2    22        2        3
3         3    11        5        1
4         4     5        4        2
5         5    20        2        2


      In the simulation above, you (Passenger 1 on floor 11) are assigned to Elevator 5 on floor 10 because it is only 1 floor away, regardless of which direction it is moving.

      The nearest-elevator algorithm ignores an important factor: direction. An elevator one floor away but moving in the wrong direction may take longer to reach you than an elevator several floors away that is already heading toward your floor. A more useful approach is to estimate the time each elevator would take to reach the passenger. This is the basic idea behind an Estimated Time of Arrival (ETA) approach. Our next simulation will consider each elevator's current floor, direction, and destination. The important change is that we no longer assign a passenger solely on the basis of physical distance. Instead, we estimate how long each elevator would take to reach the passenger's floor.

      Assumptions for the simulation:

  1. There are 23 floors, 6 elevators, and 5 passengers waiting to board.
  2. Each elevator travels at one floor per second.
  3. Each elevator has a current floor, direction, and destination.
  4. Reversing direction adds a 2-second turnaround penalty.
  5. All five passengers request DOWN service.
  6. An elevator can accept multiple requests.
  7. We assign each passenger to the elevator with the shortest estimated arrival time.

For simplicity, we assume elevators reverse direction at their current destination and do not stop for intermediate passengers while estimating arrival time.

# Estimated Time of Arrival (ETA) algorithm
set.seed(123)

# Building specifications
num_floors <- 23
num_elevators <- 6

# Initialize elevators
elevators <- data.frame(
  Elevator = 1:num_elevators,
  Floor = sample(1:num_floors, num_elevators, replace = TRUE),
  Direction = NA,
  Destination = NA
)

# Randomly assign elevator directions and destinations
for (i in 1:num_elevators) {
  
  possible_directions <- c(-1, 0, 1)
  elevators$Direction[i] <- sample(possible_directions, 1)
  
  # Prevent elevators from moving beyond building limits
  if (elevators$Floor[i] == 1 && elevators$Direction[i] == -1) {
    elevators$Direction[i] <- 0
  }
  
  if (elevators$Floor[i] == num_floors && elevators$Direction[i] == 1) {
    elevators$Direction[i] <- 0
  }
  
  # Assign a destination STRICTLY ahead of the current floor
  if (elevators$Direction[i] == 1) {
    up_floors <- (elevators$Floor[i] + 1):num_floors
    elevators$Destination[i] <- if (length(up_floors) == 1) up_floors else sample(up_floors, 1)
  } else if (elevators$Direction[i] == -1) {
    down_floors <- 1:(elevators$Floor[i] - 1)
    elevators$Destination[i] <- if (length(down_floors) == 1) down_floors else sample(down_floors, 1)
  } else {
    elevators$Destination[i] <- elevators$Floor[i]
  }
}


# Function to estimate elevator arrival time for a DOWN request
# turnaround_penalty = 2 is an assumed penalty
arrival_time <- function(current, direction, destination,
                         passenger_floor, passenger_dir = -1,
                         turnaround_penalty = 2) {
  
  # 1. Elevator is stationary
  if (direction == 0) {
    return(abs(current - passenger_floor))
  }
  
  # 2. Elevator is traveling UP
  if (direction == 1) {
    # Since the passenger wants to go DOWN (passenger_dir == -1), 
    # an UP elevator must finish its upward trip and reverse direction first.
    return((destination - current) +
             abs(destination - passenger_floor) +
             turnaround_penalty)
  }
  
  # 3. Elevator is traveling DOWN
  if (direction == -1) {
    # Check if the passenger is on or below the current floor, 
    # AND on or above the elevator's current destination
    if (passenger_floor = destination) {
      return(current - passenger_floor)
    } else if (passenger_floor < destination) {
      # Passenger is further down in the SAME direction (no turnaround needed)
      return(current - passenger_floor)
    } else {
      # Passenger is ABOVE the elevator; elevator must finish going down,
      # reverse direction, and travel back up to the passenger
      return((current - destination) +
               abs(passenger_floor - destination) +
               turnaround_penalty)
    }
  }
}


# Passenger floors (Passenger 1 is on floor 11)
passengers <- c(11, sample(1:num_floors, 4, replace = TRUE))

# Assign passengers to elevators
assignments <- data.frame(
  Passenger = 1:5,
  Floor = passengers,
  Elevator = NA,
  Estimated_Time = NA
)

for (i in 1:5) {
  times <- sapply(1:num_elevators, function(j) {
    arrival_time(
      elevators$Floor[j],
      elevators$Direction[j],
      elevators$Destination[j],
      passengers[i]
    )
  })
  
  best_elevator <- which.min(times)
  assignments$Elevator[i] <- best_elevator
  assignments$Estimated_Time[i] <- times[best_elevator]
}

cat("\nELEVATOR STATUS\n")
print(elevators)

cat("\nPASSENGER ASSIGNMENTS\n")
print(assignments)


ELEVATOR STATUS
  Elevator Floor Direction Destination
1        1    15        -1          14
2        2    19        -1           9
3        3    14        -1          11
4        4     3         1          18
5        5    10         1          12
6        6    18         0          18

PASSENGER ASSIGNMENTS
  Passenger Floor Elevator Estimated_Time
1         1    11        3              3
2         2    22        6              4
3         3    11        3              3
4         4     5        2             14
5         5    20        6              2
      Here, Elevator 5 (floor 10) is moving upward toward floor 12, so the system instead assigns you to Elevator 3 on floor 14, which is already heading down and reaches you in 3 seconds.

      More sophisticated systems use dynamic group control. They continually evaluate elevator positions, directions, existing calls, and predicted travel times. As new requests arrive or elevator conditions change, the system may revise its assignments.

Further Reading: Richard D. Peters, "Elevator Dispatching" (ELEVCON Paris 2014, Peters Research Ltd).
https://download.peters-research.com/library/Elevator_Dispatching.pdf
####################################################################

End < >

To leave a comment for the author, please follow the link and comment on their blog: Online College Math Teacher.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Exit mobile version