Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
5b8a0b7
add include providing uint32_t
reneSchm Dec 7, 2023
5d38f2f
move interact and migrate_to to world
reneSchm Dec 7, 2023
e3a30d5
- remove location* from Person
reneSchm Dec 7, 2023
ccfbf2d
[wip] remove m_persons from Location
reneSchm Jan 3, 2024
adcac26
Merge branch 'main' into 757-dont-store-pointers-in-the-abm
reneSchm Jan 3, 2024
89a574c
Person::get_location returns id by value
reneSchm Jan 24, 2024
124567d
add caching to replace person pointers in location
reneSchm Jan 24, 2024
6296cdc
Merge branch 'main' into 757-dont-store-pointers-in-the-abm
reneSchm Jan 24, 2024
f0d70a9
change world interface to use mostly ids instead of references
reneSchm Jan 29, 2024
7ce7eae
Merge branch 'main' into 757-dont-store-pointers-in-the-abm
reneSchm Jan 29, 2024
a1bc2d4
remove some unused headers
reneSchm Jan 29, 2024
aeb171d
World::m_locations no longer uses pointers
reneSchm Jan 29, 2024
57613a3
update includes and include guards
reneSchm Jan 29, 2024
8d37523
remove m_persons from cells,
reneSchm Jan 30, 2024
c7b6343
remove unnecessary includes
reneSchm Jan 30, 2024
17cc889
extract Person::RandomNumberGenerator from Person,
reneSchm Jan 31, 2024
b0e49ed
fix (in)validating exposure caches for simulations
reneSchm Jan 31, 2024
8293c79
avoid hashing LocationId
reneSchm Jan 31, 2024
e50bd75
reorder some arguments of free functions,
reneSchm Feb 1, 2024
144252a
vastly improve performance.
reneSchm Feb 2, 2024
9a15ad7
Merge branch 'main' into 757-dont-store-pointers-in-the-abm
reneSchm Feb 5, 2024
da18750
hide cache data behind read/write functions
reneSchm Feb 7, 2024
54fac3a
add copyright comment
reneSchm Feb 9, 2024
3196a33
use atomics for caching. omp parallelisation works
reneSchm Feb 13, 2024
c575af3
Merge branch 'main' into 757-dont-store-pointers-in-the-abm
reneSchm Feb 13, 2024
43294fc
caches now use CustemIndexArray and Eigen::Vector.
reneSchm Mar 11, 2024
997d10d
Merge branch 'main' into 757-dont-store-pointers-in-the-abm
reneSchm Mar 11, 2024
30d1ddf
only rebuild exposure caches on demand.
reneSchm Mar 12, 2024
5050022
clean up. comments and interface changes.
reneSchm Mar 13, 2024
bcbc17e
clean up part 2.
reneSchm Mar 14, 2024
bdbd207
add credit for authors of the original code
reneSchm Mar 14, 2024
b4d3851
make some implicit conversions explicit
reneSchm Mar 14, 2024
833e914
Merge branch 'main' into 757-dont-store-pointers-in-the-abm
reneSchm Mar 14, 2024
cfe81cb
fix failing overload resolution by explicit casts
reneSchm Mar 14, 2024
24ff9a8
change argument order in mio::abm::migrate,
reneSchm Mar 15, 2024
d8adc01
update tests in line with changes to cpp code
reneSchm Mar 15, 2024
f98a20b
remove abm.h
reneSchm Mar 15, 2024
5363a8d
update include in abm simulation binding
reneSchm Mar 18, 2024
2e0d3ba
Merge branch 'main' into 757-dont-store-pointers-in-the-abm
reneSchm Mar 19, 2024
9da767c
rename files functions.* to model_functions.*
reneSchm Mar 25, 2024
17544fd
add a test for random_migration
reneSchm Mar 25, 2024
66065ec
Merge branch 'main' into 756-dont-store-pointers-in-the-abm
reneSchm Mar 25, 2024
df5f724
fix initialization order in new test
reneSchm Mar 25, 2024
a302af5
Merge branch 'main' into 757-dont-store-pointers-in-the-abm
reneSchm Apr 11, 2024
fca5bfc
Merge branch 'main' into 757-dont-store-pointers-in-the-abm
reneSchm Apr 22, 2024
751e81a
[ci skip] Apply suggestions from code review
reneSchm May 13, 2024
3b2f4e6
Merge branch 'main' into 757-dont-store-pointers-in-the-abm
reneSchm May 24, 2024
6d96014
Merge branch 'main' into 757-dont-store-pointers-in-the-abm
reneSchm Jun 13, 2024
bde288f
rename iterators
reneSchm Jul 1, 2024
361c586
remove trailing underscore from include guards
reneSchm Jul 1, 2024
65d27cc
delete developement artifacts
reneSchm Jul 1, 2024
62d2f83
[ci skip][wip] move location type to location
reneSchm Jul 8, 2024
8fe0c9a
move location type to location
reneSchm Jul 8, 2024
2f3b4f3
update py-simulation and fix CI
reneSchm Jul 9, 2024
b8653a1
Merge branch 'main' into 757-dont-store-pointers-in-the-abm
reneSchm Jul 9, 2024
7ccacbf
delete whitespace
reneSchm Jul 9, 2024
f806c5a
make another conversion explicit
reneSchm Jul 9, 2024
7a06363
delete whitespace
reneSchm Jul 9, 2024
852d68e
remove unrelated code
reneSchm Jul 9, 2024
90babe1
make yet another conversion explicit
reneSchm Jul 9, 2024
0c783eb
make yet yet another conversion explicit
reneSchm Jul 10, 2024
e1842dc
fix some id issues in abm tests
reneSchm Jul 10, 2024
cc4e900
set correct location in TestMigrationRules.event_return
reneSchm Jul 10, 2024
78e1923
Add or clean up comments, make minor improvements
reneSchm Jul 11, 2024
5bf395a
slightly extend TestPerson.migrate
reneSchm Jul 11, 2024
215ebff
review: update documentation
reneSchm Jul 17, 2024
a761592
replace static_cast by the shorter py::overload_cast
reneSchm Jul 18, 2024
e380124
fix last commit
reneSchm Jul 19, 2024
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
12 changes: 6 additions & 6 deletions cpp/benchmarks/abm.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "abm/simulation.h"
#include "memilio/utils/stl_util.h"

