-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqap.cpp
More file actions
410 lines (333 loc) · 10.5 KB
/
Copy pathqap.cpp
File metadata and controls
410 lines (333 loc) · 10.5 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
409
// Copyright (c) 2024-2026 Stefan Teleman.
//
// Licensed under the MIT License.
// See https://opensource.org/license/mit
// SPDX-License-Identifier: MIT
#include <iostream>
#include <iomanip>
#include <vector>
#include <fstream>
#include <algorithm>
#include <limits>
#include <cstdlib>
#include <cstring>
#include <cstdint>
#include <ctime>
#include <cerrno>
#include <getopt.h>
static struct timespec tp_start;
static struct timespec tp_end;
// [[nodiscard]]: a failed timestamp leaves *ts untouched, which would make
// PrintTimediff report a difference against uninitialized storage. Callers
// must decide what to do rather than drop the status on the floor.
[[nodiscard]] static int Timestamp(struct timespec* ts) {
if (clock_gettime(CLOCK_REALTIME, ts) != 0) {
std::cerr << "clock_gettime(2) failed: " << strerror(errno)
<< std::endl;
return -1;
}
return 0;
}
static void PrintTimediff(const struct timespec* S,
const struct timespec* E) {
struct timespec R;
if (clock_getres(CLOCK_REALTIME, &R) != 0) {
std::cerr << "clock_getres(2) failed: " << strerror(errno)
<< std::endl;
return;
}
// Take the difference as one nanosecond count so that the nanosecond field
// borrows from the second field. Differencing the two fields separately and
// then taking labs() of the remainder loses the borrow entirely, and is only
// correct when the interval does not cross a second boundary.
int64_t Nns = ((int64_t) E->tv_sec - (int64_t) S->tv_sec) * 1000000000L +
((int64_t) E->tv_nsec - (int64_t) S->tv_nsec);
const char* Sgn = "";
// Only reachable if the clock stepped backwards mid-run; show it rather
// than hiding it in an absolute value.
if (Nns < 0) {
Nns = -Nns;
Sgn = "-";
}
char Fill = std::cout.fill();
std::cout << "Clock Resolution: " << (int64_t) R.tv_sec << '.'
<< std::setfill('0') << std::setw(9) << (int64_t) R.tv_nsec
<< '.' << std::endl;
std::cout << "CPU time: " << Sgn << Nns / 1000000000L << '.'
<< std::setfill('0') << std::setw(9) << Nns % 1000000000L << " second(s)."
<< std::endl;
std::cout.fill(Fill);
}
class QAP {
private:
std::vector<std::vector<uint32_t>> FLG;
std::vector<std::vector<uint32_t>> DST;
std::vector<uint32_t> AS;
std::vector<uint32_t> BAS;
uint64_t IT;
uint32_t MC;
private:
void ReadFromFile(const std::string& FileName,
std::vector<std::vector<uint32_t>>& IV) {
IV.clear();
std::ifstream IFS(FileName);
if (IFS.good()) {
std::string Line;
const char* Sep = ", \t\n";
char* End;
while (std::getline(IFS, Line)) {
if (!Line.empty()) {
std::vector<uint32_t> V;
if (char* Tok = strtok_r(const_cast<char*>(Line.data()), Sep, &End))
V.push_back(static_cast<uint32_t>(std::stoul(Tok)));
while (char* Tok = strtok_r(NULL, Sep, &End))
V.push_back(static_cast<uint32_t>(std::stoul(Tok)));
IV.push_back(V);
}
}
IFS.close();
} else {
std::cerr << "Invalid input file " << FileName << " was specified."
<< std::endl;
std::exit(1);
}
}
void PrintVector(const std::vector<uint32_t>& V) const {
std::cout << "{ ";
if (!V.empty()) {
std::vector<uint32_t>::const_iterator I = V.begin();
std::vector<uint32_t>::const_iterator E = V.end();
std::cout << *I;
while (++I != E) {
std::cout << ", " << *I;
}
}
std::cout << " }";
}
void PrintGraphVector(const std::vector<std::vector<uint32_t>>& V) const {
std::cout << "{ ";
if (!V.empty()) {
std::vector<std::vector<uint32_t>>::const_iterator I = V.begin();
std::vector<std::vector<uint32_t>>::const_iterator E = V.end();
PrintVector(*I);
while (++I != E) {
std::cout << ',' << std::endl << " ";
PrintVector(*I);
}
}
std::cout << " }" << std::endl;
}
public:
QAP() : FLG(), DST(), AS(), BAS(), IT(0UL),
MC(std::numeric_limits<uint32_t>::max())
{ }
void ReadFromFile(const std::string& FLGName,
const std::string& DSTName) {
ReadFromFile(FLGName, FLG);
ReadFromFile(DSTName, DST);
}
void ReadFromSingleFile(const std::string& FileName) {
std::ifstream IFS(FileName);
if (IFS.good()) {
std::string Line;
const char* Sep = ", \t\n";
char* End;
uint32_t N = 0U;
uint32_t C;
while (std::getline(IFS, Line)) {
if (!Line.empty()) {
if (char* Tok = strtok_r(const_cast<char*>(Line.data()), Sep, &End))
N = static_cast<uint32_t>(std::stoul(Tok));
while (strtok_r(NULL, Sep, &End));
break;
}
}
std::vector<uint32_t> V;
C = 0U;
while (std::getline(IFS, Line) && C++ <= N) {
if (!Line.empty()) {
V.clear();
if (char* Tok = strtok_r(const_cast<char*>(Line.data()), Sep, &End))
V.push_back(static_cast<uint32_t>(std::stoul(Tok)));
while (char* Tok = strtok_r(NULL, Sep, &End))
V.push_back(static_cast<uint32_t>(std::stoul(Tok)));
FLG.push_back(V);
}
}
C = 0U;
while (std::getline(IFS, Line) && C++ <= N) {
if (!Line.empty()) {
V.clear();
if (char* Tok = strtok_r(const_cast<char*>(Line.data()), Sep, &End))
V.push_back(static_cast<uint32_t>(std::stoul(Tok)));
while (char* Tok = strtok_r(NULL, Sep, &End))
V.push_back(static_cast<uint32_t>(std::stoul(Tok)));
DST.push_back(V);
}
}
} else {
std::cerr << "Invalid input file " << FileName << " was specified."
<< std::endl;
std::exit(1);
}
}
bool Verify() const {
if (FLG.size() != DST.size()) {
std::cerr << "Outer Vectors are not the same size." << std::endl;
return false;
}
std::vector<std::vector<uint32_t>>::const_iterator FI;
std::vector<std::vector<uint32_t>>::const_iterator FE = FLG.end();
std::vector<std::vector<uint32_t>>::const_iterator DI;
std::vector<std::vector<uint32_t>>::const_iterator DE = DST.end();
for (FI = FLG.begin(), DI = DST.begin(); FI != FE && DI != DE;
++FI, ++DI) {
if ((*FI).size() != (*DI).size()) {
std::cerr << "Inner Vectors are not the same size." << std::endl;
return false;
}
}
return true;
}
uint32_t ComputeCost() const {
uint32_t Cost = 0U;
for (uint32_t I = 0; I < AS.size(); ++I)
for (uint32_t J = 0; J < AS.size(); ++J)
Cost += FLG[I][J] * DST[AS[I]][AS[J]];
return Cost;
}
uint32_t QuadraticAssignment() {
AS.resize(FLG.size());
for (uint32_t I = 0; I < FLG.size(); ++I)
AS[I] = I;
MC = std::numeric_limits<uint32_t>::max();
BAS = AS;
do {
uint32_t Cost = ComputeCost();
// Keep the permutation, not just the cost: AS walks back round to
// sorted order when the enumeration completes, so it cannot be read
// back afterwards.
if (Cost < MC) {
MC = Cost;
BAS = AS;
}
++IT;
} while (std::next_permutation(AS.begin(), AS.end()));
return MC;
}
uint64_t Iterations() const {
return IT;
}
void PrintFlowGraphVector() const {
PrintGraphVector(FLG);
}
void PrintDistanceVector() const {
PrintGraphVector(DST);
}
void PrintAssignmentVector() const {
PrintVector(BAS);
}
void Print() const {
std::cout << "Flow Graph Vector:" << std::endl;
PrintFlowGraphVector();
std::cout << std::endl;
std::cout << "Distance Vector:" << std::endl;
PrintDistanceVector();
std::cout << std::endl;
std::cout << "Assignment Vector:" << std::endl;
PrintAssignmentVector();
std::cout << std::endl << std::endl;
}
};
static bool ValidateArguments(const std::string& SName,
const std::string& FLName,
const std::string& DSTName) {
if (!SName.empty())
return true;
if (FLName.empty()) {
std::cerr << "Flows input file name is invalid." << std::endl;
return false;
}
if (DSTName.empty()) {
std::cerr << "Distances input file name is invalid." << std::endl;
return false;
}
return true;
}
static void PrintHelp() {
std::cerr << "Usage: qap [-h | --help]" << std::endl;
std::cerr << " [-s <input-file> | --single-file <input-file>]"
<< std::endl;
std::cerr << " (use single-file matrix input format)." << std::endl;
std::cerr << " [ -f <flow-input-file> | --flinput <flow-input-file>]"
<< std::endl;
std::cerr << " [ -d <distance-input-file> | --dstinput <distance-input-file>]"
<< std::endl;
std::cerr << " [-p | --print]" << std::endl;
std::cerr << " (print vector contents)." << std::endl;
}
static struct option long_options[] = {
{ "help", no_argument, 0, 'h' },
{ "print", no_argument, 0, 'p' },
{ "single-file", required_argument, 0, 's' },
{ "flinput", required_argument, 0, 'f' },
{ "dstinput", required_argument, 0, 'd' },
{ 0, 0, 0, 0 }
};
int main(int argc, char* const argv[]) {
int C;
int OIx = 0;
std::string FLGFile;
std::string DSTFile;
std::string SFile;
bool Print = false;
while (1) {
C = getopt_long(argc, argv, "hps:f:d:", long_options, &OIx);
if (C == -1)
break;
switch (C) {
case 'h':
PrintHelp();
return 0;
case 'p':
Print = true;
break;
case 's':
SFile = optarg;
break;
case 'f':
FLGFile = optarg;
break;
case 'd':
DSTFile = optarg;
break;
default:
PrintHelp();
return 1;
}
}
if (!ValidateArguments(SFile, FLGFile, DSTFile)) {
PrintHelp();
return 1;
}
QAP Q;
if (!SFile.empty())
Q.ReadFromSingleFile(SFile);
else
Q.ReadFromFile(FLGFile, DSTFile);
if (!Q.Verify())
return 1;
// Order matters: call Timestamp first so it is never short-circuited away.
bool Timed = Timestamp(&tp_start) == 0;
uint32_t MC = Q.QuadraticAssignment();
Timed = (Timestamp(&tp_end) == 0) && Timed;
std::cout << "Minimum cost: " << MC << std::endl;
std::cout << "Iterations: " << Q.Iterations() << std::endl;
if (Print)
Q.Print();
// A clock failure must not discard the result of the search itself;
// Timestamp has already said what went wrong on stderr.
if (Timed)
PrintTimediff(&tp_start, &tp_end);
return 0;
}