Question: I need help coding this in C++ please! input file and starter file provided at the bottom Assignment Write a program that simulates the minute-by-minute
I need help coding this in C++ please! input file and starter file provided at the bottom
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:
Customers arrive at the checkout line and stand in line until the cashier is free.
When they reach the front of the line, they occupy the cashier for some period of time (referred to as ServiceTime) measured in minutes.
After the cashier is free, the next customer is served immediately.
Customers arrive at the checkout line at ArrivalRate per minute. Use the function included below (customerArrived()) to return the number of customers arriving in a given minute, determined randomly.
The line can only hold so many people, MaxLineSize, until new arriving customers get frustrated and leave the store without purchasing anything.
The overall time of the simulation is SimulationTime, measured in minutes.
The program should take 5 inputs (to be read from a text file named simtest.txt, one per line):
SimulationName - a string name for the simulation.
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.
ServiceTime - the amount each customer requires at the register (the same for all customers).
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 (is "dropped").
At the end of each simulation, the program should output:
The total number of customers serviced
The average time per customer spent in line
The average length of the line during the simulation
The total number of customers who were dropped (found the line too long, then left the store).
You are free to use any STL templates as needed (queue, vector, etc.). Note that you will not need a priority queue for this lab (only a regular queue, since all customers are of equal urgency).
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 (0.1 * 2000) to arrive. If we add the number of customers serviced (125) with the customers leaving (97) that gives us a number (222) which is close enough to 200 to be possible for one run of the simulation.
Simulation name: Short Lines -------------------------------------- Simulation time: 2000 Arrival rate: 0.1 Service time: 15 Max line size: 5 Customers served: 125 Average wait time: 66.688 Average line length: 4.246 Total dropped customers: 97
What to Submit
To compelte this lab submit your .cpp file (source code for your solution) in Canvas.
Hints
Simulation Loop
To run each simulation, you will want some kind of loop that runs based on the passing of time. A simple integer counter will work. Start the counter at 0 and run until the counter hits SimulationTime. This counter can also serve as your representation of the current time of the simulation at any given point.
Representing Customers
You will need a data structure to represent a customer. What information do you want to save when a customer enters the line? If you save a timestamp at the entry time, you will be able to calculate the amount of time they waited by comparing this timestamp to the current time when they are ready to go to the cashier.
Random Functions
The provided function customerArrived() returns true if a customer arrived at that minute, false otherwise. The input variable 'prob' should be expressed as a decimal percentage, for example, .80 if there is an 80% chance of a customer arriving. This comes from the input parameter 'ArrivalRate'.
bool customerArrived(double prob) { double rv = rand() / (double(RAND_MAX) + 1); return (rv < prob); } Before calling customerArrived() be sure to seed the random number generator (you can do this in main()):
srand(time(0));
input file simtest.txt contents:
Short Lines 2000 0.1 15 5 Long Lines 2000 0.5 15 10 Mixed 2000 0.25 8 7
starter file:
/* * Program #2 Starter File * ---------------------- * This program simulates a checkout line, such as one you * might encounter in a grocery store. Customers arrive at * the checkout stand and get in line. Those customers wait * in the line until the cashier is free, at which point * they are served and occupy the cashier for some period * of time. After the service time is complete, the cashier * is free to serve the next customer in the line. * * In each unit of time, up to the constant SIMULATION_TIME, * the following operations are performed: * * 1. Determine whether a new customer has arrived. * New customers arrive randomly, with a probability * determined by the constant ARRIVAL_RATE, taken from the * input file. * * 2. If the cashier is busy, note that the cashier has * spent another minute with that customer. Eventually, * the customer's time request is satisfied, which frees * the cashier. * * 3. If the cashier is free, serve the next customer in line. * Each customer is given the same amount of time at the * register (SERVICE_TIME), which is taken from the file. * * At the end of the simulation, the program displays the * simulation constants and the following computed results: * * o The number of customers served * o The average time spent in line * o The average number of people in line * o The number of people who left because the line was full. */ #include
// // Runs the actual simulation. This function returns the results // of the simulation through the reference parameters, which record // the number of customers served, the total number of minutes that // customers were waiting in a queue, and the sum of the queue length // in each time step. // void runSimulation(int simTime, double arrivalRate, int serviceTime, int maxLine, int &nServed, int &totalWait, int &totalLength, int &totalDropped) { queue
bool readNextSimulation(ifstream &f, string &name, int &simulationTime, double &arrivalRate, int &serviceTime, int &lineSize) { string buf; string fileValues[5]; int i = 0; while (getline(f, buf)) { // We've reached the end of the file, indicate to caller // there are no more simulations. if (f.eof()) break; fileValues[i++] = buf; if (i == PARAMS_MAX) { // Fill in the values, converting to the right type name = fileValues[0]; // Remove linefeed if necessary if (name.size() != 0 && name[name.size() - 1] == ' ') name.erase(name.size() - 1); simulationTime = atoi(fileValues[1].c_str()); arrivalRate = atof(fileValues[2].c_str()); serviceTime = atoi(fileValues[3].c_str()); lineSize = atoi(fileValues[4].c_str()); return (true); } } return (false); } // // Reports the results of the simulation in tabular format. // void printReport(string name, int simTime, double arrivalRate, int serviceTime, int maxLine, int nServed, int totalWait, int totalLength, int totalDropped) { // Left-justify everything cout << left; // Print a table cout << endl; cout << setw(25) << "Simulation name: " << setw(12) << name << endl; cout << "--------------------------------------" << endl; cout << setw(25) << "Simulation time: " << setw(12) << simTime << endl; cout << setw(25) << "Arrival rate: " << setw(12) << arrivalRate << endl; cout << setw(25) << "Service time: " << setw(12) << serviceTime << endl; cout << setw(25) << "Max line size: " << setw(12) << maxLine << endl; cout << endl; cout << setw(25) << "Customers served:" << setw(12) << nServed << endl; cout << setw(25) << "Average wait time:" << double(totalWait) / nServed << endl; cout << setw(25) << "Average line length:" << setw(12) << double(totalLength) / simTime << endl; cout << setw(25) << "Total dropped customers:" << setw(12) << totalDropped << endl; } /* Main program */
int main() { // Sim results int nServed; int totalWait; int totalLength; int totalDropped; // Sim parameters string name; int simTime; double arrivalRate; int serviceTime; int maxLine; string buf; ifstream f("simtest.txt"); // initialize the random number generator srand(int(time(NULL))); if (!f.good()) { cerr << "Invalid file, exiting." << endl; return (-1); } while (readNextSimulation(f, name, simTime, arrivalRate, serviceTime, maxLine)) { runSimulation(simTime, arrivalRate, serviceTime, maxLine, nServed, totalWait, totalLength, totalDropped); printReport(name, simTime, arrivalRate, serviceTime, maxLine, nServed, totalWait, totalLength, totalDropped); } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
