Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 13: Queues Announcements.

Similar presentations


Presentation on theme: "1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 13: Queues Announcements."— Presentation transcript:

1 1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 13: Queues Announcements

2 2 CMPSCI 187 Applications of Queues l Queues form the basis for many real-world situations. l A well developed theory exists: queuing theory. l Queuing processes F Some number of customers coming to servers to receive service F The throughput of the server may be limited H Customers have to wait in line before they can be served H The service supplied by the server also takes time l Examples: F Lines in banks, supermarkets F Patients in an emergency room F Parts on an assembly line waiting to be assembled F Vehicles waiting for a tollbooth F Printers in a computer system F Information networks l Priority queues: elements are dequeued on the basis of their queue position AND their priority. variable

3 3 CMPSCI 187 Example: An Emergency Room Waiting Room Waiting time. Length of queue. Service time. Arrival time or arrival rate. Treatment Rooms How many treatment rooms/doctors for 'reasonable waits'?

4 4 CMPSCI 187 General Idea l Simulations depend upon random events F Patients arrive randomly F Patients require different 'service' times l Use a big loop to simulate the passing of time F Each time through the loop represents 1 time unit F For each time interval, need to H Update each doctor's activity (busy, will complete current patient service this turn) H Find out how many patients arrive (random) H Determine how much service (e.g. time) each will require (random) H Queue each patient H Distribute patients to doctors that are free by dequeuing them H Update the statistics we want to keep l We'll use minutes (basic time unit) and seconds for the service time required (arbitrary decision)

5 5 CMPSCI 187 Emergency Room Arrival Study 095 13 21 31 40 #patients /minute Percentage of 1 minute intervals

6 6 CMPSCI 187 Patient Arrival l Suppose we do a study and determine: 0151-15 12016-35 22536-60 31061-70 43071-100 #patients /minute Percentage of 1 minute intervals Range 1-100 Random Number =41 Number of patients arriving this minute l This is a general technique l Can do the same thing for 'service' time

7 7 CMPSCI 187 What are reasonable values? l Need to estimate them l Number of patients arriving (in any given minute) F 95% of the time no one arrives F 3% of the time 1 person arrives F 1% of the time 2 people arrive F 1% of the time 3 people arrive F 0% of the time 4 or more people arrive l Service time: F Minimum service time ~ 6 minutes F Maximum service time ~ 24 minutes F Average service time ~ 12 minutes l The beauty of a simulation is you can play with these values very easily. 095 13 21 31 40 #patients /minute Percentage of 1 minute intervals

