Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Exam 10/01/2024

The Elevator Problem

Part I - Elevator Concepts - 12 pts........................................................................................2


A) One floor at a time without user requests - 5 pts........................................................... 2
B) Closest floor - 2 pts........................................................................................................4
C) Adding user requests to the simulation - 3 pts.............................................................. 5
D) Stopping at intermediate level calls - 2 pts.................................................................... 8
Part II - Up and Down Elevator - 8 pts................................................................................ 10
A) Up and Down elevator with random calls - 5 pts......................................................... 10
B) Implementing a capacity limit - 3 pts............................................................................13

PALISSON Antoine The Elevator Problem 1


Exam 10/01/2024

Part I - Elevator Concepts - 12 pts

A) One floor at a time without user requests - 5 pts


Imagine an elevator in a building responding to a sequence of floor calls. Your function
should simulate the elevator's movement floor by floor, taking into account the time spent
moving and stopping at each floor as per the calls. The simulation ends when all calls have
been processed.

1. (2 pts) First, write a function level_search that should process a list of requested
floors (calls) and a specific target floor (target). It will remove all instances of the
target floor from the list and count the number of stops made at this specific floor.
The function should return the modified calls list and the number of stops count.

Example:

2. (3 pts) Write a function named elevator_A to simulate the movement of an elevator


responding to a series of floor calls. It will track and display the elevator's journey,
showing its position, the time elapsed, and the remaining calls to be attended in
real-time.
a. Inputs: a list of integers (calls) containing floors that the elevator is called to.

b. Constraints:
i. The elevator starts from the ground floor (0).
ii. The elevator moves to the last call of the calls list and continues to do
so until the calls list is empty.
iii. The time increments differently depending on the elevator's action:
moving one floor takes 2 seconds, and stopping at a floor (when it
reaches a call) takes 10 seconds.
iv. In this function, the elevator tackles one step at a time. It only stops at
the current target floor and doesn’t consider the intermediate ones.
v. When the elevator reaches the target floor, remove all the
appearances of the target floor in the calls list by using the
level_search function.

c. Outputs: At each step, the function should display the current time, the
current position, the current targeted floor and the remaining floors.

PALISSON Antoine The Elevator Problem 2


Exam 10/01/2024

Example of output:

PALISSON Antoine The Elevator Problem 3


Exam 10/01/2024

B) Closest floor - 2 pts


(2 pts) Modify the existing elevator_A function to create a new function elevator_B. This
updated function will simulate an elevator's optimized response to floor calls. Instead of
sequentially attending to calls, elevator_B will choose the closest call to the current position
at each step in order to simulate a more efficient elevator system. The function continues to
display the elevator's position, the time elapsed, and the closest call being attended in
real-time, along with the remaining calls.

Example of output:

PALISSON Antoine The Elevator Problem 4


Exam 10/01/2024

C) Adding user requests to the simulation - 3 pts


1. (1 pt) First, write a function named user_request that simulates user requests in an
elevator system. This function will generate random floor calls based on the number
of users entering the elevator. The generated calls should exclude the elevator's
current floor to reflect realistic scenarios where users request to go to floors other
than the one they are currently on.

From now, you will consider that there are 13 floors from -2 to 10 included.

Example:

PALISSON Antoine The Elevator Problem 5


Exam 10/01/2024

2. (2 pts) Write a function elevator_C, which integrates both external floor calls (calls)
and internal user requests (inside_calls). It will simulate a more realistic elevator
operation by processing external calls to pick up users and then attending to their
individual floor requests made from inside the elevator. It will track and display the
elevator's journey, showing its position, the time elapsed, and the remaining calls to
be attended in real-time.

a. Inputs: a list of integers (calls) containing floors that the elevator is called to.

b. Constraints:
i. The elevator starts from the ground floor (0).
ii. The time increments differently depending on the elevator's action:
moving one floor takes 2 seconds, and stopping at a floor (when it
reaches a call) takes 10 seconds.
iii. The elevator tackles one step at a time. It only stops at the current
target floor and doesn’t consider the intermediate ones.
iv. The function maintains two lists: one for the external calls (calls) and
another for the internal calls (inside_calls) which will be generated
based on the number of users entering the elevator at each stop.
v. First, the elevator moves to the closest call of the calls list but:
1. When the elevator reaches a floor from the calls list, remove
that call and all the appearances in the calls list using the
level_search function (part A).
2. Then, use the user_request function to simulate new users
entering the elevator and choosing their destination floors.
3. Process all the user requests (inside_calls) by moving to the
closest call of the inside_calls list until it is empty.
4. While processing the user requests, if the elevator stops at a
level where there are calls from the calls list make them enter
and add their requests to the inside_calls list.

