Skip to content

Commit 5f7fb0d

Browse files
khoanguyen-devreneSchm
authored andcommitted
869 default values of agegroupgotoschoolwork abm parameters (#973)
1 parent e5dd173 commit 5f7fb0d

File tree

10 files changed

+86
-15
lines changed

10 files changed

+86
-15
lines changed

cpp/examples/abm_history_object.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ int main()
7272
// Set same infection parameter for all age groups. For example, the incubation period is 4 days.
7373
world.parameters.get<mio::abm::IncubationPeriod>() = 4.;
7474

75+
// Set the age group the can go to school is AgeGroup(1) (i.e. 5-14)
76+
world.parameters.get<mio::abm::AgeGroupGotoSchool>()[age_group_5_to_14] = true;
77+
// Set the age group the can go to work is AgeGroup(2) and AgeGroup(3) (i.e. 15-34 and 35-59)
78+
world.parameters.get<mio::abm::AgeGroupGotoWork>().set_multiple({age_group_15_to_34, age_group_35_to_59}, true);
79+
80+
7581
// There are 3 households for each household group.
7682
int n_households = 3;
7783

cpp/examples/abm_minimal.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,9 @@ int main()
4141
world.parameters.get<mio::abm::IncubationPeriod>() = 4.;
4242

4343
// Set the age group the can go to school is AgeGroup(1) (i.e. 5-14)
44-
world.parameters.get<mio::abm::AgeGroupGotoSchool>() = false;
4544
world.parameters.get<mio::abm::AgeGroupGotoSchool>()[age_group_5_to_14] = true;
4645
// Set the age group the can go to work is AgeGroup(2) and AgeGroup(3) (i.e. 15-34 and 35-59)
47-
world.parameters.get<mio::abm::AgeGroupGotoWork>() = false;
48-
world.parameters.get<mio::abm::AgeGroupGotoWork>()[age_group_15_to_34] = true;
49-
world.parameters.get<mio::abm::AgeGroupGotoWork>()[age_group_35_to_59] = true;
46+
world.parameters.get<mio::abm::AgeGroupGotoWork>().set_multiple({age_group_15_to_34, age_group_35_to_59}, true);
5047

5148
// Check if the parameters satisfy their contraints.
5249
world.parameters.check_constraints();

cpp/memilio/utils/custom_index_array.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* Copyright (C) 2020-2024 MEmilio
33
*
4-
* Authors: Daniel Abele
4+
* Authors: Daniel Abele, Khoa Nguyen
55
*
66
* Contact: Martin J. Kuehn <Martin.Kuehn@DLR.de>
77
*
@@ -325,6 +325,18 @@ class CustomIndexArray
325325
return (Eigen::Index)flatten_index(index, m_dimensions);
326326
}
327327

328+
/**
329+
* @brief Set multiple entries to the same value.
330+
* @param indices A list of indices to be set to the same value.
331+
* @param value The value to set.
332+
*/
333+
void set_multiple(const std::vector<typename CustomIndexArray<Typ, Tags...>::Index>& indices, const Typ& value)
334+
{
335+
for (const auto& index : indices) {
336+
m_y[get_flat_index(index)] = value;
337+
}
338+
}
339+
328340
private:
329341
// Random Access Iterator for CustomIndexArray
330342
// To Do: As of Eigen 3.4, this is not needed anymore,

