Question

In: Computer Science

Queues are often used to represent lists of things that are being processed according to the...

Queues are often used to represent lists of things that are being processed according to the order in which they arrived -- i.e. "first come, first served".  

Assignment

Write a program that simulates the minute-by-minute operation of a checkout line, such as one you might find in a retail store. Use the following parameters:

1) Customers arrive at the checkout line and stand in line until the cashier is free.

2) When they reach the front of the line, they occupy the cashier for some period of time (referred to as ServiceTime) measured in minutes.

3) After the cashier is free, the next customer is served immediately.

4) Customers arrive at the checkout line at ArrivalRate per minute. Use the function included below (randomChance()) to return the number of customers arriving in a given minute, determined randomly.

5) The line can only hold so many people, MaxLineSize, until new arriving customers get frustrated and leave the store without purchasing anything.

6) ServiceTime is determined at the point the customer reaches the cashier, and should be taken from the random interval MinServiceTime and MaxServiceTime -- use the function randomInt() provided.

7) The overall time of the simulation is SimulationTime, measured in minutes.

The program should take 6 inputs (to be read from a text file named simulation.txt, as numbers only, one per line, in this order):

- SimulationTime - total number of minutes to run the simulation (whole number).

- ArrivalRate - per-minute arrival rate of customers (a floating point number greater than 0 and less than 1). This number is the "percent chance" that a customer will arrive in a given minute. For example, if it is 0.4, there is a 40% chance a customer will arrive in that minute.

- MinServiceTime - the minimum expected service time, in minutes (whole number).

- MaxServiceTime - the maximum expected service time, in minutes (whole number).

- MaxLineSize - the maximum size of the line. If a new customer arrives and the line has this many customers waiting, the new customer leaves the store unserviced.

- IrateCustomerThreshold - nobody enjoys standing in line, right? This represents the number of minutes after which a customer becomes angry waiting in line (a whole number, at least 1). These customers do not leave, they only need to be counted.

At the end of each simulation, the program should output:

- The total number of customers serviced

- The total number of customers who found the line too long and left the store.

- The average time per customer spent in line

- The average number of customers in line

- The number of irate customers (those that had to wait at least IrateCustomerThreshold minutes)

You are free to use any STL templates as needed (queue or vector, for example).

An example input file is posted in this week's Module here.

Example Run

The output should look similar to this:

Simulation Results
------------------
Overall simulation time:     2000
Arrival rate:                 0.1
Minimum service time:           5
Maximum service time:          15
Maximum line size:              5

Customers serviced:           183
Customers leaving:             25
Average time spent in line: 33.86
Average line length:         3.15
Irate customers                10

What to Submit

Submit your .cpp file (source code for your solution) in Canvas.

Random Functions

Use these provided functions for generating your random chance (for a customer arrival) and interval for service times.

bool randomChance(double prob) { 
    double rv = rand() / (double(RAND_MAX) + 1); 
    return (rv < prob); 
} 

int randomInt(int min, int max) { 
    return (rand() % (max - min) + min); 
} 

Before calling these functions be sure to seed the random number generator (you can do this in main()):

srand(time(0));

Outline:

#include <iostream>
#include <queue>

using namespace std;

