Adding seed for better pedestrians reproducibility

This commit is contained in:
bernatx 2021-09-14 10:41:56 +02:00 committed by bernat
parent e48d658699
commit 328cfca118
11 changed files with 46 additions and 0 deletions

View File

@ -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:

View File

@ -160,6 +160,10 @@ namespace client {
_episode.Lock()->SetPedestriansCrossFactor(percentage);
}
void World::SetPedestriansSeed(int seed) {
_episode.Lock()->SetPedestriansSeed(seed);
}
SharedPtr<Actor> World::GetTrafficSign(const Landmark& landmark) const {
SharedPtr<ActorList> actors = GetActors();
SharedPtr<TrafficSign> result;

View File

@ -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<Actor> GetTrafficSign(const Landmark& landmark) const;
SharedPtr<Actor> GetTrafficLight(const Landmark& landmark) const;

View File

@ -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;

View File

@ -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 -----------------------------------------
// ===========================================================================

View File

@ -296,6 +296,8 @@ namespace detail {
void SetPedestriansCrossFactor(float percentage);
void SetPedestriansSeed(int seed);
/// @}
// =========================================================================
/// @name General operations with actors

View File

@ -85,6 +85,10 @@ namespace detail {
_nav.SetPedestriansCrossFactor(percentage);
}
void SetPedestriansSeed(int seed) {
_nav.SetSeed(seed);
}
private:
Client &_client;

View File

@ -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;

View File

@ -71,6 +71,8 @@ namespace nav {
bool GetAgentRoute(ActorId id, carla::geom::Location from, carla::geom::Location to,
std::vector<carla::geom::Location> &path, std::vector<unsigned char> &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

View File

@ -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"))

View File

@ -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):