I'm Mateusz Bis and I have been asked by AVSytem to create this project. Below you can read about my thought process, observations, goals, results and about how to use the project. Also, please, feel invited to browse all the code in the repository.
- Observations
- My goals
- Project structure
- Algorithm's details
- Run the web app
- Use the web app
- Run the project on the command line
- Import the project in code
- If I had more time...
Before starting to code, I made a list of observations related to elevators in general:
- usually, people have a routine:
- in the morning, people go down to floor zero to i.e. go to work
- during the day the traffic is mixed, some people go up, some go down
- in the evening, people return home and go up to their flats
- the above schema is also true for offices but works in a reverse order
- there are several frustrations when using elevators:
- waiting too long for the elevator to arrive
- going up to fetch another person before descending to the desired floor
- not being able to enter an elevator that is full of people
For the project, I decided to code a solution in Typescript that:
- 🌐 is written in English, not Polish
- 🔥 addresses all of the above observations
- 🚀 Works both in a Node.js and a browser environment
- 💄 has a nice web UI
The project consists of 3 parts:
- The actual algorithm located in ./src/algorithm
- A React + Material UI + Vite web app that allows to use the algo in the browser
- A sample test suite for playing with the algo through the command line
The full algorithm, located in ./src/algorithm folder, features comments explaining each part of the code, and can by use by importing and creating an instance of the ElevatorSystem class from ./src/algorithm/index.ts
The most important components of the algo:
It symbolizes a request of a passenger. There are 2 types of calls:
- entry Call - on which floor a person wants to enter the elevator
- exit Call - on which floor a person wants to exit the elevator
A call also has a direction, either up or down
Moreover, there are 3 possible priorities of calls:
- Priority 0 (served first) - the person is waiting above the elevator that is going up
- Priority 1 - the person wants to go down, and the elevator is going down
- Priority 2 (served last) - the person wants to go up but is below the elevator that is going down
The function that allocates elevators to the waiting passengers. It simulates how long it would take each elevator to get to the waiting passenger and chooses the lift with the shortest time
The way the elevator chooses which calls to serve and in what order is handled with a priority heap queue (defined in utils/PriorityQueue.ts) which keeps the most important requests at the top. The elevator serves calls one by one, but It can also resolve similar requests at the same time (it knows that several people can enter or leave the elevator at the same time).
The elevator can follow 2 different strategies while staying idle and waiting:
- The BEFORE_ADTERNOON strategy - during this part of the day people usually go down in order to leave the building, therefore the elevators, when idle, are spread evenly around different floors. Thanks to this displacement, they can handle plenty of traffic from various floors
- The AFTER_ADTERNOON strategy - during this part of the day, people usually return home and go up the floor to their flats, therefore the elevators, wait for the on floor zero.
Please, feel invited to view the full code in ./src/algorithm
- Install Node.js v20 or newer
- Install dependencies
$ npm i- start the dev server
$ npm run dev- build the app
$ npm run build
Features of the web app
- can handle as many elevators and as many floor as you'd like
- allows changing most of the algorithm's settings
The structure of the web app:
- The canvas
- it is an HTML table that has 3 + n columns, where n is the number of elevators
- the 1st column shows the number of the floor
- the 2nd column shows how many people are waiting for an elevator on each floor
- the 3rd-(3+n-1)st columns show where each elevator currently is
- the last column shows how many people are expected to arrive at each floor
- The sidebar
- allows starting, stopping and resetting the algorithm
- allows generating passengers (also automatically)
- allows changing the algorithm's settings (click the button to actually apply the settings)
Inside ./src/algorithm/index.test.ts there is a sample test where one can easily play with the algo through the command line
- Install Node.js v20 or newer
- Install dependencies
$ npm i- Run index.test.ts
$ npm run test- import the ElevatorSystem class and create and instance
import { ElevatorSystem } from "./src/algorithm";
const building = new ElevatorSystem();- Specify how many floors you want
const numberOfFloors = 20;
building.setFloors(numberOfFloors);- Specify how many elevators you want and create a config for the elevators
import { type ElevatorConfigI, type DisplayDataI } from "./src/algorithm";
const numberOfElevators = 3;
async function displayElevator(d: DisplayDataI): Promise<void> {
console.log(d);
}
const config: ElevatorConfigI = {
loadingTime: 300,
unloadingTime: 300,
velocity: 1,
capacity: 8,
interFloorHeight: 3,
animate: displayElevator, // a function that displays
};
building.setElevators(numberOfElevators, config);- Choose a strategy
import { Strategies } from "./src/algorithm/utils";
building.setStrategy(Strategies.BEFORE_AFTERNOON); // Sets algorithm in the main class- Finally, start generating passengers
building.generatePassenger(10, 0); // from floor 10 to floor 0I would add...
- 🧪 proper unit tests for functions inside algorithm/models/Elevator.ts, algorithm/index.ts, algorithm/utils.ts
- 🧪 proper black box integration tests for the algorithm
- 🚀 code coverage tests
- 💄 a prettier UI
- 📱 better responsive design for mobile devices
- 🧪 E2E tests for the web app
- ✨ support for floors below zero (i.e. underground parking lots)
- ✨ let every elevator have different configurations (capacity, speed, etc.)
- ✨ let every elevator have floor ranges (e.g. one elevator operates between floors -2 and 20 while the other one between 0 and 20)
- ✨ convenient, built'in controls for starting and stoping the algorithm