-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOneDimADSGenerator.java
More file actions
175 lines (161 loc) · 5.68 KB
/
OneDimADSGenerator.java
File metadata and controls
175 lines (161 loc) · 5.68 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
import java.util.*;
// goal for next week: debug to get 32, would be nice if get 64
// go back to Z_16, make |ds| = 11, lambda = (11 * 10) = 8x + (15 - x)*7 -> 5?
public class OneDimADSGenerator extends ADSGenerator {
// additional field
int[] coverTimesCntr;
//static int numLambdaCovered;
//int numNotLambdaCovered;
// keep track of all elements that are generated lambda times by curr ads
Map<String, int[]> lambdaElemTracker;
/* constructor
*/
public OneDimADSGenerator() {
super();
coverTimesCntr = new int[this.groupOrder];
lambdaElemTracker = new HashMap<>();
}
public OneDimADSGenerator(int groupOrder, int lambda, int numElemLambda,
int ADSOrder) {
this.dimension = 1;
array = new int[dimension];
this.groupOrder = groupOrder;
array[0] = this.groupOrder;
this.lambda = lambda;
this.numElemLambda = numElemLambda;
this.ADSOrder = ADSOrder;
this.validADSPreCheck();
coverTimesCntr = new int[this.groupOrder];
lambdaElemTracker = new HashMap<>();
}
public void dummyPrint() {
System.out.println(1);
}
public void printADS(Set<List<int[]>> ads) {
for (List<int[]> set : ads) {
System.out.print("[");
String encoding = "";
for (int[] entry : set) {
System.out.print(entry[0] + " ");
encoding += encode(entry);
}
System.out.println("] - " + Arrays.toString(lambdaElemTracker.get(encoding)));
}
}
/* generate all possible sets of order ADSOrder
*/
public Set<List<int[]>> getADSCandidates(int[][] allGroupElem) {
//System.out.println("in one-dim getADSCandidates");
List<int[]> currSet = new ArrayList<>();
int firstElemInd = 0;
currSet.add(allGroupElem[firstElemInd]);
firstElemInd++;
Set<List<int[]>> allSets = new LinkedHashSet<>();
getNext(firstElemInd, ADSOrder - 1, allGroupElem, currSet, allSets, 0);
//System.out.println(allSets.size());
return allSets;
}
/* recursive method to help generate next ADS candidate
*/
private void getNext(int currInd, int numLeft, int[][] allGroupElem,
List<int[]> currSet, Set<List<int[]>> allSets,
int numLambdaCovered) {
if (numLeft == 0) {
List<int[]> newSet = new ArrayList<>(currSet);
allSets.add(newSet);
// add all the elements that are covered lambda times to the hashmap
// tracker
int i = 0;
int[] elem = new int[this.numElemLambda];
//System.out.println("set just added, and numLmbdaCover " + numLambdaCovered);
for (int j = 0; j < coverTimesCntr.length; j++) {
if (coverTimesCntr[j] == lambda) {
elem[i++] = j;
}
}
String encoding = "";
for (int[] entry : newSet) {
encoding += encode(entry);
}
lambdaElemTracker.put(encoding, elem);
return;
} else if (allGroupElem.length - currInd >= numLeft) {
List<Integer> differences = new ArrayList<>();
// attach current elem to currSet
//for (int i = currInd; i < allGroupElem.length; i++) {
int i = currInd;
// add new to set, update ctnr
boolean stop = false;
for (int[] num : currSet) {
int diff1 = (num[0] - allGroupElem[i][0] + groupOrder) % groupOrder;
int diff2 = (allGroupElem[i][0] - num[0] + groupOrder) % groupOrder;
differences.add(diff1);
differences.add(diff2);
coverTimesCntr[diff1]++;
coverTimesCntr[diff2]++;
if (coverTimesCntr[diff1] == lambda) {
//System.out.println(diff1 + " reached lambda");
numLambdaCovered++;
}
if (coverTimesCntr[diff2] == lambda && diff2 != diff1) {
//System.out.println(diff2 + " reached lambda");
numLambdaCovered++;
}
if (coverTimesCntr[diff1] > lambda ||
coverTimesCntr[diff2] > lambda ||
numLambdaCovered > this.numElemLambda) {
// stop with this num
/*
if (numLambdaCovered > this.numElemLambda) {
System.out.println("STOP b/c numLambdaCovered exceed");
} else if (coverTimesCntr[diff1] > lambda) {
System.out.println("STOP b/c " + diff1 + " covered " +
coverTimesCntr[diff1] + " times");
} else {
System.out.println("STOP b/c " + diff2 + " covered " +
coverTimesCntr[diff2] + " times");
}
*/
stop = true;
break;
}
}
if (!stop) {
currSet.add(allGroupElem[i]);
for (int k = i + 1; k < groupOrder; k++) {
getNext(k, numLeft - 1, allGroupElem, currSet, allSets, numLambdaCovered);
if (allSets.size() > 0)
return;
}
currSet.remove(allGroupElem[i]);
}
/*
System.out.print("before removing: ");
for (int[] e : currSet) {
System.out.print(e[0] + ", ");
}
System.out.println();
*/
for (int diff : differences) {
coverTimesCntr[diff]--;
/*
if (coverTimesCntr[diff] == lambda) {
numLambdaCovered--;
coverTimesCntr[diff]--;
System.out.println("popping out " + diff + " covered lambda times, "
+ "now " + coverTimesCntr[diff]);
System.out.println("numLambdaCovered " + numLambdaCovered);
} else {
coverTimesCntr[diff]--;
}
*/
}
//}
//System.out.println("numLambdaCovered " + numLambdaCovered);
}
}
public Set<List<int[]>> getADS(Set<List<int[]>> candidates,
int[][] allGroupElements) {
return candidates;
}
}