From 328cfca118cd7542ad6f329f3ced6256b9b3f0b5 Mon Sep 17 00:00:00 2001 From: bernatx Date: Tue, 14 Sep 2021 10:41:56 +0200 Subject: [PATCH] Adding seed for better pedestrians reproducibility --- CHANGELOG.md | 3 +++ LibCarla/source/carla/client/World.cpp | 4 ++++ LibCarla/source/carla/client/World.h | 3 +++ LibCarla/source/carla/client/detail/Episode.h | 6 ++++++ LibCarla/source/carla/client/detail/Simulator.cpp | 7 +++++++ LibCarla/source/carla/client/detail/Simulator.h | 2 ++ LibCarla/source/carla/client/detail/WalkerNavigation.h | 4 ++++ LibCarla/source/carla/nav/Navigation.cpp | 5 +++++ LibCarla/source/carla/nav/Navigation.h | 2 ++ PythonAPI/carla/source/libcarla/World.cpp | 1 + PythonAPI/examples/generate_traffic.py | 9 +++++++++ 11 files changed, 46 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e6d173a9..c40df7e0c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,9 @@ * Improved handling of collisions in Traffic Manager when driving at very high speeds. * Added open/close doors feature for vehicles. * Added API functions to 3D vectors: `squared_length`, `length`, `make_unit_vector`, `dot`, `dot_2d`, `distance`, `distance_2d`, `distance_squared`, `distance_squared_2d`, `get_vector_angle` + * Added a seed for better reproducibility of pedestrians + - New API function `set_pedestrians_seed` + - New parameter **--seedw** in generate_traffic.py script * Added API functions to 2D vectors: `squared_length`, `length`, `make_unit_vector` * Added missing dependency `libomp5` to Release.Dockerfile * Added API functions to interact with pedestrian bones: diff --git a/LibCarla/source/carla/client/World.cpp b/LibCarla/source/carla/client/World.cpp index 4e0768680..bb177e99a 100644 --- a/LibCarla/source/carla/client/World.cpp +++ b/LibCarla/source/carla/client/World.cpp @@ -160,6 +160,10 @@ namespace client { _episode.Lock()->SetPedestriansCrossFactor(percentage); } + void World::SetPedestriansSeed(int seed) { + _episode.Lock()->SetPedestriansSeed(seed); + } + SharedPtr World::GetTrafficSign(const Landmark& landmark) const { SharedPtr actors = GetActors(); SharedPtr result; diff --git a/LibCarla/source/carla/client/World.h b/LibCarla/source/carla/client/World.h index 0b5ded61e..ebb1f3221 100644 --- a/LibCarla/source/carla/client/World.h +++ b/LibCarla/source/carla/client/World.h @@ -145,6 +145,9 @@ namespace client { /// percentage of 1.0f means all pedestrians can cross roads if needed void SetPedestriansCrossFactor(float percentage); + /// set the seed to use with random numbers in the pedestrians module + void SetPedestriansSeed(int seed); + SharedPtr GetTrafficSign(const Landmark& landmark) const; SharedPtr GetTrafficLight(const Landmark& landmark) const; diff --git a/LibCarla/source/carla/client/detail/Episode.h b/LibCarla/source/carla/client/detail/Episode.h index 0bfba58a0..0e1ae550e 100644 --- a/LibCarla/source/carla/client/detail/Episode.h +++ b/LibCarla/source/carla/client/detail/Episode.h @@ -101,6 +101,12 @@ namespace detail { nav->SetPedestriansCrossFactor(percentage); } + void SetPedestriansSeed(int seed) { + auto nav = _navigation.load(); + DEBUG_ASSERT(nav != nullptr); + nav->SetPedestriansSeed(seed); + } + void AddPendingException(std::string e) { _pending_exceptions = true; _pending_exceptions_msg = e; diff --git a/LibCarla/source/carla/client/detail/Simulator.cpp b/LibCarla/source/carla/client/detail/Simulator.cpp index 99848578e..38b28f791 100644 --- a/LibCarla/source/carla/client/detail/Simulator.cpp +++ b/LibCarla/source/carla/client/detail/Simulator.cpp @@ -308,6 +308,13 @@ namespace detail { navigation->SetPedestriansCrossFactor(percentage); } + void Simulator::SetPedestriansSeed(int seed) { + DEBUG_ASSERT(_episode != nullptr); + auto navigation = _episode->CreateNavigationIfMissing(); + DEBUG_ASSERT(navigation != nullptr); + navigation->SetPedestriansSeed(seed); + } + // =========================================================================== // -- General operations with actors ----------------------------------------- // =========================================================================== diff --git a/LibCarla/source/carla/client/detail/Simulator.h b/LibCarla/source/carla/client/detail/Simulator.h index 424839948..437289cf6 100644 --- a/LibCarla/source/carla/client/detail/Simulator.h +++ b/LibCarla/source/carla/client/detail/Simulator.h @@ -296,6 +296,8 @@ namespace detail { void SetPedestriansCrossFactor(float percentage); + void SetPedestriansSeed(int seed); + /// @} // ========================================================================= /// @name General operations with actors diff --git a/LibCarla/source/carla/client/detail/WalkerNavigation.h b/LibCarla/source/carla/client/detail/WalkerNavigation.h index 41afd4ca0..a9e3361b6 100644 --- a/LibCarla/source/carla/client/detail/WalkerNavigation.h +++ b/LibCarla/source/carla/client/detail/WalkerNavigation.h @@ -85,6 +85,10 @@ namespace detail { _nav.SetPedestriansCrossFactor(percentage); } + void SetPedestriansSeed(int seed) { + _nav.SetSeed(seed); + } + private: Client &_client; diff --git a/LibCarla/source/carla/nav/Navigation.cpp b/LibCarla/source/carla/nav/Navigation.cpp index 3e79632f4..debca0be5 100644 --- a/LibCarla/source/carla/nav/Navigation.cpp +++ b/LibCarla/source/carla/nav/Navigation.cpp @@ -67,6 +67,11 @@ namespace nav { dtFreeNavMesh(_nav_mesh); } + // set the seed to use with random numbers + void Navigation::SetSeed(unsigned int seed) { + srand(seed); + } + // load navigation data bool Navigation::Load(const std::string &filename) { std::ifstream f; diff --git a/LibCarla/source/carla/nav/Navigation.h b/LibCarla/source/carla/nav/Navigation.h index 5b538c60e..be35d740d 100644 --- a/LibCarla/source/carla/nav/Navigation.h +++ b/LibCarla/source/carla/nav/Navigation.h @@ -71,6 +71,8 @@ namespace nav { bool GetAgentRoute(ActorId id, carla::geom::Location from, carla::geom::Location to, std::vector &path, std::vector &area); + /// set the seed to use with random numbers + void SetSeed(unsigned int seed); /// create the crowd object void CreateCrowd(void); /// create a new walker diff --git a/PythonAPI/carla/source/libcarla/World.cpp b/PythonAPI/carla/source/libcarla/World.cpp index c71892ea7..f3b5afa27 100644 --- a/PythonAPI/carla/source/libcarla/World.cpp +++ b/PythonAPI/carla/source/libcarla/World.cpp @@ -319,6 +319,7 @@ void export_world() { .def("remove_on_tick", &cc::World::RemoveOnTick, (arg("callback_id"))) .def("tick", &Tick, (arg("seconds")=0.0)) .def("set_pedestrians_cross_factor", CALL_WITHOUT_GIL_1(cc::World, SetPedestriansCrossFactor, float), (arg("percentage"))) + .def("set_pedestrians_seed", CALL_WITHOUT_GIL_1(cc::World, SetPedestriansSeed, int), (arg("seed"))) .def("get_traffic_sign", CONST_CALL_WITHOUT_GIL_1(cc::World, GetTrafficSign, cc::Landmark), arg("landmark")) .def("get_traffic_light", CONST_CALL_WITHOUT_GIL_1(cc::World, GetTrafficLight, cc::Landmark), arg("landmark")) .def("get_traffic_light_from_opendrive_id", CONST_CALL_WITHOUT_GIL_1(cc::World, GetTrafficLightFromOpenDRIVE, const carla::road::SignId&), arg("traffic_light_id")) diff --git a/PythonAPI/examples/generate_traffic.py b/PythonAPI/examples/generate_traffic.py index cea091d31..a5995f0e1 100755 --- a/PythonAPI/examples/generate_traffic.py +++ b/PythonAPI/examples/generate_traffic.py @@ -122,6 +122,12 @@ def main(): metavar='S', type=int, help='Set random device seed and deterministic mode for Traffic Manager') + argparser.add_argument( + '--seedw', + metavar='S', + default=0, + type=int, + help='Set the seed for pedestrians module') argparser.add_argument( '--car-lights-on', action='store_true', @@ -259,6 +265,9 @@ def main(): # some settings percentagePedestriansRunning = 0.0 # how many pedestrians will run percentagePedestriansCrossing = 0.0 # how many pedestrians will walk through the road + if args.seedw: + world.set_pedestrians_seed(args.seedw) + random.seed(args.seedw) # 1. take all the random locations to spawn spawn_points = [] for i in range(args.number_of_walkers):