-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimitiveLearner.java
More file actions
323 lines (242 loc) · 8.98 KB
/
PrimitiveLearner.java
File metadata and controls
323 lines (242 loc) · 8.98 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
package org.ssascaling.model.selection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.ssascaling.model.selection.mi.MutualInformation;
import org.ssascaling.primitive.Primitive;
import org.ssascaling.qos.QualityOfService;
import org.ssascaling.util.Repository;
public class PrimitiveLearner {
public static final double threshold = 0.5;
private Map<Primitive, Double> newInputesMap = new HashMap<Primitive, Double>();
private static final boolean isConsiderDirectSpaceInRedundancy = false;
private static final boolean isConsiderDirectSpaceWithRedundancy = false;
private static final boolean isMul = true;
private static final boolean isTotal = true;
public Set<Primitive> select(QualityOfService output, Set<Primitive> primitives){
long time = System.currentTimeMillis();
final Map<Integer, List<DependencyPair>> inputMap = new HashMap<Integer, List<DependencyPair>>();
final Set<Primitive> inputs = new HashSet<Primitive>();
newInputesMap.clear();
double value = 0.0;
//System.out.print("*******count: " + Repository.countDirectForAnObjective(output) + "\n");
for (Primitive p : primitives) {
if ((value = MutualInformation.calculateSymmetricUncertainty(output.getArray(), p.getArray())) > 0) {
if (p.isDirect(output)) {
//System.out.print("\"D," + value + ":" + p.getAlias() + " - " + p.getName() + "\",\n");
inputs.add(p);
} else {
// System.out.print("\"inD," + value + ":" + p.getAlias() + " - " + p.getName() + "\",\n");
if (!inputMap.containsKey(p.getGroup())) {
inputMap.put(p.getGroup(), new ArrayList<DependencyPair>());
}
inputMap.get(p.getGroup()).add(new DependencyPair(p,value));
}
// This may include unselected primitives. However, it is fine to have them.
newInputesMap.put(p, value);
}
}
System.out.print("Number of direrct primitives: " + inputs.size() + "\n");
System.out.print("Number of possible primitives: " + primitives.size() + "\n");
/*if (isConsiderDirectSpaceInRedundancy) {
List<DependencyPair> list = new ArrayList<DependencyPair> ();
for (Primitive p : inputs) {
list.add(new DependencyPair(p,newInputesMap.get(p)));
}
final Set<Primitive> directSet = randomOptimizeSelection(list);
inputs.clear();
inputs.addAll(directSet);
}
if (isConsiderDirectSpaceWithRedundancy) {
for (List<DependencyPair> list : inputMap.values()) {
for (DependencyPair dp : list) {
double v = 0;
for (Primitive p : inputs) {
v += MutualInformation.calculateSymmetricUncertainty(dp.getPrimitive().getArray(), p.getArray());
}
dp.setRedundancyToDirectSpace(v);
}
}
}*/
if (inputMap.size() > 1) {
throw new RuntimeException("We currently only allow one group for non-direct primitives!");
}
for (Integer groupID : inputMap.keySet()) {
final Set<Primitive> nonPrimary = randomOptimizeSelection(inputMap
.get(groupID));
for (Primitive p : nonPrimary) {
inputs.add(p);
}
}
System.out.print("Number of total selected primitives: " + inputs.size() + "\n");
for (Primitive p : inputs) {
System.out.print("=========================\n");
System.out.print("Final Selected: " + p.getAlias() + " : " + p.getName() + "\n");
System.out.print("=========================\n");
}
System.out.print("Time for primitives selection:" + (System.currentTimeMillis() - time) + "ms\n");
return inputs;
}
public double getValue (Primitive primitive) {
return newInputesMap.get(primitive);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
private Set<Primitive> thresholdSelection (List inputList) {
Collections.sort(inputList);
Set<Primitive> newInputes = new HashSet<Primitive>();
for (int i = inputList.size() - 1; i > inputList.size() * (1-threshold); i--) {
newInputes.add(((DependencyPair)inputList.get(i)).getPrimitive());
}
return newInputes;
}
@SuppressWarnings({ "rawtypes", "unchecked", "unused" })
private Set<Primitive> randomOptimizeSelection (List inputList) {
//Collections.sort(inputList);
Set<Primitive> newInputes = new HashSet<Primitive>();
Set<Primitive> finalInputs = new HashSet<Primitive>();
double finalp = 0.0;
for (int k = 0; k < 50000/*the length of random optimization can be changed*/; k++) {
Collections.shuffle(inputList);
newInputes.clear();
double total = 0.0;
double add = 0.0;
double minus = 1.0;
for (int i = inputList.size() - 1; i > -1; i--) {
// System.out.print("Add " +
// ((DependencyPair)inputList.get(i)).value + " Minus " +
// ((DependencyPair)inputList.get(i)).getRedundancyValue(newInputes)+
// "\n");
double a = ((DependencyPair) inputList.get(i)).value;
double b = ((DependencyPair) inputList.get(i))
.getRedundancyValue(newInputes);
int no = newInputes.size() + 1;
// System.out.print( (add+a)/no -
// (minus+b)/(no*no)+" Selected \n");
//System.out.print("Old " + total + ", New" + (minus + b == 0 ? (add + a) / no
//: (((add + a) / no) / ((minus + b) / (no * no)))) + "\n");
/*if (total < (minus + b == 0 ? (add + a) / no
: (((add + a) / no) / ((minus + b) / ((no * no - no)/2))))) {
newInputes.add(((DependencyPair) inputList.get(i))
.getPrimitive());
add += a;
minus += b;
total = (minus == 0 ? add / no : (add / no)
/ (minus / ((no * no - no)/2)));
}*/
if (total < ((add + a) / (minus + b))) {
newInputes.add(((DependencyPair) inputList.get(i))
.getPrimitive());
add += a;
minus += b;
total = (add) / (minus);
}
/*if (isMul && isTotal) {
if (total < ((add + a) / (minus + b))) {
newInputes.add(((DependencyPair) inputList.get(i))
.getPrimitive());
add += a;
minus += b;
total = (add) / (minus);
}
} else if (isMul && !isTotal) {
if (add == 0) {
// The 1+ should not be added in this way
minus = 0;
}
int n = no;
if (no == 1) {
n = 2;
}
if (total < (((add+ a)/no) / (1+((minus+b)/((n*n-n)/2))))) {
newInputes.add(((DependencyPair) inputList.get(i))
.getPrimitive());
add += a;
minus += b;
total = (((add)/no) / (1+((minus)/((n*n-n)/2))));
}
} else if (!isMul && !isTotal) {
if (add == 0) {
minus = 0;
}
int n = no;
if (no == 1) {
n = 2;
}
if (total < (((add+ a)/no) - ((minus+b)/((n*n-n)/2)))) {
newInputes.add(((DependencyPair) inputList.get(i))
.getPrimitive());
add += a;
minus += b;
total = (((add)/no) - ((minus)/((n*n-n)/2)));
}
} else if (!isMul && isTotal) {
if (add == 0) {
minus = 0;
}
if (total < ((add + a) - (minus + b))) {
newInputes.add(((DependencyPair) inputList.get(i))
.getPrimitive());
add += a;
minus += b;
total = (add) - (minus);
}
}*/
}
if (finalp < total) {
///System.out.print("Best so far: " + finalp + ", with number of " + finalInputs.size() + ", Fine " + total + " with number of "
//+ newInputes.size() + "\n");
//System.out.print(add + "\n");
finalp = total;
finalInputs.clear();
finalInputs.addAll(newInputes);
}
//System.out.print("Best so far: " + finalp + ", with number of " + finalInputs.size() + ", Fine " + total + " with number of "
//+ newInputes.size() + "\n");
}
System.out.print("Total " + finalp + "\n");
for (Primitive p : finalInputs) {
System.out.print("=========================\n");
System.out.print("Selected: " + p.getAlias() + " : " + p.getName() + "\n");
System.out.print("=========================\n");
}
return finalInputs;
}
private void Shake (Set<DependencyPair> solution) {
}
private class DependencyPair implements Comparable<DependencyPair>{
private Primitive p;
private double value;
private double redundancyToDirectSpace = 0;
Map<Primitive, Double> redundancy = new HashMap<Primitive, Double>();
public DependencyPair(Primitive p, double value) {
super();
this.p = p;
this.value = value;
}
public double getRedundancyValue(Set<Primitive> primitives) {
double total = 0.0;
for (Primitive primitive : primitives) {
if (!redundancy.containsKey(primitive)){
redundancy.put(primitive,
MutualInformation.calculateSymmetricUncertainty(primitive.getArray(), p.getArray()));
}
total += redundancy.get(primitive);
}
return total;// + redundancyToDirectSpace;
}
public Primitive getPrimitive() {
return p;
}
@Override
public int compareTo(DependencyPair o) {
return this.value < o.value? -1 : 1;
}
public void setRedundancyToDirectSpace(double redundancyToDirectSpace) {
this.redundancyToDirectSpace = redundancyToDirectSpace;
}
}
}