Skip to content

Examples

Complete, runnable examples demonstrating different simulation scenarios and techniques from the discrete-sim package.

All Examples

1. M/M/1 Queue

Classic single-server queue with theoretical validation

  • Exponential inter-arrival and service times
  • Statistics validation against M/M/1 formulas
  • Reproducible results with seeded RNG
  • Complexity: Beginner

2. Bank Tellers

Multi-server bank with transaction types and SLA tracking

  • Multiple servers handling different transaction types (quick vs. complex)
  • SLA compliance tracking (10-minute target)
  • Performance assessment and staffing recommendations
  • Complexity: Intermediate

3. Bank Express Lane

Priority queue system with express service

  • Priority-based queuing (express=0, regular=10)
  • Non-preemptive resource allocation
  • Multiple independent arrival processes
  • Wait time comparison by priority
  • Complexity: Intermediate

4. Restaurant

Restaurant with tables and servers

  • Variable group sizes (1-6 people)
  • Multiple resource types (tables + servers)
  • Multi-stage service (order, eat, payment)
  • Customer satisfaction metrics
  • Complexity: Intermediate

5. Warehouse Operations

Multi-stage logistics with bottleneck analysis

  • Sequential workflow (dock → forklift → inspection → storage)
  • Multiple resource types with dependencies
  • Bottleneck identification
  • Resource utilization comparison
  • Complexity: Advanced

6. Hospital Emergency Room

Emergency department with preemptive triage

  • Preemptive resource allocation (critical patients interrupt lower-priority)
  • Process interruption and recovery handling
  • Multi-level priority system (critical/urgent/standard)
  • PreemptionError exception handling
  • Complexity: Advanced

Quick Example

Here's a simple queue system to get started:

typescript
import { Simulation, Resource, Random, timeout } from 'discrete-sim';

// Setup
const sim = new Simulation();
const rng = new Random(42);
const server = new Resource(sim, 1, { name: 'Server' });

// Customer process
function* customer(id: number) {
  console.log(`[${sim.now}] Customer ${id} arrived`);

  yield server.request();
  console.log(`[${sim.now}] Customer ${id} being served`);

  yield* timeout(rng.exponential(3));

  server.release();
  console.log(`[${sim.now}] Customer ${id} departed`);
}

// Generate arrivals
function* arrivalProcess() {
  for (let i = 0; i < 10; i++) {
    sim.process(() => customer(i));
    yield* timeout(rng.exponential(2));
  }
}

// Run
sim.process(arrivalProcess);
sim.run();

// Results
console.log(`Utilization: ${sim.statistics.getAverage('Server:utilization')}`);

By Feature

Priority Queuing

Multiple Resources

  • Parallel: Restaurant - Tables + servers used simultaneously
  • Sequential: Warehouse - Docks → forklifts → inspectors in sequence

Statistics & Analysis

  • Theoretical Validation: M/M/1 Queue - Compare to queueing theory formulas
  • SLA Tracking: Bank Tellers - Measure compliance with service targets
  • Bottleneck Detection: Warehouse - Identify resource constraints

Process Patterns


By Use Case

Financial Services

Operations

Healthcare

Learning


Running Examples

All examples are in the discrete-sim repository:

bash
# Clone the repository
git clone https://github.com/anesask/discrete-sim
cd discrete-sim

# Install dependencies
npm install

# Run any example
npx tsx examples/mm1-queue/index.ts
npx tsx examples/bank-tellers/index.ts
npx tsx examples/bank-express-lane/index.ts
npx tsx examples/restaurant/index.ts
npx tsx examples/warehouse/index.ts
npx tsx examples/hospital-emergency/index.ts

Or import in your own code:

typescript
import { runSimulation } from 'discrete-sim/examples/mm1-queue';

const results = runSimulation();
console.log(results);

Key Concepts

Each example demonstrates different aspects of discrete event simulation:

ConceptExamples
Basic QueuingM/M/1 Queue
Multi-server SystemsBank Tellers, Restaurant
Priority QueuesBank Express Lane, Hospital ER
Resource ContentionWarehouse, Restaurant
Process StagesWarehouse, Restaurant
Statistical ValidationM/M/1 Queue
Performance MetricsAll examples
Capacity PlanningBank Tellers, Hospital ER

Next Steps

Released under the MIT License.