-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHMArranger.java
More file actions
409 lines (359 loc) · 10.9 KB
/
HMArranger.java
File metadata and controls
409 lines (359 loc) · 10.9 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
import java.io.*;
import java.util.*;
// k is the cell type
public class HMArranger {
static class Permutation<T> implements Iterable<T> {
private List<T> things;
private List<Integer> perm;
public Permutation() {
things = new ArrayList<T>();
perm = new ArrayList<Integer>();
}
public Permutation(List<? extends T> items) {
things = new ArrayList<T>(items);
perm = new ArrayList<Integer>();
for(int i=0; i<things.size(); i++)
perm.add(i);
}
public Iterator<T> iterator() {
class PIterator<Q> implements Iterator<Q> {
private List<Q> myThings;
private Iterator<Integer> idxIter;
public PIterator(List<Q> myThings, Iterator<Integer> idxIter) {
this.myThings = myThings;
this.idxIter = idxIter;
}
@Override
public boolean hasNext() {
return idxIter.hasNext();
}
@Override
public Q next() {
int i = idxIter.next();
return myThings.get(i);
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
return new PIterator(things, perm.iterator());
}
public void shuffle() {
Collections.shuffle(perm);
}
public T get(int i) {
int j = perm.get(i);
return things.get(j);
}
public void add(T t) {
things.add(t);
perm.add(perm.size());
}
public Permutation<T> copy() {
Permutation<T> p = new Permutation();
p.things = things;
p.perm = new ArrayList<Integer>(perm);
return p;
}
public Permutation<T> mutate(int N, Random r) {
if(N == 2) return mutate2(r);
// choose N things, shuffle those indices
Set<Integer> seen_s = new HashSet<Integer>();
List<Integer> seen_l = new ArrayList<Integer>();
while(seen_s.size() < N) {
int i = r.nextInt(perm.size());
if(seen_s.add(i)) seen_l.add(i);
}
// System.out.println("[mutate] seen_l = " + seen_l);
Permutation<T> p = copy();
// System.out.println("[mutate] before p.perm = " + p.perm);
p.shift(seen_l);
// System.out.println("[mutate] after p.perm = " + p.perm);
return p;
}
private Permutation<T> mutate2(Random r) {
final int N = perm.size();
final int left = r.nextInt(N);
int right = left;
while(right == left)
right = r.nextInt(N);
Permutation<T> p = copy();
int temp = p.perm.get(left);
p.perm.set(left, p.perm.get(right));
p.perm.set(right, temp);
return p;
}
/**
* generalization of swap
* shift([i,j]) => swap(i,j)
* shift([i,j,k]) => perm[i,j,k] = perm[j,k,i]
* ...
*/
private void shift(List<Integer> idx) {
int leftMost = perm.get(idx.get(0));
for(int i=0; i<idx.size()-1; i++) {
int left = idx.get(i);
int right = idx.get(i+1);
perm.set(left, perm.get(right));
}
perm.set(idx.get(idx.size()-1), leftMost);
}
public int size() {
assert things.size() == perm.size();
return things.size();
}
}
static interface Row<T> {
public String getName();
public T get(int i);
public List<T> getCols();
public int numCols();
public double disagreement(Row<T> other); // must be >= 0
}
static class DoubleRow implements Row<Double> {
private String name;
private List<Double> elems;
public DoubleRow(String name, List<Double> elems) {
this.name = new String(name);
this.elems = new ArrayList<Double>(elems);
}
public String getName() { return name; }
public Double get(int i) { return elems.get(i); }
public List<Double> getCols() { return elems; }
public int numCols() { return elems.size(); }
public double disagreement(Row<Double> other) {
assert numCols() == other.numCols();
double nd;
double ds = 0.0;
for(int i=0; i<numCols(); i++) {
double d = get(i) - other.get(i);
ds += d*d;
// reward disagreeing on the diagonal
final double diag_rew_const = 0.1;
if(i > 0) {
nd = get(i) - other.get(i-1);
ds -= diag_rew_const * (nd*nd);
}
if(i < numCols()-1) {
nd = get(i) - other.get(i+1);
ds -= diag_rew_const * (nd*nd);
}
}
return ds;
}
}
private Permutation<DoubleRow> rows;
private List<String> colNames;
private double greedy = 1.0;
private int effort = 100;
private int change_size = 2;
private int neighbors = 50;
private Random rand = new Random();
private double p_shuffle = 0.0, p_return_to_best = 0.005;
private Permutation<DoubleRow> best_rows;
private double best_score = -1.0/0.0;
public HMArranger(List<String> colNames) {
this.colNames = new ArrayList(colNames);
rows = new Permutation<DoubleRow>();
best_rows = rows;
}
public HMArranger(List<String> colNames, int change_size) {
this(colNames);
this.change_size = change_size;
}
public String getParamString() {
return String.format("greedy = %.1f, change_size = %d, p_shuffle = %.1e, p_return_to_best = %.1e, neighbors=%d",
greedy, change_size, p_shuffle, p_return_to_best, neighbors);
}
public List<String> getColNames() { return colNames; }
public void setRandom(Random r) { rand = r; }
public void setGreedy(double g) { greedy = g; }
public void setNeighbors(int n) { neighbors = n; }
public int numRows() { return rows.size(); }
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append(String.format("greedy = %.1f, effort = %d, " +
"change size = %d\n", greedy, effort, change_size));
sb.append("titles = " + colNames + "\n");
for(DoubleRow r : rows)
sb.append(r.getName() + " -> " + r.getCols() + "\n");
return sb.toString();
}
public void addRow(String rowname, List<Double> elements) {
DoubleRow r = new DoubleRow(rowname, elements);
rows.add(r);
}
private static <S> double score(Permutation<? extends Row<S>> rows, int neighbors) {
double c2 = 0.95;
double penalty = 0;
for(int i=0; i<rows.size()-1; i++) {
Row<S> r1 = rows.get(i);
// neighbors
double c = 1.0/c2;
for(int j=1; j<neighbors && i+j<rows.size(); j++) {
c *= c2;
Row<S> r2 = rows.get(i+j);
penalty += c2 * r1.disagreement(r2);
}
}
return -penalty;
}
private static <S> double score(Permutation<? extends Row<S>> rows, List<Row> changed) {
return 0.0;
}
public boolean optimize(int maxIter) {
int i;
for(i=0; i<maxIter; i++) {
double r = rand.nextDouble();
if(r < p_shuffle)
rows.shuffle();
else if(r < p_shuffle + p_return_to_best)
rows = best_rows.copy();
else
improve();
if(i % 5000 == 0) {
System.out.printf("[optimize] score = %.2f, best score = %.3f, params = %s\n",
getScore(), getBestScore(), getParamString());
}
}
rows = best_rows;
best_rows = best_rows.copy();
return i < maxIter;
}
public void shuffle() {
System.out.println("shuffling, rows.size = " + rows.size());
rows.shuffle();
}
public boolean improve() {
// p(accept) = logistic(greedy*(score(conf1) - score(conf2)))
double old_score = score(rows, neighbors);
for(int i=0; i<effort; i++) {
Permutation<DoubleRow> mutation = rows.mutate(change_size, rand);
double new_score = score(mutation, neighbors);
double d = greedy * (new_score - old_score);
double accept = 1.0/(1.0+Math.exp(-d));
// System.out.printf("[improve] i=%d, old_score = %.1f, new_score = %.1f, " +
// "greedy = %.1f, d = %.1f, accept = %.1f\n",
// i, old_score, new_score, greedy, d, accept);
if(rand.nextDouble() < accept) {
rows = mutation;
updateBest(rows);
return true;
}
}
updateBest(rows);
return false;
}
public double getScore() {
return HMArranger.score(rows, neighbors);
}
private void updateBest(Permutation<DoubleRow> p) {
double s = score(p, neighbors);
if(s > best_score) {
best_score = s;
best_rows = p;
}
}
public double getBestScore() { return best_score; }
static class HMThread implements Runnable {
public HMArranger hma;
public int maxIter;
public HMThread(HMArranger hma, int maxIter) {
this.hma = hma;
this.maxIter = maxIter;
}
@Override
public void run() {
//hma.shuffle();
hma.optimize(maxIter);
}
}
public static void writeout(HMArranger hma, File csv) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csv), "UTF-8"));
bw.write(csvLine("rowName", hma.getColNames())+"\n");
for(DoubleRow r : hma.best_rows)
bw.write(csvLine(r.name, r.elems) + "\n");
bw.close();
}
public static String csvLine(String leftMost, List<?> right) {
StringBuffer sb = new StringBuffer();
sb.append(leftMost);
for(Object o : right)
sb.append(", " + o);
return sb.toString();
}
public static String csvLine(List<?> toks) {
return csvLine(toks.get(0)+"", toks.subList(1, toks.size()));
}
public static void main(String[] args) throws Exception {
final int maxIter = 20; // runs up to (maxIter*k*log(k)) iterations for a length k csv
final int neighbors = 100;
final int N = 4; // how many threads / parallel searches
final int[] changeSizes = new int[]{2}; // how many indices per mutation
final double greedy = 2.0; // 0 = random search, infinity = hillclimbing
if(args.length != 1) {
System.out.println("please provide one csv filename");
return;
}
File csv = new File(args[0]);
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(csv), "UTF-8"));
Thread[] threads = new Thread[N];
HMThread[] hmts = new HMThread[N];
// read the header
String line= br.readLine();
String[] ar = line.split(",\\s*");
List<String> titles = Arrays.asList(ar).subList(1, ar.length);
for(int i=0; i<N; i++) {
int change_size = changeSizes[i % changeSizes.length];
HMArranger hma = new HMArranger(titles, change_size);
hma.setRandom(new Random(i*3571));
hma.setGreedy(greedy);
hma.setNeighbors(neighbors);
hmts[i] = new HMThread(hma, 100);
threads[i] = new Thread(hmts[i]);
}
// read remaining rows
while((line = br.readLine()) != null) {
line = line.trim();
ar = line.split(",\\s*");
String title = ar[0];
List<Double> elements = new ArrayList<Double>();
for(int i=1; i<ar.length; i++)
elements.add(Double.parseDouble(ar[i]));
for(int i=0; i<N; i++)
hmts[i].hma.addRow(title, elements);
}
br.close();
for(int i=0; i<N; i++) {
HMArranger hma = hmts[i].hma;
double s0 = hma.getScore();
//hma.shuffle();
double s1 = hma.getScore();
System.out.printf("in score %.1f, rand score %.1f\n", s0, s1);
}
// calcuate good permutation
for(int i=0; i<N; i++) {
double k = hmts[i].hma.numRows();
hmts[i].maxIter = (int)(maxIter * k * Math.log(k));
System.out.printf("starting thread with maxIter = %d\n", hmts[i].maxIter);
threads[i].start();
}
for(int i=0; i<N; i++) threads[i].join();
double best_score = -999999;
HMArranger best = null;
for(int i=0; i<N; i++) {
double s = hmts[i].hma.getBestScore();
String p = hmts[i].hma.getParamString();
System.out.println(i + "(" + p + ") => " + s);
// System.out.println(hmts[i].hma);
if(s > best_score) {
best_score = s;
best = hmts[i].hma;
}
}
// writeout best permutation
writeout(best, new File(csv.getCanonicalPath() + ".better"));
}
}