-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
276 lines (239 loc) · 9.12 KB
/
main.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
#include "main.hpp"
#include <filesystem> // std::filesystem::exists
#include <chrono> // time measurement
#include <numeric> // accumulate
#include <string.h> // strcmp
#include <unistd.h> // getopt
#include <algorithm> // reverse
#include <random> // shuffle
#include "be.hpp"
#include "io.hpp"
#include "log.hpp"
#include "order.hpp"
#include "conversion.hpp"
// fmt library
#include <fmt/core.h>
#include <fmt/ranges.h>
#include <fmt/chrono.h>
// print tables after parsing
#define PRINT_TABLES
// export dot representation of created automata
//#define EXPORT_AUTOMATA_DOT
bool parallel = false;
static inline void print_usage(const char *bin) {
fmt::print(stderr, "Usage: {} [-h] [-a bub|brz|hop] [-i bound] [-o wmf|mf|miw|md|random|*.pt] ", bin);
fmt::print(stderr, "[-t unique|random] [-s seed] [-O order] [-r] -f instance\n");
}
int main(int argc, char *argv[]) {
fa_minimization_algorithm = FA_MIN_BUBENZER;
value tolerance = 0;
size_t ibound = 0;
int ord_heur = O_WEIGHTED_MIN_FILL;
int tie_heur = T_UNIQUENESS;
char *instance = nullptr;
char *pseudotree = nullptr;
char *order_file = nullptr;
size_t seed = time(nullptr);
bool print_red = false;
int opt;
while ((opt = getopt(argc, argv, "a:i:f:o:t:s:O:hr")) != -1) {
switch (opt) {
case 'a':
if (strcmp(optarg, "bub") == 0) {
fa_minimization_algorithm = FA_MIN_BUBENZER;
} else if (strcmp(optarg, "brz") == 0) {
fa_minimization_algorithm = FA_MIN_BRZOZOWSKI;
} else if (strcmp(optarg, "hop") == 0) {
fa_minimization_algorithm = FA_MIN_HOPCROFT;
} else {
fmt::print(stderr, "{}: invalid minimisation algorithm -- '{}'\n", argv[0], optarg);
print_usage(argv[0]);
return EXIT_FAILURE;
}
continue;
case 'i':
ibound = std::max(0, atoi(optarg));
continue;
case 'f':
if (std::filesystem::exists(optarg)) {
instance = optarg;
} else {
fmt::print(stderr, "{}: file not found -- '{}'\n", argv[0], optarg);
print_usage(argv[0]);
return EXIT_FAILURE;
}
continue;
case 'o':
if (strcmp(optarg, "wmf") == 0) {
ord_heur = O_WEIGHTED_MIN_FILL;
} else if (strcmp(optarg, "mf") == 0) {
ord_heur = O_MIN_FILL;
} else if (strcmp(optarg, "miw") == 0) {
ord_heur = O_MIN_INDUCED_WIDTH;
} else if (strcmp(optarg, "md") == 0) {
ord_heur = O_MIN_DEGREE;
} else if (strcmp(optarg, "random") == 0) {
ord_heur = O_RANDOM;
} else if (std::filesystem::exists(optarg)) {
pseudotree = optarg;
} else {
fmt::print(stderr, "{}: file not found -- '{}'\n", argv[0], optarg);
print_usage(argv[0]);
return EXIT_FAILURE;
}
continue;
case 't':
if (strcmp(optarg, "unique") == 0) {
tie_heur = T_UNIQUENESS;
} else if (strcmp(optarg, "random") == 0) {
tie_heur = T_RANDOM;
} else {
fmt::print(stderr, "{}: tie-breaking heuristic not valid -- '{}'\n", argv[0], optarg);
print_usage(argv[0]);
return EXIT_FAILURE;
}
continue;
case 's':
seed = atoi(optarg);
continue;
case 'r':
print_red = true;
continue;
case 'O':
order_file = optarg;
continue;
case 'h':
default :
print_usage(argv[0]);
return EXIT_FAILURE;
}
}
if (!instance) {
fmt::print(stderr, "{}: instance not specified!\n", argv[0]);
print_usage(argv[0]);
return EXIT_FAILURE;
}
log_line();
log_title("Finite-State Automata Bucket Elimination (FABE)");
log_title("https://github.com/filippobistaffa/FABE");
log_line();
log_fmt("Instance", instance);
int inst_type;
if (strstr(instance, "wcsp")) {
log_fmt("Instance type", "WCSP");
//log_fmt("Bucket elimination algorithm", "MIN-SUM");
inst_type = WCSP;
} else {
log_fmt("Instance type", "UAI");
//log_fmt("Bucket elimination algorithm", "MIN-SUM with -log()");
inst_type = MPE;
}
std::string algorithms[] = { "Hopcroft", "Brzozowski", "Bubenzer" };
log_fmt("Automata minimisation algorithm", algorithms[fa_minimization_algorithm]);
log_fmt("I-bound", (ibound == 0) ? "inf" : fmt::format("{}", ibound));
if constexpr (QUANTISATION) {
if (inst_type == WCSP) {
log_fmt("Precision", "-");
} else {
log_fmt("Precision", 1 / QUANTISATION);
}
} else {
log_fmt("Precision", "-");
}
//log_fmt("Tolerance", tolerance);
//log_fmt("Parallel mode", parallel ? "Enabled" : "Disabled");
// look for a known threshold to remove rows
value threshold = std::numeric_limits<value>::max();
for (size_t i = 0; i < N_DATASETS; ++i) {
if (strstr(instance, datasets[i]) != NULL) {
threshold = thresholds[i];
}
}
log_fmt("Thresholds value", threshold);
auto [ domains, adj ] = read_domains_adj(instance, inst_type);
//print_adj(adj);
//fmt::print("\n");
log_fmt("Seed", seed);
std::string ord_heur_names[] = { "WEIGHTED-MIN-FILL", "MIN-FILL", "MIN-INDUCED-WIDTH", "MIN-DEGREE" };
std::string tie_heur_names[] = { "MIN-UNIQUENESS", "RANDOM" };
std::vector<size_t> order;
auto start_t = std::chrono::high_resolution_clock::now();
if (pseudotree) {
log_fmt("Variable order heuristic", pseudotree);
order = read_pseudotree_order(pseudotree, domains);
} else {
srand(seed);
if (ord_heur == O_RANDOM) {
log_fmt("Variable order heuristic", "RANDOM");
order.resize(domains.size());
std::iota(order.begin(), order.end(), 0);
std::shuffle(order.begin(), order.end(), std::default_random_engine{seed});
} else {
log_fmt("Variable order heuristic", ord_heur_names[ord_heur]);
log_fmt("Tie-breaking heuristic", tie_heur_names[tie_heur]);
order = greedy_order(adj, ord_heur, tie_heur);
}
}
//fmt::print("Order: {}\n", order);
std::reverse(order.begin(), order.end());
std::vector<size_t> pos(order.size());
for (size_t i = 0; i < order.size(); ++i) {
pos[order[i]] = i;
}
std::chrono::duration<double> runtime = std::chrono::high_resolution_clock::now() - start_t;
log_fmt("Order computation runtime", fmt::format("{:%T}", runtime));
start_t = std::chrono::high_resolution_clock::now();
log_fmt("Induced width", induced_width(adj, order));
if (order_file) {
export_order(order, domains, order_file);
log_fmt("Order file", order_file);
log_line();
return EXIT_SUCCESS;
}
auto tables = read_tables(instance, inst_type, pos, threshold);
#ifdef PRINT_TABLES
fmt::print("\n");
for (auto const &table : tables) {
print_table(table);
fmt::print("\n");
}
#endif
std::vector<automata> automatas(tables.size());
double total_rows = 0;
double actual_rows = 0;
value tot_error = 0;
#pragma omp parallel for schedule(dynamic) if (parallel)
for (size_t i = 0; i < tables.size(); ++i) {
auto [ a, error ] = compute_automata(tables[i], tolerance);
automatas[i] = a;
tot_error += error;
actual_rows += automatas[i].rows.size();
total_rows += accumulate(automatas[i].domains.begin(),
automatas[i].domains.end(),
1, std::multiplies<size_t>());
}
log_string("Value redundancy", fmt::format("{}/{} ({:.3f})", total_rows - actual_rows, total_rows, 1 - actual_rows / total_rows));
#ifdef EXPORT_AUTOMATA_DOT
for (auto const &a : automatas) {
automata_dot(a, "dot");
}
#endif
auto buckets = compute_buckets(automatas, pos);
log_line();
if (print_red) {
exit(0);
}
const auto optimal = bucket_elimination(buckets, inst_type == MPE, order, pos, domains, ibound);
runtime = std::chrono::high_resolution_clock::now() - start_t;
if (inst_type == WCSP) {
log_fmt("Solution value", optimal);
} else {
log_fmt("Solution value (-log)", fmt::format("{:.3f} ({:.3f})", exp(-(optimal)), optimal));
}
if (tot_error != 0) {
log_string("Optimality gap", fmt::format("{} ({:3f}%)", tot_error, 100 * (tot_error) / optimal));
}
log_fmt("Bucket elimination runtime", fmt::format("{:%T}", runtime));
log_line();
return EXIT_SUCCESS;
}