Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cost matrix early abort #3611

Merged
merged 9 commits into from
May 19, 2022
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@
* CHANGED: modernized spatialite syntax [#3580](https://github.com/valhalla/valhalla/pull/3580)
* ADDED: Options to generate partial results for time distance matrix when there is one source (one to many) or one target (many to one). [#3181](https://github.com/valhalla/valhalla/pull/3181)
* ADDED: Enhance valhalla_build_elevation with LZ4 recompression support [#3607](https://github.com/valhalla/valhalla/pull/3607)
* CHANGED: removed UK admin and upgraded its constituents to countries [#3619](https://github.com/valhalla/valhalla/pull/3619) * CHANGED: expansion service: only track requested max time/distance [#3532](https://github.com/valhalla/valhalla/pull/3509)
* CHANGED: removed UK admin and upgraded its constituents to countries [#3619](https://github.com/valhalla/valhalla/pull/3619)
* CHANGED: expansion service: only track requested max time/distance [#3532](https://github.com/valhalla/valhalla/pull/3509)
* ADDED: Shorten down the request delay, when some sources/targets searches are early aborted [#3611](https://github.com/valhalla/valhalla/pull/3611)

## Release Date: 2021-10-07 Valhalla 3.1.4
* **Removed**
Expand Down
28 changes: 28 additions & 0 deletions src/thor/costmatrix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,20 @@ std::vector<TimeDistance> CostMatrix::SourceToTarget(
target_status_[i].threshold--;
BackwardSearch(i, graphreader);
if (target_status_[i].threshold == 0) {
for (uint32_t source = 0; source < source_count_; source++) {
// Get all targets remaining for the origin
auto& targets = source_status_[source].remaining_locations;
auto it = targets.find(i);
if (it != targets.end()) {
targets.erase(it);
if (targets.empty() && source_status_[source].threshold > 0) {
source_status_[i].threshold = -1;
if (remaining_sources_ > 0) {
remaining_sources_--;
}
}
}
}
target_status_[i].threshold = -1;
if (remaining_targets_ > 0) {
remaining_targets_--;
Expand All @@ -155,6 +169,20 @@ std::vector<TimeDistance> CostMatrix::SourceToTarget(
source_status_[i].threshold--;
ForwardSearch(i, n, graphreader);
if (source_status_[i].threshold == 0) {
for (uint32_t target = 0; target < target_count_; target++) {
// Get all sources remaining for the destination
auto& sources = target_status_[target].remaining_locations;
auto it = sources.find(i);
if (it != sources.end()) {
sources.erase(it);
if (sources.empty() && target_status_[target].threshold > 0) {
target_status_[i].threshold = -1;
if (remaining_targets_ > 0) {
remaining_targets_--;
}
}
}
}
source_status_[i].threshold = -1;
if (remaining_sources_ > 0) {
remaining_sources_--;
Expand Down
26 changes: 26 additions & 0 deletions test/matrix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,32 @@ TEST(Matrix, test_matrix) {
" to expected value for CostMatrix";
}

CostMatrix cost_matrix_abort_source;
results = cost_matrix_abort_source.SourceToTarget(request.options().sources(),
request.options().targets(), reader, mode_costing,
sif::TravelMode::kDrive, 100000.0);

uint32_t found = 0;
for (uint32_t i = 0; i < results.size(); ++i) {
if (results[i].dist < kMaxCost) {
++found;
}
}
EXPECT_EQ(found, 15) << " not the number of results as expected";

CostMatrix cost_matrix_abort_target;
results = cost_matrix_abort_target.SourceToTarget(request.options().sources(),
request.options().targets(), reader, mode_costing,
sif::TravelMode::kDrive, 50000.0);

found = 0;
for (uint32_t i = 0; i < results.size(); ++i) {
if (results[i].dist < kMaxCost) {
++found;
}
}
EXPECT_EQ(found, 10) << " not the number of results as expected";

TimeDistanceMatrix timedist_matrix;
results = timedist_matrix.SourceToTarget(request.options().sources(), request.options().targets(),
reader, mode_costing, sif::TravelMode::kDrive, 400000.0);
Expand Down