int main()
{
queue<int> myQ;

// For each test case, do a loop like this:
for (int i = 0; i < SIMULATION_TIME; i++) {
if (randomChance(ARRIVAL_RATE)) {

// Did a customer arrive in this minute?
// If so, put them on the queue
// - Is the queue full? If so, they left.
// If not, continue with rest of the loop

// Checking on the checkout line:
// - Is the current person done?
// - If so, take the next person
// - Get
// - Record how long they waited
// - Generate a random for how long
// they will take at the checkout

// Record various metrics:
// - Average wait time
// - Is the customer irate?
// - Average queue length
// - Increment number of customers serviced
// - How many left because the line was full?

}

// Print a report here

return 0;
}

Test input:

2000
0.10
5
15
5
20
10000
0.05
5
15
5
18
2000
0.20
5
20
10
10
2000
0.20
1
5
10
25
20000
0.50
1
2
50
10

Solutions

Expert Solution

The input file contains input for multiple simulations, therefore, input is read till the end of file. Rest explanation is in the code comments. Read file using fstream library.

Please note that input is read from simulation.txt. Also, as random numbers are involved, the output would be different for every code run.

Program:

#include <iostream>
#include <queue>
#include <stdlib.h>   // to use random
#include <time.h>   // for random seed
#include <fstream>   // read file

using namespace std;

bool randomChance(double prob) {
double rv = rand() / (double(RAND_MAX) + 1);
return (rv < prob);
}

int randomInt(int min, int max) {
return (rand() % (max - min) + min);
}

int main()
{
   // input file
   ifstream fin;
   fin.open("simulation.txt");

   // get results for all test cases
int testCaseCount = 0;
while(!fin.eof()) {
  
testCaseCount ++;
  
   // queue of customers arriving
   queue<int> myQ;
  
   // inputs
   int SimulationTime, MinServiceTime, MaxServiceTime, MaxLineSize, IrateCustomerThreshold;
   double ArrivalRate;
  
   // variables required for simulation.
   int currentPerson = 0, startTime, waitTime;
   bool isPersonServed = false;
  
   // output variables
   int customersServiced = 0, customersLeft = 0, irateCustomers = 0;
   double avgWaitTime = 0.0, avgLineLength = 0.0;
  
   // take input
   fin>>SimulationTime;
   fin>>ArrivalRate;
   fin>>MinServiceTime;
   fin>>MaxServiceTime;
   fin>>MaxLineSize;
   fin>>IrateCustomerThreshold;
  
   srand(time(0));
  
   // For each test case, do a loop like this:
   for (int i = 0; i < SimulationTime; i++) {
       // Did a customer arrive in this minute?
       // If so, put them on the queue
       // - Is the queue full? If so, they left.
       // If not, continue with rest of the loop
       if (randomChance(ArrivalRate)) {
          
           if(myQ.size() == MaxLineSize) {
               customersLeft ++;
           }
           else {
               // store at which minute customer arrives
               myQ.push(i);
           }
       }
      
       // Checking on the checkout line:
       // - Is the current person done?
       // at least one person should be there
       if(currentPerson == 0 && myQ.size()>=1) {
          
           if(isPersonServed) {
           // remove the current customer
           myQ.pop();
          
           // Increment number of customers serviced
           customersServiced++;
          
           // - Record how long they waited
           // and add it to avg wait time
           waitTime = i - startTime;
           avgWaitTime += waitTime;
          
           // irate customer count
           if(waitTime>=IrateCustomerThreshold)
               irateCustomers++;
           }
      
           // queue is not empty
           if (!myQ.empty()) {
              
               // - If so, take the next person
               startTime = myQ.front();
              
               // - Generate a random for how long
               // they will take at the checkout
               currentPerson = randomInt(MinServiceTime, MaxServiceTime);
               isPersonServed = true;
              
           } else {
           // no person to serve
           isPersonServed = false;
           }
       } else if(currentPerson>0) {
           currentPerson --;   // time left to serve the current person
       }
      
       // Average queue length
       avgLineLength += myQ.size();
   }
  
   avgWaitTime = 1.0 * avgWaitTime / customersServiced;
   avgLineLength = 1.0 * avgLineLength / SimulationTime;
  
   // report
   cout<<"Simulation Results for test case #"<<testCaseCount<<endl;
cout<<"------------------"<<endl;
cout<<"Overall simulation time:\t"<<SimulationTime<<endl;
cout<<"Arrival rate:\t"<<ArrivalRate<<endl;
cout<<"Minimum service time:\t"<<MinServiceTime<<endl;
cout<<"Maximum service time:\t"<<MaxServiceTime<<endl;
cout<<"Maximum line size:\t"<<MaxLineSize<<endl;
cout<<endl;
   cout<<"Customers serviced:\t\t"<< customersServiced<<endl;
   cout<<"Customers leaving:\t\t"<<customersLeft<<endl;
cout<<"Average time spent in line:\t"<<avgWaitTime<<endl;
cout<<"Average line length:\t\t"<<avgLineLength<<endl;
   cout<<"Irate customers:\t\t"<<irateCustomers<<endl;
cout<<"------------------"<<endl<<endl;
  
}
  
// close input file
fin.close();

return 0;
}

Sample Output:

Code screenshot:


Related Solutions

Queues are often used to represent lists of things that are being processed according to the...
Queues are often used to represent lists of things that are being processed according to the order in which they arrived -- i.e. "first come, first served".   Assignment Write a program that simulates the minute-by-minute operation of a checkout line, such as one you might find in a retail store. Use the following parameters: 1) Customers arrive at the checkout line and stand in line until the cashier is free. 2) When they reach the front of the line, they...
Make two lists. List A should identify the things that represent the core cultures of organizations....
Make two lists. List A should identify the things that represent the core cultures of organizations. List B should id identify the tings that represent the observable cultures of the organization. For each item on the two lists , identify one or more indicators that you might use to describe this aspect of the culture for an actual organization.
GDP is often used as a measure of well-being. Is it a reasonable measure of well-being?...
GDP is often used as a measure of well-being. Is it a reasonable measure of well-being? If so, why does Norway with its high standard of living have a relatively low GDP? Why do India and China, with their relatively low standards of living have some of the highest GDP in the world?
Explain how Big Data and the Internet of Things are being used in healthcare right now,...
Explain how Big Data and the Internet of Things are being used in healthcare right now, and what the implications for the future might be.
1.   If the auditor suspects that payables are not being processed correctly yet credit memoranda are being...
1.   If the auditor suspects that payables are not being processed correctly yet credit memoranda are being issued to vendors for goods returns, what test of control procedures might the auditor perform to investigate this problem? 2.  If the auditor suspects that a client is not recording all payables, what test of control procedure might the auditor perform to obtain the evidence? 3. Identify and discuss two test of controls the auditor might test in Accounts Payable 4,  If the auditor suspects that...
JAVA *** All data structures, including array operations, queues, stacks, linked lists, trees, etc need to...
JAVA *** All data structures, including array operations, queues, stacks, linked lists, trees, etc need to be implemented by you. Write a menu driven program that implements the following Binary Search Tree Operations FIND (item) INSERT (item) DELETE (item) DELETE_TREE (delete all nodes - be careful with the traversal!)
According to Zimmels (1983), the sizes of particles used in sedimentation experiments often have a uniform...
According to Zimmels (1983), the sizes of particles used in sedimentation experiments often have a uniform distribution. In sedimentation involving mixtures of particles of various sizes, the larger particles hinder the movements of the smaller ones. Thus, it is important to study both the mean and the variance of particle sizes. Suppose that spherical particles have diameters that are uniformly distributed between 0.02 and 0.07 centimeters. Find the mean and variance of the volumes of these particles. (Recall that the...
1. What are the Limits of the material being processed in the Fused Deposition Modeling (FDM),...
1. What are the Limits of the material being processed in the Fused Deposition Modeling (FDM), apart from using Thermoplastic only? 2. What are the Limits of the equipment being used in the Fused Deposition Modeling (FDM), apart from producing small parts only?
1. One example of the Limits of the material being processed in fused deposition modeling is...
1. One example of the Limits of the material being processed in fused deposition modeling is that the material has to be a Thermoplastics. Provide some other examples of the limits of material in FDM. 2. One example of the Limits of the equipment being used in fused deposition modeling is that the machine can only produce small parts. Provide some other examples of the limits of equipmentin FDM.
M1_P5. Cattle are sent to a feedlot to be grain-fed before being processed into beef. The...
M1_P5. Cattle are sent to a feedlot to be grain-fed before being processed into beef. The owners of a feedlot seek to determine the amounts of cattle feed to buy so that the minimum nutritional standards are satisfied to ensure proper weigh gain, while total feed costs are minimized. The feed mix used is made up of three grains that contain the following nutrients per pound of feed. Nutrient (ounces per pound of feed) FEED A B C D Cost...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT