/
costmatrix.cc
1283 lines (1136 loc) · 56.3 KB
/
costmatrix.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <algorithm>
#include <cmath>
#include <vector>
#include "baldr/datetime.h"
#include "midgard/encoded.h"
#include "midgard/logging.h"
#include "sif/recost.h"
#include "thor/costmatrix.h"
#include "worker.h"
#include <robin_hood.h>
using namespace valhalla::baldr;
using namespace valhalla::sif;
namespace {
constexpr uint32_t kMaxMatrixIterations = 2000000;
constexpr uint32_t kMaxThreshold = std::numeric_limits<int>::max();
constexpr uint32_t kMaxLocationReservation = 25; // the default config for max matrix locations
// Find a threshold to continue the search - should be based on
// the max edge cost in the adjacency set?
int GetThreshold(const travel_mode_t mode, const int n) {
return (mode == travel_mode_t::kDrive) ? std::min(2800, std::max(100, n / 3)) : 500;
}
bool equals(const valhalla::LatLng& a, const valhalla::LatLng& b) {
return a.has_lat_case() == b.has_lat_case() && a.has_lng_case() == b.has_lng_case() &&
(!a.has_lat_case() || a.lat() == b.lat()) && (!a.has_lng_case() || a.lng() == b.lng());
}
inline const valhalla::PathEdge& find_correlated_edge(const valhalla::Location& location,
const GraphId& edge_id) {
for (const auto& e : location.correlation().edges()) {
if (e.graph_id() == edge_id)
return e;
}
throw std::logic_error("Could not find candidate edge used for label");
}
} // namespace
namespace valhalla {
namespace thor {
class CostMatrix::ReachedMap : public robin_hood::unordered_map<uint64_t, std::vector<uint32_t>> {};
// Constructor with cost threshold.
CostMatrix::CostMatrix(const boost::property_tree::ptree& config)
: MatrixAlgorithm(config),
max_reserved_labels_count_(config.get<uint32_t>("max_reserved_labels_count_bidir_dijkstras",
kInitialEdgeLabelCountBidirDijkstra)),
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), targets_{new ReachedMap}, sources_{new ReachedMap} {
}
CostMatrix::~CostMatrix() {
}
float CostMatrix::GetCostThreshold(const float max_matrix_distance) {
float cost_threshold;
switch (mode_) {
case travel_mode_t::kBicycle:
cost_threshold = max_matrix_distance / kCostThresholdBicycleDivisor;
break;
case travel_mode_t::kPedestrian:
case travel_mode_t::kPublicTransit:
cost_threshold = max_matrix_distance / kCostThresholdPedestrianDivisor;
break;
case travel_mode_t::kDrive:
default:
cost_threshold = max_matrix_distance / kCostThresholdAutoDivisor;
}
// Increase the cost threshold to make sure requests near the max distance succeed.
// Some costing models and locations require higher thresholds to succeed.
return cost_threshold * 2.0f;
}
// Clear the temporary information generated during time + distance matrix
// construction.
void CostMatrix::Clear() {
// Clear the target edge markings
targets_->clear();
if (check_reverse_connections_)
sources_->clear();
// Clear all source adjacency lists, edge labels, and edge status
// Resize and shrink_to_fit so all capacity is reduced.
auto label_reservation = clear_reserved_memory_ ? 0 : max_reserved_labels_count_;
auto locs_reservation = clear_reserved_memory_ ? 0 : max_reserved_locations_count_;
for (const auto exp_dir : {MATRIX_FORW, MATRIX_REV}) {
// resize all relevant structures down to configured amount of locations (25 default)
if (locs_count_[exp_dir] > locs_reservation) {
edgelabel_[exp_dir].resize(locs_reservation);
edgelabel_[exp_dir].shrink_to_fit();
adjacency_[exp_dir].resize(locs_reservation);
adjacency_[exp_dir].shrink_to_fit();
edgestatus_[exp_dir].resize(locs_reservation);
edgestatus_[exp_dir].shrink_to_fit();
}
for (auto& iter : edgelabel_[exp_dir]) {
if (iter.size() > label_reservation) {
iter.resize(label_reservation);
iter.shrink_to_fit();
}
iter.clear();
}
for (auto& iter : edgestatus_[exp_dir]) {
iter.clear();
}
for (auto& iter : adjacency_[exp_dir]) {
iter.clear();
}
hierarchy_limits_[exp_dir].clear();
locs_status_[exp_dir].clear();
}
best_connection_.clear();
ignore_hierarchy_limits_ = false;
}
// Form a time distance matrix from the set of source locations
// to the set of target locations.
bool 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) {
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();
auto& source_location_list = *request.mutable_options()->mutable_sources();
auto& target_location_list = *request.mutable_options()->mutable_targets();
current_cost_threshold_ = GetCostThreshold(max_matrix_distance);
auto time_infos = SetOriginTimes(source_location_list, graphreader);
// Initialize best connections and status. Any locations that are the
// same get set to 0 time, distance and are not added to the remaining
// location set.
Initialize(source_location_list, target_location_list, request.matrix());
// Set the source and target locations
// TODO: for now we only allow depart_at/current date_time
SetSources(graphreader, source_location_list, time_infos);
SetTargets(graphreader, target_location_list);
// Update hierarchy limits
if (!ignore_hierarchy_limits_)
ModifyHierarchyLimits();
// Perform backward search from all target locations. Perform forward
// search from all source locations. Connections between the 2 search
// spaces is checked during the forward search.
uint32_t n = 0;
uint32_t interrupt_n = 0;
while (true) {
// First iterate over all targets, then over all sources: we only for sure
// check the connection between both trees on the forward search, so reverse
// has to come first
for (uint32_t i = 0; i < locs_count_[MATRIX_REV]; i++) {
if (locs_status_[MATRIX_REV][i].threshold > 0) {
locs_status_[MATRIX_REV][i].threshold--;
Expand<MatrixExpansionType::reverse>(i, n, graphreader);
// if we exhausted this search
if (locs_status_[MATRIX_REV][i].threshold == 0) {
for (uint32_t source = 0; source < locs_count_[MATRIX_FORW]; source++) {
// update the target for each source's remaining targets, if it still exists
auto& targets = locs_status_[MATRIX_FORW][source].unfound_connections;
auto it = targets.find(i);
if (it != targets.end()) {
// remove the target so we don't come here again
targets.erase(it);
// if there's no more target and the current source has not exhausted
// we update the source's threshold so that it doesn't enter its outer "if" statement
// anymore
if (targets.empty() && locs_status_[MATRIX_FORW][source].threshold > 0) {
// TODO(nils): shouldn't we extend the search here similar to bidir A*
// i.e. if pruning was disabled we extend the search in the other direction
locs_status_[MATRIX_FORW][i].threshold = -1;
if (locs_remaining_[MATRIX_FORW] > 0) {
locs_remaining_[MATRIX_FORW]--;
}
}
}
}
// in any case make sure this was the last time we looked at this target
locs_status_[MATRIX_REV][i].threshold = -1;
if (locs_remaining_[MATRIX_REV] > 0) {
locs_remaining_[MATRIX_REV]--;
}
}
}
}
for (uint32_t i = 0; i < locs_count_[MATRIX_FORW]; i++) {
if (locs_status_[MATRIX_FORW][i].threshold > 0) {
locs_status_[MATRIX_FORW][i].threshold--;
Expand<MatrixExpansionType::forward>(i, n, graphreader, time_infos[i], invariant);
// if we exhausted this search
if (locs_status_[MATRIX_FORW][i].threshold == 0) {
for (uint32_t target = 0; target < locs_count_[MATRIX_REV]; target++) {
// if we still didn't find the connection between this pair
auto& sources = locs_status_[MATRIX_REV][target].unfound_connections;
auto it = sources.find(i);
if (it != sources.end()) {
// remove the source so we don't come here again
sources.erase(it);
// if there's no more sources and the current target has not exhausted
// we update the target's threshold so that it doesn't enter this outer "if" statement
// anymore
if (sources.empty() && locs_status_[MATRIX_REV][target].threshold > 0) {
// TODO(nils): shouldn't we extend the search here similar to bidir A*
// i.e. if pruning was disabled we extend the search in the other direction
locs_status_[MATRIX_REV][i].threshold = -1;
if (locs_remaining_[MATRIX_REV] > 0) {
locs_remaining_[MATRIX_REV]--;
}
}
}
}
// in any case make sure this was the last time we looked at this source
locs_status_[MATRIX_FORW][i].threshold = -1;
if (locs_remaining_[MATRIX_FORW] > 0) {
locs_remaining_[MATRIX_FORW]--;
}
}
}
}
// Break out when remaining sources and targets to expand are both 0
if (locs_remaining_[MATRIX_FORW] == 0 && locs_remaining_[MATRIX_REV] == 0) {
LOG_DEBUG("SourceToTarget iterations: n = " + std::to_string(n));
break;
}
// Protect against edge cases that may lead to never breaking out of
// this loop. This should never occur but lets make sure.
if (n >= kMaxMatrixIterations) {
throw valhalla_exception_t{430};
}
// Allow this process to be aborted
if (interrupt_ && (interrupt_n++ % kInterruptIterationsInterval) == 0) {
(*interrupt_)();
}
n++;
}
// resize/reserve all properties of Matrix on first pass only
valhalla::Matrix& matrix = *request.mutable_matrix();
reserve_pbf_arrays(matrix, best_connection_.size(), costing_->pass());
// Form the matrix PBF output
graph_tile_ptr tile;
bool connection_failed = false;
for (uint32_t connection_idx = 0; connection_idx < best_connection_.size(); connection_idx++) {
auto best_connection = best_connection_[connection_idx];
// if this is the second pass we don't have to process previously found ones again
if (costing_->pass() > 0 && !(matrix.second_pass(connection_idx))) {
continue;
}
uint32_t target_idx = connection_idx % target_location_list.size();
uint32_t source_idx = connection_idx / target_location_list.size();
// first recost and form the path, if desired (either time and/or geometry requested)
const auto shape = RecostFormPath(graphreader, best_connection, source_location_list[source_idx],
target_location_list[target_idx], source_idx, target_idx,
time_infos[source_idx], invariant, shape_format);
float time = best_connection.cost.secs;
if (time < kMaxCost) {
auto dt_info =
DateTime::offset_date(source_location_list[source_idx].date_time(),
time_infos[source_idx].timezone_index,
graphreader.GetTimezoneFromEdge(edgelabel_[MATRIX_REV][target_idx]
.front()
.edgeid(),
tile),
time);
*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;
} else {
// let's try a second pass for this connection
matrix.mutable_second_pass()->Set(connection_idx, true);
connection_failed = true;
}
matrix.mutable_from_indices()->Set(connection_idx, source_idx);
matrix.mutable_to_indices()->Set(connection_idx, target_idx);
matrix.mutable_distances()->Set(connection_idx, best_connection.distance);
matrix.mutable_times()->Set(connection_idx, time);
*matrix.mutable_shapes(connection_idx) = shape;
}
return !connection_failed;
}
// Initialize all time distance to "not found". Any locations that
// are the same get set to 0 time, distance and do not add to the
// remaining locations set.
void CostMatrix::Initialize(
const google::protobuf::RepeatedPtrField<valhalla::Location>& source_locations,
const google::protobuf::RepeatedPtrField<valhalla::Location>& target_locations,
const valhalla::Matrix& matrix) {
locs_count_[MATRIX_FORW] = source_locations.size();
locs_count_[MATRIX_REV] = target_locations.size();
const auto& hlimits = costing_->GetHierarchyLimits();
ignore_hierarchy_limits_ =
std::all_of(hlimits.begin() + 1, hlimits.begin() + TileHierarchy::levels().size(),
[](const HierarchyLimits& limits) {
return limits.max_up_transitions == kUnlimitedTransitions;
});
// Add initial sources status
for (const auto exp_dir : {MATRIX_FORW, MATRIX_REV}) {
const auto count = locs_count_[exp_dir];
locs_status_[exp_dir].reserve(count);
hierarchy_limits_[exp_dir].resize(count);
adjacency_[exp_dir].resize(count);
edgestatus_[exp_dir].resize(count);
edgelabel_[exp_dir].resize(count);
for (uint32_t i = 0; i < count; i++) {
// Allocate the adjacency list and hierarchy limits for this source.
// Use the cost threshold to size the adjacency list.
edgelabel_[exp_dir][i].reserve(max_reserved_labels_count_);
adjacency_[exp_dir][i].reuse(0, current_cost_threshold_, costing_->UnitSize(),
&edgelabel_[exp_dir][i]);
locs_status_[exp_dir].emplace_back(kMaxThreshold);
hierarchy_limits_[exp_dir][i] = hlimits;
}
}
// Initialize best connection
GraphId empty;
Cost trivial_cost(0.0f, 0.0f);
Cost max_cost(kMaxCost, kMaxCost);
best_connection_.reserve(locs_count_[MATRIX_FORW] * locs_count_[MATRIX_REV]);
for (uint32_t i = 0; i < locs_count_[MATRIX_FORW]; i++) {
for (uint32_t j = 0; j < locs_count_[MATRIX_REV]; j++) {
const auto connection_idx = i * static_cast<uint32_t>(target_locations.size()) + j;
if (equals(source_locations.Get(i).ll(), target_locations.Get(j).ll())) {
best_connection_.emplace_back(empty, empty, trivial_cost, 0.0f);
best_connection_.back().found = true;
} else if (costing_->pass() > 0 && !matrix.second_pass(connection_idx)) {
// we've found this connection in a previous pass, we only need the time & distance
best_connection_.emplace_back(empty, empty, Cost{0.0f, matrix.times(connection_idx)},
matrix.distances(connection_idx));
best_connection_.back().found = true;
} else {
// in a second pass this block makes sure that if e.g. A -> B is found, but B -> A isn't,
// we still expand both A & B to get the bidirectional benefit
best_connection_.emplace_back(empty, empty, max_cost, static_cast<uint32_t>(kMaxCost));
locs_status_[MATRIX_FORW][i].unfound_connections.insert(j);
locs_status_[MATRIX_REV][j].unfound_connections.insert(i);
}
}
}
// Set the remaining number of sources and targets
locs_remaining_[MATRIX_FORW] = 0;
for (auto& s : locs_status_[MATRIX_FORW]) {
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;
}
}
locs_remaining_[MATRIX_REV] = 0;
for (auto& t : locs_status_[MATRIX_REV]) {
if (!t.unfound_connections.empty()) {
locs_remaining_[MATRIX_REV]++;
} else {
// don't look at targets which don't have unfound connections, important for second pass
t.threshold = 0;
}
}
}
template <const MatrixExpansionType expansion_direction, const bool FORWARD>
bool CostMatrix::ExpandInner(baldr::GraphReader& graphreader,
const uint32_t index,
const sif::BDEdgeLabel& pred,
const baldr::DirectedEdge* opp_pred_edge,
const baldr::NodeInfo* nodeinfo,
const uint32_t pred_idx,
const EdgeMetadata& meta,
uint32_t& shortcuts,
const graph_tile_ptr& tile,
const baldr::TimeInfo& time_info) {
// Skip if this is a regular edge superseded by a shortcut.
if (shortcuts & meta.edge->superseded()) {
return false;
}
graph_tile_ptr t2 = nullptr;
baldr::GraphId opp_edge_id;
const auto get_opp_edge_data = [&t2, &opp_edge_id, &graphreader, &meta, &tile]() {
t2 = meta.edge->leaves_tile() ? graphreader.GetGraphTile(meta.edge->endnode()) : tile;
if (t2 == nullptr) {
return false;
}
opp_edge_id = t2->GetOpposingEdgeId(meta.edge);
return true;
};
if (meta.edge->is_shortcut()) {
// Skip shortcuts if hierarchy limits are disabled or the opposing tile doesn't exist
if (ignore_hierarchy_limits_ || !get_opp_edge_data())
return false;
// Skip shortcut edges until we have stopped expanding on the next level. Use regular
// edges while still expanding on the next level since we can still transition down to
// that level. If using a shortcut, set the shortcuts mask. Skip if this is a regular
// edge superseded by a shortcut.
if (hierarchy_limits_[FORWARD][index][meta.edge_id.level() + 1].StopExpanding()) {
shortcuts |= meta.edge->shortcut();
} else {
return false;
}
}
// Skip this edge if permanently labeled (best path already found to this
// directed edge) or if no access for this mode.
if (meta.edge_status->set() == EdgeSet::kPermanent) {
return true;
}
const baldr::DirectedEdge* opp_edge = nullptr;
if (!FORWARD) {
// Check the access mode and skip this edge if access is not allowed in the reverse
// direction. This avoids the (somewhat expensive) retrieval of the opposing directed
// edge when no access is allowed in the reverse direction.
if (!(meta.edge->reverseaccess() & access_mode_)) {
return false;
}
if (t2 == nullptr && !get_opp_edge_data()) {
return false;
}
opp_edge = t2->directededge(opp_edge_id);
}
auto& edgelabels = edgelabel_[FORWARD][index];
// Skip this edge if no access is allowed (based on costing method)
// or if a complex restriction prevents transition onto this edge.
uint8_t restriction_idx = -1;
if (FORWARD) {
if (!costing_->Allowed(meta.edge, false, pred, tile, meta.edge_id, time_info.local_time,
time_info.timezone_index, restriction_idx) ||
costing_->Restricted(meta.edge, pred, edgelabels, tile, meta.edge_id, true,
&edgestatus_[FORWARD][index], time_info.local_time,
time_info.timezone_index)) {
return false;
}
} else {
if (!costing_->AllowedReverse(meta.edge, pred, opp_edge, t2, opp_edge_id, time_info.local_time,
time_info.timezone_index, restriction_idx) ||
costing_->Restricted(meta.edge, pred, edgelabels, tile, meta.edge_id, false,
&edgestatus_[FORWARD][index], time_info.local_time,
time_info.timezone_index)) {
return false;
}
}
// Get cost. Separate out transition cost.
uint8_t flow_sources;
Cost newcost = pred.cost() + (FORWARD ? costing_->EdgeCost(meta.edge, tile, time_info, flow_sources)
: costing_->EdgeCost(opp_edge, t2, time_info, flow_sources));
sif::Cost tc =
FORWARD ? costing_->TransitionCost(meta.edge, nodeinfo, pred)
: costing_->TransitionCostReverse(meta.edge->localedgeidx(), nodeinfo, opp_edge,
opp_pred_edge,
static_cast<bool>(flow_sources & kDefaultFlowMask),
pred.internal_turn());
newcost += tc;
const auto pred_dist = pred.path_distance() + meta.edge->length();
auto& adj = adjacency_[FORWARD][index];
// Check if edge is temporarily labeled and this path has less cost. If
// less cost the predecessor is updated along with new cost and distance.
if (meta.edge_status->set() == EdgeSet::kTemporary) {
BDEdgeLabel& lab = edgelabel_[FORWARD][index][meta.edge_status->index()];
if (newcost.cost < lab.cost().cost) {
adj.decrease(meta.edge_status->index(), newcost.cost);
lab.Update(pred_idx, newcost, newcost.cost, tc, pred_dist, restriction_idx);
}
// Returning true since this means we approved the edge
return true;
}
// Get end node tile (skip if tile is not found) and opposing edge Id
if (t2 == nullptr && !get_opp_edge_data()) {
return false;
}
// not_thru_pruning_ is only set to false on the 2nd pass in matrix_action.
// TODO(nils): one of these cases where I think reverse tree should look at the opposing edge,
// not the expanding one, same for quite some attributes below (and same in bidir a*)
bool thru = not_thru_pruning_ ? (pred.not_thru_pruning() || !meta.edge->not_thru()) : false;
// Add edge label, add to the adjacency list and set edge status
uint32_t idx = edgelabels.size();
*meta.edge_status = {EdgeSet::kTemporary, idx};
if (FORWARD) {
edgelabels.emplace_back(pred_idx, meta.edge_id, opp_edge_id, meta.edge, newcost, mode_, tc,
pred_dist, thru,
(pred.closure_pruning() || !costing_->IsClosed(meta.edge, tile)),
static_cast<bool>(flow_sources & kDefaultFlowMask),
costing_->TurnType(pred.opp_local_idx(), nodeinfo, meta.edge),
restriction_idx, 0,
meta.edge->destonly() ||
(costing_->is_hgv() && meta.edge->destonly_hgv()));
} else {
edgelabels.emplace_back(pred_idx, meta.edge_id, opp_edge_id, meta.edge, newcost, mode_, tc,
pred_dist, thru,
(pred.closure_pruning() || !costing_->IsClosed(meta.edge, tile)),
static_cast<bool>(flow_sources & kDefaultFlowMask),
costing_->TurnType(meta.edge->localedgeidx(), nodeinfo, opp_edge,
opp_pred_edge),
restriction_idx, 0,
opp_edge->destonly() || (costing_->is_hgv() && opp_edge->destonly_hgv()));
}
adj.add(idx);
// mark the edge as settled for the connection check
if (!FORWARD) {
(*targets_)[meta.edge_id].push_back(index);
} else if (check_reverse_connections_) {
(*sources_)[meta.edge_id].push_back(index);
}
// setting this edge as reached
if (expansion_callback_) {
expansion_callback_(graphreader, meta.edge_id, pred.edgeid(), "costmatrix", "r", newcost.secs,
pred_dist, newcost.cost);
}
return !(pred.not_thru_pruning() && meta.edge->not_thru());
}
template <const MatrixExpansionType expansion_direction, const bool FORWARD>
bool CostMatrix::Expand(const uint32_t index,
const uint32_t n,
baldr::GraphReader& graphreader,
const baldr::TimeInfo& time_info,
const bool invariant) {
auto& adj = adjacency_[FORWARD][index];
auto& edgelabels = edgelabel_[FORWARD][index];
uint32_t pred_idx = adj.pop();
if (pred_idx == kInvalidLabel) {
// search is exhausted - mark this and update so we don't
// extend searches more than we need to
for (uint32_t st = 0; st < locs_count_[!FORWARD]; st++) {
if (FORWARD) {
UpdateStatus(index, st);
} else {
UpdateStatus(st, index);
}
}
locs_status_[FORWARD][index].threshold = 0;
return false;
}
// Get edge label and check cost threshold
auto pred = edgelabels[pred_idx];
if (pred.cost().secs > current_cost_threshold_) {
locs_status_[FORWARD][index].threshold = 0;
return false;
}
// Settle this edge and log it if requested
auto& edgestatus = edgestatus_[FORWARD][index];
edgestatus.Update(pred.edgeid(), EdgeSet::kPermanent);
if (expansion_callback_) {
auto prev_pred =
pred.predecessor() == kInvalidLabel ? GraphId{} : edgelabels[pred.predecessor()].edgeid();
expansion_callback_(graphreader, pred.edgeid(), prev_pred, "costmatrix", "s", pred.cost().secs,
pred.path_distance(), pred.cost().cost);
}
if (FORWARD) {
CheckForwardConnections(index, pred, n, graphreader);
} else if (check_reverse_connections_) {
CheckReverseConnections(index, pred, n, graphreader);
}
GraphId node = pred.endnode();
// Prune path if predecessor is not a through edge or if the maximum
// number of upward transitions has been exceeded on this hierarchy level.
if ((pred.not_thru() && pred.not_thru_pruning()) ||
(!ignore_hierarchy_limits_ &&
hierarchy_limits_[FORWARD][index][node.level()].StopExpanding())) {
return false;
}
// Get the tile and the node info. Skip if tile is null (can happen
// with regional data sets) or if no access at the node.
graph_tile_ptr tile = graphreader.GetGraphTile(node);
if (tile == nullptr) {
return false;
}
const NodeInfo* nodeinfo = tile->node(node);
// set the time info
auto seconds_offset = invariant ? 0.f : pred.cost().secs;
auto offset_time = FORWARD
? time_info.forward(seconds_offset, static_cast<int>(nodeinfo->timezone()))
: time_info.reverse(seconds_offset, static_cast<int>(nodeinfo->timezone()));
// Get the opposing predecessor directed edge if this is reverse.
const DirectedEdge* opp_pred_edge = nullptr;
if (!FORWARD) {
const auto rev_pred_tile = graphreader.GetGraphTile(pred.opp_edgeid(), tile);
if (rev_pred_tile == nullptr) {
return false;
}
opp_pred_edge = rev_pred_tile->directededge(pred.opp_edgeid());
}
// keep track of shortcuts
uint32_t shortcuts = 0;
// If we encounter a node with an access restriction like a barrier we allow a uturn
if (!costing_->Allowed(nodeinfo)) {
const DirectedEdge* opp_edge = nullptr;
const GraphId opp_edge_id = graphreader.GetOpposingEdgeId(pred.edgeid(), opp_edge, tile);
// Mark the predecessor as a deadend to be consistent with how the
// edgelabels are set when an *actual* deadend (i.e. some dangling OSM geometry)
// is labelled
pred.set_deadend(true);
// Check if edge is null before using it (can happen with regional data sets)
return opp_edge && ExpandInner<expansion_direction>(graphreader, index, pred, opp_pred_edge,
nodeinfo, pred_idx,
{opp_edge, opp_edge_id,
edgestatus.GetPtr(opp_edge_id, tile)},
shortcuts, tile, offset_time);
}
// catch u-turn attempts
bool disable_uturn = false;
EdgeMetadata meta = EdgeMetadata::make(node, nodeinfo, tile, edgestatus);
EdgeMetadata uturn_meta{};
// Expand from end node in <expansion_direction> direction.
for (uint32_t i = 0; i < nodeinfo->edge_count(); ++i, ++meta) {
// Begin by checking if this is the opposing edge to pred.
// If so, it means we are attempting a u-turn. In that case, lets wait with evaluating
// this edge until last. If any other edges were emplaced, it means we should not
// even try to evaluate a u-turn since u-turns should only happen for deadends
const bool is_uturn = pred.opp_local_idx() == meta.edge->localedgeidx();
uturn_meta = is_uturn ? meta : uturn_meta;
// Expand but only if this isnt the uturn, we'll try that later if nothing else works out
disable_uturn =
(!is_uturn &&
ExpandInner<expansion_direction>(graphreader, index, pred, opp_pred_edge, nodeinfo, pred_idx,
meta, shortcuts, tile, offset_time)) ||
disable_uturn;
}
// Handle transitions - expand from the end node of each transition
if (nodeinfo->transition_count() > 0) {
const NodeTransition* trans = tile->transition(nodeinfo->transition_index());
auto& hierarchy_limits = hierarchy_limits_[FORWARD][index];
for (uint32_t i = 0; i < nodeinfo->transition_count(); ++i, ++trans) {
// if this is a downward transition (ups are always allowed) AND we are no longer allowed OR
// we cant get the tile at that level (local extracts could have this problem) THEN bail
graph_tile_ptr trans_tile = nullptr;
if ((!trans->up() && !ignore_hierarchy_limits_ &&
hierarchy_limits[trans->endnode().level()].StopExpanding()) ||
!(trans_tile = graphreader.GetGraphTile(trans->endnode()))) {
continue;
}
// setup for expansion at this level
hierarchy_limits[node.level()].up_transition_count += trans->up();
const auto* trans_node = trans_tile->node(trans->endnode());
EdgeMetadata trans_meta =
EdgeMetadata::make(trans->endnode(), trans_node, trans_tile, edgestatus);
uint32_t trans_shortcuts = 0;
// expand the edges from this node at this level
for (uint32_t i = 0; i < trans_node->edge_count(); ++i, ++trans_meta) {
disable_uturn = ExpandInner<expansion_direction>(graphreader, index, pred, opp_pred_edge,
trans_node, pred_idx, trans_meta,
trans_shortcuts, trans_tile, offset_time) ||
disable_uturn;
}
}
}
// Now, after having looked at all the edges, including edges on other levels,
// we can say if this is a deadend or not, and if so, evaluate the uturn-edge (if it exists)
if (!disable_uturn && uturn_meta) {
// If we found no suitable edge to add, it means we're at a deadend
// so lets go back and re-evaluate a potential u-turn
pred.set_deadend(true);
// TODO(nils): what if there is a shortcut that supersedes our u-turn? can that even be?
// We then need to decide if we should expand the shortcut or the non-shortcut edge...
// Expand the uturn possibility
disable_uturn =
ExpandInner<expansion_direction>(graphreader, index, pred, opp_pred_edge, nodeinfo, pred_idx,
uturn_meta, shortcuts, tile, offset_time);
}
return disable_uturn;
}
// Check if the edge on the forward search connects to a reached edge
// on the reverse search trees.
void CostMatrix::CheckForwardConnections(const uint32_t source,
const BDEdgeLabel& fwd_pred,
const uint32_t n,
GraphReader& graphreader) {
// Disallow connections that are part of an uturn on an internal edge
if (fwd_pred.internal_turn() != InternalTurn::kNoTurn) {
return;
}
// Disallow connections that are part of a complex restriction.
// TODO - validate that we do not need to "walk" the paths forward
// and backward to see if they match a restriction.
if (fwd_pred.on_complex_rest()) {
// TODO(nils): bidir a* is digging deeper
return;
}
// Get the opposing edge. Get a list of target locations whose reverse
// search has reached this edge.
GraphId rev_edgeid = fwd_pred.opp_edgeid();
auto targets = targets_->find(rev_edgeid);
if (targets == targets_->end()) {
return;
}
// Iterate through the targets
for (auto target : targets->second) {
uint32_t idx = source * locs_count_[MATRIX_REV] + target;
if (best_connection_[idx].found) {
continue;
}
// Update any targets whose threshold has been reached
if (best_connection_[idx].max_iterations > 0 && n > best_connection_[idx].max_iterations) {
best_connection_[idx].found = true;
continue;
}
// If we came down here, we know this opposing edge is either settled, or it's a
// target correlated edge which hasn't been pulled out of the queue yet, so a path
// has been found to the end node of this directed edge
const auto& rev_edgestate = edgestatus_[MATRIX_REV][target];
EdgeStatusInfo rev_edgestatus = rev_edgestate.Get(rev_edgeid);
const auto& rev_edgelabels = edgelabel_[MATRIX_REV][target];
uint32_t rev_predidx = rev_edgelabels[rev_edgestatus.index()].predecessor();
const BDEdgeLabel& rev_label = rev_edgelabels[rev_edgestatus.index()];
// Special case - common edge for source and target are both initial edges
if (fwd_pred.predecessor() == kInvalidLabel && rev_predidx == kInvalidLabel) {
// bail if either edge wasn't allowed (see notes in SetSources/Targets)
if (!fwd_pred.path_id() || !rev_label.path_id()) {
return;
}
// remember: transition_cost is abused in SetSources/Targets: cost is secs, secs is length
float s =
std::abs(fwd_pred.cost().secs + rev_label.cost().secs - rev_label.transition_cost().cost);
// Update best connection and set found = true.
// distance computation only works with the casts.
uint32_t d = std::abs(static_cast<int>(fwd_pred.path_distance()) +
static_cast<int>(rev_label.path_distance()) -
static_cast<int>(rev_label.transition_cost().secs));
best_connection_[idx].Update(fwd_pred.edgeid(), rev_edgeid, Cost(s, s), d);
best_connection_[idx].found = true;
// Update status and update threshold if this is the last location
// to find for this source or target
UpdateStatus(source, target);
} else {
float oppcost = (rev_predidx == kInvalidLabel) ? 0.f : rev_edgelabels[rev_predidx].cost().cost;
float c = fwd_pred.cost().cost + oppcost + rev_label.transition_cost().cost;
// Check if best connection
if (c < best_connection_[idx].cost.cost) {
float oppsec = (rev_predidx == kInvalidLabel) ? 0.f : rev_edgelabels[rev_predidx].cost().secs;
uint32_t oppdist =
(rev_predidx == kInvalidLabel) ? 0U : rev_edgelabels[rev_predidx].path_distance();
float s = fwd_pred.cost().secs + oppsec + rev_label.transition_cost().secs;
uint32_t d = fwd_pred.path_distance() + oppdist;
// Update best connection and set a threshold
best_connection_[idx].Update(fwd_pred.edgeid(), rev_edgeid, Cost(c, s), d);
if (best_connection_[idx].max_iterations == 0) {
best_connection_[idx].max_iterations =
n + GetThreshold(mode_, edgelabel_[MATRIX_FORW][source].size() +
edgelabel_[MATRIX_REV][target].size());
}
// Update status and update threshold if this is the last location
// to find for this source or target
UpdateStatus(source, target);
}
}
// setting this edge as connected
if (expansion_callback_) {
auto prev_pred = fwd_pred.predecessor() == kInvalidLabel
? GraphId{}
: edgelabel_[MATRIX_FORW][source][fwd_pred.predecessor()].edgeid();
expansion_callback_(graphreader, fwd_pred.edgeid(), prev_pred, "costmatrix", "c",
fwd_pred.cost().secs, fwd_pred.path_distance(), fwd_pred.cost().cost);
}
}
return;
}
void CostMatrix::CheckReverseConnections(const uint32_t target,
const BDEdgeLabel& rev_pred,
const uint32_t n,
GraphReader& graphreader) {
// Disallow connections that are part of an uturn on an internal edge
if (rev_pred.internal_turn() != InternalTurn::kNoTurn) {
return;
}
// Disallow connections that are part of a complex restriction.
// TODO - validate that we do not need to "walk" the paths forward
// and backward to see if they match a restriction.
if (rev_pred.on_complex_rest()) {
return;
}
// Get the opposing edge. Get a list of source locations whose forward
// search has reached this edge.
GraphId fwd_edgeid = rev_pred.opp_edgeid();
auto sources = sources_->find(fwd_edgeid);
if (sources == sources_->end()) {
return;
}
// Iterate through the sources
for (auto source : sources->second) {
uint32_t source_idx = source * locs_count_[MATRIX_REV] + target;
if (best_connection_[source_idx].found) {
continue;
}
// Update any targets whose threshold has been reached
if (best_connection_[source_idx].max_iterations > 0 &&
n > best_connection_[source_idx].max_iterations) {
best_connection_[source_idx].found = true;
continue;
}
// If this edge has been reached then a shortest path has been found
// to the end node of this directed edge.
EdgeStatusInfo fwd_edgestatus = edgestatus_[MATRIX_FORW][source].Get(fwd_edgeid);
if (fwd_edgestatus.set() != EdgeSet::kUnreachedOrReset) {
const auto& fwd_edgelabels = edgelabel_[MATRIX_FORW][source];
uint32_t fwd_predidx = fwd_edgelabels[fwd_edgestatus.index()].predecessor();
const BDEdgeLabel& fwd_label = fwd_edgelabels[fwd_edgestatus.index()];
// Special case - common edge for source and target are both initial edges
if (rev_pred.predecessor() == kInvalidLabel && fwd_predidx == kInvalidLabel) {
// bail if either edge wasn't allowed
if (!rev_pred.path_id() || !fwd_label.path_id()) {
return;
}
// remember: transition_cost is abused in SetSources/Targets: cost is secs, secs is length
float s =
std::abs(rev_pred.cost().secs + fwd_label.cost().secs - fwd_label.transition_cost().cost);
// Update best connection and set found = true.
// distance computation only works with the casts.
uint32_t d = std::abs(static_cast<int>(rev_pred.path_distance()) +
static_cast<int>(fwd_label.path_distance()) -
static_cast<int>(fwd_label.transition_cost().secs));
best_connection_[source_idx].Update(fwd_edgeid, rev_pred.edgeid(), Cost(s, s), d);
best_connection_[source_idx].found = true;
// Update status and update threshold if this is the last location
// to find for this source or target
UpdateStatus(source, target);
} else {
float oppcost = (fwd_predidx == kInvalidLabel) ? 0 : fwd_edgelabels[fwd_predidx].cost().cost;
float c = rev_pred.cost().cost + oppcost + fwd_label.transition_cost().cost;
// Check if best connection
if (c < best_connection_[source_idx].cost.cost) {
float fwd_sec =
(fwd_predidx == kInvalidLabel) ? 0 : fwd_edgelabels[fwd_predidx].cost().secs;
uint32_t fwd_dist =
(fwd_predidx == kInvalidLabel) ? 0 : fwd_edgelabels[fwd_predidx].path_distance();
float s = rev_pred.cost().secs + fwd_sec + fwd_label.transition_cost().secs;
uint32_t d = rev_pred.path_distance() + fwd_dist;
// Update best connection and set a threshold
best_connection_[source_idx].Update(fwd_edgeid, rev_pred.edgeid(), Cost(c, s), d);
if (best_connection_[source_idx].max_iterations == 0) {
best_connection_[source_idx].max_iterations =
n + GetThreshold(mode_, edgelabel_[MATRIX_FORW][source].size() +
edgelabel_[MATRIX_REV][target].size());
}
// Update status and update threshold if this is the last location
// to find for this source or target
UpdateStatus(source, target);
}
}
// setting this edge as connected
if (expansion_callback_) {
auto prev_pred = rev_pred.predecessor() == kInvalidLabel
? GraphId{}
: edgelabel_[MATRIX_REV][source][rev_pred.predecessor()].edgeid();
expansion_callback_(graphreader, rev_pred.edgeid(), prev_pred, "costmatrix", "c",
rev_pred.cost().secs, rev_pred.path_distance(), rev_pred.cost().cost);
}
}
}
return;
}
// Update status when a connection is found.
void CostMatrix::UpdateStatus(const uint32_t source, const uint32_t target) {
// Remove the target from the source status
auto& s = locs_status_[MATRIX_FORW][source].unfound_connections;
auto it = s.find(target);
if (it != s.end()) {
s.erase(it);
if (s.empty() && locs_status_[MATRIX_FORW][source].threshold > 0) {
// At least 1 connection has been found to each target for this source.
// Set a threshold to continue search for a limited number of times.
locs_status_[MATRIX_FORW][source].threshold =
GetThreshold(mode_, edgelabel_[MATRIX_FORW][source].size() +
edgelabel_[MATRIX_REV][target].size());
}
}
// Remove the source from the target status
auto& t = locs_status_[MATRIX_REV][target].unfound_connections;
it = t.find(source);
if (it != t.end()) {
t.erase(it);
if (t.empty() && locs_status_[MATRIX_REV][target].threshold > 0) {
// At least 1 connection has been found to each source for this target.
// Set a threshold to continue search for a limited number of times.
locs_status_[MATRIX_REV][target].threshold =
GetThreshold(mode_, edgelabel_[MATRIX_FORW][source].size() +
edgelabel_[MATRIX_REV][target].size());
}
}
}
// Sets the source/origin locations. Search expands forward from these
// locations.
void CostMatrix::SetSources(GraphReader& graphreader,
const google::protobuf::RepeatedPtrField<valhalla::Location>& sources,
const std::vector<baldr::TimeInfo>& time_infos) {
// Go through each source location
uint32_t index = 0;
Cost empty_cost;
for (const auto& origin : sources) {
// Only skip inbound edges if we have other options
bool has_other_edges = false;
std::for_each(origin.correlation().edges().begin(), origin.correlation().edges().end(),
[&has_other_edges](const valhalla::PathEdge& e) {
has_other_edges = has_other_edges || !e.end_node();
});
// Iterate through edges and add to adjacency list
for (const auto& edge : origin.correlation().edges()) {
// If origin is at a node - skip any inbound edge (dist = 1)
if (has_other_edges && edge.end_node()) {
continue;
}
// Disallow any user avoid edges if the avoid location is ahead of the origin along the edge
GraphId edgeid(edge.graph_id());