-
Notifications
You must be signed in to change notification settings - Fork 676
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
Refactor matrix_action #4535
Refactor matrix_action #4535
Changes from all commits
96cb6d6
da69b08
9f3f340
6919d7c
96deff2
23aa7fb
656352d
5f93bc5
e4263ce
fefe4d9
952bc17
c35d876
d90c569
c5f45f5
02a3f47
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,16 +49,15 @@ class CostMatrix::ReachedMap : public robin_hood::unordered_map<uint64_t, std::v | |
|
||
// Constructor with cost threshold. | ||
CostMatrix::CostMatrix(const boost::property_tree::ptree& config) | ||
: max_reserved_labels_count_(config.get<uint32_t>("max_reserved_labels_count_bidir_dijkstras", | ||
: MatrixAlgorithm(config), | ||
max_reserved_labels_count_(config.get<uint32_t>("max_reserved_labels_count_bidir_dijkstras", | ||
kInitialEdgeLabelCountBidirDijkstra)), | ||
clear_reserved_memory_(config.get<bool>("clear_reserved_memory", false)), | ||
max_reserved_locations_count_( | ||
config.get<uint32_t>("max_reserved_locations_costmatrix", kMaxLocationReservation)), | ||
check_reverse_connections_(config.get<bool>("costmatrix_check_reverse_connection", false)), | ||
access_mode_(kAutoAccess), | ||
mode_(travel_mode_t::kDrive), locs_count_{0, 0}, locs_remaining_{0, 0}, | ||
current_cost_threshold_(0), | ||
has_time_(false), targets_{new ReachedMap}, sources_{new ReachedMap} { | ||
current_cost_threshold_(0), targets_{new ReachedMap}, sources_{new ReachedMap} { | ||
} | ||
|
||
CostMatrix::~CostMatrix() { | ||
|
@@ -86,7 +85,7 @@ float CostMatrix::GetCostThreshold(const float max_matrix_distance) { | |
|
||
// Clear the temporary information generated during time + distance matrix | ||
// construction. | ||
void CostMatrix::clear() { | ||
void CostMatrix::Clear() { | ||
// Clear the target edge markings | ||
targets_->clear(); | ||
if (check_reverse_connections_) | ||
|
@@ -132,19 +131,17 @@ void CostMatrix::SourceToTarget(Api& request, | |
baldr::GraphReader& graphreader, | ||
const sif::mode_costing_t& mode_costing, | ||
const sif::travel_mode_t mode, | ||
const float max_matrix_distance, | ||
const bool has_time, | ||
const bool invariant, | ||
const ShapeFormat& shape_format) { | ||
Comment on lines
-136
to
-138
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I pulled all these out of the signature and set them inside the function instead. was pointless to have them in the signature, it'll all in the |
||
const float max_matrix_distance) { | ||
|
||
LOG_INFO("matrix::CostMatrix"); | ||
request.mutable_matrix()->set_algorithm(Matrix::CostMatrix); | ||
bool invariant = request.options().date_time_type() == Options::invariant; | ||
auto shape_format = request.options().shape_format(); | ||
|
||
// Set the mode and costing | ||
mode_ = mode; | ||
costing_ = mode_costing[static_cast<uint32_t>(mode_)]; | ||
access_mode_ = costing_->access_mode(); | ||
has_time_ = has_time; | ||
|
||
auto& source_location_list = *request.mutable_options()->mutable_sources(); | ||
auto& target_location_list = *request.mutable_options()->mutable_targets(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,43 +21,9 @@ | |
namespace valhalla { | ||
namespace thor { | ||
|
||
std::string thor_worker_t::matrix(Api& request) { | ||
// time this whole method and save that statistic | ||
auto _ = measure_scope_time(request); | ||
|
||
auto& options = *request.mutable_options(); | ||
adjust_scores(options); | ||
auto costing = parse_costing(request); | ||
|
||
// TODO: do this for others as well | ||
costmatrix_.set_interrupt(interrupt); | ||
|
||
// lambdas to do the real work | ||
auto costmatrix = [&](const bool has_time) { | ||
return costmatrix_.SourceToTarget(request, *reader, mode_costing, mode, | ||
max_matrix_distance.find(costing)->second, has_time, | ||
options.date_time_type() == Options::invariant, | ||
options.shape_format()); | ||
}; | ||
auto timedistancematrix = [&]() { | ||
if (options.shape_format() != no_shape) | ||
add_warning(request, 207); | ||
return time_distance_matrix_.SourceToTarget(request, *reader, mode_costing, mode, | ||
max_matrix_distance.find(costing)->second, | ||
options.matrix_locations(), | ||
options.date_time_type() == Options::invariant); | ||
}; | ||
MatrixAlgorithm* thor_worker_t::get_matrix_algorithm(Api& request, const bool has_time) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the diff in here is impossible to read. will show tmrw in code editor what's going on there. the basic gist is:
|
||
|
||
if (costing == "bikeshare") { | ||
if (options.shape_format() != no_shape) | ||
add_warning(request, 207); | ||
time_distance_bss_matrix_.SourceToTarget(request, *reader, mode_costing, mode, | ||
max_matrix_distance.find(costing)->second, | ||
options.matrix_locations()); | ||
return tyr::serializeMatrix(request); | ||
} | ||
|
||
Matrix::Algorithm matrix_algo = Matrix::CostMatrix; | ||
Matrix::Algorithm config_algo = Matrix::CostMatrix; | ||
switch (source_to_target_algorithm) { | ||
case SELECT_OPTIMAL: | ||
// TODO - Do further performance testing to pick the best algorithm for the job | ||
|
@@ -66,13 +32,13 @@ | |
case travel_mode_t::kBicycle: | ||
// Use CostMatrix if number of sources and number of targets | ||
// exceeds some threshold | ||
if (static_cast<uint32_t>(options.sources().size()) <= kCostMatrixThreshold || | ||
static_cast<uint32_t>(options.targets().size()) <= kCostMatrixThreshold) { | ||
matrix_algo = Matrix::TimeDistanceMatrix; | ||
if (static_cast<uint32_t>(request.options().sources().size()) <= kCostMatrixThreshold || | ||
static_cast<uint32_t>(request.options().targets().size()) <= kCostMatrixThreshold) { | ||
config_algo = Matrix::TimeDistanceMatrix; | ||
} | ||
break; | ||
case travel_mode_t::kPublicTransit: | ||
matrix_algo = Matrix::TimeDistanceMatrix; | ||
config_algo = Matrix::TimeDistanceMatrix; | ||
break; | ||
default: | ||
break; | ||
|
@@ -81,32 +47,60 @@ | |
case COST_MATRIX: | ||
break; | ||
case TIME_DISTANCE_MATRIX: | ||
matrix_algo = Matrix::TimeDistanceMatrix; | ||
config_algo = Matrix::TimeDistanceMatrix; | ||
break; | ||
} | ||
|
||
// similar to routing: prefer the exact unidirectional algo if not requested otherwise | ||
// don't use matrix_type, we only need it to set the right warnings for what will be used | ||
bool has_time = | ||
check_matrix_time(request, options.prioritize_bidirectional() ? Matrix::CostMatrix | ||
: Matrix::TimeDistanceMatrix); | ||
if (has_time && !options.prioritize_bidirectional() && source_to_target_algorithm != COST_MATRIX) { | ||
timedistancematrix(); | ||
} else if (has_time && options.prioritize_bidirectional() && | ||
if (has_time && !request.options().prioritize_bidirectional() && | ||
source_to_target_algorithm != COST_MATRIX) { | ||
return &time_distance_matrix_; | ||
} else if (has_time && request.options().prioritize_bidirectional() && | ||
source_to_target_algorithm != TIME_DISTANCE_MATRIX) { | ||
costmatrix(has_time); | ||
} else if (matrix_algo == Matrix::CostMatrix) { | ||
// if this happens, the server config only allows for timedist matrix | ||
if (has_time && !options.prioritize_bidirectional()) { | ||
return &costmatrix_; | ||
} else if (config_algo == Matrix::CostMatrix) { | ||
if (has_time && !request.options().prioritize_bidirectional()) { | ||
add_warning(request, 301); | ||
} | ||
costmatrix(has_time); | ||
return &costmatrix_; | ||
} else { | ||
if (has_time && options.prioritize_bidirectional()) { | ||
// if this happens, the server config only allows for timedist matrix | ||
if (has_time && request.options().prioritize_bidirectional()) { | ||
add_warning(request, 300); | ||
} | ||
timedistancematrix(); | ||
return &time_distance_matrix_; | ||
} | ||
} | ||
|
||
std::string thor_worker_t::matrix(Api& request) { | ||
// time this whole method and save that statistic | ||
auto _ = measure_scope_time(request); | ||
|
||
auto& options = *request.mutable_options(); | ||
adjust_scores(options); | ||
auto costing = parse_costing(request); | ||
|
||
bool has_time = | ||
check_matrix_time(request, options.prioritize_bidirectional() ? Matrix::CostMatrix | ||
: Matrix::TimeDistanceMatrix); | ||
|
||
// allow all algos to be cancelled | ||
for (auto* alg : std::vector<MatrixAlgorithm*>{ | ||
&costmatrix_, | ||
&time_distance_matrix_, | ||
&time_distance_bss_matrix_, | ||
}) { | ||
alg->set_interrupt(interrupt); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added the |
||
alg->set_has_time(has_time); | ||
} | ||
|
||
auto* algo = | ||
costing == "bikeshare" ? &time_distance_bss_matrix_ : get_matrix_algorithm(request, has_time); | ||
|
||
algo->SourceToTarget(request, *reader, mode_costing, mode, | ||
max_matrix_distance.find(costing)->second); | ||
|
||
return tyr::serializeMatrix(request); | ||
} | ||
} // namespace thor | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is now in the base class