can you please write in a single html file a simple call center model discrete simulation that shows process steps, the time and a "real time" simulation that can be sped up? Thanks
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Certainly! Below is a single HTML file that implements a simple discrete event simulation of a call center. It shows the process steps, the simulation time, and runs in "real time" with a speed control slider to speed up or slow down the simulation.
Features:
Calls arrive randomly (Poisson process).
Calls go through steps: Waiting → Being Served → Finished.
One agent serves calls one at a time.
Displays current simulation time.
Shows the queue length and current call status.
Speed control slider to speed up or slow down the simulation.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Simple Call Center Discrete Event Simulation</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
#simulation {
margin-top: 20px;
}
.status {
margin: 10px 0;
}
label {
font-weight: bold;
}
#speedControl {
width: 300px;
}
#log {
margin-top: 20px;
max-height: 200px;
overflow-y: auto;
background: #f0f0f0;
padding: 10px;
font-family: monospace;
border: 1px solid #ccc;
}
</style>
</head>
<body>
<h1>Simple Call Center Discrete Event Simulation</h1>
<div>
<label for="speedControl">Simulation Speed (x real time): </label>
<input type="range" id="speedControl" min="0.1" max="10" step="0.1" value="1" />
<span id="speedValue">1.0</span>x
</div>
<div id="simulation">
<div class="status"><label>Simulation Time:</label> <span id="simTime">0.00</span> minutes</div>
<div class="status"><label>Queue Length:</label> <span id="queueLength">0</span></div>
<div class="status"><label>Agent Status:</label> <span id="agentStatus">Idle</span></div>
<div class="status"><label>Current Call ID:</label> <span id="currentCall">None</span></div>
</div>
<div id="log"></div>
<script>
(() => {
// Parameters
const ARRIVAL_RATE = 1 / 2; // calls per minute (mean interarrival time = 2 min)
const SERVICE_TIME_MEAN = 3; // mean service time in minutes
// State
let simTime = 0; // simulation time in minutes
let eventList = [];
let queue = [];
let agentBusy = false;
let currentCall = null;
let callIdCounter = 0;
// Real time control
let speed = 1; // multiplier for simulation speed
let lastRealTime = performance.now();
// DOM elements
const simTimeEl = document.getElementById('simTime');
const queueLengthEl = document.getElementById('queueLength');
const agentStatusEl = document.getElementById('agentStatus');
const currentCallEl = document.getElementById('currentCall');
const logEl = document.getElementById('log');
const speedControl = document.getElementById('speedControl');
const speedValue = document.getElementById('speedValue');
// Utility: exponential random variable
function expRV(rate) {
return -Math.log(1 - Math.random()) / rate;
}
// Event types
const EVENT_ARRIVAL = 'arrival';
const EVENT_SERVICE_END = 'service_end';
// Schedule event
function scheduleEvent(time, type, data = {}) {
eventList.push({ time, type, data });
eventList.sort((a, b) => a.time - b.time);
}
// Log event
function log(msg) {
const timeStr = simTime.toFixed(2).padStart(6, ' ');
logEl.textContent += `[${timeStr}] ${msg}\n`;
logEl.scrollTop = logEl.scrollHeight;
}
// Process arrival event
function processArrival(event) {
const callId = ++callIdCounter;
log(`Call ${callId} arrived.`);
queue.push(callId);
updateQueueDisplay();
// Schedule next arrival
const nextArrivalTime = simTime + expRV(ARRIVAL_RATE);
scheduleEvent(nextArrivalTime, EVENT_ARRIVAL);
// If agent idle, start service immediately
if (!agentBusy) {
startService();
}
}
// Start service for next call in queue
function startService() {
if (queue.length === 0) {
agentBusy = false;
currentCall = null;
updateAgentDisplay();
return;
}
agentBusy = true;
currentCall = queue.shift();
updateQueueDisplay();
updateAgentDisplay();
log(`Call ${currentCall} started service.`);
// Schedule service end
const serviceTime = expRV(1 / SERVICE_TIME_MEAN);
scheduleEvent(simTime + serviceTime, EVENT_SERVICE_END, { callId: currentCall });
}
// Process service end event
function processServiceEnd(event) {
const callId = event.data.callId;
log(`Call ${callId} finished service.`);
currentCall = null;
updateAgentDisplay();
startService();
}
// Update displays
function updateQueueDisplay() {
queueLengthEl.textContent = queue.length;
}
function updateAgentDisplay() {
agentStatusEl.textContent = agentBusy ? 'Busy' : 'Idle';
currentCallEl.textContent = currentCall !== null ? currentCall : 'None';
}
function updateSimTimeDisplay() {
simTimeEl.textContent = simTime.toFixed(2);
}
// Initialize simulation
function init() {
simTime = 0;
eventList = [];
queue = [];
agentBusy = false;
currentCall = null;
callIdCounter = 0;
logEl.textContent = '';
updateQueueDisplay();
updateAgentDisplay();
updateSimTimeDisplay();
// Schedule first arrival
scheduleEvent(expRV(ARRIVAL_RATE), EVENT_ARRIVAL);
}
// Main simulation loop
function stepSimulation(deltaSimTime) {
simTime += deltaSimTime;
// Process all events up to current simTime
while (eventList.length > 0 && eventList[0].time <= simTime) {
const event = eventList.shift();
simTime = event.time; // jump to event time
switch (event.type) {
case EVENT_ARRIVAL:
processArrival(event);
break;
case EVENT_SERVICE_END:
processServiceEnd(event);
break;
}
updateSimTimeDisplay();
}
updateSimTimeDisplay();
}
// Animation loop for real-time simulation
function animate() {
const now = performance.now();
const realDelta = (now - lastRealTime) / 1000; // seconds
lastRealTime = now;
// Convert real time delta to simulation time delta
const simDelta = realDelta * speed;
stepSimulation(simDelta);
requestAnimationFrame(animate);
}
// Speed control event
speedControl.addEventListener('input', () => {
speed = parseFloat(speedControl.value);
speedValue.textContent = speed.toFixed(1);
});
// Start everything
init();
lastRealTime = performance.now();
animate();
})();
</script>
</body>
</html>
How to use:
Open this file in any modern browser.
The simulation will start automatically.
Calls arrive randomly, get queued, and served by a single agent.
The log shows events with timestamps.
Use the slider to speed up or slow down the simulation (0.1x to 10x real time).
If you want to extend this model (multiple agents, call priorities, etc.), just ask!