This repository has been archived by the owner on Nov 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
test.cpp
88 lines (73 loc) · 2.08 KB
/
test.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
/********************************************************************
********************************************************************
** O(n^3) implementation derived from libhungarian by Cyrill Stachniss, 2004
**
** C++ class implementation of the Hungarian algorithm by David Schwarz, 2012.
**
** Minor modifications by Justin Buchanan
**
** Solving the Minimum Assignment Problem using the
** Hungarian Method.
**
** ** This file may be freely copied and distributed! **
**
**
** This file is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied
** warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
** PURPOSE.
**
********************************************************************
********************************************************************/
#include <iostream>
#include "hungarian.hpp"
using namespace std;
const Hungarian::Matrix EXAMPLE1 = {
{100, 100, 1},
{100, 2, 21512},
{1, 4, 9852},
{6, 30252, 400},
};
const Hungarian::Matrix SOLUTION1 = {
{0, 0, 1, 0},
{0, 1, 0, 0},
{1, 0, 0, 0},
{0, 0, 0, 1},
};
// clang-format off
const Hungarian::Matrix EXAMPLE2 = {
{100, 1},
{100, 12},
{1, 4},
{6, 30252}
};
// TODO: this isn't right
const Hungarian::Matrix SOLUTION2 = {
{0,1,0,0},
{0,0,1,0},
{1,0,0,0},
{0,0,0,1},
};
// clang-format on
bool runExample(const Hungarian::Matrix& cost,
const Hungarian::Matrix& solution) {
Hungarian::Result r = Hungarian::Solve(cost, Hungarian::MODE_MINIMIZE_COST);
cerr << "cost-matrix:";
Hungarian::PrintMatrix(r.cost);
if (!r.success) {
cerr << "Failed to find solution :(" << endl;
return false;
}
cerr << "assignment:";
Hungarian::PrintMatrix(r.assignment);
const bool correct = (r.assignment == solution);
cerr << "Solution correct? " << correct << endl;
return correct;
}
int main() {
bool success = true;
if (!runExample(EXAMPLE1, SOLUTION1)) success = false;
cerr << "--------------------" << endl;
if (!runExample(EXAMPLE2, SOLUTION2)) success = false;
return success ? 0 : 1;
}