-
Notifications
You must be signed in to change notification settings - Fork 0
/
hackermans.cpp
785 lines (695 loc) · 19.2 KB
/
hackermans.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
#include <iostream>
#include <fstream>
#include <assert.h>
#define TESTING 0
// We first define our two data structures Grid and User
typedef struct User
{
int speed;
int data;
int factor;
} User;
typedef struct Score
{
float speed;
int data;
} Score;
int flr(float speed)
{
return (speed + 1e-6);
}
class Grid
{
private:
// Private variables
int m;
int n;
int alpha;
int* ids;
// Private helper functions
int place_user(int user_id, int col, int row)
{
if (row < 0 || m <= row || col < 0 || n <= col)
return -1;
ids[col * m + row] = user_id;
return 0;
}
int get_top(int col)
{
int start = col * m;
int cur = 0;
for (int cur = 0; cur < m; cur++)
if (ids[start + cur] == 0)
return cur;
return cur;
}
int excluded(int user_id, int col)
{
int start = col * m;
for (int cur = 0; cur < m; cur++)
if (ids[start + cur] == user_id)
return 0;
return 1;
}
int length(int user_id, int col)
{
int start = col * m;
int cur = 0;
int within = 0;
while (ids[start + cur] != 0 && cur < m)
{
if (ids[start + cur] == user_id)
within = 1;
cur++;
}
return cur * within;
}
int best_without(int user_id, int o_user_id)
{
// get column with the least users or -1 if none
int score = n;
int col = -1;
int count;
for (int i = 0; i < n; i++)
{
count = length(user_id, i);
if (count < score && count > 0 && excluded(o_user_id, i))
{
score = count;
col = i;
}
}
return col;
}
int worst_without(int user_id, int o_user_id)
{
// get column with the most users or -1 if none without o_user_id
int score = 0;
int col = -1;
int count;
int exc;
for (int i = 0; i < n; i++)
{
count = length(user_id, i);
exc = excluded(o_user_id, i);
if (count > score && exc > 0)
{
score = count;
col = i;
}
}
return col;
}
public:
// (De)Constructors
Grid(int* header)
{
m = header[0];
n = header[1];
alpha = header[3];
ids = new int[m * n];
for (int i = 0; i < m * n; i++)
ids[i] = 0;
}
~Grid()
{
delete ids;
}
// Public function interface
int get_n() { return n; }
int get_m() { return m; }
int get_used(int* used, int col)
{
int start = col * m;
int cur = 0;
int id;
while (ids[start + cur] != 0 && cur < m)
{
id = ids[start + cur] - 1;
if (ids[start + cur] != -1)
used[id] = 1;
cur++;
}
return cur;
}
int comp_col_data(int* data, float* speed_matrix, int col, int* speed_map)
{
int ttl_data = 0;
int start = col * m;
int cur, id;
for (int i = 0; i < m; i++)
{
id = ids[start + i] - 1;
if (id == -1) // reached empty spot
return ttl_data;
cur = std::min((float) data[id],
(float) speed_map[flr(speed_matrix[start + i])]);
ttl_data += cur;
}
return ttl_data;
}
int push(int user_id, int col)
{
int row = get_top(col);
return place_user(user_id, col, row);
}
int pop(int col)
{
int row = get_top(col);
return place_user(0, col, row - 1);
}
int remove_user(int user_id, int col)
{
int start = col * m;
int cur = 0;
while (ids[start + cur] != user_id && cur < m)
cur++;
if (cur == m)
return -1;
int rem = m - cur - 1;
//we then shift all values after
for (int i = 0; i < rem; i++)
{
ids[start + cur + i] = ids[start + cur + i + 1];
}
ids[start + cur + rem] = 0;
return start + cur;
}
int swap(int user_id_e, int user_id_l)
{
// swap the user_id_e with excess from its best col with user_id_l worst
int best_col = best_without(user_id_e, user_id_l);
int worst_col = worst_without(user_id_l, user_id_e);
if (best_col == -1 || worst_col == -1)
return -1;
int ret;
ret = remove_user(user_id_e, best_col);
if (ret == -1)
return -1;
ret = remove_user(user_id_l, worst_col);
if (ret == -1)
{
// recover old state
push(user_id_e, best_col);
return -1;
}
ret = push(user_id_e, worst_col);
ret = push(user_id_l, best_col);
return ret;
}
void speed_col(User** users, float* speed_matrix, int col)
{
int start = col * m;
int* cur_col = ids + start;
for (int i = 0; i < m; i++)
speed_matrix[start + i] = 0.0;
int row, id;
int total_factor = 0;
float dep;
for (row = 0; row < m; row++)
{
id = cur_col[row];
if (id == 0)
break;
total_factor += users[id - 1]->factor;
}
for (row = 0; row < m; row++)
{
id = cur_col[row];
if (id == 0)
break;
int factor = users[id - 1]->factor;
dep = (factor * (total_factor - factor)) / 10000.0;
speed_matrix[start + row] = std::max((float) 0.0,
(float) users[id - 1]->speed * (1 - dep));
}
}
void compute_speeds(User** users, int num_users, float* speed_matrix)
{
for (int col = 0; col < n; col++)
speed_col(users, speed_matrix, col);
}
// generally called after compute_speeds
void mk_scores(Score** scores, int num_users, float* speed_matrix,
int* speed_map)
{
int tally[num_users];
for (int i = 0; i < num_users; i++)
{
scores[i]->speed = 0.0;
scores[i]->data = 0;
tally[i] = 0;
}
float speed;
int id;
for (int col = 0; col < n; col++)
for (int row = 0; row < m; row++)
{
id = ids[col * m + row];
if (id == 0)
break;
speed = speed_matrix[col * m + row];
scores[id - 1]->speed += speed;
scores[id - 1]->data += speed_map[flr(speed)];
tally[id - 1] += 1;
}
// average out the speed for the scores
for (int i = 0; i < num_users; i++)
scores[i]->speed /= std::max(1, tally[i]);
}
int* copy_ids()
{
// output the transpose (the original matrix)
int s = m * n;
int* new_ids = new int[s];
for (int row = 0; row < m; row++)
for (int col = 0; col < n; col++)
new_ids[row * n + col] = ids[col * m + row];
return new_ids;
}
void show()
{
for (int row = 0; row < m; row++)
{
for (int col = 0; col < n; col++)
{
int user_id = ids[col * m + row];
if (user_id)
{
std::cout << "U" << user_id;
}
else
{
std::cout << "-";
}
if (col != n - 1)
{
std::cout << ",";
}
}
std::cout << std::endl;
}
}
int worst(int user_id)
{
// get column with the most users or -1 if none
int score = 0;
int col = -1;
int count;
for (int i = 0; i < n; i++)
{
count = length(user_id, i);
if (count > score)
{
score = count;
col = i;
}
}
return col;
}
};
// We then define functions for loading inputs from files
// header should be an array of length 4
void load_header(int* header, const char* fname)
{
std::fstream fs;
fs.open(fname, std::fstream::in);
int cur;
fs >> cur;
header[0] = cur;
fs.get();
fs >> cur;
header[1] = cur;
fs >> cur;
header[2] = cur;
fs >> cur;
header[3] = cur;
fs.close();
}
// call load_header first to determine the right allocation size of
// users
void load_users(User** users, int num_users, const char* fname)
{
std::fstream fs;
fs.open(fname, std::fstream::in);
int cur;
// traverse the header
fs >> cur;
fs.get();
fs >> cur;
fs.get();
fs >> cur;
fs.get();
fs >> cur;
fs.get();
User* user;
for (int i = 0; i < num_users; i++)
{
#if TESTING
std::cout << "User " << i << ": ";
#endif
user = users[i];
fs >> cur; // user id which is implicit
fs.get();
fs >> cur; // user speed
fs.get();
user->speed = (float) cur;
#if TESTING
std::cout << user->speed << ", ";
#endif
fs >> cur; // user data
fs.get();
user->data = cur;
#if TESTING
std::cout << user->data << std::endl;
#endif
fs >> cur; // user factor
fs.get();
user->factor = cur;
}
fs.close();
}
void load_speed2bw(const char* fname, int* s2bw_map, int map_size)
{
int cur;
std::fstream fs;
fs.open(fname, std::fstream::in);
for (int i = 0; i < map_size; i++)
{
fs >> cur; // array index which is implicit from speed
fs.get();
fs >> cur; // the actual mapping
s2bw_map[i] = cur;
}
fs.close();
}
void show_scores(User** users, Score** scores, int num_users, int alpha)
{
int max_speed = 0;
int max_data = 0;
float objective = 0.0;
float penalty = 0.0;
int pen;
for (int i = 0; i < num_users; i++)
{
max_speed += users[i]->speed;
max_data += users[i]->data;
objective += scores[i]->speed;
penalty += std::max((float) 0.0,
(float) users[i]->data - scores[i]->data);
}
objective /= max_speed;
penalty /= (float) max_data;
for (int i = 0; i < num_users; i++)
{
pen = std::max((float) 0.0,
(float) users[i]->data - scores[i]->data);
std::cout << pen << ",";
}
std::cout << penalty << std::endl;
for (int i = 0; i < num_users; i++)
{
std::cout << scores[i]->speed << ",";
}
std::cout << objective << std::endl;
std::cout << objective - alpha * penalty << std::endl;
}
void test();
void fill(User** users, int num_users, Grid& grid,
float* speed_matrix, int* speed_map);
void trim(User** users, int num_users, Grid& grid,
float* speed_matrix, int* speed_map);
void swap(User** users, int num_users, Grid& grid,
float* speed_matrix, int* speed_map);
bool feasible(User** users, Score** scores, int num_users)
{
float penalty = 0.0;
for (int i = 0; i < num_users; i++)
{
penalty += std::max((float) 0.0,
(float) users[i]->data - scores[i]->data);
}
return penalty == 0;
}
int main(int argc, char** args)
{
#if TESTING
test();
#endif
if (argc != 2)
{
std::cout << "bad args" << std::endl;
return -1;
}
clock_t start = clock();
const char* fn = args[1];
int header[4];
load_header(header, fn);
int m = header[0];
int n = header[1];
int num_users = header[2];
int alpha = header[3];
int s = n * m;
float* speed_matrix = new float[s];
User* users[num_users];
for (int i = 0; i < num_users; i++)
users[i] = new User;
load_users(users, num_users, fn);
Grid grid = Grid(header);
int map_size = 27;
int* speed_map = new int[map_size];
load_speed2bw("toy_example/speed_to_map.csv", speed_map, map_size);
Score* scores [num_users];
for (int i = 0; i < num_users; i++)
scores[i] = new Score;
int max_recur = 5;
while (true)
{
fill(users, num_users, grid, speed_matrix, speed_map);
grid.compute_speeds(users, num_users, speed_matrix);
grid.mk_scores(scores, num_users, speed_matrix, speed_map);
if (feasible(users, scores, num_users)) break;
max_recur--;
if (max_recur < 0) break;
swap(users, num_users, grid, speed_matrix, speed_map);
trim(users, num_users, grid, speed_matrix, speed_map);
}
grid.show();
show_scores(users, scores, num_users, alpha);
std::cout << clock() - start << std::endl;
for (int i = 0; i < num_users; i++)
delete users[i];
delete [] speed_map;
return 0;
}
int get_most_data(int* used, int* data, int num_users)
{
int most_data = 0;
int user_idx = -1;
for (int i = 0; i < num_users; i++)
{
if (used[i] == 0 && (data[i] > most_data))
{
most_data = data[i];
user_idx = i;
}
}
if (user_idx != -1)
used[user_idx] = 1;
return user_idx;
}
void fill(User** users, int num_users, Grid& grid,
float* speed_matrix, int* speed_map)
{
int m = grid.get_m();
int n = grid.get_n();
float cur, prev;
int nxt_user, row;
int used[num_users];
int data[num_users];
Score* scores [num_users];
for (int i = 0; i < num_users; i++)
scores[i] = new Score;
for (int col = 0; col < n; col++)
{
grid.compute_speeds(users, num_users, speed_matrix);
grid.mk_scores(scores, num_users, speed_matrix, speed_map);
for (int i = 0; i < num_users; i++)
{
used[i] = 0;
data[i] = users[i]->data - scores[i]->data;
}
row = grid.get_used(used, col);
grid.speed_col(users, speed_matrix, col);
cur = grid.comp_col_data(data, speed_matrix, col, speed_map);
for (int i = 0; i < num_users; i++)
{
prev = cur;
// the column is full
if (row == m)
break;
row++;
// determine next user (or lack thereof)
nxt_user = get_most_data(used, data, num_users);
if (nxt_user == -1)
break;
grid.push(nxt_user + 1, col);
// update speeds
grid.speed_col(users, speed_matrix, col);
cur = grid.comp_col_data(data, speed_matrix, col, speed_map);
if (cur < prev)
{
cur = prev;
grid.pop(col);
}
}
}
}
void trim(User** users, int num_users, Grid& grid,
float* speed_matrix, int* speed_map)
{
Score* scores [num_users];
for (int i = 0; i < num_users; i++)
scores[i] = new Score;
grid.compute_speeds(users, num_users, speed_matrix);
grid.mk_scores(scores, num_users, speed_matrix, speed_map);
int col, data;
float avg_speed;
for (int usr = 0; usr < num_users; usr++)
{
data = users[usr]->data;
avg_speed = scores[usr]->speed;
while (data > scores[usr]->data)
{
col = grid.worst(usr + 1);
if (col != -1)
grid.remove_user(usr + 1, col);
else break;
data -= speed_map[flr(avg_speed)];
}
}
}
int get_most_excess(int* used, int* data, int num_users)
{
int most_excess = 0;
int user_idx = -1;
for (int i = 0; i < num_users; i++)
{
if (used[i] == 0 && (data[i] < most_excess))
{
most_excess = data[i];
user_idx = i;
}
}
if (user_idx != -1)
used[user_idx] = 1;
return user_idx;
}
void swap(User** users, int num_users, Grid& grid,
float* speed_matrix, int* speed_map)
{
Score* scores [num_users];
int used[num_users];
int data[num_users];
for (int i = 0; i < num_users; i++)
scores[i] = new Score;
grid.compute_speeds(users, num_users, speed_matrix);
grid.mk_scores(scores, num_users, speed_matrix, speed_map);
for (int i = 0; i < num_users; i++)
{
used[i] = 0;
data[i] = users[i]->data - scores[i]->data;
}
int user_id_e, user_id_l;
while (true)
{
user_id_e = get_most_excess(used, data, num_users);
user_id_l = get_most_data(used, data, num_users);
if (user_id_e == -1 || user_id_l == -1)
break;
grid.swap(user_id_e, user_id_l);
}
}
void test()
{
std::cout << "starting test..." << std::endl;
int header[4];
load_header(header, "toy_example/toy_testcase.csv");
int m = header[0];
int n = header[1];
int num_users = header[2];
int alpha = header[3];
int s = n * m;
assert(m == 3);
assert(n == 5);
assert(num_users == 4);
assert(alpha == 100);
User* users[num_users];
for (int i = 0; i < num_users; i++)
users[i] = new User;
load_users(users, num_users, "toy_example/toy_testcase.csv");
assert(users[0]->speed == 20);
assert(users[0]->data == 4000);
assert(users[0]->factor == 30);
assert(users[1]->speed == 15);
assert(users[1]->data == 8000);
assert(users[1]->factor == 25);
assert(users[2]->speed == 26);
assert(users[2]->data == 8200);
assert(users[2]->factor == 60);
assert(users[3]->speed == 20);
assert(users[3]->data == 18000);
assert(users[3]->factor == 40);
Grid grid = Grid(header);
grid.push(4, 0);
grid.push(3, 0);
grid.push(4, 1);
grid.push(2, 1);
grid.push(3, 2);
grid.push(1, 2);
grid.push(4, 3);
grid.push(2, 3);
grid.push(4, 4);
grid.push(2, 4);
int t_ids[15] = {4,4,3,4,4,3,2,1,2,2,0,0,0,0,0};
int* c_ids = grid.copy_ids();
for (int i = 0; i < 15; i++)
assert(t_ids[i] == c_ids[i]);
int map_size = 27;
int t_sm[map_size]
= {0,290,575,813,1082,1351,1620,1889,2158,2427,2696
,2965,3234,3503,3772,4041,4310,4579,4848,5117,5386
,5655,5924,6093,6360,6611,6800
};
Score* scores [num_users];
for (int i = 0; i < num_users; i++)
scores[i] = new Score;
int* speed_map = new int[map_size];
load_speed2bw("toy_example/speed_to_map.csv", speed_map, map_size);
for (int i = 0; i < map_size; i++)
assert(speed_map[i] == t_sm[i]);
float* speed_matrix = new float[s];
grid.compute_speeds(users, num_users, speed_matrix);
grid.mk_scores(scores, num_users, speed_matrix, speed_map);
grid.show();
show_scores(users, scores, num_users, alpha);
grid.push(3, 4);
assert(-1 == grid.swap(3,1));
assert(0 == grid.swap(1,4));
assert(0 == grid.swap(2,3));
delete c_ids;
c_ids = grid.copy_ids();
assert(c_ids[7] == 4);
assert(c_ids[14] == 1);
assert(c_ids[5] == 2);
assert(c_ids[6] == 3);
delete [] speed_matrix;
delete [] speed_map;
delete c_ids;
for (int i = 0; i < num_users; i++)
{
delete users[i];
delete scores[i];
}
std::cout << "tests ok..." << std::endl;
}