#include "benchmark/benchmark.h"

mio::abm::Simulation make_simulation(size_t num_persons, std::initializer_list<uint32_t> seeds)
Expand All @@ -23,10 +23,10 @@ mio::abm::Simulation make_simulation(size_t num_persons, std::initializer_list<u
home_size = 0;
}

auto age = mio::AgeGroup(mio::UniformIntDistribution<size_t>::get_instance()(
auto age = mio::AgeGroup(mio::UniformIntDistribution<size_t>::get_instance()(
world.get_rng(), size_t(0), world.parameters.get_num_groups() - 1));
auto& person = world.add_person(home, age);
person.set_assigned_location(home);
auto person = world.add_person(home, age);
world.get_person(person).set_assigned_location(home);
home_size++;
}

Expand All @@ -49,7 +49,7 @@ mio::abm::Simulation make_simulation(size_t num_persons, std::initializer_list<u

//infections and masks
for (auto& person : world.get_persons()) {
auto prng = mio::abm::Person::RandomNumberGenerator(world.get_rng(), person);
auto prng = mio::abm::PersonalRandomNumberGenerator(world.get_rng(), person);
//some % of people are infected, large enough to have some infection activity without everyone dying
auto pct_infected = 0.05;
if (mio::UniformDistribution<double>::get_instance()(prng, 0.0, 1.0) < pct_infected) {
Expand Down Expand Up @@ -114,7 +114,7 @@ mio::abm::Simulation make_simulation(size_t num_persons, std::initializer_list<u
/**
* Benchmark for the ABM simulation.
* @param num_persons Number of persons in the simulation.
* @param seeds Seeds for the random number generator.
* @param seeds Seeds for the random number generator.
*/
void abm_benchmark(benchmark::State& state, size_t num_persons, std::initializer_list<uint32_t> seeds)
{
Expand Down
41 changes: 20 additions & 21 deletions cpp/examples/abm_history_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "abm/abm.h"
#include "abm/household.h"
#include <cstdio>
#include "abm/lockdown_rules.h"
#include "abm/simulation.h"
#include "abm/world.h"
#include "memilio/io/io.h"
#include "abm/location_type.h"
#include "memilio/io/history.h"

#include <fstream>
#include <string>
#include <iostream>
#include "memilio/io/history.h"

std::string convert_loc_id_to_string(std::tuple<mio::abm::LocationType, uint32_t> tuple_id)
{
Expand Down Expand Up @@ -78,7 +77,6 @@ int main()
// Set the age group the can go to work is AgeGroup(2) and AgeGroup(3) (i.e. 15-34 and 35-59)
world.parameters.get<mio::abm::AgeGroupGotoWork>().set_multiple({age_group_15_to_34, age_group_35_to_59}, true);


// There are 3 households for each household group.
int n_households = 3;

Expand Down Expand Up @@ -110,21 +108,21 @@ int main()
// Add one social event with 5 maximum contacts.
// Maximum contacs limit the number of people that a person can infect while being at this location.
auto event = world.add_location(mio::abm::LocationType::SocialEvent);
world.get_individualized_location(event).get_infection_parameters().set<mio::abm::MaximumContacts>(5);
world.get_location(event).get_infection_parameters().set<mio::abm::MaximumContacts>(5);
// Add hospital and ICU with 5 maximum contacs.
auto hospital = world.add_location(mio::abm::LocationType::Hospital);
world.get_individualized_location(hospital).get_infection_parameters().set<mio::abm::MaximumContacts>(5);
world.get_location(hospital).get_infection_parameters().set<mio::abm::MaximumContacts>(5);
auto icu = world.add_location(mio::abm::LocationType::ICU);
world.get_individualized_location(icu).get_infection_parameters().set<mio::abm::MaximumContacts>(5);
world.get_location(icu).get_infection_parameters().set<mio::abm::MaximumContacts>(5);
// Add one supermarket, maximum constacts are assumed to be 20.
auto shop = world.add_location(mio::abm::LocationType::BasicsShop);
world.get_individualized_location(shop).get_infection_parameters().set<mio::abm::MaximumContacts>(20);
world.get_location(shop).get_infection_parameters().set<mio::abm::MaximumContacts>(20);
// At every school, the maximum contacts are 20.
auto school = world.add_location(mio::abm::LocationType::School);
world.get_individualized_location(school).get_infection_parameters().set<mio::abm::MaximumContacts>(20);
world.get_location(school).get_infection_parameters().set<mio::abm::MaximumContacts>(20);
// At every workplace, maximum contacts are 10.
auto work = world.add_location(mio::abm::LocationType::Work);
world.get_individualized_location(work).get_infection_parameters().set<mio::abm::MaximumContacts>(10);
world.get_location(work).get_infection_parameters().set<mio::abm::MaximumContacts>(10);

// People can get tested at work (and do this with 0.5 probability) from time point 0 to day 30.
auto testing_min_time = mio::abm::days(1);
Expand All @@ -141,7 +139,7 @@ int main()
// The infection states are chosen randomly.
auto persons = world.get_persons();
for (auto& person : persons) {
auto rng = mio::abm::Person::RandomNumberGenerator(world.get_rng(), person);
auto rng = mio::abm::PersonalRandomNumberGenerator(world.get_rng(), person);
mio::abm::InfectionState infection_state =
(mio::abm::InfectionState)(rand() % ((uint32_t)mio::abm::InfectionState::Count - 1));
if (infection_state != mio::abm::InfectionState::Susceptible)
Expand All @@ -150,19 +148,20 @@ int main()
}

// Assign locations to the people
for (auto& person : persons) {
for (auto& person : world.get_persons()) {
const auto pid = person.get_id();
//assign shop and event
person.set_assigned_location(event);
person.set_assigned_location(shop);
world.assign_location(pid, event);
world.assign_location(pid, shop);
//assign hospital and ICU
person.set_assigned_location(hospital);
person.set_assigned_location(icu);
world.assign_location(pid, hospital);
world.assign_location(pid, icu);
//assign work/school to people depending on their age
if (person.get_age() == age_group_5_to_14) {
person.set_assigned_location(school);
world.assign_location(pid, school);
}
if (person.get_age() == age_group_15_to_34 || person.get_age() == age_group_35_to_59) {
person.set_assigned_location(work);
world.assign_location(pid, work);
}
}

Expand All @@ -187,7 +186,7 @@ int main()
{
Type location_ids{};
for (auto& location : sim.get_world().get_locations()) {
location_ids.push_back(std::make_tuple(location.get_type(), location.get_index()));
location_ids.push_back(std::make_tuple(location.get_type(), location.get_id().get()));
}
return location_ids;
}
Expand Down
37 changes: 19 additions & 18 deletions cpp/examples/abm_minimal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "abm/abm.h"
#include "abm/household.h"
#include <fstream>
#include <string>
#include <iostream>
#include "abm/lockdown_rules.h"
#include "abm/world.h"
#include "abm/common_abm_loggers.h"

#include <fstream>

int main()
{
// This is a minimal example with children and adults < 60 year old.
Expand Down Expand Up @@ -79,26 +79,26 @@ int main()
// Add one social event with 5 maximum contacts.
// Maximum contacs limit the number of people that a person can infect while being at this location.
auto event = world.add_location(mio::abm::LocationType::SocialEvent);
world.get_individualized_location(event).get_infection_parameters().set<mio::abm::MaximumContacts>(5);
world.get_location(event).get_infection_parameters().set<mio::abm::MaximumContacts>(5);
// Add hospital and ICU with 5 maximum contacs.
auto hospital = world.add_location(mio::abm::LocationType::Hospital);
world.get_individualized_location(hospital).get_infection_parameters().set<mio::abm::MaximumContacts>(5);
world.get_location(hospital).get_infection_parameters().set<mio::abm::MaximumContacts>(5);
auto icu = world.add_location(mio::abm::LocationType::ICU);
world.get_individualized_location(icu).get_infection_parameters().set<mio::abm::MaximumContacts>(5);
world.get_location(icu).get_infection_parameters().set<mio::abm::MaximumContacts>(5);
// Add one supermarket, maximum constacts are assumed to be 20.
auto shop = world.add_location(mio::abm::LocationType::BasicsShop);
world.get_individualized_location(shop).get_infection_parameters().set<mio::abm::MaximumContacts>(20);
world.get_location(shop).get_infection_parameters().set<mio::abm::MaximumContacts>(20);
// At every school, the maximum contacts are 20.
auto school = world.add_location(mio::abm::LocationType::School);
world.get_individualized_location(school).get_infection_parameters().set<mio::abm::MaximumContacts>(20);
world.get_location(school).get_infection_parameters().set<mio::abm::MaximumContacts>(20);
// At every workplace, maximum contacts are 20.
auto work = world.add_location(mio::abm::LocationType::Work);
world.get_individualized_location(work).get_infection_parameters().set<mio::abm::MaximumContacts>(20);
world.get_location(work).get_infection_parameters().set<mio::abm::MaximumContacts>(20);

// Increase aerosol transmission for all locations
world.parameters.get<mio::abm::AerosolTransmissionRates>() = 10.0;
// Increase contact rate for all people between 15 and 34 (i.e. people meet more often in the same location)
world.get_individualized_location(work)
world.get_location(work)
.get_infection_parameters()
.get<mio::abm::ContactRates>()[{age_group_15_to_34, age_group_15_to_34}] = 10.0;

Expand All @@ -119,7 +119,7 @@ int main()
for (auto& person : world.get_persons()) {
mio::abm::InfectionState infection_state = mio::abm::InfectionState(
mio::DiscreteDistribution<size_t>::get_instance()(mio::thread_local_rng(), infection_distribution));
auto rng = mio::abm::Person::RandomNumberGenerator(world.get_rng(), person);
auto rng = mio::abm::PersonalRandomNumberGenerator(world.get_rng(), person);
if (infection_state != mio::abm::InfectionState::Susceptible) {
person.add_new_infection(mio::abm::Infection(rng, mio::abm::VirusVariant::Wildtype, person.get_age(),
world.parameters, start_date, infection_state));
Expand All @@ -128,18 +128,19 @@ int main()

// Assign locations to the people
for (auto& person : world.get_persons()) {
const auto id = person.get_id();
//assign shop and event
person.set_assigned_location(event);
person.set_assigned_location(shop);
world.assign_location(id, event);
world.assign_location(id, shop);
//assign hospital and ICU
person.set_assigned_location(hospital);
person.set_assigned_location(icu);
world.assign_location(id, hospital);
world.assign_location(id, icu);
//assign work/school to people depending on their age
if (person.get_age() == age_group_0_to_4) {
person.set_assigned_location(school);
world.assign_location(id, school);
}
if (person.get_age() == age_group_15_to_34 || person.get_age() == age_group_35_to_59) {
person.set_assigned_location(work);
world.assign_location(id, work);
}
}

Expand Down
14 changes: 8 additions & 6 deletions cpp/memilio/utils/custom_index_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,10 @@
#ifndef CUSTOMINDEXARRAY_H
#define CUSTOMINDEXARRAY_H

#include "memilio/config.h"
#include "memilio/math/eigen.h"
#include "memilio/math/eigen_util.h"
#include "memilio/utils/index.h"
#include "memilio/utils/stl_util.h"

#include <vector>
#include <array>
#include <numeric>

namespace
{

Expand Down Expand Up @@ -165,6 +159,14 @@ class CustomIndexArray
using Index = ::mio::Index<Tags...>;
using InternalArrayType = Eigen::Array<Type, Eigen::Dynamic, 1>;

/// @brief Create an empty CustomIndexArray with size 0. Use the resize member function to add entries.
explicit CustomIndexArray()
: m_dimensions(Index::Zero())
, m_numel(0)
, m_y()
{
}

/**
* @brief CustomIndexArray constructor, that initializes the array
* to constant instances of `CustsomIndexArray::Type`.
Expand Down
2 changes: 1 addition & 1 deletion cpp/memilio/utils/random_number_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

namespace mio
{

RandomNumberGenerator& thread_local_rng()
{
static thread_local auto rng = RandomNumberGenerator();
Expand Down
15 changes: 9 additions & 6 deletions cpp/memilio/utils/random_number_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
* limitations under the License.
*/

#ifndef EPI_ABM_RANDOM_NUMBER_GENERATOR_H
#define EPI_ABM_RANDOM_NUMBER_GENERATOR_H
#ifndef MIO_RANDOM_NUMBER_GENERATOR_H
#define MIO_RANDOM_NUMBER_GENERATOR_H

#include "memilio/utils/compiler_diagnostics.h"
#include "memilio/utils/logging.h"
Expand Down Expand Up @@ -182,7 +182,9 @@ static_assert(sizeof(Key<uint32_t>) == sizeof(uint32_t), "Empty Base Optimizatio
* @tparam an unsigned integer type that determines the size of the counter, i.e., the length of the random sequence.
*/
template <class T>
struct MEMILIO_ENABLE_EBO Counter : TypeSafe<T, Counter<T>>, OperatorComparison<Counter<T>>, OperatorAdditionSubtraction<Counter<T>> {
struct MEMILIO_ENABLE_EBO Counter : TypeSafe<T, Counter<T>>,
OperatorComparison<Counter<T>>,
OperatorAdditionSubtraction<Counter<T>> {
static_assert(std::is_unsigned<T>::value, "Underlying Integer type must be unsigned.");
using TypeSafe<T, Counter<T>>::TypeSafe;
};
Expand Down Expand Up @@ -243,12 +245,13 @@ Counter<UIntC> rng_totalsequence_counter(UIntN subsequence_idx, CounterS counter
static_assert(N_BITS <= C_BITS, "Subsequence index must not be bigger than total sequence counter.");
static_assert(N_BITS <= sizeof(UIntN) * BITS_PER_BYTE, "Subsequence index must be at least N bits");

assert(UIntC(subsequence_idx) <= (UIntC(1) << N_BITS) && "Subsequence index is too large."); //(1 << N) is the same as (2^N)
assert(UIntC(subsequence_idx) <= (UIntC(1) << N_BITS) &&
"Subsequence index is too large."); //(1 << N) is the same as (2^N)

//N high bits: subsequence idx
//S low bits: subsequence counter
//=> C = N + S bits: total sequence counter
//example:
//example:
//subsequence index uint32_t(181) = 0x000000B5
//subsequence counter uint32_t(41309) = 0x0000A15D
//total sequence counter = 0x000000B50000A15D
Expand All @@ -271,7 +274,7 @@ Counter<UIntC> rng_totalsequence_counter(UIntN subsequence_idx, CounterS counter
template <class UIntS, class CounterC>
Counter<UIntS> rng_subsequence_counter(CounterC counter)
{
using UIntC = typename CounterC::ValueType;
using UIntC = typename CounterC::ValueType;
static const UIntC C_BYTES = sizeof(UIntC);
static const UIntC S_BYTES = sizeof(UIntS);

Expand Down
6 changes: 6 additions & 0 deletions cpp/models/abm/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
add_library(abm
location.cpp
location.h
location_id.h
household.cpp
household.h
simulation.cpp
simulation.h
person.cpp
person.h
person_id.h
personal_rng.cpp
personal_rng.h
testing_strategy.cpp
testing_strategy.h
world.cpp
Expand All @@ -16,6 +20,8 @@ add_library(abm
parameters.cpp
migration_rules.cpp
migration_rules.h
model_functions.cpp
model_functions.h
trip_list.cpp
trip_list.h
lockdown_rules.cpp
Expand Down
3 changes: 0 additions & 3 deletions cpp/models/abm/analyze_result.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@
#ifndef ABM_ANALYZE_RESULT_H
#define ABM_ANALYZE_RESULT_H

#include "abm/simulation.h"
#include "abm/parameters.h"
#include "memilio/data/analyze_result.h"

#include <functional>
#include <vector>

namespace mio
Expand Down
Loading