-
Notifications
You must be signed in to change notification settings - Fork 235
Expand file tree
/
Copy pathassignment.cpp
More file actions
86 lines (71 loc) · 2.55 KB
/
assignment.cpp
File metadata and controls
86 lines (71 loc) · 2.55 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
#include <fstream>
#include <iostream>
#include <regex>
#include <boost/program_options.hpp>
#include <libMultiRobotPlanning/assignment.hpp>
using libMultiRobotPlanning::Assignment;
int main(int argc, char* argv[]) {
namespace po = boost::program_options;
// Declare the supported options.
po::options_description desc("Allowed options");
std::string inputFile;
std::string outputFile;
desc.add_options()("help", "produce help message")(
"input,i", po::value<std::string>(&inputFile)->required(),
"input cost (txt)")("output,o",
po::value<std::string>(&outputFile)->required(),
"output file (YAML)");
try {
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
if (vm.count("help") != 0u) {
std::cout << desc << "\n";
return 0;
}
} catch (po::error& e) {
std::cerr << e.what() << std::endl << std::endl;
std::cerr << desc << std::endl;
return 1;
}
Assignment<std::string, std::string> assignment;
std::ifstream input(inputFile);
std::regex re("(\\w+)\\s*->\\s*(\\w+)\\s*:\\s*(\\d+)");
for (std::string line; getline(input, line);) {
std::smatch match;
if (std::regex_search(line, match, re) && match.size() == 4) {
std::string agent = match.str(1);
std::string task = match.str(2);
int cost = std::stoi(match.str(3));
assignment.setCost(agent, task, cost);
} else {
std::cerr << "Couldn't match line \"" << line << "\"!" << match.size()
<< std::endl;
}
}
// const size_t numAgents = 4;
// const size_t numTasks = 4;
// const int64_t cost[numAgents][numTasks] = {{90, 76, 75, 80},
// {35, 85, 55, 65},
// {125, 95, 90, 105},
// {45, 110, 95, 115}};
// for (size_t i = 0; i < numAgents; ++i) {
// for (size_t j = 0; j < numTasks; ++j) {
// a.setCost("a" + std::to_string(i), "t" + std::to_string(j),
// cost[i][j]);
// }
// }
std::map<std::string, std::string> solution;
int64_t c = assignment.solve(solution);
std::cout << "solution with cost: " << c << std::endl;
for (const auto& s : solution) {
std::cout << s.first << ": " << s.second << std::endl;
}
std::ofstream out(outputFile);
out << "cost: " << c << std::endl;
out << "assignment:" << std::endl;
for (const auto& s : solution) {
out << " " << s.first << ": " << s.second << std::endl;
}
return 0;
}