From 1540529768b40ff01c82c286fb5ea1b8f95708d8 Mon Sep 17 00:00:00 2001 From: Sascha Korf <51127093+xsaschako@users.noreply.github.com> Date: Thu, 26 Nov 2026 20:24:31 +0100 Subject: [PATCH] 1612 Fix ABM crash for persons without assigned location of requested activity type Mobility rules can return an ActivityType for which the person has no assigned location (e.g. no workplace), which made get_assigned_location access an empty vector out of bounds. Add Person::has_assigned_location and skip the rule in Model and GraphABModel if no location is assigned. Also size m_assigned_location_model_ids by ActivityType::Count. --- cpp/models/abm/model.cpp | 6 +++- cpp/models/abm/person.cpp | 9 +++++- cpp/models/abm/person.h | 8 +++++ cpp/models/graph_abm/graph_abmodel.h | 6 +++- cpp/tests/test_abm_model.cpp | 44 ++++++++++++++++++++++++++++ 5 files changed, 70 insertions(+), 3 deletions(-) diff --git a/cpp/models/abm/model.cpp b/cpp/models/abm/model.cpp index d8dd3fd2c3..343ed2afc6 100755 --- a/cpp/models/abm/model.cpp +++ b/cpp/models/abm/model.cpp @@ -114,7 +114,11 @@ void Model::perform_mobility(TimePoint t, TimeSpan dt) auto try_mobility_rule = [&](auto rule) -> bool { // run mobility rule and check if change of location can actually happen auto target_type = rule(personal_rng, person, t, dt, parameters); - auto target = person.get_assigned_location(target_type, personal_rng); + // the Person cannot move if they have no assigned Location for the target activity, e.g. no workplace + if (!person.has_assigned_location(target_type)) { + return false; + } + auto target = person.get_assigned_location(target_type, personal_rng); if (target.second == m_id) { const Location& target_location = get_location(target.first); const LocationId current_location = person.get_location(); diff --git a/cpp/models/abm/person.cpp b/cpp/models/abm/person.cpp index 9c2ba077cf..48a9e52ad9 100755 --- a/cpp/models/abm/person.cpp +++ b/cpp/models/abm/person.cpp @@ -26,6 +26,7 @@ #include "abm/location.h" #include "abm/person_id.h" #include "memilio/utils/random_number_generator.h" +#include #include #include #include @@ -50,7 +51,7 @@ Person::Person(mio::RandomNumberGenerator& rng, LocationType location_type, Acti , m_cells{0} , m_last_transport_mode(TransportMode::Unknown) , m_test_results({TestType::Count}, TestResult()) - , m_assigned_location_model_ids((int)LocationType::Count) + , m_assigned_location_model_ids((uint32_t)ActivityType::Count) , m_person_id(person_id) { m_random_workgroup = UniformDistribution::get_instance()(rng); @@ -123,9 +124,15 @@ void Person::set_assigned_location(ActivityType activity_type, LocationId id, in m_assigned_location_model_ids[static_cast(activity_type)].push_back(model_id); } +bool Person::has_assigned_location(ActivityType activity_type) const +{ + return !m_assigned_locations[static_cast(activity_type)].empty(); +} + std::pair Person::get_assigned_location(ActivityType activity_type, PersonalRandomNumberGenerator& rng) const { + assert(has_assigned_location(activity_type) && "Person has no assigned Location of the given ActivityType."); size_t index = UniformIntDistribution::get_instance()( rng, size_t(0), m_assigned_locations[static_cast(activity_type)].size() - 1); return {m_assigned_locations[static_cast(activity_type)][index], diff --git a/cpp/models/abm/person.h b/cpp/models/abm/person.h index 510b8af7b9..6502b43425 100755 --- a/cpp/models/abm/person.h +++ b/cpp/models/abm/person.h @@ -196,8 +196,16 @@ class Person */ void set_assigned_location(ActivityType activity_type, LocationId id, int model_id); + /** + * @brief Check whether the Person has at least one assigned Location for the given ActivityType. + * @param[in] activity_type #ActivityType of interest. + * @return True if at least one Location is assigned for activity_type. + */ + bool has_assigned_location(ActivityType activity_type) const; + /** * @brief Returns the index of an assigned Location of the Person. The index is uniformly sampled from the LocationIds of the assigned ActivityType. + * The Person must have at least one assigned Location of this type, see has_assigned_location. * @param[in] activity_type #ActivityType of the assigned Location. * @param[in] rng RandomNumberGenerator for sampling the index of the assigned LocationId. * @return The index in the LocationId and the model id of the assigned Location. diff --git a/cpp/models/graph_abm/graph_abmodel.h b/cpp/models/graph_abm/graph_abmodel.h index 434d6f8e60..d197b8aea4 100644 --- a/cpp/models/graph_abm/graph_abmodel.h +++ b/cpp/models/graph_abm/graph_abmodel.h @@ -97,7 +97,11 @@ class GraphABModel : public abm::Model auto try_mobility_rule = [&](auto rule) -> bool { //run mobility rule and check if change of location can actually happen auto target_type = rule(personal_rng, person, t, dt, parameters); - auto target = person.get_assigned_location(target_type, personal_rng); + // the Person cannot move if they have no assigned Location for the target activity, e.g. no workplace + if (!person.has_assigned_location(target_type)) { + return false; + } + auto target = person.get_assigned_location(target_type, personal_rng); if (target.second == Base::m_id) { const Location& target_location = Base::get_location(target.first); const LocationId current_location = person.get_location(); diff --git a/cpp/tests/test_abm_model.cpp b/cpp/tests/test_abm_model.cpp index 72cf6ed595..beb232aa1d 100644 --- a/cpp/tests/test_abm_model.cpp +++ b/cpp/tests/test_abm_model.cpp @@ -227,6 +227,50 @@ TEST_F(TestModel, findLocation) EXPECT_EQ(model_test.find_locations(mio::abm::ActivityType::Home, person_id)[0], home_id); } +/** + * @brief Test that Persons without an assigned Location for an ActivityType do not crash the mobility rules. + */ +TEST_F(TestModel, mobilityWithMissingAssignedLocations) +{ + auto t = mio::abm::TimePoint(0); + auto dt = mio::abm::hours(1); + auto model = mio::abm::Model(num_age_groups); + model.get_rng() = this->get_rng(); + + auto home_id = model.add_location(mio::abm::LocationType::Home); + auto work_id = model.add_location(mio::abm::LocationType::Work); + model.add_location(mio::abm::LocationType::School); + model.add_location(mio::abm::LocationType::BasicsShop); + model.add_location(mio::abm::LocationType::Recreation); + + // Adults with a home but no workplace, school, shop or recreation location. + std::vector home_only; + for (int i = 0; i < 10; ++i) { + auto id = add_test_person(model, home_id, mio::abm::ActivityType::Home, age_group_35_to_59); + model.get_person(id).set_assigned_location(mio::abm::ActivityType::Home, home_id, model.get_id()); + home_only.push_back(id); + } + // A Person whose current activity (Work) has no assigned Location. + auto at_work_id = add_test_person(model, work_id, mio::abm::ActivityType::Work, age_group_35_to_59); + model.get_person(at_work_id).set_assigned_location(mio::abm::ActivityType::Home, home_id, model.get_id()); + + EXPECT_FALSE(model.get_person(at_work_id).has_assigned_location(mio::abm::ActivityType::Work)); + EXPECT_TRUE(model.get_person(at_work_id).has_assigned_location(mio::abm::ActivityType::Home)); + + // Simulate a full weekday, during which the mobility rules request Work, School, BasicsShop and Recreation. + for (int hour = 0; hour < 24; ++hour) { + model.evolve(t, dt); + t += dt; + } + + // Nobody can have been moved to a Location type they are not assigned to. + for (auto id : home_only) { + EXPECT_EQ(model.get_person(id).get_location(), home_id); + } + // The Person at work returns home after work, which is assigned. + EXPECT_EQ(model.get_person(at_work_id).get_location(), home_id); +} + /** * @brief Test the exposure contribution normalization in the Model class. */