forked from valhalla/valhalla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.cc
550 lines (456 loc) · 19.6 KB
/
matrix.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
#include "test.h"
#include <string>
#include <vector>
#include "baldr/rapidjson_utils.h"
#include "loki/worker.h"
#include "midgard/logging.h"
#include "sif/dynamiccost.h"
#include "thor/costmatrix.h"
#include "thor/timedistancematrix.h"
#include "thor/worker.h"
#include "tyr/serializers.h"
using namespace valhalla;
using namespace valhalla::thor;
using namespace valhalla::sif;
using namespace valhalla::loki;
using namespace valhalla::baldr;
using namespace valhalla::midgard;
using namespace valhalla::tyr;
namespace {
// Quick costing class derived for testing so that any changes to regular costing
// won't change the outcome of the tests. Some of the logic for this class is just
// copy pasted from AutoCost as it stands when this test was written.
class SimpleCost final : public DynamicCost {
public:
/**
* Constructor.
* @param options Request options in a pbf
*/
SimpleCost(const Costing& options) : DynamicCost(options, sif::TravelMode::kDrive, kAutoAccess) {
}
~SimpleCost() {
}
bool Allowed(const DirectedEdge* edge,
const bool /*is_dest*/,
const EdgeLabel& pred,
const graph_tile_ptr& /*tile*/,
const GraphId& edgeid,
const uint64_t /*current_time*/,
const uint32_t /*tz_index*/,
uint8_t& /*restriction_idx*/) const override {
if (!IsAccessible(edge) || (!pred.deadend() && pred.opp_local_idx() == edge->localedgeidx()) ||
(pred.restrictions() & (1 << edge->localedgeidx())) ||
edge->surface() == Surface::kImpassable || IsUserAvoidEdge(edgeid) ||
(!allow_destination_only_ && !pred.destonly() && edge->destonly())) {
return false;
}
return true;
}
bool AllowedReverse(const DirectedEdge* edge,
const EdgeLabel& pred,
const DirectedEdge* opp_edge,
const graph_tile_ptr& /*tile*/,
const GraphId& opp_edgeid,
const uint64_t /*current_time*/,
const uint32_t /*tz_index*/,
uint8_t& /*restriction_idx*/) const override {
if (!IsAccessible(opp_edge) ||
(!pred.deadend() && pred.opp_local_idx() == edge->localedgeidx()) ||
(opp_edge->restrictions() & (1 << pred.opp_local_idx())) ||
opp_edge->surface() == Surface::kImpassable || IsUserAvoidEdge(opp_edgeid) ||
(!allow_destination_only_ && !pred.destonly() && opp_edge->destonly())) {
return false;
}
return true;
}
Cost EdgeCost(const baldr::DirectedEdge* /*edge*/,
const baldr::TransitDeparture* /*departure*/,
const uint32_t /*curr_time*/) const override {
throw std::runtime_error("We shouldnt be testing transit edges");
}
Cost EdgeCost(const DirectedEdge* edge,
const graph_tile_ptr& /*tile*/,
const baldr::TimeInfo& /*time_info*/,
uint8_t& /*flow_sources*/) const override {
float sec = static_cast<float>(edge->length());
return {sec / 10.0f, sec};
}
Cost TransitionCost(const DirectedEdge* /*edge*/,
const NodeInfo* /*node*/,
const EdgeLabel& /*pred*/) const override {
return {5.0f, 5.0f};
}
Cost TransitionCostReverse(const uint32_t /*idx*/,
const NodeInfo* /*node*/,
const DirectedEdge* /*opp_edge*/,
const DirectedEdge* /*opp_pred_edge*/,
const bool /*has_measured_speed*/,
const InternalTurn /*internal_turn*/) const override {
return {5.0f, 5.0f};
}
float AStarCostFactor() const override {
return 0.1f;
}
bool Allowed(const baldr::DirectedEdge* edge, const graph_tile_ptr&, uint16_t) const override {
auto access_mask = (ignore_access_ ? kAllAccess : access_mask_);
bool accessible = (edge->forwardaccess() & access_mask) ||
(ignore_oneways_ && (edge->reverseaccess() & access_mask));
if (edge->is_shortcut() || !accessible)
return 0.0f;
else {
return 1.0f;
}
}
};
cost_ptr_t CreateSimpleCost(const Costing& options) {
return std::make_shared<SimpleCost>(options);
}
// Maximum edge score - base this on costing type.
// Large values can cause very bad performance. Setting this back
// to 2 hours for bike and pedestrian and 12 hours for driving routes.
// TODO - re-evaluate edge scores and balance performance vs. quality.
// Perhaps tie the edge score logic in with the costing type - but
// may want to do this in loki. At this point in thor the costing method
// has not yet been constructed.
const std::unordered_map<std::string, float> kMaxDistances = {
{"auto", 43200.0f}, {"auto_shorter", 43200.0f}, {"bicycle", 7200.0f},
{"bus", 43200.0f}, {"motor_scooter", 14400.0f}, {"multimodal", 7200.0f},
{"pedestrian", 7200.0f}, {"transit", 14400.0f}, {"truck", 43200.0f},
{"taxi", 43200.0f},
};
// a scale factor to apply to the score so that we bias towards closer results more
const auto cfg = test::make_config("test/data/utrecht_tiles");
const auto test_request = R"({
"sources":[
{"lat":52.106337,"lon":5.101728},
{"lat":52.111276,"lon":5.089717},
{"lat":52.103105,"lon":5.081005},
{"lat":52.103948,"lon":5.06813}
],
"targets":[
{"lat":52.106126,"lon":5.101497},
{"lat":52.100469,"lon":5.087099},
{"lat":52.103105,"lon":5.081005},
{"lat":52.094273,"lon":5.075254}
],
"costing":"auto"
})";
const auto test_request_osrm = R"({
"sources":[
{"lat":52.106337,"lon":5.101728},
{"lat":52.111276,"lon":5.089717},
{"lat":52.103105,"lon":5.081005},
{"lat":52.103948,"lon":5.06813}
],
"targets":[
{"lat":52.106126,"lon":5.101497},
{"lat":52.100469,"lon":5.087099},
{"lat":52.103105,"lon":5.081005},
{"lat":52.094273,"lon":5.075254}
],
"costing":"auto",
"format": "osrm"
})";
// clang-format off
std::vector<std::vector<uint32_t>> matrix_answers = {{28, 28}, {2027, 1837}, {2403, 2213}, {4163, 3838},
{1519, 1398}, {1808, 1638}, {2061, 1951}, {3944, 3639},
{2311, 2111}, {701, 641}, {0, 0}, {2821, 2626},
{5562, 5177}, {3952, 3707}, {4367, 4107}, {1825, 1680}};
// clang-format on
void check_osrm_response(std::string& res, std::string& algo) {
rapidjson::Document res_doc;
res_doc.Parse(res);
ASSERT_FALSE(res_doc.HasParseError());
std::string status = "Ok";
EXPECT_EQ(res_doc["code"].GetString(), status) << "Didn't work for " + algo;
EXPECT_EQ(res_doc["algorithm"].GetString(), algo) << "Didn't work for " + algo;
EXPECT_NEAR(res_doc["distances"].GetArray()[0][0].GetDouble(), 28, 1) << "Didn't work for " + algo;
EXPECT_EQ(res_doc["durations"].GetArray()[0][0].GetInt64(), 28) << "Didn't work for " + algo;
EXPECT_NEAR(res_doc["distances"].GetArray()[3][3].GetDouble(), 1680, 1)
<< "Didn't work for " + algo;
EXPECT_EQ(res_doc["durations"].GetArray()[3][3].GetInt64(), 1825) << "Didn't work for " + algo;
}
} // namespace
const uint32_t kThreshold = 1;
bool within_tolerance(const uint32_t v1, const uint32_t v2) {
return (v1 > v2) ? v1 - v2 <= kThreshold : v2 - v1 <= kThreshold;
}
TEST(Matrix, test_matrix) {
loki_worker_t loki_worker(cfg);
Api request;
ParseApi(test_request, Options::sources_to_targets, request);
loki_worker.matrix(request);
thor_worker_t::adjust_scores(*request.mutable_options());
GraphReader reader(cfg.get_child("mjolnir"));
sif::mode_costing_t mode_costing;
mode_costing[0] =
CreateSimpleCost(request.options().costings().find(request.options().costing_type())->second);
CostMatrix cost_matrix;
cost_matrix.SourceToTarget(request, reader, mode_costing, sif::TravelMode::kDrive, 400000.0);
auto matrix = request.matrix();
for (int i = 0; i < matrix.times().size(); ++i) {
EXPECT_NEAR(matrix.distances()[i], matrix_answers[i][1], kThreshold)
<< "result " + std::to_string(i) + "'s distance is not close enough" +
" to expected value for CostMatrix";
EXPECT_NEAR(matrix.times()[i], matrix_answers[i][0], kThreshold)
<< "result " + std::to_string(i) + "'s time is not close enough" +
" to expected value for CostMatrix";
}
request.clear_matrix();
CostMatrix cost_matrix_abort_source;
cost_matrix_abort_source.SourceToTarget(request, reader, mode_costing, sif::TravelMode::kDrive,
7000.0);
matrix = request.matrix();
uint32_t found = 0;
for (int i = 0; i < matrix.times().size(); ++i) {
if (matrix.distances()[i] < kMaxCost) {
++found;
}
}
EXPECT_EQ(found, 15) << " not the number of results as expected";
request.clear_matrix();
CostMatrix cost_matrix_abort_target;
cost_matrix_abort_target.SourceToTarget(request, reader, mode_costing, sif::TravelMode::kDrive,
5000.0);
matrix = request.matrix();
found = 0;
for (int i = 0; i < matrix.times().size(); ++i) {
if (matrix.distances()[i] < kMaxCost) {
++found;
}
}
EXPECT_EQ(found, 13) << " not the number of results as expected";
request.clear_matrix();
TimeDistanceMatrix timedist_matrix;
timedist_matrix.SourceToTarget(request, reader, mode_costing, sif::TravelMode::kDrive, 400000.0);
matrix = request.matrix();
for (int i = 0; i < matrix.times().size(); ++i) {
EXPECT_NEAR(matrix.distances()[i], matrix_answers[i][1], kThreshold)
<< "result " + std::to_string(i) + "'s distance is not equal" +
" to expected value for TDMatrix";
EXPECT_NEAR(matrix.times()[i], matrix_answers[i][0], kThreshold)
<< "result " + std::to_string(i) + "'s time is not equal" + " to expected value for TDMatrix";
}
}
TEST(Matrix, test_timedistancematrix_forward) {
// Input request is the same as `test_request`, but without the last target
const auto test_request_more_sources = R"({
"sources":[
{"lat":52.106337,"lon":5.101728},
{"lat":52.111276,"lon":5.089717},
{"lat":52.103105,"lon":5.081005}
],
"targets":[
{"lat":52.106126,"lon":5.101497},
{"lat":52.100469,"lon":5.087099},
{"lat":52.103105,"lon":5.081005},
{"lat":52.094273,"lon":5.075254}
],
"costing":"auto"
})";
loki_worker_t loki_worker(cfg);
Api request;
ParseApi(test_request_more_sources, Options::sources_to_targets, request);
loki_worker.matrix(request);
thor_worker_t::adjust_scores(*request.mutable_options());
GraphReader reader(cfg.get_child("mjolnir"));
sif::mode_costing_t mode_costing;
mode_costing[0] =
CreateSimpleCost(request.options().costings().find(request.options().costing_type())->second);
TimeDistanceMatrix timedist_matrix;
timedist_matrix.SourceToTarget(request, reader, mode_costing, sif::TravelMode::kDrive, 400000.0);
auto& matrix = request.matrix();
// expected results are the same as `matrix_answers`, but without the last origin
// clang-format off
std::vector<std::vector<uint32_t>> expected_results = {{28, 28}, {2027, 1837}, {2403, 2213}, {4163, 3838},
{1519, 1398}, {1808, 1638}, {2061, 1951}, {3944, 3639},
{2311, 2111}, {701, 641}, {0, 0}, {2821, 2626}};
// clang-format on
for (int i = 0; i < matrix.times().size(); ++i) {
EXPECT_NEAR(matrix.distances()[i], expected_results[i][1], kThreshold)
<< "result " + std::to_string(i) + "'s distance is not equal" +
" to expected value for TDMatrix";
EXPECT_NEAR(matrix.times()[i], expected_results[i][0], kThreshold)
<< "result " + std::to_string(i) + "'s time is not equal" + " to expected value for TDMatrix";
}
}
TEST(Matrix, test_timedistancematrix_reverse) {
// Input request is the same as `test_request`, but without the last target
const auto test_request_more_sources = R"({
"sources":[
{"lat":52.106337,"lon":5.101728},
{"lat":52.111276,"lon":5.089717},
{"lat":52.103105,"lon":5.081005},
{"lat":52.103948,"lon":5.06813}
],
"targets":[
{"lat":52.106126,"lon":5.101497},
{"lat":52.100469,"lon":5.087099},
{"lat":52.103105,"lon":5.081005}
],
"costing":"auto"
})";
loki_worker_t loki_worker(cfg);
Api request;
ParseApi(test_request_more_sources, Options::sources_to_targets, request);
loki_worker.matrix(request);
thor_worker_t::adjust_scores(*request.mutable_options());
GraphReader reader(cfg.get_child("mjolnir"));
sif::mode_costing_t mode_costing;
mode_costing[0] =
CreateSimpleCost(request.options().costings().find(request.options().costing_type())->second);
TimeDistanceMatrix timedist_matrix;
timedist_matrix.SourceToTarget(request, reader, mode_costing, sif::TravelMode::kDrive, 400000.0);
auto& matrix = request.matrix();
// expected results are the same as `matrix_answers`, but without the last target
// clang-format off
std::vector<std::vector<uint32_t>> expected_results = {{28, 28}, {2027, 1837}, {2403, 2213},
{1519, 1398}, {1808, 1638}, {2061, 1951},
{2311, 2111}, {701, 641}, {0, 0},
{5562, 5177}, {3952, 3707}, {4367, 4107}};
// clang-format on
for (int i = 0; i < matrix.times().size(); ++i) {
EXPECT_NEAR(matrix.distances()[i], expected_results[i][1], kThreshold)
<< "result " + std::to_string(i) + "'s distance is not equal" +
" to expected value for TDMatrix";
EXPECT_NEAR(matrix.times()[i], expected_results[i][0], kThreshold)
<< "result " + std::to_string(i) + "'s time is not equal" + " to expected value for TDMatrix";
}
}
TEST(Matrix, test_matrix_osrm) {
loki_worker_t loki_worker(cfg);
Api request;
ParseApi(test_request_osrm, Options::sources_to_targets, request);
loki_worker.matrix(request);
thor_worker_t::adjust_scores(*request.mutable_options());
GraphReader reader(cfg.get_child("mjolnir"));
sif::mode_costing_t mode_costing;
mode_costing[0] =
CreateSimpleCost(request.options().costings().find(request.options().costing_type())->second);
CostMatrix cost_matrix;
cost_matrix.SourceToTarget(request, reader, mode_costing, sif::TravelMode::kDrive, 400000.0);
auto json_res = tyr::serializeMatrix(request);
std::string algo = "costmatrix";
check_osrm_response(json_res, algo);
TimeDistanceMatrix timedist_matrix;
timedist_matrix.SourceToTarget(request, reader, mode_costing, sif::TravelMode::kDrive, 400000.0);
json_res = tyr::serializeMatrix(request);
algo = "timedistancematrix";
check_osrm_response(json_res, algo);
}
const auto test_request_partial = R"({
"sources":[
{"lat":52.103948,"lon":5.06813}
],
"targets":[
{"lat":52.106126,"lon":5.101497},
{"lat":52.100469,"lon":5.087099},
{"lat":52.103105,"lon":5.081005},
{"lat":52.094273,"lon":5.075254}
],
"costing":"auto",
"matrix_locations":2
})";
TEST(Matrix, partial_matrix) {
loki_worker_t loki_worker(cfg);
Api request;
ParseApi(test_request_partial, Options::sources_to_targets, request);
loki_worker.matrix(request);
thor_worker_t::adjust_scores(*request.mutable_options());
GraphReader reader(cfg.get_child("mjolnir"));
sif::mode_costing_t mode_costing;
mode_costing[0] =
CreateSimpleCost(request.options().costings().find(request.options().costing_type())->second);
TimeDistanceMatrix timedist_matrix;
timedist_matrix.SourceToTarget(request, reader, mode_costing, sif::TravelMode::kDrive, 400000.0);
auto& matrix = request.matrix();
uint32_t found = 0;
for (int i = 0; i < matrix.times().size(); ++i) {
if (matrix.distances()[i] > 0) {
++found;
}
}
EXPECT_EQ(found, 2) << " partial result did not find 2 results as expected";
}
// slim dowm matrix response: https://github.com/valhalla/valhalla/pull/3987
const auto test_matrix_default = R"({
"sources":[
{"lat":52.103948,"lon":5.06813}
],
"targets":[
{"lat":52.106126,"lon":5.101497},
{"lat":52.100469,"lon":5.087099},
{"lat":52.103105,"lon":5.081005},
{"lat":52.094273,"lon":5.075254}
],
"costing":"auto"
})";
TEST(Matrix, default_matrix) {
tyr::actor_t actor(cfg, true);
auto response = actor.matrix(test_matrix_default);
rapidjson::Document json;
json.Parse(response);
ASSERT_FALSE(json.HasParseError());
EXPECT_TRUE(json.HasMember("sources_to_targets"));
// contains 4 keys i.e, "distance", "time", "to_index" and "from_index"
EXPECT_EQ(json["sources_to_targets"].GetArray()[0][0].MemberCount(), 4);
EXPECT_TRUE(json["sources_to_targets"].GetArray()[0][0].HasMember("distance"));
EXPECT_TRUE(json["sources_to_targets"].GetArray()[0][0].HasMember("time"));
EXPECT_TRUE(json["sources_to_targets"].GetArray()[0][0].HasMember("to_index"));
EXPECT_TRUE(json["sources_to_targets"].GetArray()[0][0].HasMember("from_index"));
EXPECT_TRUE(json["sources_to_targets"].GetArray()[0][0].IsObject());
// all objects must have equal sizes
ASSERT_EQ(json["sources_to_targets"].GetArray()[0][0].MemberCapacity(),
json["sources_to_targets"].GetArray()[0][1].MemberCapacity());
// first values in the object
EXPECT_DOUBLE_EQ(json["sources_to_targets"].GetArray()[0][0].GetObject()["distance"].GetDouble(),
5.88);
EXPECT_EQ(json["sources_to_targets"].GetArray()[0][0].GetObject()["time"].GetInt64(), 473);
EXPECT_EQ(json["sources_to_targets"].GetArray()[0][0].GetObject()["to_index"].GetInt64(), 0);
EXPECT_EQ(json["sources_to_targets"].GetArray()[0][0].GetObject()["from_index"].GetInt64(), 0);
EXPECT_TRUE(json.HasMember("sources"));
EXPECT_TRUE(json.HasMember("targets"));
EXPECT_TRUE(json.HasMember("units"));
}
const auto test_matrix_verbose_false = R"({
"sources":[
{"lat":52.103948,"lon":5.06813},
{"lat":52.111276,"lon":5.089717}
],
"targets":[
{"lat":52.106126,"lon":5.101497},
{"lat":52.100469,"lon":5.087099},
{"lat":52.103105,"lon":5.081005}
],
"costing":"auto",
"verbose":false
})";
TEST(Matrix, slim_matrix) {
tyr::actor_t actor(cfg, true);
auto response = actor.matrix(test_matrix_verbose_false);
rapidjson::Document json;
json.Parse(response);
ASSERT_FALSE(json.HasParseError());
EXPECT_TRUE(json.HasMember("sources_to_targets"));
// contains two array i.e, "durations" and "distances"
EXPECT_EQ(json["sources_to_targets"].GetObject().MemberCount(), 2);
EXPECT_TRUE(json["sources_to_targets"].GetObject().HasMember("durations"));
EXPECT_TRUE(json["sources_to_targets"].GetObject().HasMember("distances"));
EXPECT_TRUE(json["sources_to_targets"].GetObject()["durations"].IsArray());
EXPECT_TRUE(json["sources_to_targets"].GetObject()["distances"].IsArray());
// "durations" and "distances" are array of equal sizes
ASSERT_EQ(json["sources_to_targets"].GetObject()["durations"][0].Size(),
json["sources_to_targets"].GetObject()["distances"][0].Size());
// first value of "distances" array
EXPECT_DOUBLE_EQ(json["sources_to_targets"].GetObject()["distances"][0][0].GetDouble(), 5.88);
// first value of "durations" array
EXPECT_EQ(json["sources_to_targets"].GetObject()["durations"][0][0].GetInt64(), 473);
EXPECT_FALSE(json.HasMember("sources"));
EXPECT_FALSE(json.HasMember("targets"));
EXPECT_TRUE(json.HasMember("units"));
}
int main(int argc, char* argv[]) {
logging::Configure({{"type", ""}}); // silence logs
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}