From 20c5af048c94efa7d384943e2211fb626ead7032 Mon Sep 17 00:00:00 2001 From: jcoupey Date: Fri, 12 Jul 2024 16:14:21 +0200 Subject: [PATCH] First sketch of HttpWrapper::get_sparse_matrices. --- src/routing/http_wrapper.cpp | 71 ++++++++++++++++++++++++++++ src/routing/http_wrapper.h | 5 ++ src/routing/libosrm_wrapper.h | 4 ++ src/routing/wrapper.h | 6 +++ src/structures/vroom/input/input.cpp | 5 +- 5 files changed, 90 insertions(+), 1 deletion(-) diff --git a/src/routing/http_wrapper.cpp b/src/routing/http_wrapper.cpp index 63847232b..89d430065 100644 --- a/src/routing/http_wrapper.cpp +++ b/src/routing/http_wrapper.cpp @@ -201,6 +201,77 @@ 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 { + std::size_t m_size = locs.size(); + Matrices m(m_size); + + for (const auto& v : vehicles) { + if (v.profile != profile) { + continue; + } + + std::vector route_locs; + route_locs.reserve(v.steps.size()); + + bool has_job_steps = false; + for (const auto& step : v.steps) { + switch (step.type) { + using enum STEP_TYPE; + case START: + if (v.has_start()) { + route_locs.push_back(v.start.value()); + } + break; + case END: + if (v.has_end()) { + route_locs.push_back(v.end.value()); + } + break; + case BREAK: + break; + case JOB: + has_job_steps = true; + route_locs.push_back(jobs[step.rank].location); + break; + } + } + + if (!has_job_steps) { + // No steps provided in input for vehicle, or only breaks in + // steps. + continue; + } + assert(route_locs.size() >= 2); + + // TODO run route queries in parallel. + std::string query = build_query(route_locs, _route_service); + + std::string json_string = this->run_query(query); + + rapidjson::Document json_result; + parse_response(json_result, json_string); + this->check_response(json_result, route_locs, _route_service); + + const auto [durations, distances] = get_legs_info(json_result); + assert(durations.size() == route_locs.size() - 1); + assert(durations.size() == distances.size()); + + for (std::size_t i = 0; i < durations.size(); ++i) { + m.durations[route_locs[i].index()][route_locs[i + 1].index()] = + durations[i]; + m.distances[route_locs[i].index()][route_locs[i + 1].index()] = + distances[i]; + } + + // TODO get geometry and store it. + } + + return m; +} + void HttpWrapper::add_geometry(Route& route) const { // Ordering locations for the given steps, excluding // breaks. diff --git a/src/routing/http_wrapper.h b/src/routing/http_wrapper.h index 6869290ed..3ee0f04ee 100644 --- a/src/routing/http_wrapper.h +++ b/src/routing/http_wrapper.h @@ -54,6 +54,11 @@ 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; + virtual bool duration_value_is_null(const rapidjson::Value& matrix_entry) const = 0; diff --git a/src/routing/libosrm_wrapper.h b/src/routing/libosrm_wrapper.h index f931444d4..3192fdacc 100644 --- a/src/routing/libosrm_wrapper.h +++ b/src/routing/libosrm_wrapper.h @@ -31,6 +31,10 @@ class LibosrmWrapper : public Wrapper { Matrices get_matrices(const std::vector& locs) const override; + Matrices get_sparse_matrices(const std::vector& locs, + const std::vector& vehicles, + const std::vector& jobs) const override; + void add_geometry(Route& route) const override; }; diff --git a/src/routing/wrapper.h b/src/routing/wrapper.h index d949d7d00..69f5e8beb 100644 --- a/src/routing/wrapper.h +++ b/src/routing/wrapper.h @@ -16,6 +16,7 @@ All rights reserved (see LICENSE). #include "structures/vroom/location.h" #include "structures/vroom/matrices.h" #include "structures/vroom/solution/route.h" +#include "structures/vroom/vehicle.h" #include "utils/exception.h" namespace vroom::routing { @@ -27,6 +28,11 @@ 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 void add_geometry(Route& route) const = 0; virtual ~Wrapper() = default; diff --git a/src/structures/vroom/input/input.cpp b/src/structures/vroom/input/input.cpp index 3d5255253..c44ca4def 100644 --- a/src/structures/vroom/input/input.cpp +++ b/src/structures/vroom/input/input.cpp @@ -903,7 +903,10 @@ routing::Matrices Input::get_matrices_by_profile(const std::string& profile, }); assert(rw != _routing_wrappers.end()); - return sparse_filling ? (*rw)->get_matrices(_locations) + return sparse_filling ? (*rw)->get_sparse_matrices(profile, + _locations, + this->vehicles, + this->jobs) : (*rw)->get_matrices(_locations); }