8 8 CMPSCI 187 class ERSimulation import java.util.Random; public class ERSimulation { private Random rd = new Random();//random number generator private int[] arrivals = {95,3,1,1};//distribution of patient arrival times (mins) private int[] service = {0,0,5,10,15,20,20,15,10,5}; //distribution of service times //in terms of service units. private int[] doctors = {0,0,0};//time remaining for each doctor’s service private int doctorsSize = doctors.length; private int patients, time, i, numOfMinutes, servTime, serviceUnit=180; //patients: number of patients arriving this time interval //time: counter keeping track of time interval //i:loop variable //numOfMinutes: duration of simulation //servTime: service time for this patient, in seconds //serviceUnit: unit of service time for patients - initially 180 secs private double maxWait = 0.0, thereIsLine = 0.0, currWait = 0.0; //maxWait: maximum wait time for any patient in queue //thereIsLine: number of minutes that queue has patients in it //currWait: waiting time at current time in simulation private boolean debug;//true to turn on debug output

9 9 CMPSCI 187 Class Constructor public ERSimulation() { this.ERSimulation(60); } public ERSimulation(int duration) { numOfMinutes = duration; //set simulation length debug=false; //no debug output }

10 10 CMPSCI 187 Determining Number of Arriving Patients this Interval public int randomSelect(int percentArray[]) { //pre: percentArray is distribution of patient arrivals // post: interval is the randomly selected interval of // the percentArray corresponding to the number of // arrivals this time interval. int randInt = Math.abs(rd.nextInt())%100+1; //randInt in the range 1-100 int perc; int interval=0; for (perc=percentArray[0]; perc<randInt;interval++) {perc=perc+percentArray[interval+1];} return interval; }

11 11 CMPSCI 187 Determining the Service Time for This Patient l Set up distribution of service times assuming some standard unit of service: F private int[] service = {0,5,10,20,30,20,10,5} l Use randomSelect to select a random index into this array l Multiply resulting index by the standard unit of service (here, 3 minutes or 180 seconds). F This is the service time for this patient, in seconds.

12 12 CMPSCI 187 The runSimul method public double runSimul() { ArrayQueue simulQ = new ArrayQueue(); //create the queue //each time through loop is one time tick (one minute) for (time=1; time<numOfMinutes; time++) { if(debug)System.out.print("t = " + time); //update each doctor’s activity for this time tick //One time unit means 60 seconds of service for (i=0;i<doctorsSize; i+1) doctors[i]=doctors[i]-60;

13 13 CMPSCI 187 …handle arriving patients //find out how many patients arrive this time interval patients=randomSelect(arrivals); if(debug)System.out.print(" Arrivals="+patients); for (i=0;i<patients;i++) { //find out how much service this patient requires servTime=randomSelect(service)*serviceUnit; //add this patient to the queue simulQ.enqueue(new Integer(servTime)); //increase waiting time - more patients are in the queue currWait=currWait+servTime; }

14 14 CMPSCI 187 …assign patients to doctors //see which doctor are free and if there are patients to assign for(i=0; i<doctorsSize && !simulQ.isEmpty();i++) if (doctors[i] <= 0)// if<=0 doctor is free { //assign new patient servTime=((Integer) simulQ.dequeue()).intValue(); //update doctor's service time doctors[i] = doctors[i]+servTime; //decrease current wait because we took one //patient off the queue currWait = currWait - servTime; }

15 15 CMPSCI 187 …update statistics //now check to see if there is a line if (!simulQ.isEmpty()) { //if yes, update statistics thereIsLine = thereIsLine + 1; if(debug)System.out.print(" wait = " + (long)currWait/60.0); if(debug)System.out.println(); if (maxWait < currWait) maxWait = currWait; } else//if there is no line, print it and continue to next time tick { if(debug)System.out.print(" wait = 0;"); if(debug)System.out.println();} } //end of time interval return maxWait; } //end runSimul

16 16 CMPSCI 187 …the get methods public double getMaxWait() {return maxWait;} public double getThereIsLine() {return thereIsLine;} public int getDoctorsSize() {return doctorsSize;} public int getNumOfMinutes() {return numOfMinutes;} }

17 17 CMPSCI 187 The Driver Class public class ERSimulationTest { public static void main (String args[]) { ERSimulation mySimul = new ERSimulation(100); double maxWait = mySimul.runSimul(); System.out.println("--------------------------SUMMARY STATISTICS-----------------------------"); System.out.println("For "+mySimul.getDoctorsSize() + " doctors, there was a line " + mySimul.getThereIsLine()/mySimul.getNumOfMinutes()*100.0 +"% of the time;"); System.out.println("maximum wait time was " + maxWait()/60.0+" min."); }

18 18 CMPSCI 187 Sample Output l Three doctors l Arrival Distribution: [80,10,5,5] l Service Time Distribution: [0,0,5,10,20,30,20,10,5] F Standard service unit: 180 seconds (3 minutes) F Service times vary from 360 seconds (6 minutes) to1440 seconds (24 minutes) l Vary standard service unit, the distributions, the number of doctors, and the service time unit.

19 19 CMPSCI 187 Sample Output t = 1 Arrivals=0 wait = 0; t = 2 Arrivals=0 wait = 0; t = 3 Arrivals=0 wait = 0; t = 4 Arrivals=1 wait = 0; t = 5 Arrivals=1 wait = 0; t = 6 Arrivals=0 wait = 0; t = 7 Arrivals=0 wait = 0; ………. t = 46 Arrivals=3 wait = 15.0 t = 47 Arrivals=0 wait = 0; t = 48 Arrivals=1 wait = 0; t = 49 Arrivals=2 wait = 33.0 t = 50 Arrivals=0 wait = 33.0 t = 51 Arrivals=2 wait = 54.0 t = 52 Arrivals=0 wait = 54.0 …….. t = 86 Arrivals=0 wait = 48.0 t = 87 Arrivals=0 wait = 42.0 t = 88 Arrivals=3 wait = 96.0 t = 89 Arrivals=1 wait = 114.0 t = 90 Arrivals=1 wait = 102.0 t = 91 Arrivals=0 wait = 102.0 t = 92 Arrivals=0 wait = 102.0 t = 93 Arrivals=0 wait = 81.0 t = 94 Arrivals=0 wait = 81.0 t = 95 Arrivals=0 wait = 81.0 t = 96 Arrivals=0 wait = 81.0 t = 97 Arrivals=0 wait = 81.0 t = 98 Arrivals=1 wait = 102.0 t = 99 Arrivals=0 wait = 102.0 --------------------------SUMMARY STATISTICS----------------------------- For 3 doctors, there was a line 52.0% of the time; maximum wait time was 114.0 min.


Download ppt "1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 13: Queues Announcements."

Similar presentations


Ads by Google