-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqSim.cpp
172 lines (141 loc) · 4.85 KB
/
qSim.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* PA4_the_murder - Jacob Tutlis - jjtutlis - James Taylor - jrtaylor
* Created by jrtaylor on 2/10/18.
*/
#include <iostream>
#include <queue>
#include <cmath>
#include "event.h"
#include "eventQueue.h"
#include "teller.h"
using namespace std;
using std::cout;
using std::endl;
/**
* calculates the standard deviation of the customer service time dataset
* credit to https://www.programiz.com/cpp-programming/examples/standard-deviation for this code
* @param customerTimes
* @param customersServed
* @return the standard deviation
*/
float calculateSD(int customerTimes[], int customersServed) {
float sum = 0.0, mean, standardDeviation = 0.0;
int i;
for(i = 0; i < customersServed; ++i)
{
sum += customerTimes[i];
}
mean = sum/10;
/* Calculates standard deviation
* Precondition: customerTimes has times and customersServed contains
* the size of customerTimes
* Postcondition: SD is calculated
*/
for(i = 0; i < 10; ++i)
standardDeviation += pow(customerTimes[i] - mean, 2);
return sqrt(standardDeviation / 10);
}
/**
* Prints the statistics from the test
* @param customerTimes an array of integers representing the service times for customers
* @param customersServed the number of customers served
*/
void printStatistics(int customerTimes[], int customersServed, int totalCustomers){
// Average service time calculation
double avgServiceTime;
avgServiceTime = 0;
for (int i = 0; i < customersServed; ++i) {
avgServiceTime += customerTimes[i];
}
avgServiceTime = avgServiceTime / customersServed;
// Printing
cout << "Customers Served: " << customersServed << " of " << totalCustomers << endl;
cout << "Average Service Time: " << avgServiceTime << " minutes" << endl;
cout << "Standard Deviation: " << calculateSD(customerTimes, customersServed) << endl;
}
int main(int argc, char *argv[]) {
srand(time(0));
//interpreting command line arguments
// total number of customers and tellers
int customers, tellers;
// time in unit of minutes
int simulationTime, averageServiceTime;
// random seed for program
unsigned int seed;
if (argc != 5 && argc != 6) {
cout << "The command line arguments are invalid!" << endl;
cout << "./qSim #customers #tellers simulationTime averageServiceTime <seed>" << endl;
return EXIT_FAILURE;
}
// Initializes command line arguments
customers = atoi(argv[1]);
tellers = atoi(argv[2]);
simulationTime = atof(argv[3]);
averageServiceTime = atof(argv[4]);
switch (argc) {
case 5:
seed = rand();
srand(seed);
break;
case 6:
seed = atoi(argv[5]);
srand(seed);
break;
}
Teller *allTellers[tellers];
for (int i = 0; i < tellers; ++i) {
Teller *t = new Teller();
allTellers[i] = t;
}
//*************************************************************************
// Start of single line
eventQueue *singleLineEQ = new eventQueue();
for (int i = 0; i < customers; ++i) {
singleLineEQ->priorityAdd(new event(simulationTime, tellers));
}
//singleLineEQ->display();
// Statistical trackers
int sLProcessingTimes[customers];
int curCust=0;
/* Runs the single line bank simulation
* Preconditions: simulationTime must be declared and tellers and customers must be created
* Postconditions: events will go through line and be processed
*/
for(int time = 0; time < simulationTime; time++){
// retrieves the first event if the time is less than or equal to the current time
event *c=singleLineEQ->getFirst();
if(c->getArrTime()<=time){
// checks each teller to process new customers
for (int i = 0; i < tellers; ++i) {
// if the given teller is active, have them check the line
if(allTellers[i]->getActiveTime() <= time){
event *e=singleLineEQ->getFirst();
if(e->getArrTime() <= time){
sLProcessingTimes[curCust] = (time - e->getArrTime());
curCust++;
singleLineEQ->delete_first();
allTellers[i]->setActiveTime(time);
}
}
}
}
}
printStatistics(sLProcessingTimes, curCust, customers);
//*************************************************************************
// Start of multi line
/*
eventQueue *multiLineEQ;
for(int time = 0; time < simulationTime; time+=.01){
// checks each teller to process new customers
for (int i = 0; i < tellers; ++i) {
Customer cur;
// retrieves the first event if the time is less than or equal to the current time
multiLineEQ;
}
}*/
// deletes tellers
for (int i = 0; i < tellers; ++i) {
delete allTellers[i];
}
return 0;
}