-
Notifications
You must be signed in to change notification settings - Fork 52
/
intermodal.cc
556 lines (482 loc) · 19.7 KB
/
intermodal.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
#include "motis/intermodal/intermodal.h"
#include <algorithm>
#include <functional>
#include <mutex>
#include <optional>
#include "utl/erase_if.h"
#include "utl/to_vec.h"
#include "utl/verify.h"
#include "motis/core/common/constants.h"
#include "motis/core/common/timing.h"
#include "motis/core/access/time_access.h"
#include "motis/core/journey/journeys_to_message.h"
#include "motis/core/journey/message_to_journeys.h"
#include "motis/module/context/motis_call.h"
#include "motis/module/context/motis_spawn.h"
#include "motis/intermodal/direct_connections.h"
#include "motis/intermodal/error.h"
#include "motis/intermodal/eval/commands.h"
#include "motis/intermodal/mumo_edge.h"
#include "motis/intermodal/query_bounds.h"
#include "motis/intermodal/statistics.h"
#include "motis/protocol/Message_generated.h"
using namespace flatbuffers;
using namespace motis::module;
using namespace motis::lookup;
using namespace motis::osrm;
using namespace motis::routing;
using namespace motis::revise;
namespace motis::intermodal {
intermodal::intermodal() : module("Intermodal Options", "intermodal") {
param(router_, "router", "routing module");
param(revise_, "revise", "revise connections");
param(timeout_, "timeout", "routing timeout in seconds (0 = no timeout)");
}
intermodal::~intermodal() = default;
void intermodal::reg_subc(motis::module::subc_reg& r) {
r.register_cmd("generate", "generate routing queries", eval::generate);
r.register_cmd("compare", "print difference between results", eval::compare);
}
void intermodal::init(motis::module::registry& r) {
r.register_op("/intermodal", [this](msg_ptr const& m) { return route(m); },
{});
if (router_.empty()) {
router_ = "/routing";
} else if (router_[0] != '/') {
router_ = "/" + router_;
}
r.subscribe("/init", [this]() { ppr_profiles_.update(); }, {});
}
std::vector<Offset<Connection>> revise_connections(
std::vector<journey> const& journeys, statistics& stats,
message_creator& mc) {
MOTIS_START_TIMING(revise_timing);
message_creator rmc;
rmc.create_and_finish(
MsgContent_ReviseRequest,
CreateReviseRequest(
rmc, rmc.CreateVector(utl::to_vec(
journeys,
[&](journey const& j) { return to_connection(rmc, j); })))
.Union(),
"/revise");
auto const res = motis_call(make_msg(rmc))->val();
auto result = utl::to_vec(*motis_content(ReviseResponse, res)->connections(),
[&mc](Connection const* con) {
return motis_copy_table(Connection, mc, con);
});
MOTIS_STOP_TIMING(revise_timing);
stats.revise_duration_ =
static_cast<uint64_t>(MOTIS_TIMING_MS(revise_timing));
return result;
}
struct parking_patch {
parking_patch(mumo_edge const* e, unsigned transport_from,
unsigned transport_to)
: e_(e), from_(transport_from), to_(transport_to) {}
mumo_edge const* e_{};
unsigned from_{};
unsigned to_{};
};
struct split_transport_result {
journey::stop& parking_stop_;
journey::transport& first_transport_;
journey::transport& second_transport_;
};
split_transport_result split_transport(journey& j,
std::vector<parking_patch>& patches,
journey::transport& transport) {
utl::verify(transport.to_ == transport.from_ + 1, "invalid transport");
auto const transport_idx = std::distance(
begin(j.transports_),
std::find_if(
begin(j.transports_), end(j.transports_), [&](auto const& t) {
return t.from_ == transport.from_ && t.to_ == transport.to_;
}));
auto const new_stop_idx = transport.to_;
auto& new_stop = *j.stops_.emplace(j.stops_.begin() + new_stop_idx);
auto const update_idx = [&](unsigned& i) {
if (i >= new_stop_idx) {
++i;
}
};
auto const update_indices = [&](auto& v) {
for (auto& e : v) {
update_idx(e.from_);
update_idx(e.to_);
}
};
update_indices(j.transports_);
update_indices(j.trips_);
update_indices(j.attributes_);
update_indices(j.free_texts_);
update_indices(j.problems_);
update_indices(patches);
j.transports_.insert(begin(j.transports_) + transport_idx,
j.transports_[transport_idx]);
auto& t1 = j.transports_[transport_idx];
auto& t2 = j.transports_[transport_idx + 1];
t1.to_--;
t2.from_++;
return {new_stop, t1, t2};
}
std::string get_parking_station(int index) {
switch (index) {
case 0: return STATION_VIA0;
case 1: return STATION_VIA1;
case 2: return STATION_VIA2;
case 3: return STATION_VIA3;
default: throw std::system_error(error::parking_edge_error);
}
}
journey::transport& get_transport(journey& j, unsigned const from,
unsigned const to) {
for (auto& t : j.transports_) {
if (t.from_ == from && t.to_ == to) {
return t;
}
}
throw std::system_error(error::parking_edge_error);
}
bool is_virtual_station(journey::stop const& s) {
return s.name_ == STATION_START || s.name_ == STATION_END;
}
void apply_parking_patches(journey& j, std::vector<parking_patch>& patches) {
auto parking_idx = 0;
for (auto& p : patches) {
auto t = get_transport(j, p.from_, p.to_);
auto const car_first = is_virtual_station(j.stops_[p.from_]);
auto const first_edge_duration =
(car_first ? p.e_->car_parking_->car_duration_
: p.e_->car_parking_->foot_duration_) *
60;
auto str = split_transport(j, patches, t);
auto const parking_station = get_parking_station(parking_idx++);
str.parking_stop_.eva_no_ = parking_station;
str.parking_stop_.name_ = parking_station;
str.parking_stop_.lat_ = p.e_->car_parking_->parking_pos_.lat_;
str.parking_stop_.lng_ = p.e_->car_parking_->parking_pos_.lng_;
str.parking_stop_.arrival_.valid_ = true;
str.parking_stop_.arrival_.timestamp_ =
j.stops_[p.from_].departure_.timestamp_ + first_edge_duration;
str.parking_stop_.arrival_.schedule_timestamp_ =
j.stops_[p.from_].departure_.schedule_timestamp_ + first_edge_duration;
str.parking_stop_.arrival_.timestamp_reason_ =
j.stops_[p.from_].departure_.timestamp_reason_;
str.parking_stop_.departure_ = str.parking_stop_.arrival_;
auto& car_transport =
car_first ? str.first_transport_ : str.second_transport_;
auto& foot_transport =
car_first ? str.second_transport_ : str.first_transport_;
car_transport.mumo_type_ = to_string(mumo_type::CAR);
foot_transport.mumo_type_ = to_string(mumo_type::FOOT);
}
}
void apply_gbfs_patches(journey& j, std::vector<parking_patch>& patches) {
for (auto const& p : patches) {
// station bike:
// replace: X --walk[type:gbfs]--> P
// to: X --walk--> (SX) --bike--> (SP) --walk--> P
// replace: P -->walk[type:gbfs]--> X
// to: P --walk--> (SP) --bike--> (SX) --walk--> X
if (std::holds_alternative<gbfs_edge::station_bike>(p.e_->gbfs_->bike_)) {
auto const& s = std::get<gbfs_edge::station_bike>(p.e_->gbfs_->bike_);
auto& t = get_transport(j, p.from_, p.to_);
auto str1 = split_transport(j, patches, t);
split_transport(j, patches, str1.second_transport_);
auto& s1 = j.stops_.at(p.from_ + 1);
s1.eva_no_ = s.from_station_id_;
s1.name_ = s.from_station_name_;
s1.lat_ = s.from_station_pos_.lat_;
s1.lng_ = s.from_station_pos_.lng_;
s1.arrival_.valid_ = true;
s1.arrival_.timestamp_ =
j.stops_[p.from_].departure_.timestamp_ + s.first_walk_duration_ * 60;
s1.arrival_.schedule_timestamp_ = s1.arrival_.timestamp_;
s1.arrival_.timestamp_reason_ =
j.stops_[p.from_].departure_.timestamp_reason_;
s1.departure_ = s1.arrival_;
auto& s2 = j.stops_.at(p.from_ + 2);
s2.eva_no_ = s.to_station_id_;
s2.name_ = s.to_station_name_;
s2.lat_ = s.to_station_pos_.lat_;
s2.lng_ = s.to_station_pos_.lng_;
s2.arrival_.valid_ = true;
s2.arrival_.timestamp_ = s1.departure_.timestamp_ + s.bike_duration_ * 60;
s2.arrival_.schedule_timestamp_ = s2.arrival_.timestamp_;
s2.arrival_.timestamp_reason_ =
j.stops_[p.from_ + 1].departure_.timestamp_reason_;
s2.departure_ = s2.arrival_;
get_transport(j, p.from_, p.from_ + 1).mumo_type_ =
to_string(mumo_type::FOOT);
get_transport(j, p.from_ + 1, p.from_ + 2).mumo_type_ =
p.e_->gbfs_->vehicle_type_;
get_transport(j, p.from_ + 2, p.from_ + 3).mumo_type_ =
to_string(mumo_type::FOOT);
}
// free bike:
// replace: X --walk[type:gbfs]--> P
// to: X --walk--> (B) --bike--> P
// replace: P -->walk[type:gbfs]--> X
// to: P --walk--> (B) --bike--> X
else if (std::holds_alternative<gbfs_edge::free_bike>(p.e_->gbfs_->bike_)) {
auto const& b = std::get<gbfs_edge::free_bike>(p.e_->gbfs_->bike_);
auto& t = get_transport(j, p.from_, p.to_);
auto str = split_transport(j, patches, t);
str.parking_stop_.eva_no_ = b.id_;
str.parking_stop_.name_ = b.id_;
str.parking_stop_.lat_ = b.pos_.lat_;
str.parking_stop_.lng_ = b.pos_.lng_;
str.parking_stop_.arrival_.valid_ = true;
str.parking_stop_.arrival_.timestamp_ =
j.stops_[p.from_].departure_.timestamp_ + b.walk_duration_ * 60;
str.parking_stop_.arrival_.schedule_timestamp_ =
str.parking_stop_.arrival_.timestamp_;
str.parking_stop_.arrival_.timestamp_reason_ =
j.stops_[p.from_].departure_.timestamp_reason_;
str.parking_stop_.departure_ = str.parking_stop_.arrival_;
str.first_transport_.mumo_type_ = to_string(mumo_type::FOOT);
str.second_transport_.mumo_type_ = to_string(mumo_type::BIKE);
}
}
}
msg_ptr postprocess_response(msg_ptr const& response_msg,
query_start const& q_start,
query_dest const& q_dest,
IntermodalRoutingRequest const* req,
std::vector<mumo_edge const*> const& edge_mapping,
statistics& stats, bool const revise,
std::vector<stats_category> const& mumo_stats,
ppr_profiles const& profiles) {
auto const dir = req->search_dir();
auto routing_response =
response_msg ? motis_content(RoutingResponse, response_msg) : nullptr;
auto journeys = routing_response == nullptr
? std::vector<journey>{}
: message_to_journeys(routing_response);
MOTIS_START_TIMING(direct_connection_timing);
auto const direct =
get_direct_connections(q_start, q_dest, req, profiles, edge_mapping);
stats.dominated_by_direct_connection_ =
remove_dominated_journeys(journeys, direct);
add_direct_connections(journeys, direct, q_start, q_dest, req);
MOTIS_STOP_TIMING(direct_connection_timing);
stats.direct_connection_duration_ =
static_cast<uint64_t>(MOTIS_TIMING_MS(direct_connection_timing));
message_creator mc;
for (auto& journey : journeys) {
auto& stops = journey.stops_;
if (stops.size() < 2) {
continue;
}
if (q_start.is_intermodal_) {
auto& start = (dir == SearchDir_Forward) ? stops.front() : stops.back();
start.lat_ = q_start.pos_.lat_;
start.lng_ = q_start.pos_.lng_;
}
if (q_dest.is_intermodal_) {
auto& dest = (dir == SearchDir_Forward) ? stops.back() : stops.front();
dest.lat_ = q_dest.pos_.lat_;
dest.lng_ = q_dest.pos_.lng_;
}
auto gbfs_patches = std::vector<parking_patch>{};
auto parking_patches = std::vector<parking_patch>{};
for (auto& t : journey.transports_) {
if (!t.is_walk_ || t.mumo_id_ < 0) {
continue;
}
auto const e = edge_mapping.at(static_cast<std::size_t>(t.mumo_id_));
t.mumo_type_ = to_string(e->type_);
t.mumo_id_ = e->id_;
if (e->type_ == mumo_type::CAR_PARKING && e->car_parking_) {
if (!e->car_parking_->uses_car_) {
t.mumo_type_ = to_string(mumo_type::FOOT);
continue;
}
parking_patches.emplace_back(e, t.from_, t.to_);
} else if (e->type_ == mumo_type::GBFS) {
gbfs_patches.emplace_back(e, t.from_, t.to_);
}
}
apply_parking_patches(journey, parking_patches);
apply_gbfs_patches(journey, gbfs_patches);
}
utl::erase_if(journeys, [](journey const& j) { return j.stops_.empty(); });
std::sort(
begin(journeys), end(journeys), [](journey const& a, journey const& b) {
return std::make_pair(a.stops_.front().departure_.schedule_timestamp_,
a.stops_.back().arrival_.schedule_timestamp_) <
std::make_pair(b.stops_.front().departure_.schedule_timestamp_,
b.stops_.back().arrival_.schedule_timestamp_);
});
auto const connections = revise
? revise_connections(journeys, stats, mc)
: utl::to_vec(journeys, [&mc](journey const& j) {
return to_connection(mc, j);
});
auto all_stats =
routing_response == nullptr
? std::vector<Offset<Statistics>>{}
: utl::to_vec(*routing_response->statistics(),
[&](Statistics const* stats) {
return motis_copy_table(Statistics, mc, stats);
});
for (auto const& s : mumo_stats) {
all_stats.emplace_back(to_fbs(mc, s));
}
all_stats.emplace_back(to_fbs(mc, to_stats_category("intermodal", stats)));
auto interval_begin = uint64_t{};
auto interval_end = uint64_t{};
if (routing_response != nullptr) {
interval_begin = routing_response->interval_begin();
interval_end = routing_response->interval_end();
} else {
switch (req->start_type()) {
case IntermodalStart_PretripStart:
interval_begin =
reinterpret_cast<IntermodalPretripStart const*>(req->start())
->interval()
->begin();
interval_end =
reinterpret_cast<IntermodalPretripStart const*>(req->start())
->interval()
->begin();
break;
case IntermodalStart_IntermodalPretripStart:
interval_begin = reinterpret_cast<PretripStart const*>(req->start())
->interval()
->begin();
interval_end = reinterpret_cast<PretripStart const*>(req->start())
->interval()
->begin();
break;
case IntermodalStart_IntermodalOntripStart:
interval_begin = interval_end =
reinterpret_cast<IntermodalOntripStart const*>(req->start())
->departure_time();
break;
case IntermodalStart_OntripStationStart:
interval_begin = interval_end =
reinterpret_cast<OntripStationStart const*>(req->start())
->departure_time();
break;
case IntermodalStart_OntripTrainStart:
interval_begin = interval_end =
reinterpret_cast<OntripTrainStart const*>(req->start())
->arrival_time();
break;
case IntermodalStart_NONE: break;
}
}
mc.create_and_finish(
MsgContent_RoutingResponse,
CreateRoutingResponse(
mc, mc.CreateVectorOfSortedTables(&all_stats),
mc.CreateVector(connections), interval_begin, interval_end,
mc.CreateVector(utl::to_vec(
direct,
[&mc](direct_connection const& c) { return to_fbs(mc, c); })))
.Union());
return make_msg(mc);
}
msg_ptr intermodal::route(msg_ptr const& msg) {
auto const req = motis_content(IntermodalRoutingRequest, msg);
message_creator mc;
statistics stats{};
auto const start = parse_query_start(mc, req);
auto const dest = parse_query_dest(mc, req);
stats.linear_distance_ =
static_cast<uint64_t>(distance(start.pos_, dest.pos_));
auto appender = [](auto& vec, auto const& from, auto const& to,
auto const& from_pos, auto const& to_pos, auto const dur,
auto const accessibility, mumo_type const type,
int const id) -> mumo_edge& {
return vec.emplace_back(from, to, from_pos, to_pos, dur, accessibility,
type, id);
};
MOTIS_START_TIMING(mumo_edge_timing);
std::vector<mumo_edge> deps;
std::vector<mumo_edge> arrs;
std::vector<stats_category> mumo_stats;
std::mutex mumo_stats_mutex;
auto const mumo_stats_appender = [&](stats_category&& s) {
std::lock_guard const guard(mumo_stats_mutex);
mumo_stats.emplace_back(s);
};
std::vector<ctx::future_ptr<ctx_data, void>> futures;
using namespace std::placeholders;
if (req->search_dir() == SearchDir_Forward) {
if (start.is_intermodal_) {
futures.emplace_back(spawn_job_void([&]() {
make_starts(
req, start.pos_, dest.pos_,
std::bind(appender, std::ref(deps), // NOLINT
STATION_START, _1, start.pos_, _2, _3, _4, _5, _6),
mumo_stats_appender, ppr_profiles_);
}));
}
if (dest.is_intermodal_) {
futures.emplace_back(spawn_job_void([&]() {
make_dests(req, dest.pos_, start.pos_,
std::bind(appender, std::ref(arrs), // NOLINT
_1, STATION_END, _2, dest.pos_, _3, _4, _5, _6),
mumo_stats_appender, ppr_profiles_);
}));
}
} else {
if (start.is_intermodal_) {
futures.emplace_back(spawn_job_void([&]() {
make_starts(
req, start.pos_, dest.pos_,
std::bind(appender, std::ref(deps), // NOLINT
_1, STATION_START, _2, start.pos_, _3, _4, _5, _6),
mumo_stats_appender, ppr_profiles_);
}));
}
if (dest.is_intermodal_) {
futures.emplace_back(spawn_job_void([&]() {
make_dests(req, dest.pos_, start.pos_,
std::bind(appender, std::ref(arrs), // NOLINT
STATION_END, _1, dest.pos_, _2, _3, _4, _5, _6),
mumo_stats_appender, ppr_profiles_);
}));
}
}
ctx::await_all(futures);
MOTIS_STOP_TIMING(mumo_edge_timing);
stats.start_edges_ = deps.size();
stats.destination_edges_ = arrs.size();
stats.mumo_edge_duration_ =
static_cast<uint64_t>(MOTIS_TIMING_MS(mumo_edge_timing));
std::vector<mumo_edge const*> edge_mapping;
auto edges = write_edges(mc, deps, arrs, edge_mapping);
auto routing_resp = msg_ptr{};
if ((!start.is_intermodal_ || !deps.empty()) &&
(!dest.is_intermodal_ || !arrs.empty())) {
auto const router =
(req->router() == nullptr || req->router()->Length() == 0U)
? ((req->search_type() == SearchType_Default ||
req->search_type() == SearchType_Accessibility) &&
start.start_type_ != Start_OntripTrainStart)
? router_
: "/routing"
: req->router()->str();
mc.create_and_finish(
MsgContent_RoutingRequest,
CreateRoutingRequest(mc, start.start_type_, start.start_, dest.station_,
req->search_type(), req->search_dir(),
mc.CreateVector(std::vector<Offset<Via>>{}),
mc.CreateVector(edges), true, true, true, 0,
timeout_)
.Union(),
router);
MOTIS_START_TIMING(routing_timing);
routing_resp = motis_call(make_msg(mc))->val();
MOTIS_STOP_TIMING(routing_timing);
stats.routing_duration_ =
static_cast<uint64_t>(MOTIS_TIMING_MS(routing_timing));
}
return postprocess_response(routing_resp, start, dest, req, edge_mapping,
stats, revise_, mumo_stats, ppr_profiles_);
}
} // namespace motis::intermodal