Skip to content

Commit 9fd8ffa

Browse files
authored
Merge pull request #143 from UrbanAnalyst/csa
properly fix #140 in the C++ csa algorithm
2 parents 104b449 + de88d86 commit 9fd8ffa

File tree

4 files changed

+15
-32
lines changed

4 files changed

+15
-32
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: gtfsrouter
22
Title: Routing with 'GTFS' (General Transit Feed Specification) Data
3-
Version: 0.1.4.008
3+
Version: 0.1.4.009
44
Authors@R: c(
55
person("Mark", "Padgham", , "mark.padgham@email.com", role = c("aut", "cre")),
66
person("Marcin", "Stepniak", , "marcinstepniak@ucm.es", role = "aut",

R/router.R

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -247,32 +247,6 @@ gtfs_csa <- function (gtfs, start_stns, end_stns, start_time,
247247
if (nrow (route) == 0) {
248248
return (NULL)
249249
}
250-
# Algorithm can generate transfers which transfer back again, because
251-
# transfers are incremented at arrival stations, so even on same trip_id,
252-
# can still have incremented value as algorithm progresses. These need to
253-
# be detected here, and 'max_transfers' reduced to remove intermediate
254-
# transfers. This is a very unsatisfactory solution to
255-
# https://github.com/UrbanAnalyst/gtfsrouter/issues/140;
256-
# full solution will come when #71 is one day implemented.
257-
trip_nums <- unique (route$trip_number)
258-
back_transfers <- vapply (trip_nums, function (n) {
259-
length (unique (diff (which (route$trip_number == n))))
260-
}, integer (1L))
261-
max_transfers <- max (back_transfers)
262-
while (any (back_transfers > 1L)) {
263-
max_transfers <- max_transfers - 1L
264-
route <- rcpp_csa (
265-
gtfs$timetable, gtfs$transfers,
266-
nrow (gtfs$stop_ids), nrow (gtfs$trip_ids),
267-
start_stns, end_stns, start_time, max_transfers
268-
)
269-
270-
trip_nums <- unique (route$trip_number)
271-
# Check for transfers back to same trips:
272-
back_transfers <- vapply (trip_nums, function (n) {
273-
length (unique (diff (which (route$trip_number == n))))
274-
}, integer (1L))
275-
}
276250

277251
route$trip_id <- gtfs$trip_ids [, trip_ids] [route$trip_number]
278252

codemeta.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"codeRepository": "https://github.com/UrbanAnalyst/gtfsrouter",
88
"issueTracker": "https://github.com/UrbanAnalyst/gtfsrouter/issues",
99
"license": "https://spdx.org/licenses/GPL-3.0",
10-
"version": "0.1.4.008",
10+
"version": "0.1.4.009",
1111
"programmingLanguage": {
1212
"@type": "ComputerLanguage",
1313
"name": "R",

src/csa.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,19 +250,28 @@ CSA_Return csa::main_csa_loop (
250250
{
251251
size_t trans_dest = t.first;
252252
int ttime = csa_in.arrival_time [i] + t.second;
253-
if (ttime <= csa_out.earliest_connection [trans_dest] &&
254-
csa_out.n_transfers [trans_dest] <= csa_pars.max_transfers)
253+
int new_n_transfers = csa_out.n_transfers [csa_in.arrival_station [i]] + 1;
254+
255+
const bool time_is_better = ttime < csa_out.earliest_connection [trans_dest];
256+
const bool time_is_equal = ttime == csa_out.earliest_connection [trans_dest];
257+
const bool fewer_transfers = new_n_transfers < csa_out.n_transfers [trans_dest];
258+
const bool same_trip =
259+
csa_out.current_trip [csa_in.arrival_station [i]] == csa_in.trip_id [i];
260+
261+
if ((time_is_better || (time_is_equal && fewer_transfers)) &&
262+
new_n_transfers <= csa_pars.max_transfers &&
263+
!(same_trip && new_n_transfers > csa_out.n_transfers [trans_dest]))
255264
{
256265
DEBUGMSG_CSA(" main loop: incrementing transfer destination " <<
257266
trans_dest << " to " <<
258-
csa_out.n_transfers [trans_dest] + 1 << " transfers.",
267+
new_n_transfers << " transfers.",
259268
csa_in.departure_station [i]);
260269

261270
// modified version of fill_one_csa_out:
262271
csa_out.earliest_connection [trans_dest] = ttime;
263272
csa_out.prev_stn [trans_dest] = csa_in.arrival_station [i];
264273
csa_out.prev_time [trans_dest] = csa_in.arrival_time [i];
265-
csa_out.n_transfers [trans_dest]++;
274+
csa_out.n_transfers [trans_dest] = new_n_transfers;
266275

267276
csa::check_end_stations (end_stations_set,
268277
trans_dest, ttime, csa_ret);

0 commit comments

Comments
 (0)