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

second pass for CostMatrix #4536

Merged
merged 30 commits into from Feb 2, 2024
Merged

second pass for CostMatrix #4536

merged 30 commits into from Feb 2, 2024

Conversation

nilsnolde
Copy link
Member

@nilsnolde nilsnolde commented Jan 29, 2024

fixes #4474

based on #4535

EDIT: on second thought, I'll leave TDMatrix as TODO for now. Less to review and less relevant overall. Not even sure it makes sense at all. Both TDMatrix algos don't seem to care about nothru pruning or even destonly (yet), hierarchies are not relevant either. Only thing they care about are conditional destonly but those are super rare.

TODO:

  • costmatrix
  • [ ] timedistancematrix
  • [ ] timedistancebssmatrix
  • tests
  • add a config parameter to control this?
  • Add a warning how many connections needed a 2nd pass

thoughts:

  • need to go through costmatrix again and make sure when a connection A -> B is not found, but B -> A is, that both A & B are doing another half-dijkstra, otherwise we don't get the bidirectional benefit; but of course need to additionally make sure they're not looking for anything else.. EDIT: checking the code again and running manually through the tests in debugging mode (impossible to prove with a test), I can confirm this is what's happening.

@nilsnolde nilsnolde changed the title second pass for matrix second pass for CostMatrix Jan 30, 2024
@@ -90,7 +90,7 @@ void BidirectionalAStar::Clear() {
// Set the ferry flag to false
has_ferry_ = false;
// Set not thru pruning to true
not_thru_pruning_ = true;
set_not_thru_pruning(true);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's a function for this, I find it nicer for comprehension to use it

Comment on lines +270 to +272
if (costing_->pass() > 0 && !(matrix.second_pass(connection_idx))) {
continue;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we're on the second pass and we didn't set the flag on the first pass, then don't bother processing anymore

auto* pbf_time_zone_name = matrix.mutable_time_zone_names()->Add();
*pbf_time_zone_name = "";
// let's try a second pass for this connection
matrix.mutable_second_pass()->Set(connection_idx, true);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

second_pass is defaulting to false, true is signaling the second pass that it wasn't found initially

Comment on lines +359 to +361
best_connection_.emplace_back(empty, empty, Cost{0.0f, matrix.times(connection_idx)},
matrix.distances(connection_idx));
best_connection_.back().found = true;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we have previously found connections, we transfer the important attributes which make up the response

if (!s.unfound_connections.empty()) {
locs_remaining_[MATRIX_FORW]++;
} else {
// don't look at sources which don't have unfound connections, important for second pass
s.threshold = 0;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setting the threshold to 0 for sources which have all destination found on the first pass, makes sure we don't even expand those sources anymore

@@ -538,7 +550,7 @@ bool CostMatrix::ExpandInner(baldr::GraphReader& graphreader,
pred_dist, newcost.cost);
}

return true;
return !(pred.not_thru_pruning() && meta.edge->not_thru());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

allow u-turns if we hit a not thru edge

@@ -312,6 +312,7 @@ std::vector<std::vector<thor::PathInfo>> thor_worker_t::get_path(PathAlgorithm*

// Check if we should run a second pass pedestrian route with different A*
// (to look for better routes where a ferry is taken)
// TODO(nils): how would a second pass find a better route, if it changes nothing ferry-related?
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seemed strange to me

Comment on lines +105 to +110
// TODO(nils): TDMatrix doesn't care about either destonly or no_thru
if (algo->name() != "costmatrix") {
algo->SourceToTarget(request, *reader, mode_costing, mode,
max_matrix_distance.find(costing)->second);
return tyr::serializeMatrix(request);
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't do second passes for tdmatrices (bss or normal), both don't have a concept of neither destonly nor not_thru

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually unidir a* also allows for destonly, not sure why?

Comment on lines +291 to +293
*matrix.mutable_date_times(connection_idx) = dt_info.date_time;
*matrix.mutable_time_zone_offsets(connection_idx) = dt_info.time_zone_offset;
*matrix.mutable_time_zone_names(connection_idx) = dt_info.time_zone_name;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also changed this whole logic, it became unbearable now that we're "randomly" processing connections, i.e. not in sequence anymore. I realized that RepeatedPtrFields don't have a Set() method but also their operator() methods aren't const, so we can just assign new values. probably that's quite a bit worse for performance, but I doubt it's very measurable wrt to the whole expansion.

Copy link
Member Author

@nilsnolde nilsnolde Jan 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

basically now we're initializing all repeated string fields with "" in reserve_pbf_arrays(), just like the other fields which have Set()

valhalla/thor/costmatrix.h Outdated Show resolved Hide resolved
test/test.cc Outdated Show resolved Hide resolved
valhalla/thor/costmatrix.h Outdated Show resolved Hide resolved
@kevinkreiser
Copy link
Member

this looks broadly good to me other than the tiny nitpicks we just discussed. let me know when those are patched and ill put a ship. would also be nice if @dnesbitt61 had a sec to look

@nilsnolde
Copy link
Member Author

@kevinkreiser I addressed the comments

@nilsnolde nilsnolde merged commit 3a92152 into master Feb 2, 2024
9 checks passed
@nilsnolde nilsnolde deleted the nn-matrix-second-pass branch February 2, 2024 18:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

second pass for matrix's unconnected pairs
2 participants