-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoU.cpp
More file actions
4330 lines (4066 loc) · 202 KB
/
CoU.cpp
File metadata and controls
4330 lines (4066 loc) · 202 KB
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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Created by yunpa38 on 2022-10-12.
// Implementation for "Co1".
//
#include "CoU.h"
/*
* Used to store the scheduling information for each group
*/
struct GroupSchedulingStruct{
int left; // real left, but only for the first packet in the group
int right; // real right, but only for the first packet in the group
int bottom;
int height;
int period; // period of the packets
int startPacketIndex; // used to calculate the basic offset of the current group
int numberOfPackets; // covered number of packets in the group
};
/*
* A higher layer of structure to store the temporary scheduling information
* When trying to combine the two consecutive groups, use one "HyperGroupSchedulingStruct"
* to store the information for the current number of groups
* The selection of the combination having the smallest impacts on the scheduling is performed
* in the main process.
*/
struct HyperGroupSchedulingStruct{
vector<GroupSchedulingStruct> groupSchedulingList;
double objectiveFunctionValue;
bool reducedControl;
int gss1offset;
int gss2offset;
int gsscombinedossfet;
vector<pair<int, int>> PRUList;
};
/*
* For combination profit:
* - calculate the increase of PRUs
*/
int combinationPRUs(GroupSchedulingStruct& gssCombined,
GroupSchedulingStruct& gss1,
GroupSchedulingStruct& gss2,
ResourceGrid& rg){
int groupOffsetIndex, groupEndIndex, groupTop, groupPeriod, groupLeft, groupRight;
int numberOfWastedResourceUnits = 0; // return value; newly wasted resource units after combination
int combinationWastedResourceUnits = 0;
int previousWastedResourceUnits = 0; // for gss1 and gss2
int rowIndex, colIndex;
// calculate the previous wasted resource units [gss1 and gss2]
// for gss1
groupLeft = gss1.left;
groupRight = gss1.right;
groupTop = gss1.bottom;
groupPeriod = gss1.period;
for(int packets = 0; packets < gss1.numberOfPackets; packets++){
groupOffsetIndex = groupLeft + packets * groupPeriod;
groupEndIndex = groupRight + packets * groupPeriod;
for(rowIndex = 0; rowIndex < groupTop; rowIndex++){
for(colIndex = groupOffsetIndex; colIndex <= groupEndIndex; colIndex++){
if(rg.data[colIndex][rowIndex] == -1){
previousWastedResourceUnits++;
}
}
}
}
// gss2
groupLeft = gss2.left;
groupRight = gss2.right;
groupTop = gss2.bottom;
groupPeriod = gss2.period;
for(int packets = 0; packets < gss2.numberOfPackets; packets++){
groupOffsetIndex = groupLeft + packets * groupPeriod;
groupEndIndex = groupRight + packets * groupPeriod;
for(rowIndex = 0; rowIndex < groupTop; rowIndex++){
for(colIndex = groupOffsetIndex; colIndex <= groupEndIndex; colIndex++){
if(rg.data[colIndex][rowIndex] == -1){
previousWastedResourceUnits++;
}
}
}
}
// gssCombined
groupLeft = gssCombined.left;
groupRight = gssCombined.right;
groupTop = gssCombined.bottom;
groupPeriod = gssCombined.period;
for(int packets = 0; packets < gssCombined.numberOfPackets; packets++){
groupOffsetIndex = groupLeft + packets * groupPeriod;
groupEndIndex = groupRight + packets * groupPeriod;
for(rowIndex = 0; rowIndex < groupTop; rowIndex++){
for(colIndex = groupOffsetIndex; colIndex <= groupEndIndex; colIndex++){
if(rg.data[colIndex][rowIndex] == -1){
combinationWastedResourceUnits++;
}
}
}
}
// cout << "PRUs: combined: " << combinationWastedResourceUnits << " gss12: " << previousWastedResourceUnits<< endl;
return combinationWastedResourceUnits - previousWastedResourceUnits;
}
/*
* For combination profit:
* - calculate the increase of DRUs (for the increase of payload)
*/
int combinationDRUs(GroupSchedulingStruct& gssCombined,
GroupSchedulingStruct& gss1,
GroupSchedulingStruct& gss2){
int width, height, drugss1, drugss2, drucombined;
// DRU for gss 1
width = gss1.right - gss1.left + 1;
height = gss1.height;
drugss1 = width * height * gss1.numberOfPackets;
// DRUs for gss 2
width = gss2.right - gss2.left + 1;
height = gss2.height;
drugss2 = width * height * gss2.numberOfPackets;
// DRUs for combined
width = gssCombined.right - gssCombined.left + 1;
height = gssCombined.height;
drucombined = width * height * gssCombined.numberOfPackets;
return drucombined - (drugss1 + drugss2);
}
/*
* For combination profit:
* - calculate the decline of control message
* - if gss1 and gss2 can be represented by one configuration (must be the same with gss combined), return 0
* - otherwise, return the size of the control message
*
* The problem: how to identify two gss belonging to one configuration
* Idea: 1) offsets of gss1 and gss2 are the same with gsscombined
* 2) width
* 3) height
*
* return value:
* - true: the combination would reduce one control message (gss1 and gss2 can not be represented by 1 conf.)
* - false: the combination has no contribution to the reduction of control message
*
*/
bool combinationCRUs(GroupSchedulingStruct& gssCombined,
GroupSchedulingStruct& gss1,
GroupSchedulingStruct& gss2){
// width
int widthgss1 = gss1.right - gss1.left + 1;
int widthgss2 = gss2.right - gss2.left + 1;
int widthgsscombined = gssCombined.right - gssCombined.left + 1;
if(widthgss1!= widthgsscombined || widthgss2 != widthgsscombined){
return true;
}else{
// height
if(gss1.height != gssCombined.height || gss2.height != gssCombined.height){
return true;
}else{
// the offset of each data packet in gsscombined = ... in gss1 + ... in gss2
// gss1
for(int i = 0; i < gss1.numberOfPackets; i++){
int ogss1 = gss1.left + gss1.period * i;
int ogsscombined = gssCombined.left + gssCombined.period * i;
if(ogss1 != ogsscombined){
return true;
}
}
// gss2
for(int i = 0; i < gss2.numberOfPackets; i++){
int ogss2 = gss2.left + gss2.period * i;
int ogsscombined = gssCombined.left + gssCombined.period * (i + gss1.numberOfPackets);
if(ogss2 != ogsscombined){
return true;
}
}
return false;
}
}
}
/*
* assess the quality of the combination
* return value: - (decline of control message - increase of PRUs - increase of DRUs)
*/
double combinationObjectiveFunction(GroupSchedulingStruct& gssCombined,
GroupSchedulingStruct& gss1,
GroupSchedulingStruct& gss2,
ResourceGrid& rg,
int controlMessageSize,
double ratio_PRU){
// TODO: combination objective:
// 1) newly increased hard-utilized resource;
// 2) new increased wasted resources (payload)
// 3): the decrease of control message
int increaseOfPRUs = combinationPRUs(gssCombined, gss1, gss2, rg);
int increaseOfDRUs = combinationDRUs(gssCombined, gss1, gss2);
bool declineControlMessage = combinationCRUs(gssCombined, gss1, gss2);
// cout << " gss1 o: " << gss1.left << " w: " << gss1.right - gss1.left + 1 << " h: " << gss1.height << endl;
// cout << " gss2 o: " << gss2.left << " w: " << gss2.right - gss2.left + 1 << " h: " << gss2.height << endl;
// cout << " gssc o: " << gssCombined.left <<
// " w: " << gssCombined.right - gssCombined.left + 1 <<
// " h: " << gssCombined.height << endl;
// cout << "cov: "<< increaseOfPRUs << " " << increaseOfDRUs << endl;
if(declineControlMessage){
return increaseOfPRUs * ratio_PRU + increaseOfDRUs - controlMessageSize;
}else{
return increaseOfPRUs * ratio_PRU + increaseOfDRUs;
}
}
// 4) increase of RB
// 5) closeness between gssCombinedTop and currentRGTop
double combinationRBsV3(GroupSchedulingStruct& gssCombined,
GroupSchedulingStruct& gss1,
GroupSchedulingStruct& gss2,
int currentRGTop){
double gssTop = gssCombined.bottom + gssCombined.height;
// double gssPreviousTop1 = gss1.bottom + gss1.height;
// double gssPreviousTop2 = gss2.bottom + gss2.height;
if(gssTop > currentRGTop){
return RBIncreaseRatio*(gssTop - currentRGTop);
}
// else{
// // close to currentRGTop --> better --> smaller value
// return RBIncreaseRatio * (currentRGTop - gssTop);
// }
// if(gssTop > gssPreviousTop1){
// tmpIncreaseRB += gss1.numberOfPackets * (gssTop - gssPreviousTop1);
// }
// if(gssTop > gssPreviousTop2){
// tmpIncreaseRB += gss2.numberOfPackets * (gssTop - gssPreviousTop2);
// }
// cout << tmpIncreaseRB << endl;
}
double combinationObjectiveFunctionV3(GroupSchedulingStruct& gssCombined,
GroupSchedulingStruct& gss1,
GroupSchedulingStruct& gss2,
ResourceGrid& rg,
int controlMessageSize,
double ratio_PRU,
int currentRGTop){
// TODO: combination objective:
// 1) newly increased hard-utilized resource;
// 2) new increased wasted resources (payload)
// 3) the decrease of control message
// 4) increase of RB
// 5) closeness between gssCombinedTop and currentRGTop
int increaseOfPRUs = combinationPRUs(gssCombined, gss1, gss2, rg);
int increaseOfDRUs = combinationDRUs(gssCombined, gss1, gss2);
bool declineControlMessage = combinationCRUs(gssCombined, gss1, gss2);
// double RBs = combinationRBsV3(gssCombined, gss1, gss2, currentRGTop);
// cout << " gss1 o: " << gss1.left << " w: " << gss1.right - gss1.left + 1 << " h: " << gss1.height << endl;
// cout << " gss2 o: " << gss2.left << " w: " << gss2.right - gss2.left + 1 << " h: " << gss2.height << endl;
// cout << " gssc o: " << gssCombined.left <<
// " w: " << gssCombined.right - gssCombined.left + 1 <<
// " h: " << gssCombined.height << endl;
// cout << "cov: "<< increaseOfPRUs << " " << increaseOfDRUs << endl;
// if(declineControlMessage){
// return increaseOfPRUs * ratio_PRU + increaseOfDRUs - controlMessageSize + RBs;
// }else{
// return increaseOfPRUs * ratio_PRU + increaseOfDRUs + RBs;
// }
if(declineControlMessage){
return increaseOfPRUs * ratio_PRU + increaseOfDRUs - controlMessageSize;
}else{
return increaseOfPRUs * ratio_PRU + increaseOfDRUs;
}
// return RBs;
// return increaseofRB;
}
/*
* Function: for a given group scheduling list, return the objective function
* suppose that the scheduling has been applied
*/
//int groupSchedulingObjectiveFunction(vector<GroupSchedulingStruct>& groupSchedulingList,
// ResourceGrid& rg, TrafficFlow& tf){
// int groupOffsetIndex, groupEndIndex, groupTop, groupPeriod, groupPackets, groupLeft, groupRight;
// int rowIndex, colIndex;
// int wastageResourceUnits = 0;
//
// for(auto gss: groupSchedulingList){
// // calculate the objective function
//
// groupLeft = gss.left;
// groupRight = gss.right;
// groupTop = gss.bottom;
// groupPackets = gss.numberOfPackets;
// groupPeriod = gss.period;
//
// for(int packets = 0; packets < groupPackets; packets++){
// groupOffsetIndex = groupLeft + packets * groupPeriod;
// groupEndIndex = groupRight + packets * groupPeriod;
// // resource units wastage...
// for(rowIndex = 0; rowIndex < groupTop; rowIndex++){
// for(colIndex = groupOffsetIndex; colIndex <= groupEndIndex; colIndex++){
// if(rg.data[colIndex][rowIndex] == -1){
// wastageResourceUnits++;
// }
// }
// }
// }
//
// }
// // calculate the sum of wasted resource units and control messages
// // TODO: balance the wastage of resource units and the involved control overhead
// // TODO: the increase of the resource units larger than the payload
//
// return wastageResourceUnits + (groupSchedulingList.size()-1) * tf.controlOverhead;
//}
/*
* FUNCTION: perform CoN algorithm (with higher degree of exploration using combination based approach)
* compare to V0, use Co1 to replace CoU at the very beginning (the schedule information for each packet)
*/
void subCoNV1(TrafficFlow& tf, ResourceGrid& rg, double rwi, double ratio_PRU){
std::vector<std::pair<int, int>> schedulingIntervals;
for(int i = 0; i < rg.numberOfSlots / tf.transmissionPeriod; i++){
schedulingIntervals.emplace_back(
std::make_pair(ceil(tf.initialOffset + tf.transmissionPeriod * i),
floor(tf.initialOffset +tf.transmissionPeriod * i + tf.latencyRequirement - 1)));
}
// for(auto si: schedulingIntervals){
// std::cout << si.first << " " << si.second << std::endl;
// }
tf.schedulingInterval = schedulingIntervals;
vector<GroupSchedulingStruct> groupSchedulingList;
vector<HyperGroupSchedulingStruct> hyperSchedulingList;
int packetIndex = 0;
rwi = 1;
// step 1: for all packets, group size = 1, find the most appropriate location for all packets
// this is the optimal solution [the highest resource efficiency]
// auto startTime_initialCo1 = std::chrono::steady_clock::now();
for(auto intervals: schedulingIntervals) {
// Initialization of the current group scheduling struct
// cout << "intervals..." << endl;
GroupSchedulingStruct gss{};
gss.left = 0;
gss.right = 0;
gss.bottom = 0;
gss.height = 999999;
gss.period = tf.transmissionPeriod;
gss.numberOfPackets = 1;
gss.startPacketIndex = packetIndex;
packetIndex++;
int lowestSettingForAllFlat [] = {0, 0, 0, 99999};
// width max
int WidthMax;
int freeSpace = intervals.second - intervals.first + 1;
int h = ceil((double) tf.payload / freeSpace);
if (h == 1) {
WidthMax = tf.payload;
} else {
WidthMax = intervals.second - intervals.first + 1;
}
// FUNCTION: calculate the minimum height for all shapes w.r.t the current flat
int offsetMin = intervals.first;
int offsetMax = intervals.second;
int bottomMin = 0;
int widthMax = WidthMax;
// cout << "Width: " << widthMax << endl;
int transmissionPeriod = tf.transmissionPeriod;
for (int alternativeWidth = 1; alternativeWidth <= widthMax; alternativeWidth++) {
int tmpHeight = (int) ceil((double) tf.payload / alternativeWidth);
// cout << "Height: " << tmpHeight << endl;
if(tmpHeight >= rg.numberOfPilots) continue;
int tmpOffsetMax = offsetMax - alternativeWidth + 1;
for (int alternativeOffset = tmpOffsetMax; alternativeOffset >= offsetMin; alternativeOffset--) {
for (int alternativeBottom = bottomMin; alternativeBottom < rg.numberOfPilots; alternativeBottom++) {
// check collision for all packets
bool allPacketsSuccessFlag = false;
for (int packets = 0; packets < 1; packets++) {
int packetLeft = alternativeOffset + transmissionPeriod * packets;
int packetRight = alternativeOffset + alternativeWidth - 1 + transmissionPeriod * packets;
int packetBottom = alternativeBottom;
int packetHeight = tmpHeight;
// collision check for one packet
bool collisionFlag = false;
for (int col = packetLeft; col <= packetRight; col++) {
for (int row = packetBottom; row < packetBottom + packetHeight; row++) {
if (rg.data[col][row] != -1) {
// collision occurs
collisionFlag = true;
break;
}
}
}
if (collisionFlag) {
// collision happened
break;
}
if (packets == 0) {
allPacketsSuccessFlag = true;
}
}
if (allPacketsSuccessFlag) {
// compare, keep the minimum one
if (alternativeBottom + tmpHeight < lowestSettingForAllFlat[2] + lowestSettingForAllFlat[3]) {
lowestSettingForAllFlat[0] = alternativeOffset;
lowestSettingForAllFlat[1] = alternativeOffset + alternativeWidth - 1;
lowestSettingForAllFlat[2] = alternativeBottom;
lowestSettingForAllFlat[3] = tmpHeight;
}
break;
}
}
}
}
gss.left = lowestSettingForAllFlat[0];
gss.right = lowestSettingForAllFlat[1];
gss.bottom = lowestSettingForAllFlat[2];
gss.height = lowestSettingForAllFlat[3];
// cout << "l: " << gss.left << " r: " << gss.right << " b: " << gss.bottom << " h: " << gss.height << endl;
groupSchedulingList.push_back(gss);
}
// initial part -- has impact, but no big
// auto startTime_interation = std::chrono::steady_clock::now();
// int groupSchedulingObjectiveFunctionValue = groupSchedulingObjectiveFunction(groupSchedulingList, rg, tf);
double scheduleProfit = 0;
hyperSchedulingList.push_back(
{groupSchedulingList, scheduleProfit});
// cout << "step 2" << endl;
/*
* Step 2: try to combine two adjacent groups, and find the most efficient combination, then store
* the newly generated scheduling info to the hyperSchedulingList
* The current scheduling info is in groupSchedulingList
*
* IMPORTANT: establish the group combination objective function
* [wasted resource units -- hard to utilize in the future]
*/
int totalNumberOfPackets = tf.numberOfConfigurations;
// n groups --> n-1 groups --> n-2 groups --> .... --> 1 groups
// total n-1 hyper scheduling info
for (int hyperIteration = totalNumberOfPackets - 1; hyperIteration >= 1; hyperIteration--) {
// cout << "\n hyper iteration.." << hyperIteration << endl;
// for each hyper-iteration, try to select the most appropriate combination of groups
// cout << "----------new combination loop----------------" << endl;
int minGroupIndex = -1; // record the index of the first group in the combination, the next is trivial
double minCombinationObjectiveValue = 99999;
int minGss1Offset, minGss2Offset, minGssCombinedOffset;
bool minReduceControl;
GroupSchedulingStruct minGss{};
for (int combinationIndex = 0; combinationIndex < hyperIteration; combinationIndex++) {
// cout << "combinationIndex: " << combinationIndex << endl;
// try to combine groups, assess the combination objective function, and select one
int startPacketIndex = groupSchedulingList[combinationIndex].startPacketIndex;
int numberOfCoveredPackets = groupSchedulingList[combinationIndex].numberOfPackets +
groupSchedulingList[combinationIndex + 1].numberOfPackets;
// try to combine....
GroupSchedulingStruct gss{};
gss.startPacketIndex = startPacketIndex;
gss.numberOfPackets = numberOfCoveredPackets;
int scheduleIntervalStart = schedulingIntervals[0].first;
int scheduleIntervalEnd = schedulingIntervals[0].second;
// for each flat, calculate the minimal height to identify the shape
// Format: left, right, bottom, height, period
int lowestSettingForAllFlat[] = {0, 0, 0, 99999, 0};
// width max
int WidthMax;
int freeSpace = scheduleIntervalEnd - scheduleIntervalStart + 1;
int h = ceil((double) tf.payload / freeSpace);
if (h == 1) {
WidthMax = tf.payload;
} else {
WidthMax = scheduleIntervalEnd - scheduleIntervalStart + 1;
}
// FUNCTION: calculate the minimum height for all shapes w.r.t the current flat
int offsetMin = scheduleIntervalStart;
int offsetMax = scheduleIntervalEnd;
int bottomMin = 0;
int widthMax;
int theoritcalWidth = (int) ceil(rwi * (double) tf.payload);
if (WidthMax <= theoritcalWidth) widthMax = WidthMax;
else widthMax = theoritcalWidth;
// cout << "Width: " << widthMax << endl;
int transmissionPeriod = tf.transmissionPeriod;
int startPacket = 0;
int endPacket = numberOfCoveredPackets;
int numberOfPacketsInTheSegment = numberOfCoveredPackets;
// record the number of packets been assigned resources
int assignedNumberOfPackets = startPacketIndex;
for (int alternativeWidth = 1; alternativeWidth <= widthMax; alternativeWidth++) {
int tmpHeight = (int) ceil((double) tf.payload / alternativeWidth);
// cout << "Height: " << tmpHeight << endl;
int tmpOffsetMax = offsetMax - alternativeWidth + 1;
for (int alternativeOffset = tmpOffsetMax; alternativeOffset >= offsetMin; alternativeOffset--) {
// decide the period of packets
int minP = transmissionPeriod -
(int) floor((double) (alternativeOffset - offsetMin) / numberOfPacketsInTheSegment);
int maxP = transmissionPeriod + (int) floor(
(double) (offsetMax - (alternativeOffset + alternativeWidth - 1)) /
numberOfPacketsInTheSegment);
for (int alternativePeriod = minP; alternativePeriod <= maxP; alternativePeriod++) {
for (int alternativeBottom = bottomMin;
alternativeBottom < rg.numberOfPilots; alternativeBottom++) {
// check collision for all packets
bool allPacketsSuccessFlag = false;
for (int packets = startPacket; packets < endPacket; packets++) {
int packetLeft = assignedNumberOfPackets * transmissionPeriod
+ alternativeOffset + alternativePeriod * packets;
int packetRight = assignedNumberOfPackets * transmissionPeriod +
alternativeOffset + alternativeWidth - 1 +
alternativePeriod * packets;
int packetBottom = alternativeBottom;
int packetHeight = tmpHeight;
// collision check for one packet
bool collisionFlag = false;
for (int col = packetLeft; col <= packetRight; col++) {
for (int row = packetBottom; row < packetBottom + packetHeight; row++) {
if (rg.data[col][row] != -1) {
// collision occurs
// cout << "collision: row: " << row << " col: " << col << endl;
// rg.printData();
collisionFlag = true;
break;
}
}
if (collisionFlag) {
// collision happened
break;
}
}
if (collisionFlag) {
// collision happened
break;
}
if (packets == endPacket - 1) {
allPacketsSuccessFlag = true;
}
}
if (allPacketsSuccessFlag) {
// compare, keep the minimum one
if (alternativeBottom + tmpHeight <
lowestSettingForAllFlat[2] + lowestSettingForAllFlat[3]) {
lowestSettingForAllFlat[0] = assignedNumberOfPackets * transmissionPeriod
+ alternativeOffset;
lowestSettingForAllFlat[1] = assignedNumberOfPackets * transmissionPeriod
+ alternativeOffset + alternativeWidth - 1;
lowestSettingForAllFlat[2] = alternativeBottom;
lowestSettingForAllFlat[3] = tmpHeight;
lowestSettingForAllFlat[4] = alternativePeriod;
// update gss
gss.left = lowestSettingForAllFlat[0];
gss.right = lowestSettingForAllFlat[1];
gss.bottom = lowestSettingForAllFlat[2];
gss.height = lowestSettingForAllFlat[3];
gss.period = lowestSettingForAllFlat[4];
}
break;
}
}
}
}
}
// obtain a new group scheduling structure, calculate the combination objective function
// cout << "pre object value..." << endl;
double combinationProfit =
combinationObjectiveFunction(gss,
groupSchedulingList[combinationIndex],
groupSchedulingList[combinationIndex + 1],
rg,
tf.controlOverhead,
ratio_PRU);
bool currentReducedCURs = combinationCRUs(gss,
groupSchedulingList[combinationIndex],
groupSchedulingList[combinationIndex + 1]);
// cout << "combination objective value: " << combinationProfit
// << "\t" << currentReducedCURs << endl;
if (combinationProfit < minCombinationObjectiveValue) {
// record the current combination
minCombinationObjectiveValue = combinationProfit;
minGroupIndex = combinationIndex;
minGss = gss;
minGss1Offset = groupSchedulingList[combinationIndex].left;
minGss2Offset = groupSchedulingList[combinationIndex + 1].left;
minGssCombinedOffset = gss.left;
minReduceControl = currentReducedCURs;
}
}
// obtain the current optimal combination, update the current group scheduling list
// cout << "original list: " << endl;
// for (auto ele: groupSchedulingList){
// cout << ele.left << " ";
// }
// cout << endl;
groupSchedulingList.erase(groupSchedulingList.begin() + minGroupIndex);
// cout << "delete first..." << endl;
// for (auto ele: groupSchedulingList){
// cout << ele.left << " ";
// }
// cout << endl;
groupSchedulingList.erase(groupSchedulingList.begin() + minGroupIndex);
// cout << "delete second..." << endl;
// for (auto ele: groupSchedulingList){
// cout << ele.left << " ";
// }
// cout << endl;
groupSchedulingList.insert(groupSchedulingList.begin() + minGroupIndex, minGss);
// cout << "add new one..." << endl;
// for (auto ele: groupSchedulingList){
// cout << ele.left << " ";
// }
// cout << endl;
//groupSchedulingObjectiveFunctionValue = groupSchedulingObjectiveFunction(groupSchedulingList, rg, tf);
scheduleProfit += minCombinationObjectiveValue;
hyperSchedulingList.push_back(
{groupSchedulingList, scheduleProfit,
minReduceControl,
minGss1Offset,
minGss2Offset,
minGssCombinedOffset});
// cout << "group scheduling objective function: " << groupSchedulingObjectiveFunctionValue << endl;
}
// auto endTime = std::chrono::steady_clock::now();
// int elapsedTimeInMilliseconds =
// std::chrono::duration_cast<std::chrono::microseconds>(endTime - startTime_interation).count();
// cout << "iteration part: " << elapsedTimeInMilliseconds << endl;
// update part: pruRatio = 0, consume less time, due to fewer configurations
// cout << "Having all hyper scheduling list:..." << endl;
// select the one with the smallest objective function
int minHyperSchedulingIndex = 0, minHyperSchedulingObjectiveValue = 999999999;
for (int hyperSchedulingIndex = 0; hyperSchedulingIndex < hyperSchedulingList.size(); hyperSchedulingIndex++) {
// cout << "size of group scheduling.." << hyperSchedulingList[hyperSchedulingIndex].groupSchedulingList.size() << endl;
if (hyperSchedulingList[hyperSchedulingIndex].objectiveFunctionValue < minHyperSchedulingObjectiveValue) {
minHyperSchedulingIndex = hyperSchedulingIndex;
minHyperSchedulingObjectiveValue = hyperSchedulingList[hyperSchedulingIndex].objectiveFunctionValue;
}
}
int numberOfActualConfigurations = 1;
set<int> configurationOffsets;
// cout << "hyperschedule size: " << hyperSchedulingList.size() << " min index: " << minHyperSchedulingIndex << endl;
for(int iSchedules = hyperSchedulingList.size() - 1; iSchedules > minHyperSchedulingIndex; iSchedules--){
if(hyperSchedulingList[iSchedules].reducedControl){
configurationOffsets.insert(hyperSchedulingList[iSchedules].gss1offset);
configurationOffsets.insert(hyperSchedulingList[iSchedules].gss2offset);
// cout << "add offsets: " << hyperSchedulingList[iSchedules].gss1offset
// << " " << hyperSchedulingList[iSchedules].gss2offset << endl;
numberOfActualConfigurations++;
}else{
configurationOffsets.insert(hyperSchedulingList[iSchedules].gsscombinedossfet);
}
}
configurationOffsets.insert(hyperSchedulingList[minHyperSchedulingIndex].gsscombinedossfet);
// cout << "min schedule index: " << minHyperSchedulingIndex << endl;
// cout << "Number of configurations: " << numberOfActualConfigurations << endl;
// update the resource grid
// cout << "size of set: " << configurationOffsets.size() << endl;
int groupOffsetIndex, groupEndIndex, groupPeriod, groupPackets, groupLeft, groupRight;
tf.numberOfConfigurationActual = numberOfActualConfigurations - 1;
tf.offsetConfigurationsActual.assign(configurationOffsets.begin(), configurationOffsets.end());
sort(tf.offsetConfigurationsActual.begin(), tf.offsetConfigurationsActual.end());
tf.offsetConfigurationsActual.erase(tf.offsetConfigurationsActual.begin());
int gssIndex = 0;
for (auto gss: hyperSchedulingList[minHyperSchedulingIndex].groupSchedulingList) {
groupLeft = gss.left;
groupRight = gss.right;
groupPackets = gss.numberOfPackets;
groupPeriod = gss.period;
// if (gssIndex > 0) {
// tf.offsetConfigurationsActual.push_back(groupLeft);
// }
gssIndex++;
// cout << "aaa" << endl;
for (int packets = 0; packets < groupPackets; packets++) {
groupOffsetIndex = groupLeft + packets * groupPeriod;
groupEndIndex = groupRight + packets * groupPeriod;
rg.update(groupOffsetIndex, groupEndIndex, gss.bottom, gss.height, tf.number);
scheduleInfoElement s{groupOffsetIndex, groupEndIndex, gss.bottom, gss.height};
tf.schedulingInfo.push_back(s);
// rg.printData();
}
// cout << "bbb" << endl;
}
}
void subCoNV3(TrafficFlow& tf, ResourceGrid& rg, double rwi, double ratio_PRU, int& currentRGTop, std::ofstream& outlog, int tfnumber){
std::vector<std::pair<int, int>> schedulingIntervals;
for(int i = 0; i < rg.numberOfSlots / tf.transmissionPeriod; i++){
schedulingIntervals.emplace_back(
std::make_pair(ceil(tf.initialOffset + tf.transmissionPeriod * i),
floor(tf.initialOffset +tf.transmissionPeriod * i + tf.latencyRequirement - 1)));
}
// for(auto si: schedulingIntervals){
// std::cout << si.first << " " << si.second << std::endl;
// }
tf.schedulingInterval = schedulingIntervals;
vector<GroupSchedulingStruct> groupSchedulingList;
vector<HyperGroupSchedulingStruct> hyperSchedulingList;
int packetIndex = 0;
rwi = 1;
// step 1: for all packets, group size = 1, find the most appropriate location for all packets
// this is the optimal solution [the highest resource efficiency]
// auto startTime_initialCo1 = std::chrono::steady_clock::now();
for(auto intervals: schedulingIntervals) {
// Initialization of the current group scheduling struct
// cout << "intervals..." << endl;
GroupSchedulingStruct gss{};
gss.left = 0;
gss.right = 0;
gss.bottom = 0;
gss.height = 999999;
gss.period = tf.transmissionPeriod;
gss.numberOfPackets = 1;
gss.startPacketIndex = packetIndex;
packetIndex++;
int lowestSettingForAllFlat [] = {0, 0, 0, 99999};
// width max
int WidthMax;
int freeSpace = intervals.second - intervals.first + 1;
int h = ceil((double) tf.payload / freeSpace);
if (h == 1) {
WidthMax = tf.payload;
} else {
WidthMax = intervals.second - intervals.first + 1;
}
// FUNCTION: calculate the minimum height for all shapes w.r.t the current flat
int offsetMin = intervals.first;
int offsetMax = intervals.second;
int bottomMin = 0;
int widthMax = WidthMax;
// cout << "Width: " << widthMax << endl;
int transmissionPeriod = tf.transmissionPeriod;
for (int alternativeWidth = 1; alternativeWidth <= widthMax; alternativeWidth++) {
int tmpHeight = (int) ceil((double) tf.payload / alternativeWidth);
// cout << "Height: " << tmpHeight << endl;
if(tmpHeight >= rg.numberOfPilots) continue;
int tmpOffsetMax = offsetMax - alternativeWidth + 1;
for (int alternativeOffset = tmpOffsetMax; alternativeOffset >= offsetMin; alternativeOffset--) {
for (int alternativeBottom = bottomMin; alternativeBottom < rg.numberOfPilots; alternativeBottom++) {
// check collision for all packets
bool allPacketsSuccessFlag = false;
for (int packets = 0; packets < 1; packets++) {
int packetLeft = alternativeOffset + transmissionPeriod * packets;
int packetRight = alternativeOffset + alternativeWidth - 1 + transmissionPeriod * packets;
int packetBottom = alternativeBottom;
int packetHeight = tmpHeight;
// collision check for one packet
bool collisionFlag = false;
for (int col = packetLeft; col <= packetRight; col++) {
for (int row = packetBottom; row < packetBottom + packetHeight; row++) {
if (rg.data[col][row] != -1) {
// collision occurs
collisionFlag = true;
break;
}
}
}
if (collisionFlag) {
// collision happened
break;
}
if (packets == 0) {
allPacketsSuccessFlag = true;
}
}
if (allPacketsSuccessFlag) {
// compare, keep the minimum one
if (alternativeBottom + tmpHeight < lowestSettingForAllFlat[2] + lowestSettingForAllFlat[3]) {
lowestSettingForAllFlat[0] = alternativeOffset;
lowestSettingForAllFlat[1] = alternativeOffset + alternativeWidth - 1;
lowestSettingForAllFlat[2] = alternativeBottom;
lowestSettingForAllFlat[3] = tmpHeight;
}
break;
}
}
}
}
gss.left = lowestSettingForAllFlat[0];
gss.right = lowestSettingForAllFlat[1];
gss.bottom = lowestSettingForAllFlat[2];
gss.height = lowestSettingForAllFlat[3];
// cout << "l: " << gss.left << " r: " << gss.right << " b: " << gss.bottom << " h: " << gss.height << endl;
if(gss.bottom+gss.height > currentRGTop){
currentRGTop = gss.bottom + gss.height;
}
groupSchedulingList.push_back(gss);
// update the current top index of RBs
}
int groupOffsetIndex, groupEndIndex, groupPeriod, groupPackets, groupLeft, groupRight;
if(output_enabled){
// update co1 per packet
for (auto gss: groupSchedulingList) {
groupLeft = gss.left;
groupRight = gss.right;
groupPackets = gss.numberOfPackets;
groupPeriod = gss.period;
for (int packets = 0; packets < groupPackets; packets++) {
groupOffsetIndex = groupLeft + packets * groupPeriod;
groupEndIndex = groupRight + packets * groupPeriod;
rg.update(groupOffsetIndex, groupEndIndex, gss.bottom, gss.height, tf.number);
}
}
outlog << char('A' + tfnumber) << " co1 per packet" << endl;
int i, j;
for(i = 0;i <= rg.numberOfPilots - 1; i++){
for(j=0;j<rg.numberOfSlots;j++){
char parsed;
if(rg.data[j][i] == -1){
parsed = '0';
}else{
parsed = 'A'+rg.data[j][i];
}
outlog << parsed << " ";
}
outlog << std::endl;
}
outlog << endl << endl << endl << endl;
// rewind the update of rg
for (auto gss: groupSchedulingList) {
groupLeft = gss.left;
groupRight = gss.right;
groupPackets = gss.numberOfPackets;
groupPeriod = gss.period;
for (int packets = 0; packets < groupPackets; packets++) {
groupOffsetIndex = groupLeft + packets * groupPeriod;
groupEndIndex = groupRight + packets * groupPeriod;
rg.update(groupOffsetIndex, groupEndIndex, gss.bottom, gss.height, -1);
}
}
}
// initial part -- has impact, but no big
// auto startTime_interation = std::chrono::steady_clock::now();
// int groupSchedulingObjectiveFunctionValue = groupSchedulingObjectiveFunction(groupSchedulingList, rg, tf);
double scheduleProfit = 0;
hyperSchedulingList.push_back(
{groupSchedulingList, scheduleProfit});
// cout << "step 2" << endl;
/*
* Step 2: try to combine two adjacent groups, and find the most efficient combination, then store
* the newly generated scheduling info to the hyperSchedulingList
* The current scheduling info is in groupSchedulingList
*
* IMPORTANT: establish the group combination objective function
* [wasted resource units -- hard to utilize in the future]
*/
int totalNumberOfPackets = tf.numberOfConfigurations;
// n groups --> n-1 groups --> n-2 groups --> .... --> 1 groups
// total n-1 hyper scheduling info
for (int hyperIteration = totalNumberOfPackets - 1; hyperIteration >= 1; hyperIteration--) {
// cout << "\n hyper iteration.." << hyperIteration << endl;
// for each hyper-iteration, try to select the most appropriate combination of groups
// cout << "----------new combination loop----------------" << endl;
int minGroupIndex = -1; // record the index of the first group in the combination, the next is trivial
double minCombinationObjectiveValue = 99999;
int minGss1Offset, minGss2Offset, minGssCombinedOffset;
bool minReduceControl;
GroupSchedulingStruct minGss{};
for (int combinationIndex = 0; combinationIndex < hyperIteration; combinationIndex++) {
// cout << "combinationIndex: " << combinationIndex << endl;
// try to combine groups, assess the combination objective function, and select one
int startPacketIndex = groupSchedulingList[combinationIndex].startPacketIndex;
int numberOfCoveredPackets = groupSchedulingList[combinationIndex].numberOfPackets +
groupSchedulingList[combinationIndex + 1].numberOfPackets;
// try to combine....
GroupSchedulingStruct gss{};
gss.startPacketIndex = startPacketIndex;
gss.numberOfPackets = numberOfCoveredPackets;
int scheduleIntervalStart = schedulingIntervals[0].first;
int scheduleIntervalEnd = schedulingIntervals[0].second;
// for each flat, calculate the minimal height to identify the shape
// Format: left, right, bottom, height, period
int lowestSettingForAllFlat[] = {0, 0, 0, 99999, 0};
// width max
int WidthMax;
int freeSpace = scheduleIntervalEnd - scheduleIntervalStart + 1;
int h = ceil((double) tf.payload / freeSpace);
if (h == 1) {
WidthMax = tf.payload;
} else {
WidthMax = scheduleIntervalEnd - scheduleIntervalStart + 1;
}
// FUNCTION: calculate the minimum height for all shapes w.r.t the current flat
int offsetMin = scheduleIntervalStart;
int offsetMax = scheduleIntervalEnd;
int bottomMin = 0;
int widthMax;
int theoritcalWidth = (int) ceil(rwi * (double) tf.payload);
if (WidthMax <= theoritcalWidth) widthMax = WidthMax;
else widthMax = theoritcalWidth;
// cout << "Width: " << widthMax << endl;
int transmissionPeriod = tf.transmissionPeriod;
int startPacket = 0;
int endPacket = numberOfCoveredPackets;
int numberOfPacketsInTheSegment = numberOfCoveredPackets;
// record the number of packets been assigned resources
int assignedNumberOfPackets = startPacketIndex;
for (int alternativeWidth = 1; alternativeWidth <= widthMax; alternativeWidth++) {
int tmpHeight = (int) ceil((double) tf.payload / alternativeWidth);
// cout << "Height: " << tmpHeight << endl;
int tmpOffsetMax = offsetMax - alternativeWidth + 1;
for (int alternativeOffset = tmpOffsetMax; alternativeOffset >= offsetMin; alternativeOffset--) {
// decide the period of packets
int minP = transmissionPeriod -
(int) floor((double) (alternativeOffset - offsetMin) / numberOfPacketsInTheSegment);
int maxP = transmissionPeriod + (int) floor(
(double) (offsetMax - (alternativeOffset + alternativeWidth - 1)) /
numberOfPacketsInTheSegment);
for (int alternativePeriod = minP; alternativePeriod <= maxP; alternativePeriod++) {
for (int alternativeBottom = bottomMin;
alternativeBottom < rg.numberOfPilots; alternativeBottom++) {
// check collision for all packets
bool allPacketsSuccessFlag = false;
for (int packets = startPacket; packets < endPacket; packets++) {
int packetLeft = assignedNumberOfPackets * transmissionPeriod
+ alternativeOffset + alternativePeriod * packets;
int packetRight = assignedNumberOfPackets * transmissionPeriod +
alternativeOffset + alternativeWidth - 1 +
alternativePeriod * packets;
int packetBottom = alternativeBottom;
int packetHeight = tmpHeight;
// collision check for one packet
bool collisionFlag = false;
for (int col = packetLeft; col <= packetRight; col++) {
for (int row = packetBottom; row < packetBottom + packetHeight; row++) {
if (rg.data[col][row] != -1) {
// collision occurs
// cout << "collision: row: " << row << " col: " << col << endl;
// rg.printData();
collisionFlag = true;
break;
}
}
if (collisionFlag) {
// collision happened
break;
}
}
if (collisionFlag) {
// collision happened
break;
}
if (packets == endPacket - 1) {
allPacketsSuccessFlag = true;
}
}
if (allPacketsSuccessFlag) {
// compare, keep the minimum one
if (alternativeBottom + tmpHeight <
lowestSettingForAllFlat[2] + lowestSettingForAllFlat[3]) {
lowestSettingForAllFlat[0] = assignedNumberOfPackets * transmissionPeriod
+ alternativeOffset;
lowestSettingForAllFlat[1] = assignedNumberOfPackets * transmissionPeriod
+ alternativeOffset + alternativeWidth - 1;
lowestSettingForAllFlat[2] = alternativeBottom;
lowestSettingForAllFlat[3] = tmpHeight;
lowestSettingForAllFlat[4] = alternativePeriod;
// update gss
gss.left = lowestSettingForAllFlat[0];
gss.right = lowestSettingForAllFlat[1];
gss.bottom = lowestSettingForAllFlat[2];
gss.height = lowestSettingForAllFlat[3];
gss.period = lowestSettingForAllFlat[4];
}
break;
}
}
}
}
}
// obtain a new group scheduling structure, calculate the combination objective function
// cout << "pre object value..." << endl;
double combinationProfit =