Skip to content

Commit

Permalink
Raptor: fix reverse isochrone
Browse files Browse the repository at this point in the history
add tests and then fix the discovered bugs :)
  • Loading branch information
antoine-de committed Jul 6, 2015
1 parent a2c1741 commit f72eb3d
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 32 deletions.
20 changes: 8 additions & 12 deletions source/disruption/tests/disruption_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@ using navitia::type::new_disruption::PtObj;
using navitia::type::new_disruption::Disruption;
using navitia::type::new_disruption::Severity;

inline u_int64_t operator"" _dt_time_stamp(const char* str, size_t s) {
return navitia::to_posix_timestamp(boost::posix_time::from_iso_string(std::string(str, s)));
}

struct DisruptionCreator {
std::string uri;
std::string object_uri;
Expand Down Expand Up @@ -140,7 +136,7 @@ class Params {
holder.disruptions.push_back(std::move(disruption));
}

Params(): b("20120614"), end_date("20130614T000000"_dt_time_stamp) {
Params(): b("20120614"), end_date("20130614T000000"_pts) {
std::vector<std::string> forbidden;
b.vj_with_network("network:R", "line:A", "11111111", "", true, "")("stop_area:stop1", 8*3600 +10*60, 8*3600 + 11 * 60)
("stop_area:stop2", 8*3600 + 20 * 60 ,8*3600 + 21*60);
Expand Down Expand Up @@ -195,7 +191,7 @@ class Params {

BOOST_FIXTURE_TEST_CASE(network_filter1, Params) {
std::vector<std::string> forbidden_uris;
auto dt = "20131219T155000"_dt_time_stamp;
auto dt = "20131219T155000"_pts;
pbnavitia::Response resp = navitia::disruption::disruptions(*(b.data),
dt, 1, 10, 0, "network.uri=network:R", forbidden_uris);

Expand All @@ -221,7 +217,7 @@ BOOST_FIXTURE_TEST_CASE(network_filter1, Params) {

BOOST_FIXTURE_TEST_CASE(network_filter2, Params) {
std::vector<std::string> forbidden_uris;
auto dt = "20131224T125000"_dt_time_stamp;
auto dt = "20131224T125000"_pts;
pbnavitia::Response resp = navitia::disruption::disruptions(*(b.data),
dt, 1, 10, 0, "network.uri=network:K", forbidden_uris);

Expand All @@ -242,7 +238,7 @@ BOOST_FIXTURE_TEST_CASE(network_filter2, Params) {

BOOST_FIXTURE_TEST_CASE(line_filter, Params) {
std::vector<std::string> forbidden_uris;
auto dt = "20131221T085000"_dt_time_stamp;
auto dt = "20131221T085000"_pts;
pbnavitia::Response resp = navitia::disruption::disruptions(*(b.data),
dt, 1 ,10 ,0 , "line.uri=line:S", forbidden_uris);

Expand All @@ -267,15 +263,15 @@ BOOST_FIXTURE_TEST_CASE(line_filter, Params) {

BOOST_FIXTURE_TEST_CASE(Test1, Params) {
std::vector<std::string> forbidden_uris;
auto dt = "20140101T0900"_dt_time_stamp;
auto dt = "20140101T0900"_pts;
pbnavitia::Response resp = navitia::disruption::disruptions(*(b.data),
dt, 1, 10, 0, "", forbidden_uris);
BOOST_REQUIRE_EQUAL(resp.response_type(), pbnavitia::ResponseType::NO_SOLUTION);
}

BOOST_FIXTURE_TEST_CASE(Test2, Params) {
std::vector<std::string> forbidden_uris;
auto dt = "20131226T0900"_dt_time_stamp;
auto dt = "20131226T0900"_pts;
pbnavitia::Response resp = navitia::disruption::disruptions(*(b.data),
dt, 1, 10, 0, "", forbidden_uris);
BOOST_REQUIRE_EQUAL(resp.disruptions_size(), 1);
Expand All @@ -294,15 +290,15 @@ BOOST_FIXTURE_TEST_CASE(Test2, Params) {
BOOST_FIXTURE_TEST_CASE(Test4, Params) {
std::cout << "bob 4?" << std::endl;
std::vector<std::string> forbidden_uris;
auto dt = "20130203T0900"_dt_time_stamp;
auto dt = "20130203T0900"_pts;
pbnavitia::Response resp = navitia::disruption::disruptions(*(b.data),
dt, 1 , 10, 0, "", forbidden_uris);
BOOST_REQUIRE_EQUAL(resp.response_type(), pbnavitia::ResponseType::NO_SOLUTION);
}

BOOST_FIXTURE_TEST_CASE(Test5, Params) {
std::vector<std::string> forbidden_uris;
auto dt = "20130212T0900"_dt_time_stamp;
auto dt = "20130212T0900"_pts;
pbnavitia::Response resp = navitia::disruption::disruptions(*(b.data),
dt, 1, 10, 0, "", forbidden_uris);

Expand Down
2 changes: 1 addition & 1 deletion source/jormungandr/tests/isochrone_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,4 @@ def test_reverse_isochrone_coord(self):

# the reverse isochrone should also find 2 journeys
normal_response = self.query(q + "&to=A")
assert len(normal_response["journeys"]) == 2
assert len(normal_response["journeys"]) == 2
14 changes: 14 additions & 0 deletions source/kraken/worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -580,8 +580,22 @@ pbnavitia::Response Worker::journeys(const pbnavitia::JourneysRequest &request,
case pbnavitia::ISOCHRONE: {
type::EntryPoint ep;
if (! origins.empty()) {
if (! request.clockwise()) {
// isochrone works only on clockwise
pbnavitia::Response response;
fill_pb_error(pbnavitia::Error::bad_format, "isochrone works only for clockwise request", response.mutable_error());
response.set_response_type(pbnavitia::NO_SOLUTION);
return response;
}
ep = origins[0];
} else {
if (request.clockwise()) {
// isochrone works only on clockwise
pbnavitia::Response response;
fill_pb_error(pbnavitia::Error::bad_format, "reverse isochrone works only for anti-clockwise request", response.mutable_error());
response.set_response_type(pbnavitia::NO_SOLUTION);
return response;
}
ep = destinations[0];
}
return navitia::routing::make_isochrone(*planner, ep, request.datetimes(0),
Expand Down
2 changes: 1 addition & 1 deletion source/routing/raptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ RAPTOR::isochrone(const vec_stop_point_duration& departures,
forbidden,
disruption_active);
clear(clockwise, bound);
init(departures, departure_datetime, true, accessibilite_params.properties);
init(departures, departure_datetime, clockwise, accessibilite_params.properties);

boucleRAPTOR(accessibilite_params, clockwise, max_transfers);
}
Expand Down
18 changes: 14 additions & 4 deletions source/routing/raptor_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -638,11 +638,21 @@ static void add_isochrone_response(RAPTOR& raptor,
continue;
}
auto pb_journey = response.add_journeys();
const auto str_departure = to_posix_timestamp(best_lbl, raptor.data);
const auto str_arrival = to_posix_timestamp(best_lbl, raptor.data);

uint64_t departure;
uint64_t arrival;
// Note: since there is no 2nd pass for the isochrone, the departure dt
// is the requested dt (or the arrival dt for non clockwise)
if (clockwise) {
departure = to_posix_timestamp(init_dt, raptor.data);
arrival = to_posix_timestamp(best_lbl, raptor.data);
} else {
departure = to_posix_timestamp(best_lbl, raptor.data);
arrival = to_posix_timestamp(init_dt, raptor.data);
}
const auto str_requested = to_posix_timestamp(init_dt, raptor.data);
pb_journey->set_arrival_date_time(str_arrival);
pb_journey->set_departure_date_time(str_departure);
pb_journey->set_arrival_date_time(arrival);
pb_journey->set_departure_date_time(departure);
pb_journey->set_requested_date_time(str_requested);
pb_journey->set_duration(duration);
pb_journey->set_nb_transfers(round - 1);
Expand Down
106 changes: 92 additions & 14 deletions source/routing/tests/routing_api_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ namespace ntest = navitia::test;
namespace bt = boost::posix_time;
namespace ng = navitia::georef;


static void dump_response(pbnavitia::Response resp, std::string test_name, bool debug_info = false) {
if (! debug_info)
return;
Expand Down Expand Up @@ -1577,29 +1578,106 @@ BOOST_AUTO_TEST_CASE(use_crow_fly){
BOOST_CHECK(nr::use_crow_fly(ep, &sp2, data));
}

struct isochrone_fixture {
isochrone_fixture(): b("20150614") {
b.vj("l1")("A", "8:25"_t)("B", "8:35"_t);
b.vj("l3")("A", "8:26"_t)("B", "8:45"_t);
b.vj("l4")("A", "8:27"_t)("C", "9:35"_t);
b.vj("l5")("A", "8:28"_t)("C", "9:50"_t);
b.vj("l6")("C", "8:29"_t)("B", "10:50"_t);

b.data->pt_data->index();
b.data->build_uri();
b.data->build_raptor();
}

ed::builder b;
};

BOOST_AUTO_TEST_CASE(isochrone) {
ed::builder b("20120614");
b.vj("l1")("A1", 8*3600 + 25 * 60)("B", 8*3600 + 35 * 60);
b.vj("l2")("A1", 8*3600 + 25 * 60)("C", 8*3600 + 35 * 60);

b.data->pt_data->index();
b.data->build_uri();
b.data->build_raptor();
/**
* classic isochrone from A, we should find 2 journeys
*/
BOOST_FIXTURE_TEST_CASE(isochrone, isochrone_fixture) {
nr::RAPTOR raptor(*(b.data));
ng::StreetNetwork sn_worker(*b.data->geo_ref);

navitia::type::EntryPoint ep {navitia::type::Type_e::StopPoint, "A"};

navitia::type::EntryPoint ep;
ep.type = navitia::type::Type_e::StopPoint;
ep.uri = "A1";
auto result = nr::make_isochrone(raptor,
ep,
"20150615T082000"_pts,
true,
{},
{},
sn_worker,
false,
3 * 60 * 60);

BOOST_REQUIRE_EQUAL(result.journeys_size(), 2);

auto result = nr::make_isochrone(raptor, ep,
navitia::test::to_posix_timestamp("20120615T082000"), true, {}, {},
sn_worker, false);
std::cout << "1/ dep " << navitia::from_posix_timestamp(result.journeys(0).departure_date_time()) << std::endl;
std::cout << "1/ arr " << navitia::from_posix_timestamp(result.journeys(0).arrival_date_time()) << std::endl;
std::cout << "2/ dep " << navitia::from_posix_timestamp(result.journeys(1).departure_date_time()) << std::endl;
std::cout << "2/ arr " << navitia::from_posix_timestamp(result.journeys(1).arrival_date_time()) << std::endl;

// Note: since there is no 2nd pass for the isochrone, the departure dt is the requested dt
BOOST_CHECK_EQUAL(result.journeys(0).departure_date_time(), "20150615T082000"_pts);
BOOST_CHECK_EQUAL(result.journeys(0).arrival_date_time(), "20150615T083500"_pts);
BOOST_CHECK_EQUAL(result.journeys(1).departure_date_time(), "20150615T082000"_pts);
BOOST_CHECK_EQUAL(result.journeys(1).arrival_date_time(), "20150615T093500"_pts);
}

/**
* reverse isochrone test, we want to arrive in B before 11:00,
* we should find 2 journeys
*/
BOOST_FIXTURE_TEST_CASE(reverse_isochrone, isochrone_fixture) {
nr::RAPTOR raptor(*(b.data));
ng::StreetNetwork sn_worker(*b.data->geo_ref);

navitia::type::EntryPoint ep {navitia::type::Type_e::StopPoint, "B"};

auto result = nr::make_isochrone(raptor,
ep,
"20150615T110000"_pts,
false,
{},
{},
sn_worker,
false,
3 * 60 * 60);

BOOST_REQUIRE_EQUAL(result.journeys_size(), 2);

BOOST_CHECK_EQUAL(result.journeys(0).departure_date_time(), "20150615T082900"_pts);
BOOST_CHECK_EQUAL(result.journeys(0).arrival_date_time(), "20150615T110000"_pts);
BOOST_CHECK_EQUAL(result.journeys(1).departure_date_time(), "20150615T082600"_pts);
BOOST_CHECK_EQUAL(result.journeys(1).arrival_date_time(), "20150615T110000"_pts);
}

/**
* only one hour from A, we cannot go to C
*/
BOOST_FIXTURE_TEST_CASE(isochrone_duration_limit, isochrone_fixture) {
nr::RAPTOR raptor(*(b.data));
ng::StreetNetwork sn_worker(*b.data->geo_ref);

navitia::type::EntryPoint ep {navitia::type::Type_e::StopPoint, "A"};

auto result = nr::make_isochrone(raptor,
ep,
"20150615T082000"_pts,
true,
{},
{},
sn_worker,
false,
1 * 60 * 60);

BOOST_REQUIRE_EQUAL(result.journeys_size(), 1);

BOOST_CHECK_EQUAL(result.journeys(0).departure_date_time(), "20150615T082000"_pts);
BOOST_CHECK_EQUAL(result.journeys(0).arrival_date_time(), "20150615T083500"_pts);
}


Expand Down
4 changes: 4 additions & 0 deletions source/tests/utils_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ u_int32_t operator"" _t(const char* str, size_t s) {
return boost::posix_time::duration_from_string(std::string(str, s)).total_seconds();
}

inline u_int64_t operator"" _pts(const char* str, size_t s) {
return navitia::to_posix_timestamp(boost::posix_time::from_iso_string(std::string(str, s)));
}

namespace std {
template<typename T>
std::ostream& operator<<(std::ostream& os, const std::set<T>& s) {
Expand Down

0 comments on commit f72eb3d

Please sign in to comment.