-
Notifications
You must be signed in to change notification settings - Fork 2
/
console.cpp
4312 lines (3883 loc) · 127 KB
/
console.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 "geo_pos.h"
#include "timer.h"
#include "io_helper.h"
#include "id_func.h"
#include "list_graph.h"
#include "filter.h"
#include "multi_arc.h"
#include "sort_arc.h"
#include "chain.h"
#include "connected_components.h"
#include "id_multi_func.h"
#include "histogram.h"
#include "preorder.h"
#include "flow_cutter.h"
#include "id_string.h"
#include "heap.h"
#include "csv.h"
#include "min_max.h"
#include "greedy_order.h"
#include "small_tree_width_order.h"
#include "dijkstra.h"
#include "node_flow_cutter.h"
#include "triangle_count.h"
#include "contraction_graph.h"
#include "separator.h"
#include "tree_node_ranking.h"
#include "tree_decomposition.h"
#include "tree.h"
#include "vector_io.h"
#include "inverse_vector.h"
#include "min_fill_in.h"
#include "refine_cut.h"
#include "inertial_flow.h"
#ifdef USE_KAHIP
#include "my_kahip.h"
#endif
#include <string>
#include <stdexcept>
#include <iostream>
#include <functional>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <random>
#include <functional>
#include <stack>
#include <omp.h>
using namespace std;
ArrayIDIDFunc tail, head;
ArrayIDFunc<int>node_weight, arc_weight;
ArrayIDIDFunc node_color, arc_color;
ArrayIDFunc<GeoPos> node_geo_pos;
ArrayIDIDFunc node_original_position;
void check_graph_consitency(){
#ifndef NDEBUG
const int node_count = tail.image_count(), arc_count = tail.preimage_count();
assert(tail.preimage_count() == arc_count);
assert(tail.image_count() == node_count);
assert(head.preimage_count() == arc_count);
assert(head.image_count() == node_count);
assert(node_weight.preimage_count() == node_count);
assert(arc_weight.preimage_count() == arc_count);
assert(node_color.preimage_count() == node_count);
assert(arc_color.preimage_count() == arc_count);
assert(node_geo_pos.preimage_count() == node_count);
assert(node_original_position.preimage_count() == node_count);
for(auto x:node_color)
assert(0<= x && x < node_color.image_count());
for(auto x:arc_color)
assert(0<= x && x < arc_color.image_count());
#endif
}
stack<ArrayIDIDFunc>node_color_stack;
flow_cutter::Config flow_cutter_config;
bool show_arc_ids = false;
bool show_undirected = false;
bool time_commands = false;
int select_color(string str){
if(str == "most_frequent_node_color")
return max_histogram_id(compute_histogram(node_color));
else if(str == "most_frequent_arc_color")
return max_histogram_id(compute_histogram(arc_color));
else if(str == "least_frequent_node_color")
return min_histogram_id(compute_histogram(node_color));
else if(str == "least_frequent_arc_color")
return min_histogram_id(compute_histogram(arc_color));
else
return stoi(str);
}
static
void keep_nodes_if(const BitIDFunc&node_keep_flag){
int new_node_count = count_true(node_keep_flag);
BitIDFunc arc_keep_flag(tail.preimage_count());
for(int i=0; i<tail.preimage_count(); ++i)
arc_keep_flag.set(i, node_keep_flag(tail(i)) && node_keep_flag(head(i)));
int new_arc_count = count_true(arc_keep_flag);
tail = keep_if(arc_keep_flag, new_arc_count, move(tail));
head = keep_if(arc_keep_flag, new_arc_count, move(head));
arc_weight = keep_if(arc_keep_flag, new_arc_count, move(arc_weight));
arc_color = keep_if(arc_keep_flag, new_arc_count, move(arc_color));
auto node_keep_perm = compute_keep_function(node_keep_flag, new_node_count);
head = chain(std::move(head), node_keep_perm);
tail = chain(std::move(tail), node_keep_perm);
node_color = keep_if(node_keep_flag, new_node_count, std::move(node_color));
node_geo_pos = keep_if(node_keep_flag, new_node_count, std::move(node_geo_pos));
node_weight = keep_if(node_keep_flag, new_node_count, std::move(node_weight));
node_original_position = keep_if(node_keep_flag, new_node_count, std::move(node_original_position));
}
static
void permutate_nodes(const ArrayIDIDFunc&p){
auto inv_p = inverse_permutation(p);
head = chain(std::move(head), inv_p);
tail = chain(std::move(tail), inv_p);
node_color = chain(p, std::move(node_color));
node_geo_pos = chain(p, std::move(node_geo_pos));
node_weight = chain(p, std::move(node_weight));
node_original_position = chain(p, std::move(node_original_position));
}
static
void keep_arcs_if(const BitIDFunc&keep_flag){
int new_arc_count = count_true(keep_flag);
tail = keep_if(keep_flag, new_arc_count, move(tail));
head = keep_if(keep_flag, new_arc_count, move(head));
arc_weight = keep_if(keep_flag, new_arc_count, move(arc_weight));
arc_color = keep_if(keep_flag, new_arc_count, move(arc_color));
}
static
void permutate_arcs(const ArrayIDIDFunc&p){
tail = chain(p, move(tail));
head = chain(p, move(head));
arc_weight = chain(p, move(arc_weight));
arc_color = chain(p, move(arc_color));
}
struct Command{
string name;
int parameter_count;
string description;
function<void(vector<string>)>func;
Command(string name, int parameter_count, string description, function<void(vector<string>)>func):
name(move(name)),
parameter_count(parameter_count),
description(move(description)),
func(move(func)){}
Command(string name, string description, function<void(void)>func):
name(move(name)),
parameter_count(0),
description(move(description)),
func([=](vector<string>){func();}){
}
};
#include "fancy_input.h"
auto w = setw(30);
int new_lock = 0;
void *operator new(size_t size)
{
++new_lock;
return malloc(size);
}
void *operator new [](size_t size)
{
++new_lock;
return malloc(size);
}
void operator delete(void*ptr)
{
--new_lock;
free(ptr);
}
void operator delete [](void*ptr)
{
--new_lock;
free(ptr);
}
vector<Command>cmd = {
{
"get_tmp_dir",
"Returns the path to the temporary files directory",
[]{
cout << get_temp_directory_path() << endl;
}
},
{
"count_active_news",
"Counts how often new was called more than delete. Use this to track down memory leaks.",
[]{
cout << new_lock << endl;
}
},
{
"help",
"It prints a list of all commands.",
[]{
cout << "The following commands exists: \n\n";
for(auto&c:cmd){
cout << " * " << c.name;
if(c.parameter_count != 0)
cout << " ("<<c.parameter_count << " args)";
cout << '\n';
}
cout << "\nUse the details command to get a detailed description of each command."<<endl;
}
},
{
"details", 1,
"It prints a detailed description of the command given as parameter.",
[](vector<string>args){
for(auto&c:cmd)
if(c.name == args[0]){
cout << "Command "<<c.name << " takes "<< c.parameter_count << " parameters. " << c.description << endl;
return;
}
cout << "Command "<<args[0] << " was not found." << endl;
}
},
{
"interactive",
"It takes command from stdin and processes them.",
[]{
#ifdef __OPTIMIZE__
cout << "Compiler optimizations are on" << endl;
#else
cout << "Compiler optimizations are off" << endl;
#endif
#ifndef NDEBUG
cout << "Asserts are on" << endl;
#else
cout << "Asserts are off" << endl;
#endif
string line;
vector<string>cmd_list;
for(auto&x:cmd)
cmd_list.push_back(x.name);
cmd_list.push_back("exit");
set_autocomplete_command_list(move(cmd_list));
while(get_command_line(line)){
try{
istringstream line_in(line);
string command;
line_in >> command;
if(command == "exit")
break;
int c = -1;
for(int i=0; i<(int)cmd.size(); ++i)
if(cmd[i].name == command){
c = i;
break;
}
if(c == -1)
throw runtime_error("Unknown command "+command);
vector<string>args;
string x;
while(line_in >> x)
args.push_back(x);
if((int)args.size() != cmd[c].parameter_count)
throw runtime_error("Wrong number of parameters to command "+cmd[c].name+". expected:"+to_string(cmd[c].parameter_count)+", got:"+to_string(args.size()));
auto prev_time_commands = time_commands;
long long time = -get_micro_time();
cmd[c].func(move(args));
time += get_micro_time();
if(time_commands && prev_time_commands){
cout << "running time : "<<time << "musec" << endl;
}
check_graph_consitency();
}catch(std::exception&err){
cout << "Exception : " << err.what() << endl;
}
}
cout << endl;
}
},
{
"report_time",
"Report the running time of every command",
[]{
time_commands = true;
}
},
{
"do_not_report_time",
"Do not report the running time of every command",
[]{
time_commands = false;
}
},
{
"examine_chordal_supergraph",
"Examines the chordal supergraph produced by contracting the nodes increasing by ID",
[]{
if(!is_symmetric(tail, head))
throw runtime_error("Graph must be symmetric");
const int node_count = tail.image_count();
if(node_count <= 1)
throw runtime_error("Graph must have at least 2 nodes");
int super_graph_arc_count = 0;
int current_tail = -1;
int current_tail_up_deg = 0;
int max_up_deg = 0;
ArrayIDFunc<int>
out_deg(node_count),
parent(node_count);
out_deg.fill(0);
parent.fill(std::numeric_limits<int>::max());
compute_chordal_supergraph(
tail, head,
[&](int x, int y){
++out_deg[x];
if(current_tail != x){
current_tail = x;
max_to(max_up_deg, current_tail_up_deg);
current_tail_up_deg = 0;
}
++super_graph_arc_count;
++current_tail_up_deg;
min_to(parent[x], y);
}
);
ArrayIDFunc<int>ancestor_count(node_count);
ancestor_count.fill(-1);
for(int x=0; x<node_count; ++x){
if(ancestor_count(x) == -1){
int n = 0;
for(int y = x; y != std::numeric_limits<int>::max(); y = parent(y)){
if(ancestor_count(y) == -1){
++n;
}else{
n += ancestor_count(y);
break;
}
}
for(int y = x; y != std::numeric_limits<int>::max(); y = parent(y)){
if(ancestor_count(y) != -1){
assert(ancestor_count(y) == n);
break;
}else{
ancestor_count[y] = n;
--n;
}
}
}
}
int max_ancestor_count = 0;
long long ancestor_count_sum = 0;
for(auto x:ancestor_count){
max_to(max_ancestor_count, x);
ancestor_count_sum += x;
}
ArrayIDFunc<int>arcs_in_search_space = out_deg;
for(int x=node_count-1; x>=0; --x){
if(parent(x) != std::numeric_limits<int>::max()){
arcs_in_search_space[x] += arcs_in_search_space(parent(x));
}
}
int max_arcs_in_search_space = 0;
long long arcs_in_search_space_sum = 0;
for(auto x:arcs_in_search_space){
max_to(max_arcs_in_search_space, x);
arcs_in_search_space_sum += x;
}
long long triangle_count = 0;
for(int x=0; x<node_count; ++x){
triangle_count += (out_deg(x)*(out_deg(x)-1))/2;
}
auto w = setw(35);
cout
<< w << "super_graph_upward_arc_count" << " : " << super_graph_arc_count << '\n'
<< w << "upper tree width bound" << " : " << max_up_deg << '\n'
<< w << "elimination tree height" << " : " << max_ancestor_count << '\n'
<< w << "average elimination tree depth" << " : " << (static_cast<double>(ancestor_count_sum)/node_count) << '\n'
<< w << "maximum arcs in search space" << " : " << max_arcs_in_search_space << '\n'
<< w << "average arcs in search space" << " : " << (static_cast<double>(arcs_in_search_space_sum)/node_count) << '\n'
<< w << "number of triangles in super graph" << " : " << triangle_count << endl;
}
},
{
"find_longest_elimination_tree_path",
"Find the node IDs of the longest path leaf root path in the elimination tree",
[]{
if(!is_symmetric(tail, head))
throw runtime_error("Graph must be symmetric");
const int node_count = tail.image_count();
if(node_count <= 1)
throw runtime_error("Graph must have at least 2 nodes");
ArrayIDFunc<int>parent(tail.image_count());
parent.fill(std::numeric_limits<int>::max());
compute_chordal_supergraph(
tail, head,
[&](int x, int y){
min_to(parent[x], y);
}
);
ArrayIDFunc<int>ancestor_count(tail.image_count());
ancestor_count.fill(-1);
for(int x=0; x<node_count; ++x){
if(ancestor_count(x) == -1){
int n = 0;
for(int y = x; y != std::numeric_limits<int>::max(); y = parent(y)){
if(ancestor_count(y) == -1){
++n;
}else{
n += ancestor_count(y);
break;
}
}
for(int y = x; y != std::numeric_limits<int>::max(); y = parent(y)){
if(ancestor_count(y) != -1){
assert(ancestor_count(y) == n);
break;
}else{
ancestor_count[y] = n;
--n;
}
}
}
}
int x = max_preimage_over_id_func(ancestor_count);
BitIDFunc in_tree(node_count);
in_tree.fill(false);
while(parent(x) != std::numeric_limits<int>::max()){
in_tree.set(x, true);
x = parent(x);
}
in_tree.set(x, true);
cout << make_id_string(in_tree) << endl;
}
},
{
"find_largest_clique_in_chordal_supergraph",
"Examines the chordal supergraph produced by contracting the nodes increasing by ID",
[]{
if(!is_symmetric(tail, head))
throw runtime_error("Graph must be symmetric");
const int node_count = tail.image_count();
if(node_count <= 1)
throw runtime_error("Graph must have at least 2 nodes");
BitIDFunc largest_clique(node_count);
int largest_clique_size = 0;
largest_clique.fill(false);
BitIDFunc current_clique(node_count);
int current_clique_size = 0;
current_clique.fill(false);
vector<int>nodes_in_current_clique;
int current_tail = -1;
compute_chordal_supergraph(
tail, head,
[&](int x, int y){
if(current_tail != x){
if(current_clique_size > largest_clique_size){
largest_clique_size = current_clique_size;
largest_clique = current_clique;
}
current_tail = x;
for(auto z:nodes_in_current_clique)
current_clique.set(z, false);
current_clique.set(x, true);
nodes_in_current_clique = {x};
current_clique_size = 1;
}
current_clique.set(y, true);
nodes_in_current_clique.push_back(y);
++current_clique_size;
}
);
cout << make_id_string(largest_clique) << endl;
}
},
{
"push_node_color",
"Saves the current node colors",
[]{
node_color_stack.push(node_color);
}
},
{
"pop_node_color",
"Loads the last stored node colors",
[]{
if(node_color_stack.empty())
throw runtime_error("No node colors were saved");
auto x = std::move(node_color_stack.top());
node_color_stack.pop();
if(x.preimage_count() != node_color.preimage_count())
cout << "Discarding top node colors because the number of nodes do not match" << endl;
else
x.swap(node_color);
}
},
{
"save_node_color", 2,
"Saves the current node colors (arg1) to file (arg2). arg1 can be \"all\"",
[](vector<string>arg){
BitIDFunc save_color(node_color.image_count());
if(arg[0] == "all")
save_color.fill(true);
else{
save_color.fill(false);
forall_in_id_string(
arg[0],
[&](int x){
if(x < 0 || x >= node_color.image_count())
throw runtime_error("Node color "+to_string(x)+" is out of bounds");
save_color.set(x, true);
}
);
}
save_text_file(
arg[1],
[&](ostream&out){
out << "node_id,color\n";
for(int i=0; i<node_color.preimage_count(); ++i)
if(save_color(node_color(i)))
out << i <<',' << node_color(i) << '\n';
}
);
}
},
{
"load_node_color", 1,
"Loads all node colors stored in file arg1",
[](vector<string>arg){
io::CSVReader<2>in(arg[0]);
in.read_header(io::ignore_extra_column, "node_id", "color");
int node_id, color;
while(in.read_row(node_id, color)){
if(color < 0)
throw runtime_error("invalid color id "+to_string(color));
if(color > node_color.image_count())
node_color.set_image_count(color+1);
if(node_id < 0 || node_id >= node_color.preimage_count())
throw runtime_error("invalid node id "+to_string(node_id));
node_color[node_id] = color;
}
}
},
{
"map_node_color_id", 2,
"All nodes with color (arg1) get assigned color (arg2).",
[](vector<string>arg){
int new_color = select_color(arg[1]), old_color = select_color(arg[0]);
if(new_color >= node_color.image_count())
node_color.set_image_count(new_color+1);
for(int i=0; i<tail.image_count(); ++i)
if(node_color(i) == old_color)
node_color[i] = new_color;
}
},
{
"load_binary_graph", 1,
"Loads a graph in the binary format.",
[](vector<string>args){
auto graph = load_binary_graph(args[0]);
tail = std::move(graph.tail);
head = std::move(graph.head);
node_weight = std::move(graph.node_weight);
arc_weight = std::move(graph.arc_weight);
node_color = ArrayIDIDFunc(tail.image_count(), 1);
node_color.fill(0);
node_geo_pos = ArrayIDFunc<GeoPos>(tail.image_count());
node_geo_pos.fill({0.0, 0.0});
node_original_position = identity_permutation(tail.image_count());
arc_color = ArrayIDIDFunc(tail.preimage_count(), 1);
arc_color.fill(0);
}
},
{
"load_connection_graph", 3,
"Loads a train network.\nstop_file = arg1\nconnection_file = arg2\nfootpath graph = arg3\n The connections are mapped to arcs. Trips to arcs colors. Stops to nodes. Geopos are loaded. Change times are mapped to node weights. Travel times are mapped to arc weights. Footpaths are also mapped to arcs. All footpaths have the same arc color.",
[](vector<string>arg){
string stop_file = arg[0];
string conn_file = arg[1];
string footpath_file = arg[2];
int stop_count = 0;
{
io::LineReader in(stop_file);
while(in.next_line())
++stop_count;
}
int footpath_count = 0;
{
io::LineReader in(footpath_file);
while(in.next_line())
++footpath_count;
}
int conn_count = 0;
int trip_count = 0;
{
io::CSVReader<1>in(conn_file);
in.read_header(io::ignore_extra_column, "trip_id");
int trip_id;
while(in.read_row(trip_id)){
++conn_count;
if(trip_id >= trip_count)
trip_count = trip_id+1;
}
}
tail = ArrayIDIDFunc(conn_count+footpath_count, stop_count);
head = ArrayIDIDFunc(conn_count+footpath_count, stop_count);
arc_weight = ArrayIDFunc<int>(conn_count+footpath_count);
node_weight = ArrayIDFunc<int>(stop_count);
node_color = ArrayIDIDFunc(stop_count, 1);
node_color.fill(0);
node_geo_pos = ArrayIDFunc<GeoPos>(stop_count);
arc_color = ArrayIDIDFunc(conn_count+footpath_count, trip_count+1);
node_original_position = identity_permutation(stop_count);
cout << w << "Footpath color" << " : "<<trip_count << endl;
int arc_id = 0;
{
io::CSVReader<5>in(conn_file);
in.read_header(io::ignore_extra_column, "dep_stop", "arr_stop", "dep_time", "arr_time", "trip_id");
int dep_stop, arr_stop, dep_time, arr_time, trip_id;
while(in.read_row(dep_stop, arr_stop, dep_time, arr_time, trip_id)){
if(dep_stop < 0 || dep_stop >= stop_count)
throw runtime_error("dep_stop out of bounds");
if(arr_stop < 0 || arr_stop >= stop_count)
throw runtime_error("arr_stop out of bounds");
if(trip_id < 0 || trip_id >= trip_count)
throw runtime_error("trip_id out of bounds");
tail[arc_id] = dep_stop;
head[arc_id] = arr_stop;
arc_weight[arc_id] = arr_time - dep_time;
arc_color[arc_id] = trip_id;
++arc_id;
}
}
{
io::CSVReader<3>in(footpath_file);
in.read_header(io::ignore_extra_column, "dep_stop", "arr_stop", "duration");
int dep_stop, arr_stop, duration;
while(in.read_row(dep_stop, arr_stop, duration)){
if(dep_stop < 0 || dep_stop >= stop_count)
throw runtime_error("dep_stop out of bounds");
if(arr_stop < 0 || arr_stop >= stop_count)
throw runtime_error("arr_stop out of bounds");
tail[arc_id] = dep_stop;
head[arc_id] = arr_stop;
arc_weight[arc_id] = duration;
arc_color[arc_id] = trip_count;
++arc_id;
}
}
{
io::CSVReader<4>in(stop_file);
in.read_header(io::ignore_extra_column, "stop_id", "change_time", "lon", "lat");
GeoPos geo_pos;
int stop_id, change_time = 0;
while(in.read_row(stop_id, change_time, geo_pos.lon, geo_pos.lat)){
if(stop_id < 0 || stop_id >= stop_count)
throw runtime_error("stop_id out of bounds");
node_geo_pos[stop_id] = geo_pos;
node_weight[stop_id] = change_time;
}
}
}
},
{
"load_dimacs_graph", 1,
"Loads a graph in the DIMACS format. All node weights are set to 1.",
[](vector<string>args){
auto graph = load_dimacs_graph(args[0]);
tail = std::move(graph.tail);
head = std::move(graph.head);
node_weight = std::move(graph.node_weight);
arc_weight = std::move(graph.arc_weight);
node_color = ArrayIDIDFunc(tail.image_count(), 1);
node_color.fill(0);
node_geo_pos = ArrayIDFunc<GeoPos>(tail.image_count());
node_geo_pos.fill({0.0, 0.0});
node_original_position = identity_permutation(tail.image_count());
arc_color = ArrayIDIDFunc(tail.preimage_count(), 1);
arc_color.fill(0);
}
},
{
"make_grid", 1,
"makes a n times n grid, n = arg0",
[](vector<string>args){
const int n = stoi(args[0]);
const int node_count = n*n;
const int arc_count = 2*( (n-1)*(n-1)*2 + 2*(n-1));
tail = ArrayIDIDFunc(arc_count, node_count);
head = ArrayIDIDFunc(arc_count, node_count);
node_weight = ArrayIDFunc<int>(node_count);
node_weight.fill(1);
arc_weight = ArrayIDFunc<int>(arc_count);
arc_weight.fill(1);
auto node = [&](int x, int y){
return x + y*n;
};
int arc = 0;
for(int y=0; y<n; ++y){
for(int x=0; x<n; ++x){
if(x != n-1){
assert(arc != arc_count);
tail[arc] = node(x, y);
head[arc] = node(x+1, y);
++arc;
}
if(x != 0){
assert(arc != arc_count);
tail[arc] = node(x, y);
head[arc] = node(x-1, y);
++arc;
}
if(y != n-1){
assert(arc != arc_count);
tail[arc] = node(x, y);
head[arc] = node(x, y+1);
++arc;
}
if(y != 0){
assert(arc != arc_count);
tail[arc] = node(x, y);
head[arc] = node(x, y-1);
++arc;
}
}
}
assert(arc == arc_count);
node_color = ArrayIDIDFunc(node_count, 1);
node_color.fill(0);
node_geo_pos = ArrayIDFunc<GeoPos>(node_count);
for(int y=0; y<n; ++y)
for(int x=0; x<n; ++x)
node_geo_pos[node(x, y)] = GeoPos{double(x)/double(n-1), double(y)/double(n-1)};
node_original_position = identity_permutation(node_count);
arc_color = ArrayIDIDFunc(arc_count, 1);
arc_color.fill(0);
ArrayIDFunc<int>level(node_count);
level.fill(0);
int current_level = 0;
for(int step=1; step<n; step*=2){
for(int y=step-1; y<n; y+=step)
for(int x=0; x<n; ++x)
level[node(x, y)] = current_level;
++current_level;
for(int x=step-1; x<n; x+=step)
for(int y=0; y<n; ++y)
level[node(x, y)] = current_level;
++current_level;
}
ArrayIDIDFunc perm = identity_permutation(node_count);
stable_sort(perm.begin(), perm.end(), [&](int x, int y){return level[x] < level[y];});
permutate_nodes(perm);
}
},
{
"load_pace_graph", 1,
"Loads a graph in the PACE 2016 format. All node weights are set to 1.",
[](vector<string>args){
auto graph = load_pace_graph(args[0]);
tail = std::move(graph.tail);
head = std::move(graph.head);
node_weight = std::move(graph.node_weight);
arc_weight = std::move(graph.arc_weight);
node_color = ArrayIDIDFunc(tail.image_count(), 1);
node_color.fill(0);
node_geo_pos = ArrayIDFunc<GeoPos>(tail.image_count());
node_geo_pos.fill({0.0, 0.0});
node_original_position = identity_permutation(tail.image_count());
arc_color = ArrayIDIDFunc(tail.preimage_count(), 1);
arc_color.fill(0);
}
},
{
"load_pace_tree_decomposition_as_chordal_graph", 1,
"Loads tree decomposition in the PACE 2016 format and turns it into a chordal graph with multi arcs.",
[](vector<string>args){
int node_count = -1;
vector<vector<int>>bags;
load_uncached_text_file(
args[0],
[&](istream&in){
string line;
while(getline(in, line)){
istringstream lin(line);
char c = lin.get();
if(c == 'b'){
int n;
lin >> n; // ignore bag size
vector<int>bag;
while(lin >> n)
bag.push_back(n-1);
bag.shrink_to_fit();
bags.push_back(move(bag));
} else if(c == 's'){
if(node_count != -1)
throw runtime_error("header appears twice");
string ignore;
lin >> ignore >> ignore >> ignore >> node_count;
}
}
if(node_count == -1)
throw runtime_error("header missing");
for(auto&bag:bags)
for(auto x:bag)
if(x >= node_count || x< 0 )
throw runtime_error("node ID out of bounds");
}
);
int arc_count = 0;
for(auto&bag:bags)
arc_count += bag.size()*(bag.size()-1);
tail = ArrayIDIDFunc(arc_count, node_count);
head = ArrayIDIDFunc(arc_count, node_count);
int next_arc_id = 0;
for(auto&bag:bags)
for(auto x:bag)
for(auto y:bag)
if(x != y){
assert(arc_count > next_arc_id);
tail[next_arc_id] = x;
head[next_arc_id] = y;
++next_arc_id;
}
assert(arc_count == next_arc_id);
node_weight = ArrayIDIDFunc(tail.image_count(), 1);
node_weight.fill(0);
arc_weight = ArrayIDIDFunc(tail.preimage_count(), 1);
arc_weight.fill(0);
node_color = ArrayIDIDFunc(tail.image_count(), 1);
node_color.fill(0);
node_geo_pos = ArrayIDFunc<GeoPos>(tail.image_count());
node_geo_pos.fill({0.0, 0.0});
node_original_position = identity_permutation(tail.image_count());
arc_color = ArrayIDIDFunc(tail.preimage_count(), 1);
arc_color.fill(0);
}
},
{
"compute_chordal_supergraph",
"Computes the chordal supergraph and replaces the current graph.",
[]{
std::vector<int>new_tail, new_head;
compute_chordal_supergraph(
tail, head,
[&](int x, int y){
new_tail.push_back(x);
new_head.push_back(y);
}
);
tail = id_id_func(new_tail.size(), tail.image_count(), [&](int x){return new_tail[x];});
head = id_id_func(new_head.size(), head.image_count(), [&](int x){return new_head[x];});
node_weight = id_func(tail.image_count(), [](int){return 1;});
arc_weight = id_func(tail.preimage_count(), [](int){return 1;});
arc_color = ArrayIDIDFunc(tail.preimage_count(), 1);
arc_color.fill(0);
}
},
{
"load_color_dimacs_graph", 1,
"Loads a graph in the DIMACS format. All node weights are set to 1.",
[](vector<string>args){
auto graph = load_color_dimacs_graph(args[0]);
tail = std::move(graph.tail);
head = std::move(graph.head);
node_weight = std::move(graph.node_weight);
arc_weight = std::move(graph.arc_weight);
node_color = ArrayIDIDFunc(tail.image_count(), 1);
node_color.fill(0);
node_geo_pos = ArrayIDFunc<GeoPos>(tail.image_count());
node_geo_pos.fill({0.0, 0.0});
node_original_position = identity_permutation(tail.image_count());
arc_color = ArrayIDIDFunc(tail.preimage_count(), 1);
arc_color.fill(0);
}
},
{
"generate_random_tree", 1,
"Generates a random tree graph with arg1 nodes.",
[](vector<string>args){
const int node_count = std::stoi(args[0]);
if(node_count < 0)
throw runtime_error("node count must not be negative");
std::tie(tail, head) = generate_random_tree(node_count);
node_weight = ArrayIDFunc<int>(tail.image_count());
node_weight.fill(1);