cpp/models/abm/parameters.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -513,9 +513,7 @@ struct AgeGroupGotoSchool {
513513
using Type = CustomIndexArray<bool, AgeGroup>;
514514
static Type get_default(AgeGroup num_agegroups)
515515
{
516-
auto a = Type(num_agegroups, false);
517-
a[AgeGroup(1)] = true;
518-
return a;
516+
return Type(num_agegroups, false);
519517
}
520518
static std::string name()
521519
{
@@ -530,10 +528,7 @@ struct AgeGroupGotoWork {
530528
using Type = CustomIndexArray<bool, AgeGroup>;
531529
static Type get_default(AgeGroup num_agegroups)
532530
{
533-
auto a = Type(num_agegroups, false);
534-
a[AgeGroup(2)] = true;
535-
a[AgeGroup(3)] = true;
536-
return a;
531+
return Type(num_agegroups, false);
537532
}
538533
static std::string name()
539534
{

cpp/simulations/abm.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,11 @@ void assign_infection_state(mio::abm::World& world, mio::abm::TimePoint t, doubl
462462

463463
void set_parameters(mio::abm::Parameters params)
464464
{
465+
// Set the age group the can go to school is AgeGroup(1) (i.e. 5-14)
466+
params.get<mio::abm::AgeGroupGotoSchool>()[age_group_5_to_14] = true;
467+
// Set the age group the can go to work is AgeGroup(2) and AgeGroup(3) (i.e. 15-34 and 35-59)
468+
params.get<mio::abm::AgeGroupGotoWork>().set_multiple({age_group_15_to_34, age_group_35_to_59}, true);
469+
465470
params.set<mio::abm::IncubationPeriod>({{mio::abm::VirusVariant::Count, mio::AgeGroup(num_age_groups)}, 4.});
466471

467472
// Set protection level from high viral load. Information based on: https://doi.org/10.1093/cid/ciaa886

cpp/simulations/abm_braunschweig.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,11 @@ void create_world_from_data(mio::abm::World& world, const std::string& filename,
392392

393393
void set_parameters(mio::abm::Parameters params)
394394
{
395+
// Set the age group the can go to school is AgeGroup(1) (i.e. 5-14)
396+
params.get<mio::abm::AgeGroupGotoSchool>()[age_group_5_to_14] = true;
397+
// Set the age group the can go to work is AgeGroup(2) and AgeGroup(3) (i.e. 15-34 and 35-59)
398+
params.get<mio::abm::AgeGroupGotoWork>().set_multiple({age_group_15_to_34, age_group_35_to_59}, true);
399+
395400
params.set<mio::abm::IncubationPeriod>({{mio::abm::VirusVariant::Count, mio::AgeGroup(num_age_groups)}, 4.});
396401

397402
// Set protection level from high viral load. Information based on: https://doi.org/10.1093/cid/ciaa886

cpp/tests/test_abm_location.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ TEST(TestLocation, reachCapacity)
6666
world.parameters
6767
.get<mio::abm::InfectedNoSymptomsToRecovered>()[{mio::abm::VirusVariant::Wildtype, age_group_15_to_34}] =
6868
2 * dt.days();
69+
world.parameters.get<mio::abm::AgeGroupGotoSchool>().set_multiple({age_group_5_to_14}, true);
70+
world.parameters.get<mio::abm::AgeGroupGotoWork>().set_multiple({age_group_15_to_34, age_group_35_to_59}, true);
6971

7072
auto home_id = world.add_location(mio::abm::LocationType::Home);
7173
auto school_id = world.add_location(mio::abm::LocationType::School);

cpp/tests/test_abm_person.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ TEST(TestPerson, quarantine)
118118
infection_parameters
119119
.get<mio::abm::InfectedSymptomsToRecovered>()[{mio::abm::VirusVariant::Wildtype, age_group_35_to_59}] =
120120
0.5 * dt.days();
121+
infection_parameters.get<mio::abm::AgeGroupGotoSchool>().set_multiple({age_group_5_to_14}, true);
122+
infection_parameters.get<mio::abm::AgeGroupGotoWork>().set_multiple({age_group_15_to_34, age_group_35_to_59}, true);
121123

122124
auto person = make_test_person(home, age_group_35_to_59, mio::abm::InfectionState::InfectedSymptoms, t_morning,
123125
infection_parameters);
@@ -126,11 +128,11 @@ TEST(TestPerson, quarantine)
126128
person.get_tested(rng_person, t_morning, test_params);
127129

128130
EXPECT_EQ(person.get_infection_state(t_morning), mio::abm::InfectionState::InfectedSymptoms);
129-
EXPECT_EQ(mio::abm::go_to_work(rng_person, person, t_morning, dt, mio::abm::Parameters(num_age_groups)),
131+
EXPECT_EQ(mio::abm::go_to_work(rng_person, person, t_morning, dt, infection_parameters),
130132
mio::abm::LocationType::Home);
131133
EXPECT_EQ(person.get_infection_state(t_morning + dt), mio::abm::InfectionState::Recovered);
132134
person.remove_quarantine();
133-
EXPECT_EQ(mio::abm::go_to_work(rng_person, person, t_morning, dt, mio::abm::Parameters(num_age_groups)),
135+
EXPECT_EQ(mio::abm::go_to_work(rng_person, person, t_morning, dt, infection_parameters),
134136
mio::abm::LocationType::Work);
135137
}
136138

cpp/tests/test_abm_world.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ TEST(TestWorld, evolveMigration) // TODO: this is not a unit test, this is a uni
185185
world.parameters
186186
.get<mio::abm::InfectedNoSymptomsToRecovered>()[{mio::abm::VirusVariant::Wildtype, age_group_15_to_34}] =
187187
2 * dt.days();
188+
world.parameters.get<mio::abm::AgeGroupGotoSchool>().set_multiple({age_group_5_to_14}, true);
189+
world.parameters.get<mio::abm::AgeGroupGotoWork>().set_multiple({age_group_15_to_34, age_group_35_to_59}, true);
188190

189191
auto home_id = world.add_location(mio::abm::LocationType::Home);
190192
auto school_id = world.add_location(mio::abm::LocationType::School);

cpp/tests/test_custom_index_array.cpp

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* Copyright (C) 2020-2024 MEmilio
33
*
4-
* Authors: Daniel Abele
4+
* Authors: Daniel Abele, Khoa Nguyen
55
*
66
* Contact: Martin J. Kuehn <Martin.Kuehn@DLR.de>
77
*
@@ -380,3 +380,48 @@ TEST(CustomIndexArray, resize_one_dimension)
380380
ASSERT_EQ(array.size().indices, std::make_tuple(Tag0{3}, Tag1{2}));
381381
ASSERT_EQ(array.numel(), 6);
382382
}
383+
384+
// Additional test for checking the set_multiple functionality
385+
TEST(CustomIndexArray, setMultiple_validIndices) {
386+
using ArrayType = mio::CustomIndexArray<int, Dim1, Dim2>;
387+
// Initialize with zeros
388+
ArrayType array({mio::Index<Dim1>(3), Dim2::Count}, 0);
389+
390+
// Define indices to set
391+
std::vector<ArrayType::Index> indices = {
392+
{mio::Index<Dim1>(0), Dim2::Male},
393+
{mio::Index<Dim1>(2), Dim2::Female}
394+
};
395+
396+
// Set these indices to 42
397+
array.set_multiple(indices, 42);
398+
399+
// Verify that the correct indices have been updated
400+
EXPECT_EQ((array[{mio::Index<Dim1>(0), Dim2::Male}]), 42);
401+
EXPECT_EQ((array[{mio::Index<Dim1>(2), Dim2::Female}]), 42);
402+
403+
// Verify that other indices are unchanged
404+
EXPECT_EQ((array[{mio::Index<Dim1>(0), Dim2::Female}]), 0);
405+
EXPECT_EQ((array[{mio::Index<Dim1>(1), Dim2::Male}]), 0);
406+
EXPECT_EQ((array[{mio::Index<Dim1>(1), Dim2::Female}]), 0);
407+
EXPECT_EQ((array[{mio::Index<Dim1>(2), Dim2::Male}]), 0);
408+
}
409+
410+
TEST(CustomIndexArray, setMultiple_emptyIndices) {
411+
using ArrayType = mio::CustomIndexArray<int, Dim1, Dim2>;
412+
// Initialize with fives
413+
ArrayType array({mio::Index<Dim1>(2), Dim2::Count}, 5);
414+
415+
// Empty vector of indices
416+
std::vector<ArrayType::Index> indices;
417+
418+
// Attempt to set multiple indices to 42
419+
array.set_multiple(indices, 42);
420+
421+
// Verify that all entries remain unchanged
422+
for (int age = 0; age < 2; ++age) {
423+
for (int gender = 0; gender < static_cast<int>(Dim2::Count); ++gender) {
424+
EXPECT_EQ((array[{mio::Index<Dim1>(age), static_cast<Dim2>(gender)}]), 5);
425+
}
426+
}
427+
}

0 commit comments

Comments
 (0)