c. Outputs: At each step, the function should display the current time, the
current position, the current targeted floor and the remaining floors.

PALISSON Antoine The Elevator Problem 6


Exam 10/01/2024

Example of output:

PALISSON Antoine The Elevator Problem 7


Exam 10/01/2024

D) Stopping at intermediate level calls - 2 pts


(2 pts) Update the elevator_C function to create a new function named elevator_D that
stops at intermediate floors if there are pending level calls (calls list) or in the inside calls
(inside_calls list). The closest floor is still considered in the calls list but the oldest floor
request is now considered in the inside_calls list. This adjustment aims to create a more
dynamic and realistic representation of how an actual elevator operates in a building.

Tips: When the elevator reaches a floor, check both lists (calls and inside_calls) for any
required stops at that floor. Use the level_search and the user_request functions. Don’t
forget to adjust the time increments to account for these additional stops.

PALISSON Antoine The Elevator Problem 8


Exam 10/01/2024

Example of output:

PALISSON Antoine The Elevator Problem 9


Exam 10/01/2024

Part II - Up and Down Elevator - 8 pts


In this second part of the exam, you will apply the knowledge acquired in Part I to a more
dynamic and complex elevator simulation scenario. You will create a series of functions to
simulate an elevator operating with an "up" and "down" strategy. Unlike Part I, there will be
no predefined list of calls. Instead, you will simulate real-time call generation, with the
elevator reacting dynamically to these calls.

A) Up and Down elevator with random calls - 5 pts


1. (1 pt) Write a function named generate_random_calls to simulate the generation of
elevator calls in real-time. This function will randomly create a set of floor calls,
simulating passengers pressing call buttons at various floors. The function takes one
parameter, n, representing the number of seconds passed. For each second, a call is
generated with a probability of 7.5%. The function then returns a list of floor numbers
where calls are generated.

Example:

PALISSON Antoine The Elevator Problem 10


Exam 10/01/2024

2. (4 pts) Write a function named elevator_up_down to simulate an elevator operating


under an "up-and-down" strategy. This strategy involves the elevator moving in one
direction (up or down) until it has responded to all calls in that direction before
reversing its course. The function should dynamically handle real-time generated
calls managing both internal (inside of the elevator) and external (floor) requests.

a. Inputs: an integer (seconds) that specifies the simulation duration.

b. Constraints:
i. The elevator starts from the ground floor (0) and can move up or down
based on calls.
ii. The elevator has two types of calls: internal (inside_calls) and level
calls (level_calls), initially empty.
iii. In each cycle of the simulation:
1. Use the generate_random_calls function to add new level
calls randomly over time:
a. Waiting (no calls) takes 1 second.
b. Moving from one floor to the next one takes 2 seconds.
c. Stopping at a floor takes 10 seconds.
2. At any floor, search for matching calls in both level and inside
call lists using level_search. If there are people entering the
elevator, add their request to the inside_calls list using the
user_request function.
3. Determine the elevator's next move based on the current calls
and direction. If moving 'up' and the highest call is above the
current position, continue up; otherwise, change direction.
4. Repeat similar logic for the 'down' direction.
iv. Be sure to use the random library and random.seed(42) in order to
have comparable results between runs.

c. Outputs: At each step, the function should display the current time, the
current position and the remaining floors.

PALISSON Antoine The Elevator Problem 11


Exam 10/01/2024

Starting output that you should obtain with random.seed(42) :

PALISSON Antoine The Elevator Problem 12


Exam 10/01/2024

B) Implementing a capacity limit - 3 pts

1. (1 pt) Modify the existing level_search function to create a new version,


level_search_load, which incorporates a maximum capacity limit for the elevator.
This updated function will process elevator calls at a specific floor (target) and will
only allow a limited number of passengers to enter based on the available capacity
(free_spot) in the elevator.

Example:

2. (2 pts) Update the elevator_up_down function by creating the


elevator_up_down_max_load function which has a maximum load capacity for the
elevator. Use the level_search_load function to ensure that the number of
passengers boarding does not exceed the current available space (free_spot).

PALISSON Antoine The Elevator Problem 13

You might also like