Skip to content

Commit

Permalink
unique id
Browse files Browse the repository at this point in the history
  • Loading branch information
jubicker committed Jan 14, 2025
1 parent f21f37f commit 6a76553
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
19 changes: 7 additions & 12 deletions cpp/models/abm/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,24 @@ LocationId Model::add_location(LocationType type, uint32_t num_cells)

PersonId Model::add_person(const LocationId id, AgeGroup age)
{
return add_person(Person(m_rng, get_location(id).get_type(), id, m_id, age));
uint64_t unique_id = (static_cast<int64_t>(m_id)) << 32 | static_cast<uint32_t>(m_persons.size());
return add_person(Person(m_rng, get_location(id).get_type(), id, m_id, age, PersonId::invalid_id(), unique_id));
}

PersonId Model::add_person(Person&& person)
{
assert(person.get_location() != LocationId::invalid_id() && "Added Person's location must be valid.");
assert(person.get_location() < LocationId((uint32_t)m_locations.size()) &&
"Added Person's location is not in Model.");
assert(person.get_unique_id() != INVALID_UNIQUE_ID && "Added Person's unique id must be valid.");
assert(person.get_age() < (AgeGroup)parameters.get_num_groups() && "Added Person's AgeGroup is too large.");
PersonId new_id{static_cast<uint32_t>(m_persons.size())};
if (person.get_unique_id() == INVALID_UNIQUE_ID) {
//if the person does not have a valid unique id yet, it gets one which is coumbound by model id and index in m_persons
uint64_t unique_id = (static_cast<int64_t>(m_id)) << 32 | static_cast<uint32_t>(m_persons.size());
person.set_assigned_location(LocationType::Cemetery, m_cemetery_id, m_id);
m_persons.emplace_back(person, new_id, unique_id);
}
else {
//if the person already has a valid unique id, it only gets a new PersonId
m_persons.emplace_back(person, new_id);
}
//set correct person id aka index in m_persons vector
person.set_id(new_id);
person.set_assigned_location(LocationType::Cemetery, m_cemetery_id, m_id);
m_persons.emplace_back(person);
m_activeness_statuses.push_back(true);
auto& new_person = m_persons.back();

if (m_is_local_population_cache_valid) {
++m_local_population_cache[new_person.get_location().get()];
}
Expand Down
10 changes: 6 additions & 4 deletions cpp/tests/abm_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@
*/
#include "abm_helpers.h"
#include "abm/person.h"
#include "abm/person_id.h"
#include "memilio/utils/random_number_generator.h"

mio::abm::Person make_test_person(mio::RandomNumberGenerator& rng, mio::abm::Location& location, mio::AgeGroup age,
mio::abm::InfectionState infection_state, mio::abm::TimePoint t,
mio::abm::Parameters params)
mio::abm::Parameters params, uint64_t id)
{
assert(age.get() < params.get_num_groups());
mio::abm::Person p(rng, location.get_type(), location.get_id(), location.get_model_id(), age);
mio::abm::Person p(rng, location.get_type(), location.get_id(), location.get_model_id(), age,
mio::abm::PersonId::invalid_id(), id);
if (infection_state != mio::abm::InfectionState::Susceptible) {
auto rng_p = mio::abm::PersonalRandomNumberGenerator(rng, p);
p.add_new_infection(
Expand All @@ -38,8 +40,8 @@ mio::abm::Person make_test_person(mio::RandomNumberGenerator& rng, mio::abm::Loc
mio::abm::PersonId add_test_person(mio::abm::Model& model, mio::abm::LocationId loc_id, mio::AgeGroup age,
mio::abm::InfectionState infection_state, mio::abm::TimePoint t)
{
return model.add_person(
make_test_person(model.get_rng(), model.get_location(loc_id), age, infection_state, t, model.parameters));
return model.add_person(make_test_person(model.get_rng(), model.get_location(loc_id), age, infection_state, t,
model.parameters, static_cast<uint64_t>(model.get_persons().size())));
}

void interact_testing(mio::abm::PersonalRandomNumberGenerator& personal_rng, mio::abm::Person& person,
Expand Down
5 changes: 3 additions & 2 deletions cpp/tests/abm_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,11 @@ struct ScopedMockDistribution {
/**
* @brief Create a Person without a Model object. Intended for simple use in tests.
*/
mio::abm::Person make_test_person(mio::RandomNumberGenerator& rng, mio::abm::Location& location, mio::AgeGroup age = age_group_15_to_34,
mio::abm::Person make_test_person(mio::RandomNumberGenerator& rng, mio::abm::Location& location,
mio::AgeGroup age = age_group_15_to_34,
mio::abm::InfectionState infection_state = mio::abm::InfectionState::Susceptible,
mio::abm::TimePoint t = mio::abm::TimePoint(0),
mio::abm::Parameters params = mio::abm::Parameters(num_age_groups));
mio::abm::Parameters params = mio::abm::Parameters(num_age_groups), uint64_t id = 0);

/**
* @brief Add a Person to the Model. Intended for simple use in tests.
Expand Down

0 comments on commit 6a76553

Please sign in to comment.