001package algs13; 002import stdlib.*; 003 004public class XMM1Queue { 005 public static void main(String[] args) { 006 int SIZE = 60; 007 args = new String[] { ".2", ".333" }; SIZE = 60; 008 //args = new String[] { ".2", ".25" }; SIZE = 120; 009 //args = new String[] { ".2", ".21" }; SIZE = 240; 010 011 double lambda = Double.parseDouble(args[0]); // arrival rate 012 double mu = Double.parseDouble(args[1]); // service rate 013 014 Queue<Double> q = new Queue<>(); // arrival times of customers 015 double nextArrival = StdRandom.exp(lambda); // time of next arrival 016 double nextDeparture = Double.POSITIVE_INFINITY; // time of next departure 017 018 // histogram object 019 XHistogram hist = new XHistogram(SIZE); 020 021 // simulate an M/M/1 queue 022 while (true) { 023 024 // it's an arrival 025 if (nextArrival <= nextDeparture) { 026 if (q.isEmpty()) nextDeparture = nextArrival + StdRandom.exp(mu); 027 q.enqueue(nextArrival); 028 nextArrival += StdRandom.exp(lambda); 029 } 030 031 // it's a departure 032 else { 033 double wait = nextDeparture - q.dequeue(); 034 StdOut.format("Wait = %6.2f, queue size = %d\n", wait, q.size()); 035 hist.addDataPoint(Math.min(SIZE-1, (int) (Math.round(wait)))); 036 StdDraw.clear (); 037 hist.draw (); 038 StdDraw.show (1); 039 if (q.isEmpty()) nextDeparture = Double.POSITIVE_INFINITY; 040 else nextDeparture += StdRandom.exp(mu); 041 042 } 043 } 044 045 } 046 047} 048