Skip to content

Commit 2fde9bd

Browse files
committed
formatting and problems in abm tutorial
1 parent f859b60 commit 2fde9bd

3 files changed

Lines changed: 67 additions & 75 deletions

File tree

cpp-tutorials/abm/parameter_setter.cpp

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,59 +10,63 @@ const auto age_group_35_to_59 = mio::AgeGroup(3);
1010
const auto age_group_60_to_79 = mio::AgeGroup(4);
1111
const auto age_group_80_plus = mio::AgeGroup(5);
1212

13-
std::pair<double, double> get_my_and_sigma(std::pair<double, double> mean_and_std)
13+
std::pair<double, double> get_mu_and_sigma(std::pair<double, double> mean_and_std)
1414
{
1515
auto mean = mean_and_std.first;
1616
auto stddev = mean_and_std.second;
17-
double my = log(mean * mean / sqrt(mean * mean + stddev * stddev));
17+
double mu = log(mean * mean / sqrt(mean * mean + stddev * stddev));
1818
double sigma = sqrt(log(1 + stddev * stddev / (mean * mean)));
19-
return {my, sigma};
19+
return {mu, sigma};
2020
}
2121

2222
void set_world_parameters(mio::abm::Parameters& params)
2323
{
24-
auto incubation_period_my_sigma = get_my_and_sigma({4.5, 1.5});
25-
params.get<mio::abm::TimeExposedToNoSymptoms>() = mio::ParameterDistributionLogNormal(incubation_period_my_sigma.first, incubation_period_my_sigma.second);
24+
auto incubation_period_mu_sigma = get_mu_and_sigma({4.5, 1.5});
25+
params.get<mio::abm::TimeExposedToNoSymptoms>() =
26+
mio::ParameterDistributionLogNormal(incubation_period_mu_sigma.first, incubation_period_mu_sigma.second);
2627

27-
auto InfectedNoSymptoms_to_symptoms_my_sigma = get_my_and_sigma({1.1, 0.9});
28-
params.get<mio::abm::TimeInfectedNoSymptomsToSymptoms>() = mio::ParameterDistributionLogNormal(InfectedNoSymptoms_to_symptoms_my_sigma.first,
29-
InfectedNoSymptoms_to_symptoms_my_sigma.second);
28+
auto InfectedNoSymptoms_to_symptoms_mu_sigma = get_mu_and_sigma({1.1, 0.9});
29+
params.get<mio::abm::TimeInfectedNoSymptomsToSymptoms>() = mio::ParameterDistributionLogNormal(
30+
InfectedNoSymptoms_to_symptoms_mu_sigma.first, InfectedNoSymptoms_to_symptoms_mu_sigma.second);
3031

31-
auto TimeInfectedNoSymptomsToRecovered_my_sigma = get_my_and_sigma({8.0, 2.0});
32-
params.get<mio::abm::TimeInfectedNoSymptomsToRecovered>() = mio::ParameterDistributionLogNormal(TimeInfectedNoSymptomsToRecovered_my_sigma.first,
33-
TimeInfectedNoSymptomsToRecovered_my_sigma.second);
32+
auto TimeInfectedNoSymptomsToRecovered_mu_sigma = get_mu_and_sigma({8.0, 2.0});
33+
params.get<mio::abm::TimeInfectedNoSymptomsToRecovered>() = mio::ParameterDistributionLogNormal(
34+
TimeInfectedNoSymptomsToRecovered_mu_sigma.first, TimeInfectedNoSymptomsToRecovered_mu_sigma.second);
3435

35-
auto TimeInfectedSymptomsToSevere_my_sigma = get_my_and_sigma({6.6, 4.9});
36-
params.get<mio::abm::TimeInfectedSymptomsToSevere>() = mio::ParameterDistributionLogNormal(TimeInfectedSymptomsToSevere_my_sigma.first,
37-
TimeInfectedSymptomsToSevere_my_sigma.second);
36+
auto TimeInfectedSymptomsToSevere_mu_sigma = get_mu_and_sigma({6.6, 4.9});
37+
params.get<mio::abm::TimeInfectedSymptomsToSevere>() = mio::ParameterDistributionLogNormal(
38+
TimeInfectedSymptomsToSevere_mu_sigma.first, TimeInfectedSymptomsToSevere_mu_sigma.second);
3839

39-
auto TimeInfectedSymptomsToRecovered_my_sigma = get_my_and_sigma({8.0, 2.0});
40-
params.get<mio::abm::TimeInfectedSymptomsToRecovered>() = mio::ParameterDistributionLogNormal(TimeInfectedSymptomsToRecovered_my_sigma.first,
41-
TimeInfectedSymptomsToRecovered_my_sigma.second);
40+
auto TimeInfectedSymptomsToRecovered_mu_sigma = get_mu_and_sigma({8.0, 2.0});
41+
params.get<mio::abm::TimeInfectedSymptomsToRecovered>() = mio::ParameterDistributionLogNormal(
42+
TimeInfectedSymptomsToRecovered_mu_sigma.first, TimeInfectedSymptomsToRecovered_mu_sigma.second);
4243

43-
auto TimeInfectedSevereToCritical_my_sigma = get_my_and_sigma({1.5, 2.0});
44-
params.get<mio::abm::TimeInfectedSevereToCritical>() = mio::ParameterDistributionLogNormal(TimeInfectedSevereToCritical_my_sigma.first,
45-
TimeInfectedSevereToCritical_my_sigma.second);
44+
auto TimeInfectedSevereToCritical_mu_sigma = get_mu_and_sigma({1.5, 2.0});
45+
params.get<mio::abm::TimeInfectedSevereToCritical>() = mio::ParameterDistributionLogNormal(
46+
TimeInfectedSevereToCritical_mu_sigma.first, TimeInfectedSevereToCritical_mu_sigma.second);
4647

47-
auto TimeInfectedSevereToRecovered_my_sigma = get_my_and_sigma({18.1, 6.3});
48-
params.get<mio::abm::TimeInfectedSevereToRecovered>() = mio::ParameterDistributionLogNormal(TimeInfectedSevereToRecovered_my_sigma.first,
49-
TimeInfectedSevereToRecovered_my_sigma.second);
48+
auto TimeInfectedSevereToRecovered_mu_sigma = get_mu_and_sigma({18.1, 6.3});
49+
params.get<mio::abm::TimeInfectedSevereToRecovered>() = mio::ParameterDistributionLogNormal(
50+
TimeInfectedSevereToRecovered_mu_sigma.first, TimeInfectedSevereToRecovered_mu_sigma.second);
5051

51-
auto TimeInfectedCriticalToDead_my_sigma = get_my_and_sigma({10.7, 4.8});
52-
params.get<mio::abm::TimeInfectedCriticalToDead>() = mio::ParameterDistributionLogNormal(TimeInfectedCriticalToDead_my_sigma.first,
53-
TimeInfectedCriticalToDead_my_sigma.second);
52+
auto TimeInfectedCriticalToDead_mu_sigma = get_mu_and_sigma({10.7, 4.8});
53+
params.get<mio::abm::TimeInfectedCriticalToDead>() = mio::ParameterDistributionLogNormal(
54+
TimeInfectedCriticalToDead_mu_sigma.first, TimeInfectedCriticalToDead_mu_sigma.second);
5455

55-
auto TimeInfectedCriticalToRecovered_my_sigma = get_my_and_sigma({18.1, 6.3});
56-
params.get<mio::abm::TimeInfectedCriticalToRecovered>() = mio::ParameterDistributionLogNormal(TimeInfectedCriticalToRecovered_my_sigma.first,
57-
TimeInfectedCriticalToRecovered_my_sigma.second);
56+
auto TimeInfectedCriticalToRecovered_mu_sigma = get_mu_and_sigma({18.1, 6.3});
57+
params.get<mio::abm::TimeInfectedCriticalToRecovered>() = mio::ParameterDistributionLogNormal(
58+
TimeInfectedCriticalToRecovered_mu_sigma.first, TimeInfectedCriticalToRecovered_mu_sigma.second);
5859

5960
// Set percentage parameters
60-
params.get<mio::abm::SymptomsPerInfectedNoSymptoms>()[{mio::abm::VirusVariant::Wildtype, age_group_0_to_4}] = 0.50;
61-
params.get<mio::abm::SymptomsPerInfectedNoSymptoms>()[{mio::abm::VirusVariant::Wildtype, age_group_5_to_14}] = 0.55;
62-
params.get<mio::abm::SymptomsPerInfectedNoSymptoms>()[{mio::abm::VirusVariant::Wildtype, age_group_15_to_34}] = 0.60;
63-
params.get<mio::abm::SymptomsPerInfectedNoSymptoms>()[{mio::abm::VirusVariant::Wildtype, age_group_35_to_59}] = 0.70;
64-
params.get<mio::abm::SymptomsPerInfectedNoSymptoms>()[{mio::abm::VirusVariant::Wildtype, age_group_60_to_79}] = 0.83;
65-
params.get<mio::abm::SymptomsPerInfectedNoSymptoms>()[{mio::abm::VirusVariant::Wildtype, age_group_80_plus}] = 0.90;
61+
params.get<mio::abm::SymptomsPerInfectedNoSymptoms>()[{mio::abm::VirusVariant::Wildtype, age_group_0_to_4}] = 0.50;
62+
params.get<mio::abm::SymptomsPerInfectedNoSymptoms>()[{mio::abm::VirusVariant::Wildtype, age_group_5_to_14}] = 0.55;
63+
params.get<mio::abm::SymptomsPerInfectedNoSymptoms>()[{mio::abm::VirusVariant::Wildtype, age_group_15_to_34}] =
64+
0.60;
65+
params.get<mio::abm::SymptomsPerInfectedNoSymptoms>()[{mio::abm::VirusVariant::Wildtype, age_group_35_to_59}] =
66+
0.70;
67+
params.get<mio::abm::SymptomsPerInfectedNoSymptoms>()[{mio::abm::VirusVariant::Wildtype, age_group_60_to_79}] =
68+
0.83;
69+
params.get<mio::abm::SymptomsPerInfectedNoSymptoms>()[{mio::abm::VirusVariant::Wildtype, age_group_80_plus}] = 0.90;
6670

6771
params.get<mio::abm::SeverePerInfectedSymptoms>()[{mio::abm::VirusVariant::Wildtype, age_group_0_to_4}] = 0.02;
6872
params.get<mio::abm::SeverePerInfectedSymptoms>()[{mio::abm::VirusVariant::Wildtype, age_group_5_to_14}] = 0.03;
@@ -87,7 +91,7 @@ void set_world_parameters(mio::abm::Parameters& params)
8791

8892
// Set infection parameters
8993
params.get<mio::abm::InfectionRateFromViralShed>()[{mio::abm::VirusVariant::Wildtype}] = 5.0;
90-
params.get<mio::abm::AerosolTransmissionRates>() = 0.0;
94+
params.get<mio::abm::AerosolTransmissionRates>() = 0.0;
9195
}
9296

