Single-Server Queueing System Simulation

mail@pastecode.io avatarunknown
java
8 days ago
2.8 kB
3
Indexable
Never
import java.util.*;

class Event {
    int eventType; // 0 for arrival, 1 for departure
    double timestamp;

    public Event(int eventType, double timestamp) {
        this.eventType = eventType;
        this.timestamp = timestamp;
    }
}

class Customer {
    double arrivalTime;
    double serviceTime;

    public Customer(double arrivalTime, double serviceTime) {
        this.arrivalTime = arrivalTime;
        this.serviceTime = serviceTime;
    }
}

public class SingleServerQueueSimulation {
    // ... (Previous code for variables, initialization, and random number generator)

    public void processArrival() {
        // Create a new customer with exponential inter-arrival time
        double interArrivalTime = exponential(meanInterArrivalTime);
        Customer customer = new Customer(clock + interArrivalTime, normal(meanServiceTime, sigma));
        
        // Update clock
        clock = customer.arrivalTime;

        // If the server is idle, start service immediately
        if (numberInService == 0) {
            startService(customer);
        } else {
            // Server is busy, customer joins the queue
            queueLength++;
            customers.offer(customer);
            maxQueueLength = Math.max(maxQueueLength, queueLength);
        }

        // Schedule the next arrival event
        futureEventList.offer(new Event(0, customer.arrivalTime));
    }

    public void processDeparture() {
        // Update server and queue
        numberInService--;

        // Calculate customer's response time and update statistics
        Customer departingCustomer = customers.poll();
        double responseTime = clock - departingCustomer.arrivalTime;
        sumResponseTime += responseTime;

        if (responseTime >= 4.0) {
            longService++;
        }

        // If there are customers in the queue, start service for the next customer
        if (queueLength > 0) {
            Customer nextCustomer = customers.poll();
            queueLength--;
            startService(nextCustomer);
        }
    }

    // ... (Previous code for initialization, runSimulation, and generateReport)

    // Exponential random number generator
    private double exponential(double mean) {
        return -mean * Math.log(random.nextDouble());
    }

    // Normal random number generator
    private double normal(double mean, double sigma) {
        return mean + sigma * random.nextGaussian();
    }

    // Other methods and variables as needed

    public static void main(String[] args) {
        SingleServerQueueSimulation simulation = new SingleServerQueueSimulation();
        simulation.initialize();
        simulation.runSimulation();
        simulation.generateReport();
    }
}