From bc797b4a9fbabff0ea92a8f0b4960c33074b26ad Mon Sep 17 00:00:00 2001 From: jcoupey Date: Mon, 15 Jul 2024 15:52:55 +0200 Subject: [PATCH] Store and re-use route geometries in plan mode. --- src/routing/http_wrapper.cpp | 12 +++++++----- src/routing/http_wrapper.h | 10 ++++++---- src/routing/libosrm_wrapper.cpp | 11 ++++++----- src/routing/libosrm_wrapper.h | 10 ++++++---- src/routing/wrapper.h | 10 ++++++---- src/structures/vroom/input/input.cpp | 14 ++++---------- src/structures/vroom/input/input.h | 4 ++++ 7 files changed, 39 insertions(+), 32 deletions(-) diff --git a/src/routing/http_wrapper.cpp b/src/routing/http_wrapper.cpp index 9f438a4b3..1f1f26c2a 100644 --- a/src/routing/http_wrapper.cpp +++ b/src/routing/http_wrapper.cpp @@ -201,10 +201,12 @@ Matrices HttpWrapper::get_matrices(const std::vector& locs) const { return m; } -Matrices HttpWrapper::get_sparse_matrices(const std::string& profile, - const std::vector& locs, - const std::vector& vehicles, - const std::vector& jobs) const { +Matrices HttpWrapper::get_sparse_matrices( + const std::string& profile, + const std::vector& locs, + const std::vector& vehicles, + const std::vector& jobs, + std::unordered_map& v_id_to_geom) const { std::size_t m_size = locs.size(); Matrices m(m_size); @@ -265,7 +267,7 @@ Matrices HttpWrapper::get_sparse_matrices(const std::string& profile, get_leg_distance(legs[i]); } - // TODO get geometry and store it. + v_id_to_geom.insert(std::make_pair(v.id, get_geometry(json_result))); } return m; diff --git a/src/routing/http_wrapper.h b/src/routing/http_wrapper.h index f4d7feb20..1abcb6438 100644 --- a/src/routing/http_wrapper.h +++ b/src/routing/http_wrapper.h @@ -54,10 +54,12 @@ class HttpWrapper : public Wrapper { Matrices get_matrices(const std::vector& locs) const override; - Matrices get_sparse_matrices(const std::string& profile, - const std::vector& locs, - const std::vector& vehicles, - const std::vector& jobs) const override; + Matrices get_sparse_matrices( + const std::string& profile, + const std::vector& locs, + const std::vector& vehicles, + const std::vector& jobs, + std::unordered_map& v_id_to_geom) const override; virtual bool duration_value_is_null(const rapidjson::Value& matrix_entry) const = 0; diff --git a/src/routing/libosrm_wrapper.cpp b/src/routing/libosrm_wrapper.cpp index b422887e2..4cb1884e5 100644 --- a/src/routing/libosrm_wrapper.cpp +++ b/src/routing/libosrm_wrapper.cpp @@ -120,11 +120,12 @@ Matrices LibosrmWrapper::get_matrices(const std::vector& locs) const { return m; } -Matrices -LibosrmWrapper::get_sparse_matrices(const std::string& profile, - const std::vector& locs, - const std::vector& vehicles, - const std::vector& jobs) const { +Matrices LibosrmWrapper::get_sparse_matrices( + const std::string& profile, + const std::vector& locs, + const std::vector& vehicles, + const std::vector& jobs, + std::unordered_map& v_id_to_geom) const { std::size_t m_size = locs.size(); Matrices m(m_size); diff --git a/src/routing/libosrm_wrapper.h b/src/routing/libosrm_wrapper.h index 85627d2ef..fa4f3272a 100644 --- a/src/routing/libosrm_wrapper.h +++ b/src/routing/libosrm_wrapper.h @@ -31,10 +31,12 @@ class LibosrmWrapper : public Wrapper { Matrices get_matrices(const std::vector& locs) const override; - Matrices get_sparse_matrices(const std::string& profile, - const std::vector& locs, - const std::vector& vehicles, - const std::vector& jobs) const override; + Matrices get_sparse_matrices( + const std::string& profile, + const std::vector& locs, + const std::vector& vehicles, + const std::vector& jobs, + std::unordered_map& v_id_to_geom) const override; void add_geometry(Route& route) const override; }; diff --git a/src/routing/wrapper.h b/src/routing/wrapper.h index 69f5e8beb..da32f2109 100644 --- a/src/routing/wrapper.h +++ b/src/routing/wrapper.h @@ -28,10 +28,12 @@ class Wrapper { virtual Matrices get_matrices(const std::vector& locs) const = 0; - virtual Matrices get_sparse_matrices(const std::string& profile, - const std::vector& locs, - const std::vector& vehicles, - const std::vector& jobs) const = 0; + virtual Matrices get_sparse_matrices( + const std::string& profile, + const std::vector& locs, + const std::vector& vehicles, + const std::vector& jobs, + std::unordered_map& v_id_to_geom) const = 0; virtual void add_geometry(Route& route) const = 0; diff --git a/src/structures/vroom/input/input.cpp b/src/structures/vroom/input/input.cpp index e040c0da4..36501ce13 100644 --- a/src/structures/vroom/input/input.cpp +++ b/src/structures/vroom/input/input.cpp @@ -906,7 +906,8 @@ routing::Matrices Input::get_matrices_by_profile(const std::string& profile, return sparse_filling ? (*rw)->get_sparse_matrices(profile, _locations, this->vehicles, - this->jobs) + this->jobs, + _vehicle_id_to_geometry) : (*rw)->get_matrices(_locations); } @@ -1208,15 +1209,8 @@ Solution Input::check(unsigned nb_thread) { if (_geometry) { for (auto& route : sol.routes) { - const auto& profile = route.profile; - auto rw = std::ranges::find_if(_routing_wrappers, [&](const auto& wr) { - return wr->profile == profile; - }); - if (rw == _routing_wrappers.end()) { - throw InputException( - "Route geometry request with non-routable profile " + profile + "."); - } - (*rw)->add_geometry(route); + assert(_vehicle_id_to_geometry.contains(route.vehicle)); + route.geometry = std::move(_vehicle_id_to_geometry.at(route.vehicle)); } _end_routing = std::chrono::high_resolution_clock::now(); diff --git a/src/structures/vroom/input/input.h b/src/structures/vroom/input/input.h index 23e9d285a..e60404ef4 100644 --- a/src/structures/vroom/input/input.h +++ b/src/structures/vroom/input/input.h @@ -78,6 +78,10 @@ class Input { bool _all_locations_have_coords{true}; std::vector> _jobs_vehicles_evals; + // Used in plan mode since we store route geometries while + // generating sparse matrices. + std::unordered_map _vehicle_id_to_geometry; + unsigned _amount_size{0}; Amount _zero{0};