-
Notifications
You must be signed in to change notification settings - Fork 0
/
Company.cpp
593 lines (494 loc) · 17.3 KB
/
Company.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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
#include "Company.h"
#include "PreparationEvent.h"
#include"CancellationEvent.h"
#include"PromotionEvent.h"
#include"UI.h"
#include<fstream>
#include<string>
using namespace std;
Company::Company() {
//initalize IO & UI
uiPtr = new UI;
int x;
x = uiPtr->Readmanual();
IN_PATH = "Sample" + to_string(x) + ".txt";
OUT_PATH="Output_File.txt";
load();
//initalize IO & UI
//inialize Trucks
loadingTrucks.insert(Normal, NULL);
loadingTrucks.insert(Special, NULL);
loadingTrucks.insert(VIP, NULL);
movingTrucks = new PriorityQueue<Truck*>(getTotalTruckCount());
//inialize Trucks
//inialize Counters
TotalTruckActiveTime = TotalTruckUtilization = PromotedCargoCount = 0;
//inialize output file
out_file.open(OUT_PATH);
out_file << "CDT\tID\tPT\tWT\tTID\n";
}
void Company::Simulate() {
while (!isSimulationEnd())
{
ExecuteEvent();
AutoPromotion();
moveTrucks();
DeliverCargos();
TrucksReturnBack();
EndCheckUP();
checkLoadingTrucks();
assign(NightMode());
uiPtr->Print(this);
currentTime.Update();
}
EndSimulation();
}
///////////////////////////////////////////////////Change states of Trucks
void Company::TrucksReturnBack()
{
//if Finished Move
while (!movingTrucks->isEmpty()&& movingTrucks->peekFront()->isFinishedDelivery(currentTime))
{
Truck* temp = movingTrucks->peekFront();
temp->EndJourney();
movingTrucks->dequeue();
Queue<Truck *>* listAddedTo = NULL;
Types t= temp->getTruckType();
if (temp->CheckUP(currentTime, J,uiPtr))
maintainingTrucks[temp->getIsNightShift()][t].enqueue(temp, temp->getFinishingCheckUPTime().ConvertToInt());
else
emptyTrucks[temp->getIsNightShift()][t].enqueue(temp, temp->getPriority());
}
///
}
void Company::EndCheckUP() {
for(int Truck_MODE = DAY; Truck_MODE <= NIGHT;Truck_MODE++){
for (int trucktype = Normal; trucktype <= VIP; trucktype++) {
while (!maintainingTrucks[Truck_MODE][trucktype].isEmpty() && maintainingTrucks[Truck_MODE][trucktype].peekFront()->isFinishedCheckUp(currentTime))
{
Truck* t = maintainingTrucks[Truck_MODE][trucktype].peekFront();
emptyTrucks[Truck_MODE][trucktype].enqueue(t, t->getPriority());
t->endCheckUp();
maintainingTrucks[Truck_MODE][trucktype].dequeue();
}
}
}
}
///////////////////////////////////////////////////Change states of Trucks
///////////////////////////////////////////////////AutoP
void Company::AutoPromotion() {
while (!waitingNormalCargo.isEmpty() && waitingNormalCargo.getFront()->AutoPCheck(currentTime, AutoP))
{
Cargo* temp = waitingNormalCargo.getFront();
waitingNormalCargo.removeFront();
Promote(temp);
}
while (!waitingSpecialCargo.isEmpty() && waitingSpecialCargo.peekFront()->AutoPCheck(currentTime, AutoP))
{
Cargo* temp = waitingSpecialCargo.peekFront();
waitingSpecialCargo.dequeue();
Promote(temp);
}
}
void Company::Promote(Cargo* c)
{
c->setTypes('V');
waitingVIPCargo.enqueue(c, c->getPriority());
PromotedCargoCount++;
}
///////////////////////////////////////////////////AutoP
/////////////////////////////////////////////////// I|O
void Company::load()
{
ifstream file;
file.open(IN_PATH);
file >> NormalTruckCount;
for (int i = 0; i < NormalTruckCount; i++) {
Truck* t = new NormalTruck(file);
emptyTrucks[t->getIsNightShift()][Normal].enqueue(t, t->getPriority());
}
file >> SpecialTruckCount;
for (int i = 0; i < SpecialTruckCount; i++) {
Truck* t = new SpecialTruck(file);
emptyTrucks[t->getIsNightShift()][Special].enqueue(t, t->getPriority());
}
file >> VIPTruckCount;
for (int i = 0; i < VIPTruckCount; i++) {
Truck* t = new VIPTruck(file);
emptyTrucks[t->getIsNightShift()][VIP].enqueue(t, t->getPriority());
}
file >> J;
int CheckUPTime;
file >> CheckUPTime;
NormalTruck::setCheckUPTime(CheckUPTime);
file >> CheckUPTime;
SpecialTruck::setCheckUPTime(CheckUPTime);
file >> CheckUPTime;
VIPTruck::setCheckUPTime(CheckUPTime);
file >> AutoP;
file >> MaxW;
int EventNumber;
file >> EventNumber;
Event* newEvent = nullptr;
for (int i = 0; i < EventNumber; i++)
{
char eventType;
file >> eventType;
switch (eventType)
{
case 'R':
newEvent = new PreparationEvent;
break;
case 'X':
newEvent = new CancellationEvent;
break;
case 'P':
newEvent = new PromotionEvent;
break;
default:
break;
}
newEvent->Read(file);
EventList.enqueue(newEvent);
}
file.close();
}
void Company::EndSimulation(){
/*
* @Summary
* looping on the (empty & night) trucks to calculate the Total truck active time
* and the total truck utilization
*/
for(int Truck_Mode = DAY;Truck_Mode <= NIGHT;Truck_Mode++)
for(int Truck_Type = Normal; Truck_Type <= VIP; Truck_Type++){
while (!emptyTrucks[Truck_Mode][Truck_Type].isEmpty()) {
Truck* t = emptyTrucks[Truck_Mode][Truck_Type].peekFront();
TotalTruckActiveTime += t->getTotalActiveTime();
TotalTruckUtilization += t->getTruckUtilization(currentTime);
emptyTrucks[Truck_Mode][Truck_Type].dequeue();
}
}
Out_End();
}
void Company::Out_End() {
out_file << ".........................................\n";
out_file << ".........................................\n";
out_file << "Cargos: " << getDeliveredCargosCount() << "[N: " << DeliveredCargos[0].getSize() << ", S: " << DeliveredCargos[1].getSize() << ", V: " << DeliveredCargos[2].getSize() << "]";
out_file << "\nCargo Avg Wait = " << Time(0,TotalCargoWait.ConvertToInt() / getDeliveredCargosCount()).TimePrint();
out_file << "\nAuto-Promoted Cargos : " << PromotedCargoCount/ getDeliveredCargosCount()*100 << "%\n";
out_file << "Trucks: " << getTotalTruckCount() << "[N: " << NormalTruckCount << ", S: " << SpecialTruckCount << ", V: " << VIPTruckCount << "]";
out_file << "\nAvg Active Time = " << TotalTruckActiveTime / ((double)currentTime.ConvertToInt() * getTotalTruckCount()) * 100 << '%';
out_file << "\nAvg utilization = " << TotalTruckUtilization / getTotalTruckCount() * 100 << "%\n";
}
///////////////////////////////////////////////////I/O
// The conditions for continue of the simulation
inline bool Company::isSimulationEnd() {
return (
EventList.isEmpty() &&
waitingNormalCargo.isEmpty() &&
waitingSpecialCargo.isEmpty() &&
waitingVIPCargo.isEmpty() &&
!getLoadingTrucksCount() &&
movingTrucks->isEmpty() &&
!getMaintainingTrucksCount()[NIGHT] &&
!getMaintainingTrucksCount()[DAY]
);
}
void Company::ExecuteEvent() {
/*
* @Summary
* cheking if there is an event and the execution time has come
* then executing the event delete it and dequeue
*/
while (!EventList.isEmpty() && currentTime == EventList.peekFront()->getEventTime())
{
EventList.peekFront()->Execute(this);
delete EventList.peekFront();
EventList.dequeue();
}
}
void Company::checkLoadingTrucks()
{
/*
* @Summary
* [BOOL]addLoadingTrucks Functions : Handeling Truck_Capacity && Max_Wait : and if could Load return True
* Calling addLoadingTrucks and send trucks with Assignment Order tell True or Options End
*/
//! ASSUME : that if there's no available Trucks First go Second or Third
//! as mentiond below
/* PriorityQueue ChoosenList */
PriorityQueue<Truck*>* choosenList[3];
for (int i = 0; i <= 2; i++) {
////if no ther list or Night
//if (emptyTrucks[DAY][i].isEmpty() || NightMode()) choosenList[i] = &emptyTrucks[NIGHT][i];
//else if (emptyTrucks[NIGHT][i].isEmpty()) choosenList[i] = &emptyTrucks[DAY][i];
//else if (emptyTrucks[NIGHT][i].peekFront()->getPriority() < emptyTrucks[DAY][i].peekFront()->getPriority())
// choosenList[i] = &emptyTrucks[NIGHT][i];
//else choosenList[i] = &emptyTrucks[DAY][i];
if (NightMode() || emptyTrucks[DAY][i].isEmpty()) {
if (emptyTrucks[NIGHT][i].isEmpty())
choosenList[i] = &maintainingTrucks[NightMode()][i];
else
choosenList[i] = &emptyTrucks[NIGHT][i];
}
//else if (emptyTrucks[NIGHT][i].isEmpty() && emptyTrucks[DAY][i].isEmpty()) {
// if (maintainingTrucks[NIGHT][i].peekFront()->getPriority() < maintainingTrucks[DAY][i].peekFront()->getPriority())
// choosenList[i] = &maintainingTrucks[NIGHT][i];
// else choosenList[i] = &maintainingTrucks[DAY][i];
//}
if (!NightMode()) {
if (getEmptyTrucksCount()[0] + getEmptyTrucksCount()[1] == 0) {
if (maintainingTrucks[NIGHT][i].isEmpty()) choosenList[i] = &maintainingTrucks[DAY][i];
else if (maintainingTrucks[DAY][i].isEmpty())choosenList[i] = &maintainingTrucks[NIGHT][i];
else if (maintainingTrucks[NIGHT][i].peekFront()->getPriority() < maintainingTrucks[DAY][i].peekFront()->getPriority())
choosenList[i] = &maintainingTrucks[NIGHT][i];
else choosenList[i] = &maintainingTrucks[DAY][i];
if (!choosenList[i]->isEmpty())
{
Truck* t = choosenList[i]->peekFront();
uiPtr->Output("Bonus The company need this truck ID: " +to_string(t->getID()) + " " + " The speed :" + to_string(t->GetSpeed()));
t->SetSpeed(choosenList[i]->peekFront()->GetSpeed() / 2);
uiPtr->Output("\nThen became :" + to_string(t->GetSpeed()));
}
}
else {
if (emptyTrucks[NIGHT][i].isEmpty()) choosenList[i] = &emptyTrucks[DAY][i];
else if (emptyTrucks[DAY][i].isEmpty())choosenList[i] = &emptyTrucks[NIGHT][i];
else if (emptyTrucks[NIGHT][i].peekFront()->getPriority() < emptyTrucks[DAY][i].peekFront()->getPriority())
choosenList[i] = &emptyTrucks[NIGHT][i];
else choosenList[i] = &emptyTrucks[DAY][i];
}
}
else {
if (getEmptyTrucksCount()[DAY] + getEmptyTrucksCount()[NIGHT] == 0){
choosenList[i] = &maintainingTrucks[NIGHT][i];
if (!choosenList[i]->isEmpty())
{
Truck* t = choosenList[i]->peekFront();
uiPtr->Output("Bonus The company need this truck ID: " + to_string(t->getID()) + " " + " The speed :" + to_string(t->GetSpeed()));
t->SetSpeed(choosenList[i]->peekFront()->GetSpeed() / 2);
uiPtr->Output("\nThen became :" + to_string(t->GetSpeed()));
}
}
else
choosenList[i] = &emptyTrucks[NIGHT][i];
}
}
/* PriorityQueue ChoosenList */
///1 - VIP Trucks
if (!loadingTrucks.getEntry(VIP) && !waitingVIPCargo.isEmpty())
{
//get the min capcity of Any Suitable Truck according to assignment Rule
int c = waitingVIPCargo.getSize();
bool Now = waitingVIPCargo.peekFront()->MaxWCheck(currentTime, MaxW);
//First: Check VIP Trucks
for (int i = 0; i <= 2; i++) {
if (!choosenList[Cargo_VIP_PRIORITY[i]]->isEmpty() && addLoadingTruck(choosenList[Cargo_VIP_PRIORITY[i]]->peekFront(), VIP, c, Now, choosenList[Cargo_VIP_PRIORITY[i]]))
break;
}
}
///2 - Special Trucks
if (!loadingTrucks.getEntry(Special) && !choosenList[Special]->isEmpty() && !waitingSpecialCargo.isEmpty()){
addLoadingTruck(choosenList[Special]->peekFront(), Special, waitingSpecialCargo.getSize(), waitingSpecialCargo.peekFront()->MaxWCheck(currentTime, MaxW), choosenList[Special]);
}
///3 - Normal Trucks
if(!loadingTrucks.getEntry(Normal) && !waitingNormalCargo.isEmpty()){
//get if cargo arrived Max Wait
bool Now = waitingNormalCargo.getFront()->MaxWCheck(currentTime, MaxW);
//get the min capcity of Any Suitable Truck according to assignment Rule
int c = waitingNormalCargo.getSize();
//check first Normal Empty Trucks
for (int i = 0; i < 2; i++) {
if (!choosenList[Cargo_NORMAL_PRIORITY[i]]->isEmpty() && addLoadingTruck(choosenList[Cargo_NORMAL_PRIORITY[i]]->peekFront(), Normal, c, Now, choosenList[Cargo_NORMAL_PRIORITY[i]]))
break;
}
}
}
bool Company::addLoadingTruck(Truck * t, Types CargoType, int MinCapacity, bool Now, PriorityQueue<Truck *> * choosenTruckList)
{
/*
* @Summary
* [BOOL] Now : True means MaxW Rule
*
*/
//1-Handeling if Truck Null
if (!t) return false;
//2-Handeling MaxW || Capacity
if (Now || t->GetCapcity() <= MinCapacity) {
//1-first Remove From empty Trucks
choosenTruckList->dequeue();
//2-inserted it in loading Trucks in position of
loadingTrucks.setEntry(CargoType, t);
t->setNowMoving(Now);
return true;
}
return false;
}
void Company::assign(bool isNight) {
if (loadingTrucks.getEntry(Normal) && !waitingNormalCargo.isEmpty())
if (loadingTrucks.getEntry(Normal)->AssignCargo(waitingNormalCargo.getFront(), currentTime, isNight))
{ // changes to calculate the DI distance
waitingNormalCargo.removeFront();
}
if (loadingTrucks.getEntry(Special) && !waitingSpecialCargo.isEmpty())
if (loadingTrucks.getEntry(Special)->AssignCargo(waitingSpecialCargo.peekFront(), currentTime, isNight))
{
waitingSpecialCargo.dequeue();
}
if (loadingTrucks.getEntry(VIP) && !waitingVIPCargo.isEmpty())
if (loadingTrucks.getEntry(VIP)->AssignCargo(waitingVIPCargo.peekFront(), currentTime, isNight))
{
waitingVIPCargo.dequeue();
}
}
///////////////////////////////////////////////////MaxW
void Company::moveTrucks()
{
/*
* @Summary
* moveTrucks: making a for loop in the loading trucks
* if the truck is able to move by the move function in side the truck
* then remove it from the loading trucks and put it in the moving trucks
* increase the moving cargo count
*/
for (int i = 0; i < loadingTrucks.getSize(); i++) {
Truck* t = loadingTrucks.getEntry(i);
if ( t && t->move(¤tTime)) {
loadingTrucks.setEntry(i, NULL);
movingTrucks->enqueue(t, (t->GetDi() + t->GetMt()).ConvertToInt());
MovingCargoCount += t->getCargoList()->getSize();
}
}
}
void Company::AddWaitingCargo(Cargo* temp)
{
if (temp->getType() == VIP)
{
waitingVIPCargo.enqueue(temp, temp->getPriority());
}
if (temp->getType() == Normal)
{
waitingNormalCargo.addBack(temp);
}
if (temp->getType() == Special)
{
waitingSpecialCargo.enqueue(temp);
}
}
void Company::DeliverCargos()
{
/*
* @Summary
* making auxiliary Pqueue of trucks
* taking the truck from the moving truk Pqueue
* calling failuer function
* and put the truck in the auxiliary Pqueue
* handelling the
and add cargo to waiting
* or Dliver the cargo and update the Total cargo time
*/
PriorityQueue<Truck*>* aux = new PriorityQueue<Truck*>(getTotalTruckCount());
while (!movingTrucks->isEmpty()) {
//Handeling Loop
Truck* t = movingTrucks->peekFront();
movingTrucks->dequeue();
t->Failuer(¤tTime, uiPtr);
aux->enqueue(t, (t->GetDi() + t->GetMt()).ConvertToInt());
///Handeling DeliverCargo One By One
Cargo* c = t->DeliverCargos(currentTime);
while (c) {
if (t->getIsFailed())
AddWaitingCargo(c);
else {
DeliveredCargos[c->getType()].enqueue(c->getID());
c->output(out_file);
TotalCargoWait = TotalCargoWait + c->GetWt();
delete c;
}
MovingCargoCount--;
c = t->DeliverCargos(currentTime);
}
}
delete movingTrucks;
movingTrucks = aux;
}
///////////////////////////////////////GETTERS
////////////////////////Counters
int Company::getWaitingCargosCount() const {
return waitingNormalCargo.getSize() + waitingSpecialCargo.getSize() + waitingVIPCargo.getSize();
}
int* Company::getEmptyTrucksCount() const {
int arr[2] =
{ emptyTrucks[DAY][Normal].getSize() + emptyTrucks[DAY][Special].getSize() + emptyTrucks[DAY][VIP].getSize()
, emptyTrucks[NIGHT][Normal].getSize() + emptyTrucks[NIGHT][Special].getSize() + emptyTrucks[NIGHT][VIP].getSize() };
return arr;
}
int Company::getNightTrucksCount() const
{
return emptyTrucks[NIGHT][0].getSize() + emptyTrucks[NIGHT][1].getSize() + emptyTrucks[NIGHT][2].getSize();
}
int Company::getMovingCargosCount() const { return MovingCargoCount;}
int Company::getLoadingTrucksCount() const {
int count = 0;
for (int i = 0; i <= VIP; i++)
count+= bool(loadingTrucks.getEntry(i));
return count;
}
int* Company::getMaintainingTrucksCount() const {
int arr[2] =
{ maintainingTrucks[DAY][Normal].getSize() + maintainingTrucks[DAY][Special].getSize() + maintainingTrucks[DAY][VIP].getSize()
, maintainingTrucks[NIGHT][Normal].getSize() + maintainingTrucks[NIGHT][Special].getSize() + maintainingTrucks[NIGHT][VIP].getSize() };
return arr;
}
int Company::getDeliveredCargosCount() const { return DeliveredCargos[0].getSize() + DeliveredCargos[1].getSize() + DeliveredCargos[2].getSize(); }
////////////////////////Counters
const Time & Company::getCurrentTime() const {
return currentTime;
}
////////////////////////Cargos
LinkedList<Cargo*>& Company::getWaitingNormalCargo() {return waitingNormalCargo;}
Queue<Cargo*>& Company::getWaitingSpecialCargo() {return waitingSpecialCargo;}
PriorityQueue<Cargo*>& Company::getWaitingVIPCargo()
{return waitingVIPCargo;}
const Queue<int>* Company::getDeliveredCargo()const { return DeliveredCargos; }
////////////////////////Cargos
////////////////////////Trucks
const PriorityQueue<Truck*>* Company::getEmptyTrucks(int Truck_Mode) const {
switch (Truck_Mode)
{
case DAY:
return emptyTrucks[DAY];
break;
default:
return emptyTrucks[NIGHT];
break;
}
}
const PriorityQueue<Truck*>* Company::getNightTrucks() const{return emptyTrucks[NIGHT]; }
const List<Truck*>& Company::getLoadingTrucks() const { return loadingTrucks; }
const PriorityQueue< Truck*>* Company::getMovingTrucks() const { return movingTrucks; }
const PriorityQueue< Truck*>* Company::getMaintainingTrucks (int Truck_Mode) const{
switch (Truck_Mode)
{
case DAY:
return maintainingTrucks[DAY];
break;
default:
return maintainingTrucks[NIGHT];
break;
}
}
////////////////////////Trucks
int Company::getTotalTruckCount() const
{
return NormalTruckCount + SpecialTruckCount + VIPTruckCount;
}
bool Company::NightMode() const
{
return currentTime.getHour() < 5 || currentTime.getHour() > 23;
}
Company::~Company() {
out_file.close();
delete movingTrucks;
delete uiPtr;
}