-
Notifications
You must be signed in to change notification settings - Fork 4
/
CSMatrix.cpp
2271 lines (1771 loc) · 68.8 KB
/
CSMatrix.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
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
#include "CSMatrix.h"
#define ENTRY_A 1
#define ENTRY_B -1
#ifdef __MACH__
#include <mach/clock.h>
#include <mach/mach.h>
#endif
void current_utc_time(struct timespec *ts) {
#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
ts->tv_sec = mts.tv_sec;
ts->tv_nsec = mts.tv_nsec;
#else
clock_gettime(CLOCK_REALTIME, ts);
#endif
}
CSMatrix::CSMatrix(LocatingArray *locatingArray) {
this->locatingArray = locatingArray;
// assign factor data variable for use with grabbing column names
this->factorData = locatingArray->getFactorData();
// to be used when populating CSMatrix
CSCol *csCol;
int sum;
// get number of factors in locating array
int factors = locatingArray->getFactors();
// get level counts (how many levels for each factor)
groupingInfo = locatingArray->getGroupingInfo();
// get the level matrix from locating array (this is the main data)
char **levelMatrix = locatingArray->getLevelMatrix();
// a row for every test
rows = locatingArray->getTests();
// initialize the data column vector
data = new vector<CSCol*>;
// initialize (factor + level)-to-index map
factorLevelMap = new int*[factors];
for (int factor_i = 0; factor_i < factors; factor_i++) {
factorLevelMap[factor_i] = new int[groupingInfo[factor_i]->levels];
}
// initialize index-to-column map (to be populated later)
mapping = new Mapping;
mapping->mappedTo = 0;
mapping->mapping = NULL;
// sum of squares of each column for normalization
vector <float>sumOfSquares;
// populate compressive sensing matrix column by column
int col_i = 0;
// Add the INTERCEPT
csCol = new CSCol;
sum = 0;
csCol->factors = 0;
csCol->setting = new FactorSetting[0];
// populate the intercept
for (int row_i = 0; row_i < rows; row_i++) {
addRow(csCol);
csCol->dataP[row_i] = ENTRY_A;
sum += 1;
}
// push into vector
data->push_back(csCol);
sumOfSquares.push_back(sum);
col_i++;
// go over all 1-way interactions
for (char factor_i = 0; factor_i < factors; factor_i++) {
for (char level_i = 0; level_i < groupingInfo[factor_i]->levels; level_i++) {
addOneWayInteraction(factor_i, level_i, levelMatrix, sumOfSquares);
// set factor level map
factorLevelMap[factor_i][level_i] = col_i - 1; // subtract 1 for INTERCEPT
// increment column index
col_i++;
}
}
int lastOneWay_i = col_i;
// initialize 1st level mapping
mapping->mapping = new Mapping*[col_i - 1];
cout << "Adding t-way interactions" << endl;
addTWayInteractions(csCol, col_i - 1, col_i, locatingArray->getT(),
mapping->mapping, sumOfSquares, groupingInfo, levelMatrix);
cout << "Went over " << col_i << " columns" << endl;
// check coverability
for (int col_i = 0; col_i < getCols(); col_i++) {
csCol = data->at(col_i);
csCol->coverable = checkColumnCoverability(csCol);
if (!csCol->coverable) {
cout << "Not coverable: " << getColName(csCol) << endl;
}
}
// perform sqaure roots on sum of squares
for (int col_i = 0; col_i < getCols(); col_i++) {
sumOfSquares[col_i] = sqrt(sumOfSquares[col_i]);
}
// normalize all columns
for (int col_i = 0; col_i < getCols(); col_i++) {
csCol = data->at(col_i);
for (int row_i = 0; row_i < getRows(); row_i++) {
// csCol->data[row_i] = csCol->data[row_i] / sumOfSquares[col_i];
}
data->at(col_i) = csCol;
}
cout << "Finished constructing CS Matrix" << endl;
}
void CSMatrix::addRow(CSCol *csCol) {
csCol->dataVector.push_back(0);
csCol->dataP = &csCol->dataVector[0];
}
void CSMatrix::remRow(CSCol *csCol) {
csCol->dataVector.pop_back();
csCol->dataP = &csCol->dataVector[0];
}
void CSMatrix::addTWayInteractions(CSCol *csColA, int colBMax_i, int &col_i, int t,
Mapping **mapping, vector <float>&sumOfSquares, GroupingInfo **groupingInfo, char **levelMatrix) {
CSCol *csCol, *csColB, *csColC;
int sum;
// the offset is 1 for the INTERCEPT
int colBOffset = 1;
int colCMax_i = 0;
// start after INTERCEPT, iterate over all main effects until colBMax_i
for (int colB_i = 0; colB_i < colBMax_i; colB_i++) {
// grab pointer to column
csColB = data->at(colB_i + colBOffset);
// the next 2 lines move colCMax_i to the 1st column with the same factor as csColB
csColC = data->at(colCMax_i + colBOffset);
if (csColB->setting[0].factor_i > csColC->setting[0].factor_i) colCMax_i = colB_i;
// set mapping for this column
mapping[colB_i] = new Mapping;
mapping[colB_i]->mapping = (t > 1 ? new Mapping*[colCMax_i] : NULL);
mapping[colB_i]->mappedTo = (csColA->factors > 0 ? col_i : colB_i + colBOffset);
Mapping *groupMapping = mapping[colB_i];
int colsInGroupB = 1;
char groupIndexB = -1;
char levelIndexB = csColB->setting[0].index;
// check if this column is grouped
if (groupingInfo[csColB->setting[0].factor_i]->grouped) {
groupIndexB = groupingInfo[csColB->setting[0].factor_i]->levelGroups[levelIndexB];
for (int level_i = levelIndexB + 1;
level_i < groupingInfo[csColB->setting[0].factor_i]->levels &&
groupingInfo[csColB->setting[0].factor_i]->levelGroups[level_i] == groupIndexB; level_i++) {
colsInGroupB++;
colB_i++;
// set to group mapping unless main effect (no grouping on main effects)
if (csColA->factors > 0) {
mapping[colB_i] = groupMapping;
} else {
mapping[colB_i] = new Mapping;
mapping[colB_i]->mapping = groupMapping->mapping;
mapping[colB_i]->mappedTo = colB_i + colBOffset;
}
}
csColB = data->at(colB_i + colBOffset);
}
// create new column for CS Matrix
csCol = new CSCol;
for (int row_i = 0; row_i < rows; row_i++) {
addRow(csCol);
}
// set the headers from the combining columns
csCol->factors = csColA->factors + 1;
csCol->setting = new FactorSetting[csCol->factors];
// copy previous factors
for (int setting_i = 0; setting_i < csColA->factors; setting_i++) {
csCol->setting[setting_i] = csColA->setting[setting_i];
}
// assign new setting
csCol->setting[csColA->factors].grouped = (groupIndexB != -1); // is 1st factor grouped
csCol->setting[csColA->factors].factor_i = csColB->setting[0].factor_i; // 1st factor
csCol->setting[csColA->factors].index = levelIndexB; // set level of 1st factor
csCol->setting[csColA->factors].levelsInGroup = colsInGroupB; // set last level if group
// populate the actual column data of CS matrix
sum = populateColumnData(csCol, levelMatrix, 0, rows);
// add new CS column to matrix if needed
bool colAddedToMatrix = false;
if (csCol->factors > 1) {
// push into vector
data->push_back(csCol);
sumOfSquares.push_back(sum);
col_i++;
colAddedToMatrix = true;
}
if (t > 1) {
// recursive call
addTWayInteractions(csCol, colCMax_i, col_i, t - 1,
mapping[colB_i]->mapping, sumOfSquares, groupingInfo, levelMatrix);
}
// deallocate the column if not added to CS matrix
if (!colAddedToMatrix) {
delete[] csCol->setting;
delete csCol;
}
}
}
int CSMatrix::populateColumnData(CSCol *csCol, char **levelMatrix, int row_top, int row_len) {
int sum = 0;
// populate every row
bool rowData; // start with true, perform AND operation
for (int row_i = row_top; row_i < row_top + row_len; row_i++) {
rowData = true; // start with true, perform AND operation
for (int setting_i = 0; setting_i < csCol->factors; setting_i++) {
rowData &= levelMatrix[row_i][csCol->setting[setting_i].factor_i] >=
csCol->setting[setting_i].index;
rowData &= levelMatrix[row_i][csCol->setting[setting_i].factor_i] <
csCol->setting[setting_i].index + csCol->setting[setting_i].levelsInGroup;
}
// AND operation
csCol->dataP[row_i] = (rowData ? ENTRY_A : ENTRY_B);
// add to sum of squares
sum += csCol->dataP[row_i] * csCol->dataP[row_i];
}
return sum;
}
void CSMatrix::reorderRows(int k, int c) {
FactorSetting *settingToResample;
int nPaths;
long long int score;
int cols = getCols();
// check advanced
CSCol **array = new CSCol*[cols];
for (int col_i = 0; col_i < cols; col_i++) {
array[col_i] = data->at(col_i);
}
int coverableMin = sortByCoverable(array, 0, getCols() - 1);
cout << "Coverable columns begin at: " << coverableMin << endl;
int tWayMin = sortByTWayInteraction(array, coverableMin, getCols() - 1);
cout << "t-way interactions begin at: " << tWayMin << endl;
long long int *rowContributions = new long long int[rows];
Path *path = new Path;
path->entryA = NULL;
path->entryB = NULL;
path->min = coverableMin;
path->max = getCols() - 1;
while (true) {
nPaths = 0;
for (int row_i = 0; row_i < rows; row_i++) rowContributions[row_i] = 0;
pathSort(array, path, 0, nPaths, NULL);
score = 0;
settingToResample = NULL;
pathLAChecker(array, path, path, 0, k, score, settingToResample, rowContributions);
minCountCheck(array, c, score, settingToResample, rowContributions);
cout << "Score: " << score;
int swaps = 0;
while (true) {
// find the max contribution out of place
int outOrderRow_i = -1;
for (int row_i = 1; row_i < rows; row_i++) {
if (rowContributions[row_i - 1] < rowContributions[row_i]) {
outOrderRow_i = row_i;
break;
}
}
if (outOrderRow_i != -1) {
int row_i2 = outOrderRow_i;
for (int row_i = row_i2 + 1; row_i < rows; row_i++) {
if (rowContributions[row_i] >= rowContributions[row_i2]) {
row_i2 = row_i;
}
}
int row_i1 = -1;
for (int row_i = 0; row_i < rows; row_i++) {
if (rowContributions[row_i2] > rowContributions[row_i]) {
row_i1 = row_i;
break;
}
}
// perform swap if necessary
swapRows(array, row_i1, row_i2);
long long int tempContribution = rowContributions[row_i1];
rowContributions[row_i1] = rowContributions[row_i2];
rowContributions[row_i2] = tempContribution;
swaps++;
} else {
break;
}
}
cout << "\tSwaps: " << swaps << endl;
if (swaps == 0) break;
}
for (int row_i = 0; row_i < rows; row_i++) cout << row_i << "\t" << rowContributions[row_i] << endl;
delete rowContributions;
}
void CSMatrix::minCountCheck(CSCol **array, int c,
long long int &score, FactorSetting *&settingToResample, long long int *rowContributions) {
int cols = getCols();
int *count = new int[cols];
for (int col_i = 0; col_i < cols; col_i++) {
count[col_i] = 0;
for (int row_i = 0; row_i < rows && count[col_i] < c; row_i++) {
if (array[col_i]->coverable && array[col_i]->dataP[row_i] == ENTRY_A) {
count[col_i]++;
// add row contributions
if (rowContributions != NULL) {
rowContributions[row_i]++;
}
}
}
}
for (int col_i = 0; col_i < cols; col_i++) {
if (array[col_i]->coverable && count[col_i] < c) {
// cout << "Below c requirement (" << count[col_i] << "): " << getColName(array[col_i]) << endl;
score += c - count[col_i];
if (settingToResample == NULL) {
// randomly choose a setting in the column to resample
settingToResample = &array[col_i]->setting[rand() % array[col_i]->factors];
}
}
}
delete[] count;
}
void CSMatrix::exactFix() {
// create a work array
CSCol **array = new CSCol*[getCols()];
for (int col_i = 0; col_i < getCols(); col_i++) {
array[col_i] = data->at(col_i);
}
long long int score = 0;
smartSort(array, 0);
score = getArrayScore(array);
cout << "Original linear LA Score: " << score << endl;
if (locatingArray->getNConGroups() == 0) {
while (score > 0) {
addRowFix(array, score);
}
cout << "Complete LA created with score: " << score << endl;
cout << "Rows: " << getRows() << endl;
} else {
cout << "Unable to perform fixla because constraints were found. Remove the constraints to perform this operation!" << endl;
}
delete[] array;
}
void CSMatrix::performCheck(int k, int c) {
int nConGroups = locatingArray->getNConGroups();
ConstraintGroup **conGroups = locatingArray->getConGroups();
long long int score = 0;
struct timespec start;
struct timespec finish;
float elapsedTime;
// create a work array
CSCol **array = new CSCol*[getCols()];
for (int col_i = 0; col_i < getCols(); col_i++) {
array[col_i] = data->at(col_i);
}
int coverableMin = sortByCoverable(array, 0, getCols() - 1);
cout << "Coverable columns begin at: " << coverableMin << endl;
int tWayMin = sortByTWayInteraction(array, coverableMin, getCols() - 1);
cout << "t-way interactions begin at: " << tWayMin << endl;
Path *path = new Path;
path->entryA = NULL;
path->entryB = NULL;
path->min = coverableMin;
path->max = getCols() - 1;
list <Path*>pathList;
int nPaths = 0;
// grab initial time
current_utc_time( &start);
pathSort(array, path, 0, nPaths, &pathList);
// check current time
current_utc_time( &finish);
// get elapsed seconds
elapsedTime = (finish.tv_sec - start.tv_sec);
// add elapsed nanoseconds
elapsedTime += (finish.tv_nsec - start.tv_nsec) / 1000000000.0;
cout << "Elapsed for path sort: " << elapsedTime << endl;
cout << "Memory check: nPaths = " << nPaths << " of size " << sizeof(Path) << endl;
cout << "Unfinished paths: " << pathList.size() << endl;
// grab initial time
current_utc_time( &start);
FactorSetting *settingToResample = NULL;
pathLAChecker(array, path, path, 0, k, score, settingToResample, NULL);
minCountCheck(array, c, score, settingToResample, NULL);
// check current time
current_utc_time( &finish);
// get elapsed seconds
elapsedTime = (finish.tv_sec - start.tv_sec);
// add elapsed nanoseconds
elapsedTime += (finish.tv_nsec - start.tv_nsec) / 1000000000.0;
cout << "Path and min count LA Score: " << score << endl;
cout << "Elapsed for path and min count check: " << elapsedTime << endl;
deletePath(path);
// grab initial time
current_utc_time(&start);
smartSort(array, 0);
// check current time
current_utc_time(&finish);
// get elapsed seconds
elapsedTime = (finish.tv_sec - start.tv_sec);
// add elapsed nanoseconds
elapsedTime += (finish.tv_nsec - start.tv_nsec) / 1000000000.0;
cout << "Elapsed for smart sort: " << elapsedTime << endl;
// grab initial time
current_utc_time(&start);
score = getArrayScore(array);
minCountCheck(array, c, score, settingToResample, NULL);
// check current time
current_utc_time(&finish);
// get elapsed seconds
elapsedTime = (finish.tv_sec - start.tv_sec);
// add elapsed nanoseconds
elapsedTime += (finish.tv_nsec - start.tv_nsec) / 1000000000.0;
cout << "Weird linear check LA Score (should not match other scores): " << score << endl;
cout << "Elapsed for linear check: " << elapsedTime << endl;
/* BRUTE FORCE */
cout << "Performing brute force check... this could take awhile" << endl;
// grab initial time
current_utc_time(&start);
score = getBruteForceArrayScore(array, k);
minCountCheck(array, c, score, settingToResample, NULL);
// check current time
current_utc_time(&finish);
// get elapsed seconds
elapsedTime = (finish.tv_sec - start.tv_sec);
// add elapsed nanoseconds
elapsedTime += (finish.tv_nsec - start.tv_nsec) / 1000000000.0;
cout << "Brute force and min count LA Score (should match path score): " << score << endl;
cout << "Elapsed for brute force check: " << elapsedTime << endl;
cout << "Checking for constraint violations..." << endl;
for (int row_i = 0; row_i < rows; row_i++) {
for (int conGroup_i = 0; conGroup_i < nConGroups; conGroup_i++) {
if (!conGroups[conGroup_i]->getResult(row_i)) {
cout << "Constraint group " << conGroup_i << " violated in row " << row_i << endl;
}
}
}
cout << "Done!" << endl;
delete[] array;
}
void CSMatrix::autoFindRows(int k, int c, int startRows) {
int iters = 1000;
int cols = getCols();
int upperBound = startRows;
int lowerBound = 1;
// check advanced
CSCol **array = new CSCol*[cols];
for (int col_i = 0; col_i < cols; col_i++) {
array[col_i] = data->at(col_i);
}
int twoWayMin;
for (twoWayMin = 0; twoWayMin < cols; twoWayMin++) {
if (array[twoWayMin]->factors > 1) break;
}
cout << "Two-Way Min: " << twoWayMin << endl;
int factors = locatingArray->getFactors();
int nPaths = 0;
long long int score;
FactorSetting *settingToResample = NULL;
Path *path = new Path;
path->entryA = NULL;
path->entryB = NULL;
// path->min = twoWayMin;
path->min = 0;
path->max = getCols() - 1;
// add more rows to reach total count
resizeArray(array, startRows);
// use a binary search to find the correct value
while (true) {
// check if it finds a proper array once in 5 times
bool testPassed = false;
for (int i = 0; i < 5; i++) {
randomizeArray(array);
list <Path*>pathList;
pathList.push_front(path);
score = 0;
randomizePaths(array, settingToResample, path, 0, k, c, score, &pathList, iters);
cout << "Score: " << score << endl;
if (settingToResample == NULL) {
testPassed = true;
break;
} else if (score > 100) {
// do not try 5 times because the score is greater than 100
break;
}
}
// reset upper / lower bounds
if (testPassed) {
upperBound = rows;
} else {
lowerBound = rows + 1;
}
// check if the upper and lower bounds match
if (upperBound < lowerBound)
cout << "Our bounds messed up :(" << endl;
if (upperBound <= lowerBound) break;
// calculate a median row count
int medRowCount = 1 * (int)((lowerBound + upperBound) / 2.0);
// resize the array
resizeArray(array, medRowCount);
}
cout << "Finished with array containing (" << lowerBound << ":" << upperBound << ") rows!" << endl;
deletePath(path);
delete[] array;
}
void CSMatrix::randomFix(int k, int c, int totalRows) {
int iters = 1000;
int cols = getCols();
// check advanced
CSCol **array = new CSCol*[cols];
for (int col_i = 0; col_i < cols; col_i++) {
array[col_i] = data->at(col_i);
}
int coverableMin = sortByCoverable(array, 0, cols - 1);
cout << "Coverable columns begin at: " << coverableMin << endl;
int tWayMin = sortByTWayInteraction(array, coverableMin, cols - 1);
cout << "t-way interactions begin at: " << tWayMin << endl;
int factors = locatingArray->getFactors();
int nPaths = 0;
long long int score;
FactorSetting *settingToResample = NULL;
Path *path = new Path;
path->entryA = NULL;
path->entryB = NULL;
path->min = coverableMin;
path->max = getCols() - 1;
// add more rows to reach total count
resizeArray(array, totalRows);
list <Path*>pathList;
pathList.push_front(path);
score = 0;
randomizePaths(array, settingToResample, path, 0, k, c, score, &pathList, iters);
minCountCheck(array, c, score, settingToResample, NULL);
cout << "Score: " << score << endl;
deletePath(path);
}
void CSMatrix::systematicRandomFix(int k, int c, int initialRows, int minChunk) {
int chunk = initialRows;
int finalizedRows = rows;
int totalRows = (finalizedRows + chunk);
int cols = getCols();
// check advanced
CSCol **array = new CSCol*[cols];
for (int col_i = 0; col_i < cols; col_i++) {
array[col_i] = data->at(col_i);
}
int coverableMin = sortByCoverable(array, 0, cols - 1);
cout << "Coverable columns begin at: " << coverableMin << endl;
int tWayMin = sortByTWayInteraction(array, coverableMin, cols - 1);
cout << "t-way interactions begin at: " << tWayMin << endl;
Path *path = new Path;
path->entryA = NULL;
path->entryB = NULL;
path->min = coverableMin;
path->max = getCols() - 1;
list <Path*>pathList;
int nPaths = 0;
pathSort(array, path, 0, nPaths, &pathList);
cout << "nPaths: " << nPaths << " of size " << sizeof(Path) << endl;
cout << "Unfinished paths: " << pathList.size() << endl;
long long int score = 0;
struct timespec start;
struct timespec finish;
float elapsedTime;
// grab initial time
current_utc_time( &start);
FactorSetting *settingToResample = NULL;
pathLAChecker(array, path, path, 0, k, score, settingToResample, NULL);
minCountCheck(array, c, score, settingToResample, NULL);
// check current time
current_utc_time( &finish);
// get elapsed seconds
elapsedTime = (finish.tv_sec - start.tv_sec);
// add elapsed nanoseconds
elapsedTime += (finish.tv_nsec - start.tv_nsec) / 1000000000.0;
cout << "Elapsed: " << elapsedTime << endl;
cout << "Score: " << score << endl;
int factors = locatingArray->getFactors();
while (settingToResample != NULL) {
resizeArray(array, totalRows);
randomizePaths(array, settingToResample, path, finalizedRows, k, c, score, &pathList, 1000);
finalizedRows = rows;
chunk -= chunk / 2;
if (chunk < minChunk) chunk = minChunk;
totalRows += chunk;
}
}
void CSMatrix::randomizePaths(CSCol **array, FactorSetting *&settingToResample, Path *path, int row_top, int k, int c, long long int &score, list <Path*>*pathList, int iters) {
int cols = getCols();
long long int newScore;
int factor_i, nPaths;
ConstraintGroup *conGroup;
char **levelMatrix = locatingArray->getLevelMatrix();
int factors = locatingArray->getFactors();
// custom factors to resample
int nCustomFactors = 0;
int *customFactorIndeces = new int[nCustomFactors];
// customFactorIndeces[0] = 16;
// customFactorIndeces[1] = 17;
// allocate memory for saving old levels of locating array
char **oldLevels = new char*[rows];
for (int row_i = 0; row_i < rows; row_i++) {
oldLevels[row_i] = new char[factors];
}
// sort paths
for (std::list<Path*>::iterator it = pathList->begin(); it != pathList->end(); it++) {
nPaths = 0;
pathSort(array, *it, row_top, nPaths, NULL);
}
// run initial checker
score = 0;
settingToResample = NULL;
pathLAChecker(array, path, path, 0, k, score, settingToResample, NULL);
minCountCheck(array, c, score, settingToResample, NULL);
cout << "Score: " << score << endl;
for (int iter = 0; iter < iters && score > 0; iter++) {
struct timespec start;
struct timespec finish;
float elapsedTime;
// ensure we recieved an actual setting
if (settingToResample == NULL) {
cout << "No resampleable setting was found" << endl;
break;
}
// get factors to resample (all factors in constraint group if one exists)
conGroup = groupingInfo[settingToResample->factor_i]->conGroup;
if (conGroup == NULL) {
// get factor to resample
factor_i = settingToResample->factor_i;
// resample locating array
for (int row_i = row_top; row_i < rows; row_i++) {
oldLevels[row_i][factor_i] = levelMatrix[row_i][factor_i];
if (rand() % 100 < 100) {
levelMatrix[row_i][factor_i] = rand() % groupingInfo[factor_i]->levels;
}
}
// repopulate columns of CS matrix
for (int level_i = 0; level_i < groupingInfo[factor_i]->levels; level_i++) {
repopulateColumns(factor_i, level_i, row_top, rows - row_top);
}
} else if (nCustomFactors > 0) {
for (int row_i = row_top; row_i < rows; row_i++) {
for (int factor_i = 0; factor_i < nCustomFactors; factor_i++) {
oldLevels[row_i][customFactorIndeces[factor_i]] = levelMatrix[row_i][customFactorIndeces[factor_i]];
}
if (rand() % 100 < 100) {
for (int factor_i = 0; factor_i < nCustomFactors; factor_i++) {
levelMatrix[row_i][customFactorIndeces[factor_i]] = rand() % groupingInfo[customFactorIndeces[factor_i]]->levels;
}
}
}
// repopulate columns of CS matrix for every factor in constraint group and for each level
for (int factor_i = 0; factor_i < nCustomFactors; factor_i++) {
for (int level_i = 0; level_i < groupingInfo[customFactorIndeces[factor_i]]->levels; level_i++) {
repopulateColumns(customFactorIndeces[factor_i], level_i, row_top, rows - row_top);
}
}
} else {
for (int row_i = row_top; row_i < rows; row_i++) {
for (int factor_i = 0; factor_i < conGroup->factors; factor_i++) {
oldLevels[row_i][conGroup->factorIndeces[factor_i]] = levelMatrix[row_i][conGroup->factorIndeces[factor_i]];
}
if (rand() % 100 < 100) {
conGroup->randPopulateLevelRow(levelMatrix[row_i]);
}
}
// repopulate columns of CS matrix for every factor in constraint group and for each level
for (int factor_i = 0; factor_i < conGroup->factors; factor_i++) {
for (int level_i = 0; level_i < groupingInfo[conGroup->factorIndeces[factor_i]]->levels; level_i++) {
repopulateColumns(conGroup->factorIndeces[factor_i], level_i, row_top, rows - row_top);
}
}
}
// grab initial time
current_utc_time( &start);
// sort paths and recheck score
for (std::list<Path*>::iterator it = pathList->begin(); it != pathList->end(); it++) {
nPaths = 0;
pathSort(array, *it, row_top, nPaths, NULL);
}
// check current time
current_utc_time( &finish);
// get elapsed seconds
elapsedTime = (finish.tv_sec - start.tv_sec);
// add elapsed nanoseconds
elapsedTime += (finish.tv_nsec - start.tv_nsec) / 1000000000.0;
// cout << "Elapsed After Sort: " << elapsedTime << endl;
newScore = 0;
FactorSetting *newSettingToResample = NULL;
// grab initial time
current_utc_time( &start);
pathLAChecker(array, path, path, 0, k, newScore, newSettingToResample, NULL);
minCountCheck(array, c, newScore, newSettingToResample, NULL);
// check current time
current_utc_time( &finish);
// get elapsed seconds
elapsedTime = (finish.tv_sec - start.tv_sec);
// add elapsed nanoseconds
elapsedTime += (finish.tv_nsec - start.tv_nsec) / 1000000000.0;
// cout << "Elapsed After Checker: " << elapsedTime << endl;
if (newScore <= score) { // add "|| true" to cause every change to be implemented, not just improving changes
cout << "Rows: " << rows << " Iter: " << iter << ": ";
cout << newScore << ": \t" << score << " \tAccepted " << endl;
settingToResample = newSettingToResample;
score = newScore;
} else {
cout << "Rows: " << rows << " Iter: " << iter << ": ";
cout << score << " \tMaintained " << endl;
if (conGroup == NULL) {
// get factor to resample
factor_i = settingToResample->factor_i;
// rollback the change
for (int row_i = row_top; row_i < rows; row_i++) {
levelMatrix[row_i][factor_i] = oldLevels[row_i][factor_i];
}
// repopulate columns of CS matrix
for (int level_i = 0; level_i < groupingInfo[factor_i]->levels; level_i++) {
repopulateColumns(factor_i, level_i, row_top, rows - row_top);
}
} else if (nCustomFactors > 0) {
for (int row_i = row_top; row_i < rows; row_i++) {
for (int factor_i = 0; factor_i < nCustomFactors; factor_i++) {
levelMatrix[row_i][customFactorIndeces[factor_i]] = oldLevels[row_i][customFactorIndeces[factor_i]];
}
}
// repopulate columns of CS matrix for every factor in constraint group and for each level
for (int factor_i = 0; factor_i < nCustomFactors; factor_i++) {
for (int level_i = 0; level_i < groupingInfo[customFactorIndeces[factor_i]]->levels; level_i++) {
repopulateColumns(customFactorIndeces[factor_i], level_i, row_top, rows - row_top);
}
}
} else {
// rollback the change
for (int row_i = row_top; row_i < rows; row_i++) {
for (int factor_i = 0; factor_i < conGroup->factors; factor_i++) {
levelMatrix[row_i][conGroup->factorIndeces[factor_i]] = oldLevels[row_i][conGroup->factorIndeces[factor_i]];
}
}
// repopulate columns of CS matrix for every factor in constraint group and for each level
for (int factor_i = 0; factor_i < conGroup->factors; factor_i++) {
for (int level_i = 0; level_i < groupingInfo[conGroup->factorIndeces[factor_i]]->levels; level_i++) {
repopulateColumns(conGroup->factorIndeces[factor_i], level_i, row_top, rows - row_top);
}
}
}
// cout << score << ": \t" << newScore << " \tRejected" << endl;
}
}
// deallocate all memory
for (int row_i = 0; row_i < rows; row_i++) {
delete[] oldLevels[row_i];
}
delete[] oldLevels;
delete[] customFactorIndeces;
// perform one last sort and save final unfinished paths to list
list <Path*>newPathList(*pathList);
pathList->clear();
// sort paths and recheck score
for (std::list<Path*>::iterator it = newPathList.begin(); it != newPathList.end(); it++) {
nPaths = 0;
pathSort(array, *it, row_top, nPaths, pathList);
}
score = 0;
settingToResample = NULL;
pathLAChecker(array, path, path, 0, k, score, settingToResample, NULL);
minCountCheck(array, c, score, settingToResample, NULL);
}
// LEGACY
void CSMatrix::randomizeRows(CSCol **backupArray, CSCol **array, long long int &csScore, int row_top, int row_len) {
int cols = getCols();
long long int newCsScore;
int factor_i, resampleFactor;
FactorSetting *settingToResample = NULL;
char **levelMatrix = locatingArray->getLevelMatrix();
char *oldLevels = new char[rows];
smartSort(array, row_top);
csScore = getArrayScore(array);
cout << "Score: " << csScore << endl;
for (int iter = 0; iter < 1000; ) {
if (csScore <= 0) break;
for (int col_i = 0; col_i < cols - 1; col_i++) {
// check if the streak ended
if (compare(array[col_i], array[col_i + 1], 0, rows) == 0) {
// copy to backup array
memcpy(backupArray, array, sizeof(CSCol*) * cols); // backup array
while (iter < 1000) {
iter++;
resampleFactor = rand() % (array[col_i]->factors + array[col_i + 1]->factors);