Skip to content

Commit

Permalink
First sketch of HttpWrapper::get_sparse_matrices.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoupey committed Jul 12, 2024
1 parent 0594c7c commit 20c5af0
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 1 deletion.
71 changes: 71 additions & 0 deletions src/routing/http_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,77 @@ 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 {
std::size_t m_size = locs.size();
Matrices m(m_size);

for (const auto& v : vehicles) {
if (v.profile != profile) {
continue;
}

std::vector<Location> 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.
Expand Down
5 changes: 5 additions & 0 deletions src/routing/http_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ 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;

virtual bool
duration_value_is_null(const rapidjson::Value& matrix_entry) const = 0;

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

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

Matrices get_sparse_matrices(const std::vector<Location>& locs,
const std::vector<Vehicle>& vehicles,
const std::vector<Vehicle>& jobs) const override;

void add_geometry(Route& route) const override;
};

Expand Down
6 changes: 6 additions & 0 deletions src/routing/wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -27,6 +28,11 @@ 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 void add_geometry(Route& route) const = 0;

virtual ~Wrapper() = default;
Expand Down
5 changes: 4 additions & 1 deletion src/structures/vroom/input/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 20c5af0

Please sign in to comment.