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

Slides:



Advertisements
Similar presentations
Structure of a Waiting Line System
Advertisements

Lab Assignment 1 COP 4600: Operating Systems Principles Dr. Sumi Helal Professor Computer & Information Science & Engineering Department University of.
Fundamentals of Python: From First Programs Through Data Structures
Event-drive SimulationCS-2303, C-Term Project #3 – Event-driven Simulation CS-2303 System Programming Concepts (Slides include materials from The.
Comp 245 Data Structures Queues. Introduction to the Queue ADT It is a FIFO (first-in, first-out) structure Access to the Queue can take place at two.
1 6.3 Binary Heap - Other Heap Operations There is no way to find any particular key without a linear scan through the entire heap. However, if we know.
Simulation A Queuing Simulation. Example The arrival pattern to a bank is not Poisson There are three clerks with different service rates A customer must.
Components and Organization of Discrete-event Simulation Model
1 Queuing Theory 2 Queuing theory is the study of waiting in lines or queues. Server Pool of potential customers Rear of queue Front of queue Line (or.
Variance Reduction Techniques
Data Communication and Networks Lecture 13 Performance December 9, 2004 Joseph Conron Computer Science Department New York University
Simulating Single server queuing models. Consider the following sequence of activities that each customer undergoes: 1.Customer arrives 2.Customer waits.
Project 2: ATM’s & Queues
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Queuing Theory. Queuing theory is the study of waiting in lines or queues. Server Pool of potential customers Rear of queue Front of queue Line (or queue)
Lecture 4 Mathematical and Statistical Models in Simulation.
Lab 01 Fundamentals SE 405 Discrete Event Simulation
Cmpt-225 Simulation. Application: Simulation Simulation  A technique for modeling the behavior of both natural and human-made systems  Goal Generate.
Graduate Program in Engineering and Technology Management
Simulation II IE 2030 Lecture 18. Outline: Simulation II Advanced simulation demo Review of concepts from Simulation I How to perform a simulation –concepts:
Introduction to Discrete Event Simulation Customer population Service system Served customers Waiting line Priority rule Service facilities Figure C.1.
TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues.
ITI Introduction to Computing II Lab-8 Dewan Tanvir Ahmed University of Ottawa.
Project 2: ATM’s & Queues. ATM’s & Queues  Certain business situations require customers to wait in line for a service Examples:  Waiting to use an.
1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other.
Array Processing - 2. Objectives Demonstrate a swap. Demonstrate a linear search of an unsorted array Demonstrate how to search an array for a high value.
Structure of a Waiting Line System Queuing theory is the study of waiting lines Four characteristics of a queuing system: –The manner in which customers.
More arrays Primitive vs. reference parameters. Arrays as parameters to functions.
1 QUEUES. 2 Definition A queue is a linear list in which data can only be inserted at one end, called the rear, and deleted from the other end, called.
Data Structures Using C++ 2E Chapter 8 Queues. Data Structures Using C++ 2E2 Objectives Learn about queues Examine various queue operations Learn how.
Waiting Lines and Queuing Models. Queuing Theory  The study of the behavior of waiting lines Importance to business There is a tradeoff between faster.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008.
Computer Science 112 Fundamentals of Programming II Modeling and Simulation.
CSC 205 Programming II Lecture 22 Carwash Simulation.
Data Structures Using Java1 Chapter 7 Queues. Data Structures Using Java2 Chapter Objectives Learn about queues Examine various queue operations Learn.
Chapter Objectives  Learn how to represent a waiting line (queue)  Become proficient using the methods in the Queue  Understand how to implement the.
Chapter 10 Verification and Validation of Simulation Models
Georgia Institute of Technology More on Creating Classes part 2 Barb Ericson Georgia Institute of Technology Oct 2005.
1 Simulation Implementation Using high-level languages.
Problem 1 Bank.  Manage customers’ bank account using the following operations: Create a new account given a customer’s name and initial account. Deposit.
Structure of a Waiting Line System Queuing theory is the study of waiting lines Four characteristics of a queuing system: –The manner in which customers.
Dr. Anis Koubâa CS433 Modeling and Simulation
(C) J. M. Garrido1 Objects in a Simulation Model There are several objects in a simulation model The activate objects are instances of the classes that.
1 Probability and Statistics Confidence Intervals.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
© 2015 McGraw-Hill Education. All rights reserved. Chapter 17 Queueing Theory.
MODELING AND SIMULATION CS 313 Simulation Examples 1.
1 BIS 3106: Business Process Management (BPM) Lecture Nine: Quantitative Process Analysis (2) Makerere University School of Computing and Informatics Technology.
Chapter 1 What is Simulation?. Fall 2001 IMSE643 Industrial Simulation What’s Simulation? Simulation – A broad collection of methods and applications.
Modeling the Optimization of a Toll Booth Plaza By Liam Connell, Erin Hoover and Zach Schutzman Figure 1. Plot of k values and outputs from Erlang’s Formula.
Simulation of Inventory Systems
Data Structures Using C++ 2E
Unit 4 Stacks And Queues King Fahd University of Petroleum & Minerals
Queues.
What’s cheating and what is not?
2.7: Simulation.
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Modeling and Simulation CS 313
COMPUTER 2430 Object Oriented Programming and Data Structures I
Chapter 13 Queues and Priority Queues
Chapter 10 Verification and Validation of Simulation Models
Simulation An Application of Queue ADT
COMP60611 Fundamentals of Parallel and Distributed Systems
Native simulation of different scheduling policies
Queuing Analysis Two analytical techniques can be employed to study queuing processes: Shock wave analysis Demand-capacity process is deterministic Suited.
COMP60621 Designing for Parallelism
MAT 4830 Mathematical Modeling
CE 221 Data Structures and Algorithms
CSCS-200 Data Structure and Algorithms
Presentation transcript:

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

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 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 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 CMPSCI 187 Emergency Room Arrival Study #patients /minute Percentage of 1 minute intervals

6 CMPSCI 187 Patient Arrival l Suppose we do a study and determine: #patients /minute Percentage of 1 minute intervals Range 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 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 #patients /minute Percentage of 1 minute intervals

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 CMPSCI 187 Class Constructor public ERSimulation() { this.ERSimulation(60); } public ERSimulation(int duration) { numOfMinutes = duration; //set simulation length debug=false; //no debug output }

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 int perc; int interval=0; for (perc=percentArray[0]; perc<randInt;interval++) {perc=perc+percentArray[interval+1];} return interval; }

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 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 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 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 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 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 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()* "% of the time;"); System.out.println("maximum wait time was " + maxWait()/60.0+" min."); }

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 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 = t = 90 Arrivals=1 wait = t = 91 Arrivals=0 wait = t = 92 Arrivals=0 wait = 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 = t = 99 Arrivals=0 wait = SUMMARY STATISTICS For 3 doctors, there was a line 52.0% of the time; maximum wait time was min.