Skip to content

Commit

Permalink
Store and re-use route geometries in plan mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoupey committed Jul 15, 2024
1 parent 5de1727 commit bc797b4
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 32 deletions.
12 changes: 7 additions & 5 deletions src/routing/http_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,12 @@ Matrices HttpWrapper::get_matrices(const std::vector<Location>& locs) const {
return m;
}

Matrices HttpWrapper::get_sparse_matrices(const std::string& profile,
const std::vector<Location>& locs,
const std::vector<Vehicle>& vehicles,
const std::vector<Job>& jobs) const {
Matrices HttpWrapper::get_sparse_matrices(
const std::string& profile,
const std::vector<Location>& locs,
const std::vector<Vehicle>& vehicles,
const std::vector<Job>& jobs,
std::unordered_map<Id, std::string>& v_id_to_geom) const {
std::size_t m_size = locs.size();
Matrices m(m_size);

Expand Down Expand Up @@ -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;
Expand Down
10 changes: 6 additions & 4 deletions src/routing/http_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ class HttpWrapper : public Wrapper {

Matrices get_matrices(const std::vector<Location>& locs) const override;

Matrices get_sparse_matrices(const std::string& profile,
const std::vector<Location>& locs,
const std::vector<Vehicle>& vehicles,
const std::vector<Job>& jobs) const override;
Matrices get_sparse_matrices(
const std::string& profile,
const std::vector<Location>& locs,
const std::vector<Vehicle>& vehicles,
const std::vector<Job>& jobs,
std::unordered_map<Id, std::string>& v_id_to_geom) const override;

virtual bool
duration_value_is_null(const rapidjson::Value& matrix_entry) const = 0;
Expand Down
11 changes: 6 additions & 5 deletions src/routing/libosrm_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,12 @@ Matrices LibosrmWrapper::get_matrices(const std::vector<Location>& locs) const {
return m;
}

Matrices
LibosrmWrapper::get_sparse_matrices(const std::string& profile,
const std::vector<Location>& locs,
const std::vector<Vehicle>& vehicles,
const std::vector<Job>& jobs) const {
Matrices LibosrmWrapper::get_sparse_matrices(
const std::string& profile,
const std::vector<Location>& locs,
const std::vector<Vehicle>& vehicles,
const std::vector<Job>& jobs,
std::unordered_map<Id, std::string>& v_id_to_geom) const {
std::size_t m_size = locs.size();
Matrices m(m_size);

Expand Down
10 changes: 6 additions & 4 deletions src/routing/libosrm_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ class LibosrmWrapper : public Wrapper {

Matrices get_matrices(const std::vector<Location>& locs) const override;

Matrices get_sparse_matrices(const std::string& profile,
const std::vector<Location>& locs,
const std::vector<Vehicle>& vehicles,
const std::vector<Job>& jobs) const override;
Matrices get_sparse_matrices(
const std::string& profile,
const std::vector<Location>& locs,
const std::vector<Vehicle>& vehicles,
const std::vector<Job>& jobs,
std::unordered_map<Id, std::string>& v_id_to_geom) const override;

void add_geometry(Route& route) const override;
};
Expand Down
10 changes: 6 additions & 4 deletions src/routing/wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ class Wrapper {

virtual Matrices get_matrices(const std::vector<Location>& locs) const = 0;

virtual Matrices get_sparse_matrices(const std::string& profile,
const std::vector<Location>& locs,
const std::vector<Vehicle>& vehicles,
const std::vector<Job>& jobs) const = 0;
virtual Matrices get_sparse_matrices(
const std::string& profile,
const std::vector<Location>& locs,
const std::vector<Vehicle>& vehicles,
const std::vector<Job>& jobs,
std::unordered_map<Id, std::string>& v_id_to_geom) const = 0;

virtual void add_geometry(Route& route) const = 0;

Expand Down
14 changes: 4 additions & 10 deletions src/structures/vroom/input/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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();
Expand Down
4 changes: 4 additions & 0 deletions src/structures/vroom/input/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ class Input {
bool _all_locations_have_coords{true};
std::vector<std::vector<Eval>> _jobs_vehicles_evals;

// Used in plan mode since we store route geometries while
// generating sparse matrices.
std::unordered_map<Id, std::string> _vehicle_id_to_geometry;

unsigned _amount_size{0};
Amount _zero{0};

Expand Down

0 comments on commit bc797b4

Please sign in to comment.