Introduction
Elevator control is a classic discrete-event simulation: the world advances in ticks, each car follows its own rules, and a dispatcher decides who serves each call. Because nothing here depends on wall-clock time or threads, the same input always produces the same, testable log.
Problem statement
Objectives
- Design a deterministic discrete-time simulation
- Implement the SCAN scheduling algorithm
- Write a cost function for dispatching
- Handle capacity limits without deadlocking the simulation
Features
- Two cars with different capacities
- SCAN movement with direction reversal
- Cost-based dispatch and re-dispatch when full
- Event log and performance statistics
Concepts demonstrated
- Simulation
- Scheduling algorithms
- State machines
- Heuristics
- Statistics
System architecture
Elevator holds its floor, direction and a TreeSet of stops; tick() either opens the doors at a stop or moves one floor, reversing when ceiling/floor show no stops ahead. costTo estimates how long the car would take to reach a call, cheap if the call is ahead in the same direction. Building.run loops over ticks: dispatch new requests, re-dispatch deferred ones, then tick every car and process boarding and alighting. Trips that don't fit are marked deferred instead of re-adding a stop, which avoids a car reopening its doors at the same floor forever.
Folder structure
Complete source code
src/Building.java
What each file does
- src/Elevator.java
- One car: SCAN movement, stops, passengers and the dispatch cost estimate.
- src/Building.java
- Requests, dispatching, the tick loop, boarding/alighting, re-dispatch and the report.
- src/Main.java
- Reads and validates requests, runs the simulation and prints the log.
How to run it
Compile
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
# tick from to [people] 0 0 7 2 0 8 1 1 3 9 1 2 5 0 3 2 5 5 3 12 2 4 6 2 2 4 0 4 9 6 1 8 4 7 9 0 2 x 1 2
Testing
- Run with
tests/sample-input.txtand compare withtests/expected-output.txt. - Request #4 (3 people) finds car B full at floor 5 and is re-assigned to car A.
- All seven valid requests are delivered by tick 39; four invalid lines are reported first.
- Running the simulation twice gives identical output because it uses no threads or timers.
Common errors
A car keeps opening its doors at the same floor
When a group can't board, don't add the same stop again; defer the trip and re-dispatch it later.
Cars oscillate between two floors
Only reverse direction when there are no stops ahead in the current direction (SCAN).
Possible improvements
- Add more cars and an express car serving only some floors
- Use LOOK or destination-dispatch algorithms and compare statistics
- Generate random traffic for morning and evening peaks
Additional challenges
- 1Minimise average wait by tuning the cost function.
- 2Add a maintenance mode that takes a car out of service.
- 3Draw the building as ASCII art each tick.
Built and tested it yourself? Mark it to update your dashboard.