forked from rawatamit/GraphMatching
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cc
96 lines (90 loc) · 3.52 KB
/
main.cc
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
#include "GraphReader.h"
#include "BipartiteGraph.h"
#include "PartnerList.h"
#include "MatchingAlgorithm.h"
#include "StableMarriage.h"
#include "Popular.h"
#include "RHeuristicHRLQ.h"
#include "HHeuristicHRLQ.h"
#include "YokoiEnvyfreeHRLQ.h"
#include "MaximalEnvyfreeHRLQ.h"
#include "Utils.h"
#include <stdexcept>
#include <iostream>
#include <unistd.h>
template<typename T>
void compute_matching(bool A_proposing, const char* input_file, const char* output_file) {
GraphReader reader(input_file);
std::unique_ptr<BipartiteGraph> G = reader.read_graph();
T alg(G, A_proposing);
if (alg.compute_matching()) {
auto& M = alg.get_matched_pairs();
std::ofstream out(output_file);
print_matching(G, M, out);
} else {
throw std::runtime_error("unable to compute matching.");
}
}
int main(int argc, char* argv[]) {
int c = 0;
bool compute_stable = false;
bool compute_popular = false;
bool compute_max_card = false;
bool compute_rhrlq = false;
bool compute_hhrlq = false;
bool compute_yhrlq = false;
bool compute_ehrlq = 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, "ABspmrhyei: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 'r': compute_rhrlq = true; break;
case 'h': compute_hhrlq = true; break;
case 'y': compute_yhrlq = true; break;
case 'e': compute_ehrlq = 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: " << (char)optopt << '\n';
}
break;
default: break;
}
}
if (not input_file or not output_file) {
// do not proceed if file names are not valid
} else if (compute_stable) {
compute_matching<StableMarriage>(A_proposing, input_file, output_file);
} else if (compute_popular) {
compute_matching<MaxCardPopular>(A_proposing, input_file, output_file);
} else if (compute_max_card) {
compute_matching<PopularAmongMaxCard>(A_proposing, input_file, output_file);
} else if (compute_rhrlq) {
compute_matching<RHeuristicHRLQ>(A_proposing, input_file, output_file);
} else if (compute_hhrlq) {
compute_matching<HHeuristicHRLQ>(A_proposing, input_file, output_file);
} else if (compute_yhrlq) {
compute_matching<YokoiEnvyfreeHRLQ>(A_proposing, input_file, output_file);
} else if (compute_ehrlq) {
compute_matching<MaximalEnvyfreeHRLQ>(A_proposing, input_file, output_file);
}
return 0;
}