In: Computer Science
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. This example is for the first test case in the sample file. Your output may vary somewhat because of the randomness in the simulation. In the below case, with ArrivalRate set to 0.1, we would expect about 200 people to arrive. If we add the number of customers serviced (183) with the customers leaving (25) that gives us a number (208) which is close enough to 200 to be possible for one run.
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));
#include <iostream>
#include <queue>
#include <vector>
#include <string>
#include <fstream>
bool randomChance(double prob);
int randomInt(int min, int max);
int main()
{
double inputData[6];
int dataCount=0;
std::ifstream input("simulation.txt"); //put your
program together with thsi file in the same folder.
while (input) {
std::string number;
getline(input, number); //read
number
inputData[dataCount] =
atof(number.c_str()); //convert to double;
dataCount++;
}
int const simulation_Time =
static_cast<int>(inputData[0]);
double const arrivalRate = inputData[1];
int const minServiceTime =
static_cast<int>(inputData[2]);
int const maxServiceTime =
static_cast<int>(inputData[3]);
int const maxLineSize =
static_cast<int>(inputData[4]);
int const irateCustomerThreshold =
static_cast<int>(inputData[5]);
int total_happy_customers = 0;
std::queue<int> customerQueue;
int customerId = 0;
double avgNumberOfCustomersInTheLine = 0;
int customersLeaving=0;
int serviceTimeTakenByCustomer=-111;
double avgTimeSpentByCustomerInLine = 0;
std::vector<int> timeSpentByCustomer;
int iterator = 0;
int numberOfIrateCustomers = 0;
for(int i=0;i<simulation_Time;i++)
{
if
(randomChance(arrivalRate))
{
if
(customerQueue.size() < maxLineSize) {
customerQueue.push(customerId);
timeSpentByCustomer.push_back(0);
customerId++;
}
else
{
customersLeaving++;
}
}
if(!customerQueue.empty()
&& serviceTimeTakenByCustomer<0)
{
serviceTimeTakenByCustomer = randomInt(minServiceTime,
maxServiceTime);
}
serviceTimeTakenByCustomer--;
if (serviceTimeTakenByCustomer ==
0)
{
customerQueue.pop();
iterator++;
total_happy_customers++;
serviceTimeTakenByCustomer = -111;
}
avgNumberOfCustomersInTheLine +=
customerQueue.size();
for (int
j=iterator;j<timeSpentByCustomer.size();j++)
{
timeSpentByCustomer[j]++;
}
}
for (int i : timeSpentByCustomer)
{
avgTimeSpentByCustomerInLine +=
i;
if(i
>=irateCustomerThreshold)
{
numberOfIrateCustomers++;
}
}
avgNumberOfCustomersInTheLine =
avgNumberOfCustomersInTheLine / simulation_Time;
avgTimeSpentByCustomerInLine =
avgTimeSpentByCustomerInLine / total_happy_customers;
std::cout << "Customers Serviced : " <<
total_happy_customers<< std::endl;
std::cout << "Customers Left : " <<
customersLeaving << std::endl;
std::cout << "Avg Time Spent By the Customers in
Line : " << avgTimeSpentByCustomerInLine <<
std::endl;
std::cout << "Avg Number of Customers in Line :
" << avgNumberOfCustomersInTheLine << std::endl;
std::cout << "Irate Customers : " <<
numberOfIrateCustomers << std::endl;
return 0;
}
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);
}
Text File Input
OutPut: