-
Notifications
You must be signed in to change notification settings - Fork 0
/
Company.cpp
654 lines (574 loc) · 16.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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
#include "Company.h"
#include <fstream>
#include "Event.h"
Company::Company()
{
pUI = new UI;
viploadingtruck = NULL;
specialloadingtruck = NULL;
normalloadingtruck = NULL;
movingcargosnum = 0;
totaltrucksactivetime = 0;
}
void Company::Load(string FileName)
{
int day, hour, CargoID, CargoDistance, LoadTime, CargoCost;
char eventtype, cargotype, c;
ifstream LoadFile(FileName);
LoadFile >> Nnum >> Snum >> VIPnum;
LoadFile >> Nspeed >> Sspeed >> VIPspeed;
LoadFile >> Ncapacity >> Scapacity >> VIPcapacity;
LoadFile >> journeys;
LoadFile >> Ncheckduration >> Scheckduration >> VIPcheckduration;
LoadFile >> numofEvents;
for (int i = 0; i < numofEvents; i++)
{
LoadFile >> eventtype; //only one event R
LoadFile >> cargotype;
setCargoType(cargotype);
LoadFile >> day >> c >> hour >> CargoID >> CargoDistance >> LoadTime >> CargoCost;
Time eventtime(day, hour);
createevent(getCargoType(), eventtime, CargoID, CargoDistance, LoadTime, CargoCost);
}
createtrucks();
}
void Company::Save(string FileName)
{
ofstream SaveFile(FileName);
SaveFile << "CDT" << " " << "ID" << " " << "PT" << " " << "WT" << " " << "TID"<< endl;
int c = deliverdcargolist.getCount();
Time totalwaitingtime(0, 0);
for (int i = 0; i <c; i++)
{
Cargo* pCargo;
deliverdcargolist.dequeue(pCargo);
SaveFile << pCargo->getdeliverytime().getday() << ":" << pCargo->getdeliverytime().gethour() << " " << pCargo->getCID() << " " << pCargo->getreadytime().getday() << ":" << pCargo->getreadytime().gethour() << " " << pCargo->getwaitingtime().getday() << ":" << pCargo->getwaitingtime().gethour() << " " <<pCargo->getTID()<< endl;
totalwaitingtime = totalwaitingtime + pCargo->getwaitingtime();
}
Time t = calcAverageWaitingTime(totalwaitingtime, c);
SaveFile << "Cargos: " << gettotalcargos() << "[N: " << getnormalcargos() << ", S: " << getspecialcargos() << ", V: " << getVIPcargos() << "]" << endl;
SaveFile << "Cargo Avg Wait = " << t.getday() << ":" << t.gethour() << endl;
SaveFile << "Trucks: " << gettotaltrucks() << "[N: " << Nnum << ", S: " << Snum << ", V: " << VIPnum << "]" << endl;
SaveFile << "Avg Active Time = " << int(averageactivetime*100) <<"%" << endl;
SaveFile << "Avg Utilization = " << int(Truckutilization*100) <<"%" << endl;
}
void Company::createtrucks()
{
for (int i = 0; i < Nnum; i++)
{
Truck* newtruck = new Truck(normal, Ncapacity, Ncheckduration, Nspeed);
normaltrucklist.enqueue(newtruck);
}
for (int i = 0; i < Snum; i++)
{
Truck* newtruck = new Truck(special, Scapacity, Scheckduration, Sspeed);
specialtrucklist.enqueue(newtruck);
}
for (int i = 0; i < VIPnum; i++)
{
Truck* newtruck = new Truck(VIP, VIPcapacity, VIPcheckduration, VIPspeed);
VIPtrucklist.enqueue(newtruck);
}
}
void Company::createevent(type cargotype, Time eventtime, int CargoID, int CargoDistance, int LoadTime, int CargoCost)
{
Event* newEvent = new Event(cargotype, eventtime, CargoID, CargoDistance, LoadTime, CargoCost);
Eventlist.enqueue(newEvent);
}
void Company::checkEventlist(const Time& currentTime)
{
Event* pEvent;
while( Eventlist.peek(pEvent)&& pEvent->geteventtime() == currentTime)
{
Eventlist.dequeue(pEvent);
pEvent->Execute(this);
}
}
void Company::addtocargolist(Cargo* pCargo)
{
type ctype = pCargo->getcargotype();
if (ctype == VIP)
{
int priority = pCargo->getpriority();
VIPcargolist.enqueue(pCargo, priority);
}
else if (ctype == normal)
{
normalcargolist.enqueue(pCargo);
}
else if (ctype == special)
{
specialcargolist.enqueue(pCargo);
}
}
void Company::update(const Time& currentTime)
{
checkEventlist(currentTime);
movefromloadingtomoving(currentTime);
movecargostodeliverd(currentTime);
movefrommovingtrucks(currentTime);
movefromchecktoavail(currentTime);
assigntoavailabetruck(currentTime);
}
Time Company::calcAverageWaitingTime(Time totTime, int n)
{
int totalhours = totTime.timetohours();
int averagehours = totalhours / n;
int day = 0;
while (averagehours >= 24)
{
averagehours -= 24;
day += 1;
}
return Time(day, averagehours);
}
void Company::calcTruckStatistics(Time currenttime)
{
int n = getnumofemptytrucks() + getnumofmoving() + getnumofloading();
int totalsimtime = currenttime.timetohours();
averageactivetime = (totaltrucksactivetime / n) / totalsimtime;
Truckutilization = averageactivetime;
}
bool Company::offhours(Time current)
{
if (current.gethour() > 23 || current.gethour() < 5)
return true;
return false;
}
void Company::assigntoavailabetruck(Time currenttime)
{
if (offhours(currenttime))
return;
else
{
assignvipcargos(currenttime);
assignspecialcargos(currenttime);
assignnormalcargos(currenttime);
}
}
void Company::assignvipcargos( Time currenttime)
{
if (viploadingtruck)
return;
Cargo* pCargo;
if (!VIPtrucklist.isEmpty())
{
if (VIPcargolist.getCount() < VIPcapacity)
return;
VIPtrucklist.dequeue(viploadingtruck);
int loadtime = 0;
Time movetime;
int maxdist = 0;
for (int i = 0; i < VIPcapacity; i++)
{
VIPcargolist.dequeue(pCargo);
loadtime += pCargo->getloadtime();
if (pCargo->getdistance() > maxdist) maxdist = pCargo->getdistance();
//get loading list and enqueue to it
viploadingtruck->addtoloadinglist(pCargo);
}
viploadingtruck->setmaxdistance(maxdist);
viploadingtruck->setsumloadtime(loadtime);
if (loadtime > (23 - currenttime.gethour()))
movetime = currenttime + loadtime+5;
else
movetime = currenttime + loadtime ;
viploadingtruck->setMoveTime(movetime);
viploadingtruck->setcargotype(VIP);
return;
}
else if (!normaltrucklist.isEmpty())
{
if (VIPcargolist.getCount() < Ncapacity)
return;
normaltrucklist.dequeue(viploadingtruck);
int maxdist = 0;
int loadtime = 0;
Time movetime;
for (int i = 0; i < Ncapacity; i++)
{
VIPcargolist.dequeue(pCargo);
loadtime += pCargo->getloadtime();
if (pCargo->getdistance() > maxdist) maxdist = pCargo->getdistance();
//get loading list and enqueue to it
viploadingtruck->addtoloadinglist(pCargo);
}
viploadingtruck->setmaxdistance(maxdist);
viploadingtruck->setsumloadtime(loadtime);
if (loadtime > (23 - currenttime.gethour()))
movetime = currenttime + loadtime + 5;
else
movetime = currenttime + loadtime;
viploadingtruck->setMoveTime(movetime);
viploadingtruck->setcargotype(VIP);
return;
}
else if (!specialtrucklist.isEmpty())
{
if (VIPcargolist.getCount() < Scapacity)
return ;
specialtrucklist.dequeue(viploadingtruck);
int maxdist = 0;
int loadtime = 0;
Time movetime;
for (int i = 0; i < Scapacity; i++)
{
VIPcargolist.dequeue(pCargo);
loadtime += pCargo->getloadtime();
if (pCargo->getdistance() > maxdist) maxdist = pCargo->getdistance() ;
//get loading list and enqueue to it
viploadingtruck->addtoloadinglist(pCargo);
}
if (loadtime > (23 - currenttime.gethour()))
movetime = currenttime + loadtime + 5;
else
movetime = currenttime + loadtime;
viploadingtruck->setMoveTime(movetime);
viploadingtruck->setmaxdistance(maxdist);
viploadingtruck->setsumloadtime(loadtime);
viploadingtruck->setcargotype(VIP);
}
}
void Company::assignspecialcargos(Time currenttime)
{
Cargo* pCargo;
if (!specialloadingtruck&&specialcargolist.getCount() >= Scapacity&&specialtrucklist.dequeue(specialloadingtruck))
{
int maxdist = 0;
int loadtime = 0;
Time movetime;
for (int i = 0; i < Scapacity; i++)
{
specialcargolist.dequeue(pCargo);
loadtime += pCargo->getloadtime();
if (pCargo->getdistance() > maxdist) maxdist = pCargo->getdistance();
//get loading list and enqueue to it
specialloadingtruck->addtoloadinglist(pCargo);
}
if (loadtime > (23 - currenttime.gethour()))
movetime = currenttime + loadtime + 5;
else
movetime = currenttime + loadtime;
specialloadingtruck->setMoveTime(movetime);
specialloadingtruck->setmaxdistance(maxdist);
specialloadingtruck->setsumloadtime(loadtime);
specialloadingtruck->setcargotype(special);
}
}
void Company::assignnormalcargos( Time currenttime)
{
if (normalloadingtruck)
return;
Cargo* pCargo;
if (!normaltrucklist.isEmpty())
{
if (normalcargolist.getCount() < Ncapacity)
return;
normaltrucklist.dequeue(normalloadingtruck);
int maxdist = 0;
int loadtime = 0;
Time movetime;
for (int i = 0; i < Ncapacity; i++)
{
normalcargolist.dequeue(pCargo);
loadtime += pCargo->getloadtime();
if (pCargo->getdistance() > maxdist) maxdist = pCargo->getdistance() ;
//get loading list and enqueue to it
normalloadingtruck->addtoloadinglist(pCargo);
}
if (loadtime > (23 - currenttime.gethour()))
movetime = currenttime + loadtime + 5;
else
movetime = currenttime + loadtime;
normalloadingtruck->setMoveTime(movetime);
normalloadingtruck->setmaxdistance(maxdist);
normalloadingtruck->setsumloadtime(loadtime);
normalloadingtruck->setcargotype(normal);
return;
}
else if (!VIPtrucklist.isEmpty())
{
if (normalcargolist.getCount() < VIPcapacity)
return;
VIPtrucklist.dequeue(normalloadingtruck);
int maxdist = 0;
int loadtime = 0;
Time movetime;
for (int i = 0; i < VIPcapacity; i++)
{
normalcargolist.dequeue(pCargo);
loadtime += pCargo->getloadtime();
if (pCargo->getdistance() > maxdist) maxdist = pCargo->getdistance() ;
//get loading list and enqueue to it
normalloadingtruck->addtoloadinglist(pCargo);
}
if (loadtime > (23 - currenttime.gethour()))
movetime = currenttime + loadtime + 5;
else
movetime = currenttime + loadtime;
normalloadingtruck->setMoveTime(movetime);
normalloadingtruck->setmaxdistance(maxdist);
normalloadingtruck->setsumloadtime(loadtime);
normalloadingtruck->setcargotype(normal);
}
}
void Company::movefromloadingtomoving(const Time& currentTime)
{
if (offhours(currentTime))
return;
if (viploadingtruck&& viploadingtruck->getMoveTime()== currentTime)
{
viploadingtruck->movecargosfromloadingtomoving(currentTime);
movingcargosnum += viploadingtruck->getcapacity();
int p = viploadingtruck->getFinishTime().timetohours();
movingtrucklist.enqueue(viploadingtruck,p);
viploadingtruck = NULL;
}
if (normalloadingtruck && normalloadingtruck->getMoveTime() == currentTime)
{
normalloadingtruck->movecargosfromloadingtomoving(currentTime);
movingcargosnum += normalloadingtruck->getcapacity();
int p = normalloadingtruck->getFinishTime().timetohours();
movingtrucklist.enqueue(normalloadingtruck, p);
normalloadingtruck = NULL;
}
if (specialloadingtruck && specialloadingtruck->getMoveTime() == currentTime)
{
specialloadingtruck->movecargosfromloadingtomoving(currentTime);
movingcargosnum += specialloadingtruck->getcapacity();
int p = specialloadingtruck->getFinishTime().timetohours();
movingtrucklist.enqueue(specialloadingtruck, p);
specialloadingtruck = NULL;
}
}
void Company::movecargostodeliverd(const Time& currentTime)
{
Queue<Truck*> temp;
Truck* pTruck;
Cargo* pCargo;
bool stop;
while (!movingtrucklist.isEmpty())
{
stop = false;
movingtrucklist.dequeue(pTruck);
temp.enqueue(pTruck);
while (!stop)
{
pCargo = pTruck->delivercargo(currentTime);
if (pCargo)
{
if (pCargo->getcargotype() == normal)
normaldeliverdcargolist.enqueue(pCargo);
else if (pCargo->getcargotype() == special)
specialdeliverdcargolist.enqueue(pCargo);
else if (pCargo->getcargotype() == VIP)
VIPdeliverdcargolist.enqueue(pCargo);
movingcargosnum--;
deliverdcargolist.enqueue(pCargo);
}
else
stop = true;
}
}
int p;
while (!temp.isEmpty())
{
temp.dequeue(pTruck);
p = pTruck->getFinishTime().timetohours();
movingtrucklist.enqueue(pTruck, p);
}
}
void Company::movefrommovingtrucks(const Time& currentTime)
{
Truck* pTruck;
while (movingtrucklist.peek(pTruck)&&pTruck->getFinishTime()==currentTime)
{
movingtrucklist.dequeue(pTruck);
pTruck->setactivetime();
totaltrucksactivetime += pTruck->getactivetime();
pTruck->incrementj();
if (pTruck->getnumofjournies() % journeys==0)
{
pTruck->setAvailTime();
if (pTruck->getTruckType() == VIP)
VIPchecktrucklist.enqueue(pTruck);
else if (pTruck->getTruckType() == normal)
normalchecktrucklist.enqueue(pTruck);
else if (pTruck->getTruckType() == special)
specialchecktrucklist.enqueue(pTruck);
}
else
{
if (pTruck->getTruckType() == VIP)
VIPtrucklist.enqueue(pTruck);
else if (pTruck->getTruckType() == normal)
normaltrucklist.enqueue(pTruck);
else if (pTruck->getTruckType() == special)
specialtrucklist.enqueue(pTruck);
}
}
}
void Company::movefromchecktoavail(const Time& currentTime)
{
Truck* pTruck;
while (VIPchecktrucklist.peek(pTruck)&& pTruck->getAvailTime() == currentTime)
{
VIPchecktrucklist.dequeue(pTruck);
VIPtrucklist.enqueue(pTruck);
}
while (normalchecktrucklist.peek(pTruck)&& pTruck->getAvailTime() == currentTime)
{
normalchecktrucklist.dequeue(pTruck);
normaltrucklist.enqueue(pTruck);
}
if (specialchecktrucklist.peek(pTruck)&& pTruck->getAvailTime() == currentTime)
{
specialchecktrucklist.dequeue(pTruck);
specialtrucklist.enqueue(pTruck);
}
}
Truck* Company::getviploadingtruck()
{
return viploadingtruck;
}
Truck* Company::getspecialloadingtruck()
{
return specialloadingtruck;
}
Truck* Company::getnormalloadingtruck()
{
return normalloadingtruck;
}
bool Company::Endsimulation(Time currenttime)
{
if (Eventlist.isEmpty() && getnumofdeliverd() == numofEvents&& getnumofemptytrucks()==gettotaltrucks())
{
calcTruckStatistics(currenttime);
return true;
}
return false;
}
int Company::getnumofwaiting()const
{
return (VIPcargolist.getCount() + normalcargolist.getCount() + specialcargolist.getCount());
}
int Company::getnumofdeliverd()const
{
return (VIPdeliverdcargolist.getCount()+normaldeliverdcargolist.getCount()+specialdeliverdcargolist.getCount());
}
int Company::getnumofloading()const
{
int num = 0;
if (viploadingtruck)
num++;
if (normalloadingtruck)
num++;
if (specialloadingtruck)
num++;
return num;
}
int Company::getnumofmoving()const
{
return movingtrucklist.getCount();
}
int Company::getnumofchecktrucks()const
{
return normalchecktrucklist.getCount()+specialchecktrucklist.getCount()+VIPchecktrucklist.getCount();
}
int Company::getnumofemptytrucks()const
{
return normaltrucklist.getCount()+specialtrucklist.getCount()+VIPtrucklist.getCount();
}
int Company::getnormalcargos() const
{
return normaldeliverdcargolist.getCount();
}
int Company::getspecialcargos() const
{
return specialdeliverdcargolist.getCount();
}
int Company::getVIPcargos() const
{
return VIPdeliverdcargolist.getCount();
}
perQueue<Cargo*>& Company::getVIPcargolist()
{
return VIPcargolist;
}
Queue<Cargo*>& Company::getnormalcargolist()
{
return normalcargolist;
}
Queue<Cargo*>& Company::getspecialcargolist()
{
return specialcargolist;
}
Queue<Cargo*>& Company::getVIPdeliverdcargolist()
{
return VIPdeliverdcargolist;
}
Queue<Cargo*>& Company::getnormaldeliverdcargolist()
{
return normaldeliverdcargolist;
}
Queue<Cargo*>& Company::getspecialdeliverdcargolist()
{
return specialdeliverdcargolist;
}
Queue<Truck*>& Company::getVIPtrucklist()
{
return VIPtrucklist;
}
Queue<Truck*>& Company::getnormaltrucklist()
{
return normaltrucklist;
}
Queue<Truck*>& Company::getspecialtrucklist()
{
return specialtrucklist;
}
perQueue<Truck*>& Company::getmovingtrucklist()
{
return movingtrucklist;
}
Queue<Truck*>& Company::getVIPchecktrucklist()
{
return VIPchecktrucklist;
}
Queue<Truck*>& Company::getnormalchecktrucklist()
{
return normalchecktrucklist;
}
Queue<Truck*>& Company::getspecialchecktrucklist()
{
return specialchecktrucklist;
}
void Company::setCargoType(char c)
{
if (c == 'N')
Cargotype = normal;
else if (c == 'S')
Cargotype = special;
else if (c == 'V')
Cargotype = VIP;
}
type Company::getCargoType()const
{
return Cargotype;
}
UI* Company::getUI()const
{
return pUI;
}
int Company::gettotalcargos()const
{
return normaldeliverdcargolist.getCount()+specialdeliverdcargolist.getCount()+VIPdeliverdcargolist.getCount();
}
int Company::gettotaltrucks() const
{
return Nnum+Snum+VIPnum;
}
int Company::getmovingcargosnum() const
{
return movingcargosnum;
}