• Tidak ada hasil yang ditemukan

Simulating a Queue

Dalam dokumen Pelajari tentang Advanced topics in Java (Halaman 180-183)

Random Numbers, Games, and Simulation

6.9 Simulating a Queue

Consider the situation at a bank or supermarket checkout, where customers arrive and must queue for service. Suppose there is one queue but several counters. If a counter is free, the person at the head of the queue goes to it. If all counters are busy, the customers must wait; the person at the head of the queue goes to the first available counter.

To illustrate, suppose there are two counters; we denote them by C1 and C2. To perform the simulation, we need to know the frequency with which customers arrive and the time it takes to serve a customer. Based on observation and experience, we may be able to say the following:

The time between customer arrivals varies randomly from one to five minutes.

•฀

The time to serve a customer varies randomly from three to ten minutes.

•฀

For the simulation to be meaningful, this data must be close to what occurs in practice. As a general rule, a simulation is only as good as the data on which it is based.

Suppose we begin at 9 a.m. We can simulate the arrival of the first ten customers by generating ten random numbers from 1 to 5, like this:

3 1 2 4 2 5 1 3 2 4

This means the first customer arrives at 9:03, the second at 9:04, the third at 9:06, the fourth at 9:10, and so on.

We can simulate the service time for these customers by generating ten random numbers from 3 to 10, like this:

5 8 7 6 9 4 7 4 9 6

This means the first customer spends five minutes at the teller, the second spends eight minutes, the third spends seven minutes, and so on.

Table 6-1 shows what happens to these ten customers.

Table 6-1. Tracking Ten Customers

Customer Arrives Start

Service

Counter Service Time

Departs Wait Time

1 9:03 9:03 C1 5 9:08 0

2 9:04 9:04 C2 8 9:12 0

3 9:06 9:08 C1 7 9:15 2

4 9:10 9:12 C2 6 9:18 2

5 9:12 9:15 C1 9 9:24 3

6 9:17 9:18 C2 4 9:22 1

7 9:18 9:22 C2 7 9:29 4

8 9:21 9:24 C1 4 9:28 3

9 9:23 9:28 C1 9 9:37 5

10 9:27 9:29 C2 6 9:35 2

The first customer arrives at 9:03 and goes straight to C1. His service time is five minutes,

•฀

so he will leave C1 at 9:08.

The second customer arrives at 9:04 and goes straight to C2. His service time is 8 minutes,

•฀

so he will leave C2 at 9:12.

The third customer arrives at 9:06. At this time, both C1 and C2 are busy, so he must wait.

•฀

C1 will be the first to become free at 9:08. This customer will begin service at 9:08. His service time is seven minutes, so he will leave C1 at 9:15. This customer had to wait in the queue for two minutes.

The fourth customer arrives at 9:10. At this time, both C1 and C2 are busy, so he must wait.

•฀

C2 will be the first to become free at 9:12. This customer will begin service at 9:12. His service time is six minutes, so he will leave C2 at 9:18. This customer had to wait in the queue for two minutes.

And so on. Work through the rest of the table to make sure you understand how those values are obtained.

Also observe that once the tellers started serving, they had no idle time. As soon as one customer left, another was waiting to be served.

6.9.1 Programming the Simulation

We now show how to write a program to produce Table 6-1. First we observe that it is no more difficult to write the program for several counters than it is for two. Hence, we will assume that there are n (n < 10) counters. For this particular example, we will set n to 2.

We will use an array depart[10] such that depart[c] will hold the time at which counter c will next become free.

We will not use depart[0]. If we need to handle more than nine counters, we just need to increase the size of depart.

Suppose the customer at the head of the queue arrives at arriveTime. He will go to the first free counter. Counter c is free if the customer arrives after the last one has left counter c, that is, if arriveTime is greater than or equal to depart[c]. If no counter is free, he must wait. He will go to the counter that will become free first, that is, the one with the lowest value in the array depart; suppose this is depart[m]. He will begin service at a time that is the later of arriveTime and depart[m].

The program begins by asking for the number of counters and the number of customers to be simulated.

The simulation starts from time 0, and all times are relative to this. The details are shown in Program P6.6.

Program P6.6

import java.util.*;

public class SimulateQueue {

public static void main(String[] args) { Scanner in = new Scanner(System.in);

System.out.printf("\nHow many counters? ");

int numCounters = in.nextInt();

System.out.printf("\nHow many customers? ");

int numCustomers = in.nextInt();

doSimulation(numCounters, numCustomers);

} //end main

public static void doSimulation(int counters, int customers) { int m, arriveTime, startServe, serveTime, waitTime;

int[] depart = new int[counters + 1];

for (int h = 1; h <= counters; h++) depart[h] = 0;

System.out.printf("\n Start Service Wait\n");

System.out.printf("Customer Arrives Service Counter Time Departs Time\n\n");

arriveTime = 0;

for (int h = 1; h <= customers; h++) { arriveTime += random(1, 5);

m = smallest(depart, 1, counters);

startServe = Math.max(arriveTime, depart[m]);

serveTime = random(3, 10);

depart[m] = startServe + serveTime;

waitTime = startServe - arriveTime;

System.out.printf("%5d %8d %7d %6d %7d %8d %5d\n",

h, arriveTime, startServe, m, serveTime, depart[m], waitTime);

} //end for h } //end doSimulation

public static int smallest(int list[], int lo, int hi) { //returns the subscript of the smallest value from list[lo..hi]

int h, k = lo;

for (h = lo + 1; h <= hi; h++) if (list[h] < list[k]) k = h;

return k;

}

public static int random(int m, int n) {

//returns a random integer from m to n, inclusive return (int) (Math.random() * (n - m + 1)) + m;

} //end random

} //end class SimulateQueue A sample run of Program P6.6 is shown here:

How many counters? 2 How many customers? 10

Start Service Wait Customer Arrives Service Counter Time Departs Time 1 3 3 1 8 11 0 2 7 7 2 9 16 0 3 10 11 1 9 20 1 4 11 16 2 4 20 5 5 14 20 1 5 25 6 6 19 20 2 9 29 1 7 23 25 1 7 32 2 8 26 29 2 8 37 3 9 29 32 1 7 39 3 10 33 37 2 6 43 4

As you can see, the waiting time is reasonably short. However, if you run the simulation with 25 customers, you will see that the waiting time increases appreciably. What if we added another counter? With simulation, it’s easy to test the effect of this without actually having to buy another machine or hire another employee.

In this case, all we have to do is enter 3 and 25 for the number of counters and customers, respectively. When we do, we will find that there is very little waiting time. We urge you to experiment with different data—counters, customers, arrival times, and service times—to see what happens.

Dalam dokumen Pelajari tentang Advanced topics in Java (Halaman 180-183)