Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion cpp/models/abm/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
9 changes: 8 additions & 1 deletion cpp/models/abm/person.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "abm/location.h"
#include "abm/person_id.h"
#include "memilio/utils/random_number_generator.h"
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <vector>
Expand All @@ -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<ScalarType>::get_instance()(rng);
Expand Down Expand Up @@ -123,9 +124,15 @@ void Person::set_assigned_location(ActivityType activity_type, LocationId id, in
m_assigned_location_model_ids[static_cast<uint32_t>(activity_type)].push_back(model_id);
}

bool Person::has_assigned_location(ActivityType activity_type) const
{
return !m_assigned_locations[static_cast<uint32_t>(activity_type)].empty();
}

std::pair<LocationId, int> 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<size_t>::get_instance()(
rng, size_t(0), m_assigned_locations[static_cast<uint32_t>(activity_type)].size() - 1);
return {m_assigned_locations[static_cast<uint32_t>(activity_type)][index],
Expand Down
8 changes: 8 additions & 0 deletions cpp/models/abm/person.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 5 additions & 1 deletion cpp/models/graph_abm/graph_abmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
44 changes: 44 additions & 0 deletions cpp/tests/test_abm_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<mio::abm::PersonId> 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.
*/
Expand Down
Loading