Repository navigation
Expand file tree
/
Copy pathmain.cc
More file actions
121 lines (107 loc) · 4.1 KB
/
Copy pathmain.cc
File metadata and controls
121 lines (107 loc) · 4.1 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
#include "BipartiteGraph.h"
#include "StableMarriage.h"
#include "Popular.h"
#include "YokoiEnvyfreeHRLQ.h"
#include "MaximalEnvyfreeHRLQ.h"
#include "CriticalRSM.h"
#include "Utils.h"
#include "GraphReader.h"
#include "Matching.h"
#include <fstream>
#include <iostream>
#include <unistd.h>
template<typename T>
bool compute_matching(bool A_proposing, const char* input_file,
const char* output_file, bool verify=false) {
// setup input/output stream as std::cin/std::cout by default
// if a file is specified use it to read/write
auto cin_buf = std::cin.rdbuf(); // save pointer to std::cin buffer
auto cout_buf = std::cout.rdbuf(); // save pointer to std::cout buffer
std::ifstream filein(input_file);
std::ofstream fileout(output_file);
if (input_file) {
std::cin.rdbuf(filein.rdbuf());
}
if (output_file) {
std::cout.rdbuf(fileout.rdbuf());
}
std::shared_ptr<BipartiteGraph> G = GraphReader(std::cin).read_graph();
T alg(G, A_proposing);
auto M = alg.compute_matching();
bool ret = true;
if (verify)
{
ret = M.verify(G);
}
// Only print matching if it is verified to be correct.
if (ret)
{
print_matching(G, M, std::cout);
}
// restore buffers
std::cin.rdbuf(cin_buf);
std::cout.rdbuf(cout_buf);
return ret;
}
int main(int argc, char* argv[]) {
int c = 0;
bool compute_stable = false;
bool compute_popular = false;
bool compute_max_card = false;
bool compute_yhrlq = false;
bool compute_ehrlq = false;
bool compute_popular_lq = false;
bool compute_critical_rsm = false;
bool A_proposing = true;
const char* input_file = nullptr;
const char* output_file = nullptr;
opterr = 0;
// choose the proposing partition using -A and -B
// -s, -p, and -m flags compute the stable, max-card popular and pop among
// max-card matchings respectively
// -r and -h compute the resident and hopsital heuristic for an HRLQ instance
// -i is the path to the input graph, -o is the path where the matching
// computed should be stored
while ((c = getopt(argc, argv, "ABspmyelri:o:")) != -1) {
switch (c) {
case 'A': A_proposing = true; break;
case 'B': A_proposing = false; break;
case 's': compute_stable = true; break;
case 'p': compute_popular = true; break;
case 'm': compute_max_card = true; break;
case 'y': compute_yhrlq = true; break;
case 'e': compute_ehrlq = true; break;
case 'l': compute_popular_lq = true; break;
case 'r': compute_critical_rsm = true; break;
case 'i': input_file = optarg; break;
case 'o': output_file = optarg; break;
case '?':
if (optopt == 'i') {
std::cerr << "Option -i requires an argument.\n";
} else if (optopt == 'o') {
std::cerr << "Option -o requires an argument.\n";
} else {
std::cerr << "Unknown option: " << static_cast<char>(optopt) << '\n';
}
break;
default: break;
}
}
bool status = true;
if (compute_stable) {
status = compute_matching<StableMarriage>(A_proposing, input_file, output_file);
} else if (compute_popular) {
status = compute_matching<MaxCardPopular>(A_proposing, input_file, output_file, true);
} else if (compute_max_card) {
status = compute_matching<PopularAmongMaxCard>(A_proposing, input_file, output_file);
} else if (compute_yhrlq) {
status = compute_matching<YokoiEnvyfreeHRLQ>(A_proposing, input_file, output_file);
} else if (compute_ehrlq) {
status = compute_matching<MaximalEnvyfreeHRLQ>(A_proposing, input_file, output_file);
} else if (compute_popular_lq) {
status = compute_matching<MaxCardPopularLQ>(A_proposing, input_file, output_file);
} else if(compute_critical_rsm) {
status = compute_matching<CriticalRSM>(A_proposing, input_file, output_file);
}
return status ? 0 : 1;
}