-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval.cpp
327 lines (298 loc) · 10.4 KB
/
eval.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
// (C) 2014 Arek Olek
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/adjacency_matrix.hpp>
#include <omp.h>
#include "bfs.hpp"
#include "dfs.hpp"
#include "fifodfs.hpp"
#include "fivethree.hpp"
#include "lost.hpp"
#include "rdfs.hpp"
#include "random.hpp"
#include "ilst.hpp"
#include "greedy.hpp"
#include "test_suite.hpp"
#include "debug.hpp"
#include "graph.hpp"
#include "options.hpp"
#include "range.hpp"
#include "timing.hpp"
boost::adjacency_matrix<boost::undirectedS> typedef amatrix;
boost::adjacency_list<boost::hash_setS, boost::vecS, boost::undirectedS> typedef ahash;
boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS> typedef alist;
boost::adjacency_list<boost::slistS, boost::vecS, boost::undirectedS> typedef aslist;
boost::adjacency_list<boost::setS, boost::vecS, boost::undirectedS> typedef aset;
boost::adjacency_list<boost::multisetS, boost::vecS, boost::undirectedS> typedef amultiset;
boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS> typedef avec;
template<class Graph, class Tree>
std::function<Tree(Graph&)> make_construction(std::string name,
unsigned index,
unsigned size,
unsigned seed,
std::string sseed) {
if (name == "bfs")
return bfs_tree<Graph, Tree> ;
if (name == "dfs")
return dfs_tree<Graph, Tree> ;
if (name == "rdfs")
return rdfs_tree<Graph, Tree> ;
if (name == "rdfs50")
return rdfs_best_tree<Graph, Tree> ;
if (name == "fifo")
return fifo_dfs_tree<Graph, Tree> ;
if (name == "random")
return std::bind(random_tree<Graph, Tree>, std::placeholders::_1, seed);
if (name == "wilson")
return std::bind(wilson_tree<Graph, Tree>, std::placeholders::_1, seed);
if (name == "greedy")
return std::bind(greedy_tree<Graph, Tree>, std::placeholders::_1, seed);
if (name == "ilst")
return ilst<Graph, Tree> ;
if (name == "5/3") {
return [=](const Graph& G) {
std::default_random_engine generator(seed);
Tree g(num_vertices(G));
copy_edges_shuffled(five_three_tree<Graph, Tree>(G), g, generator);
return g;
};
}
if (found(".xml", name)) {
return [=](const Graph& G) {
real_suite<Tree> suite(name, size, sseed);
return std::get<0>(suite.get(index));
};
}
throw std::invalid_argument("Unknown construction method: " + name);
}
template<class Graph>
std::function<unsigned(Graph&)> make_upper(std::string name) {
if (name == "53") {
return [](const Graph& G) {
return 5*(num_vertices(G)-3)/6;
};
}
return upper_bound<Graph> ;
}
std::string type(amatrix const & G) { return "matrix"; }
std::string type(ahash const & G) { return "unordered_set"; }
std::string type(aset const & G) { return "set"; }
std::string type(amultiset const & G) { return "multiset"; }
std::string type(alist const & G) { return "list"; }
std::string type(aslist const & G) { return "slist"; }
std::string type(avec const & G) { return "vector"; }
template <class Graph, class Tree, class Suite, class Strings>
void run(Suite& suite,
Strings const & constructions,
Strings const & improvements,
bool scratch,
std::string seed) {
#ifndef NPROGRESS
timing total(CLOCK_REALTIME);
total.start();
#endif
auto graph_type = type(Graph(0));
auto tree_type = type(Tree(0));
#pragma omp parallel
{
auto id = omp_get_thread_num();
double elapsed_c, elapsed_i;
std::stringstream buffer;
timing timer;
#pragma omp for schedule(dynamic) nowait
for(uint i = 0; i < suite.size(); ++i) {
auto trial = suite.get(i);
auto G = std::get<0>(trial);
for (auto cname : constructions) {
auto construct = make_construction<Graph, Tree>(cname, i, suite.size(), suite.get_seed(i), seed);
auto upper = make_upper<Graph>(suite.type());
timer.start();
auto T = construct(G);
elapsed_c = timer.stop();
assert(num_edges(T) == num_vertices(T) - 1);
assert(is_connected(T));
assert(is_subgraph(T, G));
const auto tree(T);
for (auto iname : improvements) {
if (scratch) T = tree;
auto num_internal_before = num_internal(T);
auto improve = make_improvement<Graph, Tree>(iname);
timer.start();
auto rules = improve(G, T);
elapsed_i = timer.stop();
buffer
#ifdef GRAPH_REPR
<< graph_type << '\t'
#endif
#ifdef TREE_REPR
<< tree_type << '\t'
#endif
<< std::get<1>(trial) << '\t'
<< id << '\t'
<< suite.type() << '\t'
<< std::get<2>(trial) << '\t'
<< std::get<3>(trial) << '\t'
<< num_vertices(G) << '\t'
<< num_edges(G) << '\t'
<< upper(G) << '\t'
<< cname << '\t'
<< iname << '\t'
<< num_internal_before << '\t'
<< num_internal(T) << '\t'
<< elapsed_c << '\t'
<< elapsed_i << '\t'
<< sum(rules) << '\t'
<< join(rules, "-")
<< std::endl;
;
#ifndef NDEBUG
if (suite.size() < 10)
show("graph" + std::to_string(i) + graph_type + ".dot", G, T);
//if(rules[rules.size()-2] > 10) show("graph" + std::to_string(i) + ".dot", G, T);
#endif
if (!scratch) elapsed_c += elapsed_i;
}
}
#ifndef NPROGRESS
if (id == 0 || i + 1 == suite.size()) {
std::cerr << '\t' << 100 * (i + 1) / suite.size() << "% of "
<< suite.size();
std::cerr << '\t' << readable(total.stop()) << " elapsed \r";
}
#endif
}
#pragma omp critical
std::cout << buffer.str() << std::flush;
}
#ifndef NPROGRESS
std::cerr << std::endl;
#endif
}
template <class Graph, class Tree, class Sizes, class Params, class Strings>
void run(std::string t,
unsigned z,
Sizes sizes,
Params params,
Strings const & constructions,
Strings const & improvements,
bool scratch,
std::string seed) {
if (found(".xml", t)) {
real_suite<Graph> suite(t, z, seed);
run<Graph, Tree>(suite, constructions, improvements, scratch, seed);
} else if (found(".", t)) {
file_suite<Graph> suite(t, z, seed);
run<Graph, Tree>(suite, constructions, improvements, scratch, seed);
} else {
test_suite<Graph> suite(t, z, sizes, params, seed);
run<Graph, Tree>(suite, constructions, improvements, scratch, seed);
}
}
template <class Graph, class Sizes, class Degrees, class Strings>
void run(std::string tree,
std::string model,
unsigned sample,
Sizes sizes,
Degrees degrees,
Strings const & constructions,
Strings const & improvements,
bool scratch,
std::string seed) {
#ifdef TREE_REPR
if (tree == "matrix")
run<Graph, amatrix> (model, sample, sizes, degrees, constructions, improvements, scratch, seed);
if (tree == "unordered_set")
run<Graph, ahash> (model, sample, sizes, degrees, constructions, improvements, scratch, seed);
if (tree == "multiset")
run<Graph, amultiset>(model, sample, sizes, degrees, constructions, improvements, scratch, seed);
if (tree == "list")
run<Graph, alist> (model, sample, sizes, degrees, constructions, improvements, scratch, seed);
if (tree == "set")
run<Graph, aset> (model, sample, sizes, degrees, constructions, improvements, scratch, seed);
if (tree == "vector")
run<Graph, avec> (model, sample, sizes, degrees, constructions, improvements, scratch, seed);
#endif
if (tree == "slist")
run<Graph, aslist> (model, sample, sizes, degrees, constructions, improvements, scratch, seed);
}
template <class Sizes, class Degrees, class Strings>
void run(std::string graph,
std::string tree,
std::string model,
unsigned sample,
Sizes sizes,
Degrees degrees,
Strings const & constructions,
Strings const & improvements,
bool scratch,
std::string seed) {
#ifdef GRAPH_REPR
if (graph == "matrix")
run<amatrix> (tree, model, sample, sizes, degrees, constructions, improvements, scratch, seed);
if (graph == "unordered_set")
run<ahash> (tree, model, sample, sizes, degrees, constructions, improvements, scratch, seed);
if (graph == "multiset")
run<amultiset>(tree, model, sample, sizes, degrees, constructions, improvements, scratch, seed);
if (graph == "slist")
run<aslist> (tree, model, sample, sizes, degrees, constructions, improvements, scratch, seed);
if (graph == "vector")
run<avec> (tree, model, sample, sizes, degrees, constructions, improvements, scratch, seed);
if (graph == "list")
run<alist> (tree, model, sample, sizes, degrees, constructions, improvements, scratch, seed);
#endif
if (graph == "set")
run<aset> (tree, model, sample, sizes, degrees, constructions, improvements, scratch, seed);
}
std::string time_seed() {
return std::to_string(std::chrono::system_clock::now().time_since_epoch().count());
}
int main(int argc, char** argv){
using std::string;
using std::vector;
std::ios_base::sync_with_stdio(0);
options opt(argc, argv);
auto z = opt.get<int>("-z", 100);
auto graphs = opt.getList<string>("-g", {"set"});
auto trees = opt.getList<string>("-t", {"slist"});
auto sizes = opt.getList<unsigned>("-n", {1000});
auto tests = opt.getList<string>("-m", {"gnp+mst"});
auto degrees = opt.getList<double>("-d", { 3. });
auto constructions = opt.getList<string>("-c", {"wilson"});
auto improvements = opt.getList<string>("-i", {"none"});
auto scratch = opt.has("--scratch");
auto seed = opt.get<string>("-s", time_seed());
std::cout << "# Seed: " << seed << std::endl;
std::cout
#ifdef GRAPH_REPR
<< "graph\t"
#endif
#ifdef TREE_REPR
<< "tree\t"
#endif
<< "run\t"
<< "thread\t"
<< "model\t"
<< "degree\t"
<< "param\t"
<< "vertices\t"
<< "edges\t"
<< "upper\t"
<< "construction\t"
<< "improvement\t"
<< "before\t"
<< "internal\t"
<< "ctime\t"
<< "itime\t"
<< "steps\t"
<< "rules"
<< std::endl;
for(auto t : tests)
for(auto graph : graphs)
for(auto tree : trees)
run(graph, tree, t, z, sizes, degrees, constructions, improvements,
scratch, seed);
return 0;
}