Dispatch Algorithms
Elevator scheduling is a classic resource allocation problem. Each strategy makes different trade-offs between average latency, fairness, and travel efficiency.
Serves requests in arrival order. Simple, but can starve distant floors.
function dispatch(queue): return queue[0] // serve in arrival order
Always moves to the nearest pending floor. Low average latency, but risks starvation.
function dispatch(elevator, targets): return min(targets, key = |t - elevator.floor|)
Sweeps from bottom to top and back, like a pendulum. Predictable and fair under uniform traffic.
function dispatch(elevator, targets):
if direction == UP:
above = targets >= floor (sorted asc)
return above[0] ?? 0 // reverse at bottom
else:
below = targets <= floor (sorted desc)
return below[0] ?? MAX_FLOORLike SCAN but reverses at the last real request instead of the endpoint. More efficient travel.
function dispatch(elevator, targets):
if direction == UP:
above = targets > floor (sorted asc)
if above: return above[0]
return max(targets) // reverse at last request
else:
below = targets < floor (sorted desc)
if below: return below[0]
return min(targets)Divides the building into vertical zones, one elevator per zone. Excels with concentrated floor traffic.
function dispatch(elevator, targets, zone): inZone = targets in [zone.min .. zone.max] if inZone: return sstf(elevator, inZone) return sstf(elevator, targets) // assist other zones
Architecture Note
The simulation engine (lib/engine/engine.ts) is a plain TypeScript class with no React dependencies. Strategies live in lib/engine/strategies.ts as pure functions. The Zustand store in lib/store/simStore.ts wraps the engine and drives the React UI, snapshotting state after each tick. This separation means the algorithms can be benchmarked independently — no DOM required.