Skip to content

Commit

Permalink
Merge branch 'refactor/use-format'
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoupey committed May 23, 2024
2 parents f853330 + b8029e8 commit 717c094
Show file tree
Hide file tree
Showing 16 changed files with 176 additions and 154 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#### Internals

- Refactor `Matrix` template class (#1089)
- Refactor to use `std::format` whenever possible (#1081)

#### CI

Expand Down
41 changes: 22 additions & 19 deletions src/algorithms/heuristics/heuristics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -887,8 +887,8 @@ void initial_routes(const Input& input, std::vector<Route>& routes) {
}
}
if (!(single_jobs_deliveries <= vehicle.capacity)) {
throw InputException("Route over capacity for vehicle " +
std::to_string(vehicle.id) + ".");
throw InputException(
std::format("Route over capacity for vehicle {}.", vehicle.id));
}

// Track load and travel time during the route for validity.
Expand All @@ -912,9 +912,11 @@ void initial_routes(const Input& input, std::vector<Route>& routes) {
job_ranks.push_back(job_rank);

if (!input.vehicle_ok_with_job(v, job_rank)) {
throw InputException("Missing skill or step out of reach for vehicle " +
std::to_string(vehicle.id) + " and job " +
std::to_string(job.id) + ".");
throw InputException(
std::format("Missing skill or step out of reach for vehicle {} and "
"job {}.",
vehicle.id,
job.id));
}

// Update current travel time.
Expand All @@ -940,8 +942,9 @@ void initial_routes(const Input& input, std::vector<Route>& routes) {
case JOB_TYPE::DELIVERY: {
auto search = expected_delivery_ranks.find(job_rank);
if (search == expected_delivery_ranks.end()) {
throw InputException("Invalid shipment in route for vehicle " +
std::to_string(vehicle.id) + ".");
throw InputException(
std::format("Invalid shipment in route for vehicle {}.",
vehicle.id));
}
expected_delivery_ranks.erase(search);

Expand All @@ -952,8 +955,8 @@ void initial_routes(const Input& input, std::vector<Route>& routes) {

// Check validity after this step wrt capacity.
if (!(current_load <= vehicle.capacity)) {
throw InputException("Route over capacity for vehicle " +
std::to_string(vehicle.id) + ".");
throw InputException(
std::format("Route over capacity for vehicle {}.", vehicle.id));
}
}

Expand All @@ -964,22 +967,22 @@ void initial_routes(const Input& input, std::vector<Route>& routes) {
vehicle.eval(previous_index.value(), vehicle.end.value().index());
}
if (!vehicle.ok_for_travel_time(eval_sum.duration)) {
throw InputException("Route over max_travel_time for vehicle " +
std::to_string(vehicle.id) + ".");
throw InputException(
std::format("Route over max_travel_time for vehicle {}.", vehicle.id));
}
if (!vehicle.ok_for_distance(eval_sum.distance)) {
throw InputException("Route over max_distance for vehicle " +
std::to_string(vehicle.id) + ".");
throw InputException(
std::format("Route over max_distance for vehicle {}.", vehicle.id));
}

if (vehicle.max_tasks < job_ranks.size()) {
throw InputException("Too many tasks for vehicle " +
std::to_string(vehicle.id) + ".");
throw InputException(
std::format("Too many tasks for vehicle {}.", vehicle.id));
}

if (!expected_delivery_ranks.empty()) {
throw InputException("Invalid shipment in route for vehicle " +
std::to_string(vehicle.id) + ".");
throw InputException(
std::format("Invalid shipment in route for vehicle {}.", vehicle.id));
}

// Now route is OK with regard to capacity, max_travel_time,
Expand All @@ -991,8 +994,8 @@ void initial_routes(const Input& input, std::vector<Route>& routes) {
job_ranks.end(),
0,
0)) {
throw InputException("Infeasible route for vehicle " +
std::to_string(vehicle.id) + ".");
throw InputException(
std::format("Infeasible route for vehicle {}.", vehicle.id));
}

current_r.replace(input,
Expand Down
32 changes: 16 additions & 16 deletions src/algorithms/validation/choose_ETA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ Route choose_ETA(const Input& input,
if (latest_date != std::numeric_limits<Duration>::max()) {
const auto reach_time = relative_ETA[s];
if (latest_date < reach_time) {
throw InputException("Infeasible route for vehicle " +
std::to_string(v.id) + ".");
throw InputException(
std::format("Infeasible route for vehicle {}.", v.id));
}
start_candidate = std::min(start_candidate, latest_date - reach_time);
}
Expand Down Expand Up @@ -264,8 +264,8 @@ Route choose_ETA(const Input& input,
std::max(earliest_date, step.forced_service.after.value());
}
if (earliest_date > latest_dates[s]) {
throw InputException("Infeasible route for vehicle " +
std::to_string(v.id) + ".");
throw InputException(
std::format("Infeasible route for vehicle {}.", v.id));
}

switch (step.type) {
Expand Down Expand Up @@ -610,7 +610,7 @@ Route choose_ETA(const Input& input,

rank_in_J = 1;
for (unsigned i = 0; i < n; ++i) {
auto p_name = "P" + std::to_string(i + 1);
auto p_name = std::format("P{}", i + 1);
glp_set_row_name(lp, current_row, p_name.c_str());
double action;
const auto& step = steps[1 + i];
Expand All @@ -635,7 +635,7 @@ Route choose_ETA(const Input& input,

// Lead time ("earliest violation") constraints.
for (unsigned i = 0; i < n; ++i) {
auto l_name = "L" + std::to_string(i + 1);
auto l_name = std::format("L{}", i + 1);
glp_set_row_name(lp, current_row, l_name.c_str());
glp_set_row_bnds(lp, current_row, GLP_LO, 0.0, 0.0);
++current_row;
Expand All @@ -644,14 +644,14 @@ Route choose_ETA(const Input& input,

// Delay ("latest violation") constraints.
for (unsigned i = 0; i < n; ++i) {
auto d_name = "D" + std::to_string(i + 1);
auto d_name = std::format("D{}", i + 1);
glp_set_row_name(lp, current_row, d_name.c_str());
glp_set_row_bnds(lp, current_row, GLP_UP, 0.0, 0.0);
++current_row;
}

// Vehicle TW end violation constraint.
auto d_name = "D" + std::to_string(n + 1);
auto d_name = std::format("D{}", n + 1);
glp_set_row_name(lp, current_row, d_name.c_str());
// Using v.tw.end is fine too for a default time window.
glp_set_row_bnds(lp, current_row, GLP_UP, 0.0, v.tw.end - horizon_start);
Expand All @@ -661,7 +661,7 @@ Route choose_ETA(const Input& input,

// Binary variable decision constraints.
for (unsigned i = 1; i <= n; ++i) {
auto s_name = "S" + std::to_string(i);
auto s_name = std::format("S{}", i);
glp_set_row_name(lp, current_row, s_name.c_str());
glp_set_row_bnds(lp, current_row, GLP_FX, 1.0, 1.0);
++current_row;
Expand All @@ -670,7 +670,7 @@ Route choose_ETA(const Input& input,

// Delta constraints.
for (unsigned r = 0; r < J.size(); ++r) {
auto delta_name = "Delta" + std::to_string(J[r]);
auto delta_name = std::format("Delta{}", J[r]);
glp_set_row_name(lp, current_row, delta_name.c_str());
glp_set_row_bnds(lp,
current_row,
Expand Down Expand Up @@ -701,15 +701,15 @@ Route choose_ETA(const Input& input,
unsigned current_col = 1;
// Variables for time of services (t_i values).
for (unsigned i = 0; i <= n + 1; ++i) {
auto t_name = "t" + std::to_string(i);
auto t_name = std::format("t{}", i);
glp_set_col_name(lp, current_col, t_name.c_str());

const Duration LB = t_i_LB[i];
const Duration UB = t_i_UB[i];

if (UB < LB) {
throw InputException("Infeasible route for vehicle " +
std::to_string(v.id) + ".");
throw InputException(
std::format("Infeasible route for vehicle {}.", v.id));
}

if (LB == UB) {
Expand All @@ -728,7 +728,7 @@ Route choose_ETA(const Input& input,

// Define variables for measure of TW violation.
for (unsigned i = 0; i <= n + 1; ++i) {
auto y_name = "Y" + std::to_string(i);
auto y_name = std::format("Y{}", i);
glp_set_col_name(lp, current_col, y_name.c_str());
glp_set_col_bnds(lp, current_col, GLP_LO, 0.0, 0.0);
++current_col;
Expand All @@ -741,7 +741,7 @@ Route choose_ETA(const Input& input,
const auto& tws = (step.type == STEP_TYPE::JOB) ? input.jobs[step.rank].tws
: v.breaks[step.rank].tws;
for (unsigned k = 0; k < tws.size(); ++k) {
auto x_name = "X" + std::to_string(i + 1) + "_" + std::to_string(k);
auto x_name = std::format("X{}_{}", i + 1, k);
glp_set_col_name(lp, current_col, x_name.c_str());
glp_set_col_kind(lp, current_col, GLP_BV);
if (k < first_relevant_tw_rank[i] || k > last_relevant_tw_rank[i]) {
Expand All @@ -754,7 +754,7 @@ Route choose_ETA(const Input& input,

// Delta variables.
for (unsigned i = 0; i <= n; ++i) {
auto delta_name = "delta" + std::to_string(i);
auto delta_name = std::format("delta{}", i);
glp_set_col_name(lp, current_col, delta_name.c_str());
glp_set_col_bnds(lp, current_col, GLP_LO, 0.0, 0.0);
++current_col;
Expand Down
5 changes: 2 additions & 3 deletions src/routing/ors_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ std::string OrsWrapper::build_query(const std::vector<Location>& locations,
}
body += "\":[";
for (auto const& location : locations) {
body += "[" + std::to_string(location.lon()) + "," +
std::to_string(location.lat()) + "],";
body += std::format("[{},{}],", location.lon(), location.lat());
}
body.pop_back(); // Remove trailing ','.
body += "]";
Expand All @@ -53,7 +52,7 @@ std::string OrsWrapper::build_query(const std::vector<Location>& locations,
query += " HTTP/1.0\r\n";
query += "Accept: */*\r\n";
query += "Content-Type: application/json\r\n";
query += "Content-Length: " + std::to_string(body.size()) + "\r\n";
query += std::format("Content-Length: {}\r\n", body.size());
query += "Host: " + _server.host + ":" + _server.port + "\r\n";
query += "Connection: close\r\n";
query += "\r\n" + body;
Expand Down
8 changes: 3 additions & 5 deletions src/routing/osrm_routed_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ OsrmRoutedWrapper::build_query(const std::vector<Location>& locations,

// Adding locations and radiuses values.
for (auto const& location : locations) {
query += std::to_string(location.lon()) + "," +
std::to_string(location.lat()) + ";";
query += std::format("{},{};", location.lon(), location.lat());
radiuses += DEFAULT_OSRM_SNAPPING_RADIUS + ";";
}
// Remove trailing ';'.
Expand Down Expand Up @@ -76,9 +75,8 @@ void OsrmRoutedWrapper::check_response(const rapidjson::Document& json_result,
const auto error_loc =
std::stoul(message.substr(snapping_error_base.size(),
message.size() - snapping_error_base.size()));
const auto coordinates = "[" + std::to_string(locs[error_loc].lon()) +
"," + std::to_string(locs[error_loc].lat()) +
"]";
const auto coordinates =
std::format("[{},{}]", locs[error_loc].lon(), locs[error_loc].lat());
throw RoutingException("Could not find route near location " +
coordinates);
}
Expand Down
10 changes: 5 additions & 5 deletions src/routing/valhalla_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ std::string ValhallaWrapper::get_matrix_query(
// List locations.
std::string all_locations;
for (auto const& location : locations) {
all_locations += "{\"lon\":" + std::to_string(location.lon()) + "," +
"\"lat\":" + std::to_string(location.lat()) + "},";
all_locations +=
std::format(R"({{"lon":{},"lat":{}}},)", location.lon(), location.lat());
}
all_locations.pop_back(); // Remove trailing ','.

Expand All @@ -61,9 +61,9 @@ ValhallaWrapper::get_route_query(const std::vector<Location>& locations) const {
"GET /" + _server.path + _route_service + "?json={\"locations\":[";

for (auto const& location : locations) {
query += "{\"lon\":" + std::to_string(location.lon()) + "," +
"\"lat\":" + std::to_string(location.lat()) +
R"(,"type":"break"},)";
query += std::format(R"({{"lon":{},"lat":{},"type":"break"}},)",
location.lon(),
location.lat());
}
query.pop_back(); // Remove trailing ','.

Expand Down
5 changes: 3 additions & 2 deletions src/routing/wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ class Wrapper {
if (max_unfound_routes_for_a_loc > 0) {
std::string error_msg = "Unfound route(s) ";
error_msg += error_direction;
error_msg += "location [" + std::to_string(locs[error_loc].lon()) + "," +
std::to_string(locs[error_loc].lat()) + "]";
error_msg += std::format("location [{},{}]",
locs[error_loc].lon(),
locs[error_loc].lat());

throw RoutingException(error_msg);
}
Expand Down
3 changes: 1 addition & 2 deletions src/structures/vroom/cost_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ CostWrapper::CostWrapper(double speed_factor, Cost per_hour, Cost per_km)
std::round(1 / speed_factor * DURATION_FACTOR * per_hour)),
discrete_distance_cost_factor(DISTANCE_FACTOR * per_km) {
if (speed_factor <= 0 || speed_factor > MAX_SPEED_FACTOR) {
throw InputException("Invalid speed factor: " +
std::to_string(speed_factor));
throw InputException(std::format("Invalid speed factor: {}", speed_factor));
}
}

Expand Down
Loading

0 comments on commit 717c094

Please sign in to comment.