9397
void set_local_parameters(mio::abm::Model& world)

cpp-tutorials/abm/parameter_setter.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#pragma once
22
#include "abm/model.h"
33

4-
54
void set_world_parameters(mio::abm::Parameters& params);
65
void set_local_parameters(mio::abm::Model& world);
76
std::pair<double, double> get_my_and_sigma(std::pair<double, double> mean_and_std);

cpp-tutorials/abm/tutorial_abm_households.cpp

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@
5555
int main(int argc, char* argv[])
5656
{
5757
// Usage: tutorial_abm_household [n_households] [infected_frac] [sim_days]
58-
// n_households : number of each household type (default: 125)
58+
// n_households : number of each household type (default: 125)
5959
// infected_frac : fraction initially infected (default: 0.2)
6060
// sim_days : simulation duration in days (default: 30)
61-
int arg_n_households = (argc > 1) ? std::atoi(argv[1]) : 125;
61+
int arg_n_households = (argc > 1) ? std::atoi(argv[1]) : 125;
6262
double arg_infected_frac = (argc > 2) ? std::atof(argv[2]) : 0.2;
63-
int arg_sim_days = (argc > 3) ? std::atoi(argv[3]) : 30;
63+
int arg_sim_days = (argc > 3) ? std::atoi(argv[3]) : 30;
6464

6565
// Suppress verbose log output; only warnings and errors are shown.
6666
mio::set_log_level(mio::LogLevel::warn);
@@ -70,7 +70,7 @@ int main(int argc, char* argv[])
7070
// one group when created. The number and meaning of groups must be
7171
// consistent throughout the entire setup.
7272
size_t num_age_groups = 6;
73-
const auto age_group_0_to_4 = mio::AgeGroup(0); // toddlers / kindergarten
73+
const auto age_group_0_to_4 = mio::AgeGroup(0); // toddlers / kindergarden
7474
const auto age_group_5_to_14 = mio::AgeGroup(1); // school children
7575
const auto age_group_15_to_34 = mio::AgeGroup(2); // young adults
7676
const auto age_group_35_to_59 = mio::AgeGroup(3); // middle-aged adults
@@ -92,9 +92,7 @@ int main(int argc, char* argv[])
9292
model.parameters.get<mio::abm::AgeGroupGotoSchool>() = false;
9393
model.parameters.get<mio::abm::AgeGroupGotoSchool>()[age_group_5_to_14] = true;
9494

95-
model.parameters.get<mio::abm::AgeGroupGotoWork>().set_multiple(
96-
{age_group_15_to_34, age_group_35_to_59}, true);
97-
95+
model.parameters.get<mio::abm::AgeGroupGotoWork>().set_multiple({age_group_15_to_34, age_group_35_to_59}, true);
9896

9997
// *** Define HouseholdMember types. ***
10098
// A HouseholdMember is not a person itself; it is a *template* that
@@ -103,7 +101,7 @@ int main(int argc, char* argv[])
103101

104102
// child: equally likely to be 0-4 or 5-14 years old (weights 1 and 1).
105103
auto child = mio::abm::HouseholdMember(num_age_groups);
106-
child.set_age_weight(age_group_0_to_4, 1);
104+
child.set_age_weight(age_group_0_to_4, 1);
107105
child.set_age_weight(age_group_5_to_14, 1);
108106

109107
// parent: equally likely to be 15-34 or 35-59 years old.
@@ -134,7 +132,7 @@ int main(int argc, char* argv[])
134132

135133
// --- Type A: two-person household (1 parent + 1 child) -------------------
136134
auto twoPersonHousehold = mio::abm::Household();
137-
twoPersonHousehold.add_members(child, 1);
135+
twoPersonHousehold.add_members(child, 1);
138136
twoPersonHousehold.add_members(parent, 1);
139137

140138
auto twoPersonGroup = mio::abm::HouseholdGroup();
@@ -143,7 +141,7 @@ int main(int argc, char* argv[])
143141

144142
// --- Type B: three-person household (2 parents + 1 child) ----------------
145143
auto threePersonHousehold = mio::abm::Household();
146-
threePersonHousehold.add_members(child, 1);
144+
threePersonHousehold.add_members(child, 1);
147145
threePersonHousehold.add_members(parent, 2);
148146

149147
auto threePersonGroup = mio::abm::HouseholdGroup();
@@ -169,12 +167,11 @@ int main(int argc, char* argv[])
169167
// visit: a hospital, an ICU, a social event venue, a shop, a school, and
170168
// a workplace. The returned LocationId is used to assign persons later.
171169

172-
173170
// One hospital and one ICU shared by all persons.
174171
auto hospital = model.add_location(mio::abm::LocationType::Hospital);
175-
auto icu = model.add_location(mio::abm::LocationType::ICU);
172+
auto icu = model.add_location(mio::abm::LocationType::ICU);
176173

177-
// One social event venue (e.g. a community centre).
174+
// One social event venue (e.g., a community center).
178175
auto event = model.add_location(mio::abm::LocationType::SocialEvent);
179176

180177
// One supermarket. Groceries shops allow up to 20 simultaneous contacts.
@@ -186,43 +183,38 @@ int main(int argc, char* argv[])
186183
// One workplace for all working adults.
187184
auto work = model.add_location(mio::abm::LocationType::Work);
188185

189-
190186
// *** Assign initial infection states. ***
191187
// Each person draws a random infection state from the distribution below.
192188
// Persons who are not Susceptible receive a full Infection object so their
193189
// viral-load course and state transitions are properly initialised.
194190
//
195-
// Index | InfectionState | Probability
196-
// ------|-------------------------|------------
197-
// 0 | Susceptible | 0.80
198-
// 1 | Exposed | 0.10
199-
// 2 | InfectedNoSymptoms | 0.01
200-
// 3 | InfectedSymptoms | 0.01
201-
// 4 | InfectedSevere | 0.01
202-
// 5 | InfectedCritical | 0.01
203-
// 6 | Recovered | 0.00
204-
// 7 | Dead | 0.06
191+
// Index | InfectionState | Probability
192+
// ------|--------------------------------|------------
193+
// 0 | Susceptible | 1.0 - arg_infected_frac
194+
// 1 | Exposed | 0.25 * arg_infected_frac
195+
// 2 | InfectedNoSymptoms (I_NS) | 0.5 * arg_infected_frac
196+
// 3 | InfectedSymptoms (I_Sy) | 0.25 * arg_infected_frac
197+
// 4 | InfectedSevere (I_Sev) | 0.0
198+
// 5 | InfectedCritical (I_Crit) | 0.0
199+
// 6 | Recovered | 0.0
200+
// 7 | Dead | 0.0
205201
auto start_date = mio::abm::TimePoint(0); // t = 0 s from the simulation epoch
206202

207203
// Build infection distribution from the infected fraction.
208204
// The non-susceptible portion is split: 25% Exposed, 50% I_NS, 25% I_Sy.
209205
const double f = arg_infected_frac;
210-
std::vector<ScalarType> infection_distribution{
211-
1.0 - f, f * 0.25, f * 0.50, f * 0.25, 0.0, 0.0, 0.0, 0.0};
206+
std::vector<ScalarType> infection_distribution{1.0 - f, f * 0.25, f * 0.50, f * 0.25, 0.0, 0.0, 0.0, 0.0};
212207

213208
for (auto& person : model.get_persons()) {
214209
// Draw an infection state from the distribution above.
215210
mio::abm::InfectionState infection_state = mio::abm::InfectionState(
216-
mio::DiscreteDistribution<size_t>::get_instance()(
217-
mio::thread_local_rng(), infection_distribution));
218-
211+
mio::DiscreteDistribution<size_t>::get_instance()(mio::thread_local_rng(), infection_distribution));
212+
219213
auto rng = mio::abm::PersonalRandomNumberGenerator(person);
220214
if (infection_state != mio::abm::InfectionState::Susceptible) {
221215
// Infect an agent with the drawn state
222-
person.add_new_infection(
223-
mio::abm::Infection(rng, mio::abm::VirusVariant::Wildtype,
224-
person.get_age(), model.parameters,
225-
start_date, infection_state));
216+
person.add_new_infection(mio::abm::Infection(rng, mio::abm::VirusVariant::Wildtype, person.get_age(),
217+
model.parameters, start_date, infection_state));
226218
}
227219
}
228220

@@ -244,8 +236,7 @@ int main(int argc, char* argv[])
244236
if (person.get_age() == age_group_5_to_14) {
245237
model.assign_location(id, school);
246238
}
247-
if (person.get_age() == age_group_15_to_34 ||
248-
person.get_age() == age_group_35_to_59) {
239+
if (person.get_age() == age_group_15_to_34 || person.get_age() == age_group_35_to_59) {
249240
model.assign_location(id, work);
250241
}
251242
}
@@ -266,9 +257,7 @@ int main(int argc, char* argv[])
266257
// *** Write results to file. ***
267258
std::ofstream outfile("abm_household.txt");
268259
std::get<0>(historyTimeSeries.get_log())
269-
.print_table(outfile,
270-
{"S", "E", "I_NS", "I_Sy", "I_Sev", "I_Crit", "R", "D"},
271-
7, 4);
260+
.print_table(outfile, {"S", "E", "I_NS", "I_Sy", "I_Sev", "I_Crit", "R", "D"}, 7, 4);
272261
std::cout << "Results written to abm_household.txt\n";
273262

274263
return 0;

0 commit comments

Comments
 (0)