SSQS
unknown
java
a year ago
5.8 kB
9
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 { // System state variables int queueLength = 0; int numberInService = 0; // Entity attributes and sets Queue<Customer> customers = new LinkedList<>(); // Future event list PriorityQueue<Event> futureEventList = new PriorityQueue<>(Comparator.comparingDouble(event -> event.timestamp)); // Activity durations double meanInterArrivalTime = 4.5; // minutes double meanServiceTime = 3.2; // minutes double sigma = 0.6; // standard deviation int totalCustomers = 1000; // Simulation variables double clock = 0.0; double totalBusy = 0.0; int maxQueueLength = 0; double sumResponseTime = 0.0; int numberOfDepartures = 0; int longService = 0; // Summary statistics double rho; double avgR; double pc4; // Random number generator Random random = new Random(); public void initialize() { // Initialize the simulation with initial events and customer arrivals clock = 0.0; totalBusy = 0.0; maxQueueLength = 0; sumResponseTime = 0.0; numberOfDepartures = 0; longService = 0; // Add initial events to the future event list futureEventList.offer(new Event(0, exponential(meanInterArrivalTime))); // Initial customer arrival futureEventList.offer(new Event(1, Double.POSITIVE_INFINITY)); // Initialize with a departure event (set to infinity) } public void runSimulation() { while (numberOfDepartures < totalCustomers) { Event currentEvent = futureEventList.poll(); clock = currentEvent.timestamp; if (currentEvent.eventType == 0) { processArrival(); } else { processDeparture(); } } // Calculate summary statistics rho = totalBusy / clock; avgR = sumResponseTime / totalCustomers; pc4 = (double) longService / totalCustomers; } 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); // Use offer() to enqueue 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.remove(); // Use remove() to dequeue 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.remove(); // Use remove() to dequeue queueLength--; startService(nextCustomer); } // Schedule the next departure event (if applicable) if (numberInService > 0) { double serviceTime = normal(meanServiceTime, sigma); futureEventList.offer(new Event(1, clock + serviceTime)); } numberOfDepartures++; } public void startService(Customer customer) { // Start service for the customer numberInService++; // Schedule the departure event for this customer double serviceTime = normal(meanServiceTime, sigma); futureEventList.offer(new Event(1, clock + serviceTime)); // Update total busy time of the server totalBusy += serviceTime; } public void generateReport() { // Generate simulation report with summary statistics System.out.println("Simulation Report:"); System.out.println("RHO (Server Utilization): " + rho); System.out.println("Average Response Time (AVGR): " + avgR); System.out.println("Proportion of Customers with Service Time >= 4 (PC4): " + pc4); } // 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(); } public static void main(String[] args) { SingleServerQueueSimulation simulation = new SingleServerQueueSimulation(); simulation.initialize(); simulation.runSimulation(); simulation.generateReport(); } }