-
Notifications
You must be signed in to change notification settings - Fork 345
/
Copy pathmain.cpp
224 lines (201 loc) · 7.77 KB
/
main.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
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
/*
This file is part of VROOM.
Copyright (c) 2015-2022, Julien Coupey.
All rights reserved (see LICENSE).
*/
#include <fstream>
#include <iostream>
#include <sstream>
#if USE_LIBOSRM
#include "osrm/exception.hpp"
#endif
#include "../include/cxxopts/include/cxxopts.hpp"
#include "problems/vrp.h"
#include "structures/cl_args.h"
#include "utils/exception.h"
#include "utils/helpers.h"
#include "utils/input_parser.h"
#include "utils/output_json.h"
#include "utils/version.h"
int main(int argc, char** argv) {
vroom::io::CLArgs cl_args;
std::vector<std::string> host_args;
std::vector<std::string> port_args;
std::string router_arg;
std::string limit_arg;
std::vector<std::string> heuristic_params_arg;
cxxopts::Options options("vroom",
"VROOM Copyright (C) 2015-2022, Julien Coupey\n"
"Version: " +
vroom::get_version() +
"\n\n"
"A command-line utility to solve complex vehicle "
"routing problems.\n");
// clang-format off
options
.set_width(80)
.set_tab_expansion()
.add_options("Solving")
("a,host",
"host for the routing profile",
cxxopts::value<std::vector<std::string>>(host_args)->default_value({vroom::DEFAULT_PROFILE + ":0.0.0.0"}))
("c,choose-eta",
"choose ETA for custom routes and report violations",
cxxopts::value<bool>(cl_args.check)->default_value("false"))
("g,geometry",
"add detailed route geometry and distance",
cxxopts::value<bool>(cl_args.geometry)->default_value("false"))
("h,help", "display this help and exit")
("i,input",
"read input from a file rather than from stdin",
cxxopts::value<std::string>(cl_args.input_file))
("l,limit",
"stop solving process after 'limit' seconds",
cxxopts::value<std::string>(limit_arg))
("o,output",
"write output to a file rather than stdout",
cxxopts::value<std::string>(cl_args.output_file))
("p,port",
"host port for the routing profile",
cxxopts::value<std::vector<std::string>>(port_args)->default_value({vroom::DEFAULT_PROFILE + ":5000"}))
("r,router",
"osrm, libosrm, ors or valhalla",
cxxopts::value<std::string>(router_arg)->default_value("osrm"))
("t,threads",
"number of available threads",
cxxopts::value<unsigned>(cl_args.nb_threads)->default_value("4"))
("v,version", "output version information and exit")
("x,explore",
"exploration level to use (0..5)",
cxxopts::value<unsigned>(cl_args.exploration_level)->default_value("5"))
("stdin",
"optional input positional arg",
cxxopts::value<std::string>(cl_args.input));
// clang-format on
try {
// we don't want to print debug args on --help
options.add_options("debug_group")("e,heuristic-param",
"Heuristic parameter",
cxxopts::value<std::vector<std::string>>(
heuristic_params_arg));
options.parse_positional({"stdin"});
options.positional_help("OPTIONAL INLINE JSON");
auto parsed_args = options.parse(argc, argv);
try {
if (!limit_arg.empty()) {
// Internally timeout is in milliseconds.
cl_args.timeout = 1000 * std::stof(limit_arg);
}
} catch (const std::exception& e) {
throw cxxopts::OptionException("Argument '" + limit_arg +
"' failed to parse");
}
if (parsed_args.count("help")) {
std::cout << options.help({"Solving"}) << "\n";
exit(0);
}
if (parsed_args.count("version")) {
std::cout << "vroom " << vroom::get_version() << "\n";
exit(0);
}
} catch (const cxxopts::OptionException& e) {
// cxxopts outputs the failed parameter but no other details, so we add some
// (likely) context
const auto exc = vroom::InputException(": invalid numerical value.");
const auto msg = e.what() + exc.message;
std::cerr << "[Error] " << msg << std::endl;
vroom::io::write_to_json({exc.error_code, msg}, false, cl_args.output_file);
exit(exc.error_code);
}
// parse and update some params
for (const auto& host : host_args) {
vroom::io::update_host(cl_args.servers, host);
}
for (const auto& port : port_args) {
vroom::io::update_port(cl_args.servers, port);
}
cl_args.exploration_level =
std::min(cl_args.exploration_level, cl_args.max_exploration_level);
// Determine routing engine (defaults to ROUTER::OSRM).
if (router_arg == "libosrm") {
cl_args.router = vroom::ROUTER::LIBOSRM;
} else if (router_arg == "ors") {
cl_args.router = vroom::ROUTER::ORS;
} else if (router_arg == "valhalla") {
cl_args.router = vroom::ROUTER::VALHALLA;
} else if (!router_arg.empty() and router_arg != "osrm") {
auto error_code = vroom::InputException("").error_code;
std::string message = "Invalid routing engine: " + router_arg + ".";
std::cerr << "[Error] " << message << std::endl;
vroom::io::write_to_json({error_code, message}, false, cl_args.output_file);
exit(error_code);
} else {
cl_args.router = vroom::ROUTER::OSRM;
}
try {
// Force heuristic parameters from the command-line, useful for
// debugging.
std::transform(heuristic_params_arg.begin(),
heuristic_params_arg.end(),
std::back_inserter(cl_args.h_params),
[](const auto& str_param) {
return vroom::utils::str_to_heuristic_param(str_param);
});
} catch (const vroom::Exception& e) {
std::cerr << "[Error] " << e.message << std::endl;
vroom::io::write_to_json({e.error_code, e.message},
false,
cl_args.output_file);
exit(e.error_code);
}
// Read input problem
if (cl_args.input.empty()) {
std::stringstream buffer;
if (!cl_args.input_file.empty()) {
std::ifstream ifs(cl_args.input_file);
buffer << ifs.rdbuf();
} else {
buffer << std::cin.rdbuf();
}
cl_args.input = buffer.str();
}
try {
// Build problem.
vroom::Input problem_instance(cl_args.servers, cl_args.router);
vroom::io::parse(problem_instance, cl_args.input, cl_args.geometry);
vroom::Solution sol = (cl_args.check)
? problem_instance.check(cl_args.nb_threads)
: problem_instance.solve(cl_args.exploration_level,
cl_args.nb_threads,
cl_args.timeout,
cl_args.h_params);
// Write solution.
vroom::io::write_to_json(sol, cl_args.geometry, cl_args.output_file);
} catch (const vroom::Exception& e) {
std::cerr << "[Error] " << e.message << std::endl;
vroom::io::write_to_json({e.error_code, e.message},
false,
cl_args.output_file);
exit(e.error_code);
}
#if USE_LIBOSRM
catch (const osrm::exception& e) {
// In case of an unhandled routing error.
auto error_code = vroom::RoutingException("").error_code;
auto message = "Routing problem: " + std::string(e.what());
std::cerr << "[Error] " << message << std::endl;
vroom::io::write_to_json({error_code, message}, false, cl_args.output_file);
exit(error_code);
}
#endif
catch (const std::exception& e) {
// In case of an unhandled internal error.
auto error_code = vroom::InternalException("").error_code;
std::cerr << "[Error] " << e.what() << std::endl;
vroom::io::write_to_json({error_code, e.what()},
false,
cl_args.output_file);
exit(error_code);
}
return 0;
}