/
ActivityApproach.cpp
571 lines (446 loc) · 19.2 KB
/
ActivityApproach.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
// ActivityApproach.cpp : Defines the entry point for the console application.
#include <stdio.h>
#define _TCHAR char
#include "CLista.h"
#include "Statistics.h"
#include "Activity.h"
// Set _DEBUG_ 1 if you want to see tracing of all Activitys, 0, if not
#define _DEBUG_ 1
// Activity code definition
#define ARRIVE 1
#define STARTMANAGER 2
#define ENDMANAGER 3
#define ARRIVECALL 4
#define STARTCALL 5
#define ENDCALL 6
#define STARTATM 7
#define ENDATM 8
#define STARTTELLER 9
#define ENDTELLER 10
// CLista.h is a template for linked list class
#include "CLista.h"
// Client queue and Call queue declaration
CLista<CEntity *> *client_queue;
CLista<CEntity *> *call_queue;
CLista<CEntity *> *manager_queue;
CLista<CEntity *> *atm_queue;
CLista<CEntity *> *teller_queue;
// flags for idle
bool manager_free = true;
bool teller_free = true;
bool atm_free = true;
class CBank:public CActivity // Call class as a sub class of CActivity
{
public:
// attributes for storing arrival time, and start and end conversation time
CBank():CActivity() // Constructor
{}
void ArriveClient();
void ArriveCall();
void StartManager();
void EndManager();
void StartCall();
void EndCall();
void StartTeller();
void EndTeller();
void StartATM();
void EndATM();
void ExecuteActivity();
};
class CBankExecutive: CActivityExecutive
{
public:
CBankExecutive(): CActivityExecutive()
{}
double SimulationTime() {
return CActivityExecutive::SimulationTime();
}
double SimulationEnd() {
return CActivityExecutive::SimulationEnd();
}
void SetSimulationEnd( double sim) {
simulation_end=sim;
}
CBank * GetActivity() { // Get activity in the head of the list
return (CBank *)CActivityExecutive::GetActivity();
}
double TimeScan() { // Get the time of the head of the list
return CActivityExecutive:: TimeScan();
}
double GetActivityTime() { // Get activity of the head of the list (queue)
return CActivityExecutive:: GetActivityTime();
}
void AddActivity(double tim, int act, CEntity *ent) { // Add activity inserting in a sorted list by time
CActivityExecutive::AddActivity( tim, act, ent);
}
void RemoveActivity() { // Remove the activity in the head of the queue
CActivityExecutive::RemoveActivity();
}
void ExecuteActivities() { // Simulation activity execution at time simulation
CBank *activ;
double sim_time=SimulationTime();
// Execute all activity at sim_time
while(sim_time == GetActivityTime()){
activ = GetActivity(); // Get activity at the front of the queue
activ->ExecuteActivity(); // Call for executing activity method implemented in a subclass
RemoveActivity(); // Remove activity
}
}
};
CBankExecutive *executive;
// Simulation paramters
// double sim_time, call_arrival, arrival_mean, min_service, max_service, min_call, max_call;
double sim_time;
double call_arrival, arrival_mean, arrival_manager_prob, arrival_teller_prob, atm_service_mean, atm_service_stddev, teller_service_mean, teller_service_stddev, manager_service_mean, manager_service_stddev, atm_to_teller_prob, atm_to_manager_prob, min_service, max_service, min_call, max_call, call_max_wait;
// Statistical repository
CStatistics call_wait( "output/CallWait.txt", ADD_FILE ),
call_system( "output/CallSystem.txt", ADD_FILE ),
call_duration( "output/CallDuration.txt", ADD_FILE ),
call_attended( "output/CallAttended.txt", ADD_FILE ),
client_wait( "output/ClientWait.txt", ADD_FILE ),
client_system( "output/ClientSystem.txt", ADD_FILE ),
manager_wait( "output/ManagerWait.txt", ADD_FILE ),
manager_duration( "output/ManagerDuration.txt", ADD_FILE ),
teller_wait( "output/TellerWait.txt", ADD_FILE ),
teller_duration( "output/TellerDuration.txt", ADD_FILE ),
atm_wait( "output/ATMWait.txt", ADD_FILE ),
atm_duration( "output/ATMDuration.txt", ADD_FILE );
void CBank::ArriveClient() // Arrival activity handling
{
double time1, sim_time = executive->SimulationTime();
if (activity == ARRIVE && time == sim_time) {
CEntity *client = new CEntity();
CDistribution dist;
// Current client arrival
entity->arrive = time;
entity->start = time;
if(_DEBUG_) printf("Client Arrives %f \n", time);
// Next client arrival time calculation
time1 = sim_time + dist.Exponential(arrival_mean);
client->SetActivity(ARRIVE);
// Schedule next client arrival
executive->AddActivity(time1, ARRIVE, client);
// decide where the client goes
double r = dist.Random();
if(r < arrival_teller_prob){
teller_queue->InserirFim(entity);
}else if(r < arrival_teller_prob + arrival_manager_prob){
manager_queue->InserirFim(entity);
}else{
atm_queue->InserirFim(entity);
}
}
}
void CBank::StartManager() // service Start handling
{
double time1, sim_time = executive->SimulationTime();
CDistribution dist;
CEntity *client;
if(manager_free) { // Manager is free
if (call_queue->EhVazia() && !manager_queue->EhVazia()) { // There is client in the queue
// Get client from client queue
client = (CEntity*) manager_queue->ObterInfo();
manager_queue->Remover();
client->SetActivity(ENDMANAGER);
// Collect statistics on client waiting
client_wait.Add(time - client->start);
manager_wait.Add(time - client->start);
client->start = time;
if(_DEBUG_) printf("Service Starts %f \n", time);
// Calculate service ending time
time1 = sim_time + dist.NormalLimited(manager_service_mean, manager_service_stddev, min_service, max_service);
executive->AddActivity(time1, ENDMANAGER, client);
manager_free = false;
}
}
}
void CBank::EndManager() // service End handling
{
double sim_time = executive->SimulationTime();
if (activity == ENDMANAGER && time == sim_time) {
if(_DEBUG_) printf("Service Ends %f \n", time);
// Statistical storage of client time in system ans service duration
client_system.Add(time - entity->arrive);
manager_duration.Add(time - entity->start);
entity->end = time;
manager_free = true;
delete entity;
}
}
void CBank::ArriveCall() // Call arrival handling
{
double time1, sim_time = executive->SimulationTime();
// Test if it is the current activity
if (activity == ARRIVECALL && time == sim_time) {
CEntity *call=new CEntity();
CDistribution dist;
if(_DEBUG_) printf("Call Arrives %f \n", time);
// Calculate next call arrival
time1 = sim_time + dist.Exponential(call_arrival);
entity->arrive = time;
entity->start = time;
// Schedule next call arrival
executive->AddActivity(time1, ARRIVECALL, call);
// Put call on the queue
call_queue->InserirFim(entity);
}
}
void CBank::StartCall() // Call start handling
{
double time1, wait_time, sim_time = executive->SimulationTime();
CDistribution dist;
CEntity * call;
if(manager_free){
if(!call_queue->EhVazia()){
// Get call from queue
call = (CEntity *) call_queue->ObterInfo();
call_queue->Remover();
call->SetActivity(STARTCALL);
call->start=time;
// Collect stats on call waiting if doesnt waiting for a long time
wait_time = call->start - call->arrive;
call_wait.Add(wait_time);
if(wait_time < call_max_wait){
call_attended.Add(1.0);
}else{
call_attended.Add(0.0);
return ;
}
if(_DEBUG_) printf("Call Starts %f \n", time);
// Calculate call ending time
time1 = sim_time + dist.Uniform(min_call, max_call);
// Schedule end of conversation time
executive->AddActivity(time1, ENDCALL, call);
// manager isn't free
manager_free = false;
}
}
}
void CBank::EndCall() // Call ending handling
{
double sim_time = executive->SimulationTime();
if(activity == ENDCALL && time == sim_time){
if(_DEBUG_) printf("Call Ends %f \n", time);
entity->end = time;
// Statistical storage of c
call_system.Add(entity->end - entity->arrive);
call_duration.Add(entity->end - entity->start);
// manager is free
manager_free = true;
delete entity;
}
}
void CBank::StartTeller()
{
double time1, sim_time = executive->SimulationTime();
CDistribution dist;
CEntity * client;
if(teller_free){
if(!teller_queue->EhVazia()){
// Get call from queue
client = (CEntity *) teller_queue->ObterInfo();
teller_queue->Remover();
client->SetActivity(STARTTELLER);
// Collect stats on call waiting
client_wait.Add(time - client->start);
teller_wait.Add(time - client->start);
client->start = time;
if(_DEBUG_) printf("Teller Starts %f \n", time);
// Calculate call ending time
time1 = sim_time + dist.NormalLimited(teller_service_mean, teller_service_stddev, min_service, max_service);
// Schedule end of conversation time
executive->AddActivity(time1, ENDTELLER, client);
// teller isn't free
teller_free = false;
}
}
}
void CBank::EndTeller()
{
double sim_time = executive->SimulationTime();
if(activity == ENDTELLER && time == sim_time){
if(_DEBUG_) printf("Teller Ends %f \n", time);
// Statistical storage of c
client_system.Add(time - entity->arrive);
teller_duration.Add(time - entity->start);
entity->end = time;
// teller is free
teller_free = true;
delete entity;
}
}
void CBank::StartATM()
{
double time1, sim_time = executive->SimulationTime();
CDistribution dist;
CEntity * client;
if(atm_free){
if(!atm_queue->EhVazia()){
// Get call from queue
client = (CEntity *) atm_queue->ObterInfo();
atm_queue->Remover();
client->SetActivity(STARTATM);
// Collect stats on call waiting
client_wait.Add(time - client->start);
atm_wait.Add(time - client->start);
client->start = time;
if(_DEBUG_) printf("ATM Starts %f \n", time);
// Calculate call ending time
time1 = sim_time + dist.NormalLimited(atm_service_mean, atm_service_stddev, min_service, max_service);
// Schedule end of conversation time
executive->AddActivity(time1, ENDATM, client);
// atm isn't free
atm_free = false;
}
}
}
void CBank::EndATM()
{
double sim_time = executive->SimulationTime();
CDistribution dist;
if(activity == ENDATM && time == sim_time){
if(_DEBUG_) printf("Teller Ends %f \n", time);
// Statistical storage of c
atm_duration.Add(time - entity->start);
entity->end = time;
entity->start = time;
// atm is free
atm_free = true;
double r = dist.Random();
if(r < atm_to_teller_prob){
teller_queue->InserirFim(entity);
}else if(r < atm_to_teller_prob + atm_to_manager_prob){
manager_queue->InserirFim(entity);
}else{
client_system.Add(entity->end - entity->arrive);
delete entity;
}
}
}
void CBank::ExecuteActivity() // Activity execution
{
ArriveCall();
ArriveClient();
EndATM();
EndTeller();
EndCall();
EndManager();
StartATM();
StartTeller();
StartCall();
StartManager();
}
void StatisticsReport() // Statistical display of mean, standard deviation, minimum and maximum
{
FILE * stats_file;
stats_file = fopen("output/statistics.csv", "w");
fprintf(stats_file, "Stat,Total,Mean,Dev,Min,Max\n");
printf(" \nStatistical Report \n");
printf("Call waiting \n Mean = %f ",call_wait.Mean());
printf(" Std Dev = %f \n",call_wait.StandardDeviation());
printf(" Min = %f Max = %f \n",call_wait.min,call_wait.max);
fprintf(stats_file, "Call Wait,%d,%f,%f,%f,%f\n", call_wait.N(), call_wait.Mean(), call_wait.StandardDeviation(), call_wait.min, call_wait.max);
printf("Call in the System \n Mean = %f ",call_system.Mean());
printf(" Std Dev = %f \n",call_system.StandardDeviation());
printf(" Min = %f Max = %f \n",call_system.min,call_system.max);
fprintf(stats_file, "Call in System,%d,%f,%f,%f,%f\n", call_system.N(), call_system.Mean(), call_system.StandardDeviation(), call_system.min, call_system.max);
printf("Call duration \n Mean = %f ",call_duration.Mean());
printf(" Std Dev = %f \n",call_duration.StandardDeviation());
printf(" Min = %f Max = %f \n",call_duration.min,call_duration.max);
fprintf(stats_file, "Call Duration,%d,%f,%f,%f,%f\n", call_duration.N(), call_duration.Mean(), call_duration.StandardDeviation(), call_duration.min, call_duration.max);
printf("Call Attended \n Mean = %f ",call_attended.Mean());
printf(" Std Dev = %f \n",call_attended.StandardDeviation());
printf(" Min = %f Max = %f \n",call_attended.min,call_attended.max);
fprintf(stats_file, "Calls Attended,%d,%f,%f,%f,%f\n", call_attended.N(), call_attended.Mean(), call_attended.StandardDeviation(), call_attended.min, call_attended.max);
printf("Client waiting \n Mean = %f ",client_wait.Mean());
printf(" Std Dev = %f \n",client_wait.StandardDeviation());
printf(" Min = %f Max = %f \n",client_wait.min,client_wait.max);
fprintf(stats_file, "Client Wait,%d,%f,%f,%f,%f\n", client_wait.N(), client_wait.Mean(), client_wait.StandardDeviation(), client_wait.min, client_wait.max);
printf("Client in the System \n Mean = %f ",client_system.Mean());
printf(" Std Dev = %f \n",client_system.StandardDeviation());
printf(" Min = %f Max = %f \n",client_system.min,client_system.max);
fprintf(stats_file, "Client in System,%d,%f,%f,%f,%f\n", client_system.N(), client_system.Mean(), client_system.StandardDeviation(), client_system.min, client_system.max);
printf("Client waiting for Manager \n Mean = %f ",manager_wait.Mean());
printf(" Std Dev = %f \n",manager_wait.StandardDeviation());
printf(" Min = %f Max = %f \n",manager_wait.min,manager_wait.max);
fprintf(stats_file, "Manager Wait,%d,%f,%f,%f,%f\n", manager_wait.N(), manager_wait.Mean(), manager_wait.StandardDeviation(), manager_wait.min, manager_wait.max);
printf("Manager duration \n Mean = %f ",manager_duration.Mean());
printf(" Std Dev = %f \n",manager_duration.StandardDeviation());
printf(" Min = %f Max = %f \n",manager_duration.min,manager_duration.max);
fprintf(stats_file, "Manager Duration,%d,%f,%f,%f,%f\n", manager_duration.N(), manager_duration.Mean(), manager_duration.StandardDeviation(), manager_duration.min, manager_duration.max);
printf("Client waiting for Teller \n Mean = %f ",teller_wait.Mean());
printf(" Std Dev = %f \n",teller_wait.StandardDeviation());
printf(" Min = %f Max = %f \n",teller_wait.min,teller_wait.max);
fprintf(stats_file, "Teller Wait,%d,%f,%f,%f,%f\n", teller_wait.N(), teller_wait.Mean(), teller_wait.StandardDeviation(), teller_wait.min, teller_wait.max);
printf("Teller duration \n Mean = %f ",teller_duration.Mean());
printf(" Std Dev = %f \n",teller_duration.StandardDeviation());
printf(" Min = %f Max = %f \n",teller_duration.min,teller_duration.max);
fprintf(stats_file, "Teller Duration,%d,%f,%f,%f,%f\n", teller_duration.N(), teller_duration.Mean(), teller_duration.StandardDeviation(), teller_duration.min, teller_duration.max);
printf("Client waiting for ATM \n Mean = %f ",atm_wait.Mean());
printf(" Std Dev = %f \n",atm_wait.StandardDeviation());
printf(" Min = %f Max = %f \n",atm_wait.min,atm_wait.max);
fprintf(stats_file, "ATM Wait,%d,%f,%f,%f,%f\n", atm_wait.N(), atm_wait.Mean(), atm_wait.StandardDeviation(), atm_wait.min, atm_wait.max);
printf("ATM duration \n Mean = %f ",atm_duration.Mean());
printf(" Std Dev = %f \n",atm_duration.StandardDeviation());
printf(" Min = %f Max = %f \n",atm_duration.min,atm_duration.max);
fprintf(stats_file, "ATM Duration,%d,%f,%f,%f,%f\n", atm_duration.N(), atm_duration.Mean(), atm_duration.StandardDeviation(), atm_duration.min, atm_duration.max);
fflush(stats_file);
fclose(stats_file);
}
int main(int argc, _TCHAR* argv[])
{
char c;
CEntity *client = new CEntity();
CEntity *call = new CEntity();
executive = new CBankExecutive();
client_queue = new CLista<CEntity *>;
call_queue = new CLista<CEntity *>;
manager_queue = new CLista<CEntity *>;
atm_queue = new CLista<CEntity *>;
teller_queue = new CLista<CEntity *>;
CDistribution dist;
// Simulation Paramters
// um ano = 60min * 8h * 21d * 12m
double total_time = 10*60*8*21*12;
executive->SetSimulationEnd(total_time);
// call_arrival=5.0; // call arrival mean - Negative exponential distribution
// arrival_mean=3.0; // client arrival mean - Negative exponential distribution
// min_service=0.5; // minimum of uniform distribution
// max_service=2.0; // maximum of uniform distribution
// min_call=0.5; // minimum of uniform distribution
// max_call=1.5; // maximum of uniform distribution
call_arrival = 10.0; // call arrival mean - Negative exponential distribution
arrival_mean = 5.0; // client arrival mean - Negative exponential distribution
arrival_manager_prob = 0.1;
arrival_teller_prob = 0.2;
atm_service_mean = 4.0;
atm_service_stddev = 2.0;
teller_service_mean = 7.0;
teller_service_stddev = 3.0;
manager_service_mean = 10.0;
manager_service_stddev = 4.0;
atm_to_teller_prob = 0.15;
atm_to_manager_prob = 0.15;
min_service = 0.2; // minimum of uniform distribution
max_service = 10000.0; // maximum of uniform distribution
min_call = 1.0; // minimum of uniform distribution
max_call = 10.0; // maximum of uniform distribution
call_max_wait = 10.0;
// Initial activitys: client arrival and call arrival
// Schedule next client arrival
client->SetActivity(ARRIVE);
executive->AddActivity(dist.Exponential(arrival_mean), ARRIVE, client);
// Schedule next call arrival
call->SetActivity(ARRIVECALL);
executive->AddActivity(dist.Exponential(call_arrival), ARRIVECALL, call);
// Simulation loop
while(executive->SimulationTime() < executive->SimulationEnd()) {
sim_time = executive->TimeScan(); // Time Scan = get next activity time
executive->ExecuteActivities(); // Execute all activitys at sim_time
}
// Report statistics on mean, std dev, min and max
StatisticsReport();
printf("\nPress any key to end\n");
scanf("%c",&c);
return 0;
}