+ All Categories
Home > Documents > 1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures...

1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures...

Date post: 13-Dec-2015
Category:
Upload: buddy-rich
View: 223 times
Download: 2 times
Share this document with a friend
Popular Tags:
19
1 CMPSCI 187 CMPSCI 187 Computer Science 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 13: Queues Announcements
Transcript

1

CMPSCI 187CMPSCI 187

Computer Science 187Computer Science 187Computer Science 187Computer Science 187

Introduction to Introduction to Programming with Data Structures

Lecture 13: QueuesLecture 13: Queues

AnnouncementsAnnouncements

2

CMPSCI 187CMPSCI 187

Applications of QueuesApplications of Queues Queues form the basis for many real-world situations. A well developed theory exists: queuing theory. Queuing processes

Some number of customers coming to servers to receive service The throughput of the server may be limited

Customers have to wait in line before they can be served The service supplied by the server also takes time

Examples: Lines in banks, supermarkets Patients in an emergency room Parts on an assembly line waiting to be assembled Vehicles waiting for a tollbooth Printers in a computer system Information networks

Priority queues: elements are dequeued on the basis of their queue position AND their priority.

variable

variable

variable

3

CMPSCI 187CMPSCI 187

Example: An Emergency RoomExample: 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

CMPSCI 187CMPSCI 187

General IdeaGeneral Idea

Simulations depend upon random events Patients arrive randomly Patients require different 'service' times

Use a big loop to simulate the passing of time Each time through the loop represents 1 time unit For each time interval, need to

Update each doctor's activity (busy, will complete current patient service this turn)

Find out how many patients arrive (random)Determine how much service (e.g. time) each will require (random)Queue each patientDistribute patients to doctors that are free by dequeuing themUpdate the statistics we want to keep

We'll use minutes (basic time unit) and seconds for the service time required (arbitrary decision)

5

CMPSCI 187CMPSCI 187

Emergency Room Arrival StudyEmergency Room Arrival Study

0 951 32 13 14 0

#patients/minute

Percentage of 1 minute intervals

6

CMPSCI 187CMPSCI 187

Patient ArrivalPatient Arrival

Suppose we do a study and determine:

0 15 1-151 20 16-352 25 36-603 10 61-704 30 71-100

#patients/minute

Percentage of 1 minute intervals

Range 1-100 Random Number =41

Number of patients arriving this minute

This is a general technique Can do the same thing for 'service' time

7

CMPSCI 187CMPSCI 187

What are reasonable values?What are reasonable values?

Need to estimate them Number of patients arriving (in any given minute)

95% of the time no one arrives 3% of the time 1 person arrives 1% of the time 2 people arrive 1% of the time 3 people arrive 0% of the time 4 or more people arrive

Service time: Minimum service time ~ 6 minutes Maximum service time ~ 24 minutes Average service time ~ 12 minutes

The beauty of a simulation is you can play with these values very easily.

0 951 32 13 14 0

#patients/minute

Percentage of 1 minute intervals

8

CMPSCI 187CMPSCI 187

class ERSimulationclass ERSimulationimport 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 serviceprivate 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

CMPSCI 187CMPSCI 187

Class ConstructorClass Constructor

public ERSimulation(){

this.ERSimulation(60);}

public ERSimulation(int duration){

numOfMinutes = duration; //set simulation lengthdebug=false; //no debug output

}

10

CMPSCI 187CMPSCI 187 Determining Number of Arriving Patients this Interval

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

CMPSCI 187CMPSCI 187 Determining the Service Time for This Patient

Determining the Service Time for This Patient

Set up distribution of service times assuming some standard unit of service: private int[] service = {0,5,10,20,30,20,10,5}

Use randomSelect to select a random index into this array

Multiply resulting index by the standard unit of service (here, 3 minutes or 180 seconds). This is the service time for this patient, in seconds.

12

CMPSCI 187CMPSCI 187

The runSimul methodThe 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

CMPSCI 187CMPSCI 187

…handle arriving patients…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

CMPSCI 187CMPSCI 187

…assign patients to doctors…assign patients to doctors

//see which doctor are free and if there are patients to assignfor(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

CMPSCI 187CMPSCI 187

…update statistics…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

CMPSCI 187CMPSCI 187

…the get methods…the get methods

public double getMaxWait() {return maxWait;} public double getThereIsLine() {return thereIsLine;} public int getDoctorsSize() {return doctorsSize;} public int getNumOfMinutes() {return numOfMinutes;}}

17

CMPSCI 187CMPSCI 187

The Driver ClassThe Driver Classpublic 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

CMPSCI 187CMPSCI 187

Sample OutputSample Output

Three doctors Arrival Distribution: [80,10,5,5] Service Time Distribution: [0,0,5,10,20,30,20,10,5]

Standard service unit: 180 seconds (3 minutes) Service times vary from 360 seconds (6 minutes)

to1440 seconds (24 minutes) Vary standard service unit, the distributions, the

number of doctors, and the service time unit.

19

CMPSCI 187CMPSCI 187

Sample OutputSample Outputt = 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.0t = 47 Arrivals=0 wait = 0;t = 48 Arrivals=1 wait = 0;t = 49 Arrivals=2 wait = 33.0t = 50 Arrivals=0 wait = 33.0t = 51 Arrivals=2 wait = 54.0t = 52 Arrivals=0 wait = 54.0……..

t = 86 Arrivals=0 wait = 48.0t = 87 Arrivals=0 wait = 42.0t = 88 Arrivals=3 wait = 96.0t = 89 Arrivals=1 wait = 114.0t = 90 Arrivals=1 wait = 102.0t = 91 Arrivals=0 wait = 102.0t = 92 Arrivals=0 wait = 102.0t = 93 Arrivals=0 wait = 81.0t = 94 Arrivals=0 wait = 81.0t = 95 Arrivals=0 wait = 81.0t = 96 Arrivals=0 wait = 81.0t = 97 Arrivals=0 wait = 81.0t = 98 Arrivals=1 wait = 102.0t = 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.


Recommended