Stride Reference Manual  - generated for commit 9643b11
DefaultPopBuilder.cpp
Go to the documentation of this file.
1 /*
2  * This is free software: you can redistribute it and/or modify it
3  * under the terms of the GNU General Public License as published by
4  * the Free Software Foundation, either version 3 of the License, or
5  * any later version.
6  * The software is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  * You should have received a copy of the GNU General Public License
11  * along with the software. If not, see <http://www.gnu.org/licenses/>.
12  *
13  * Copyright 2017, 2018, Kuylen E, Willem L, Broeckhove J
14  * Copyright 2018, 2019 Jan Broeckhove and Bistromatics group.
15  */
16 
22 #include "DefaultPopBuilder.h"
23 
24 #include "contact/ContactLogMode.h"
25 #include "contact/ContactType.h"
27 #include "pop/Population.h"
28 #include "pop/SurveySeeder.h"
29 #include "util/FileSys.h"
30 #include "util/RnMan.h"
31 #include "util/StringUtils.h"
32 
33 #include <boost/property_tree/ptree.hpp>
34 #include <fstream>
35 
36 namespace stride {
37 
38 using namespace ContactType;
39 
40 using namespace util;
41 using namespace boost::property_tree;
42 using namespace std;
43 
44 shared_ptr<Population> DefaultPopBuilder::MakePersons(shared_ptr<Population> pop)
45 {
46  //------------------------------------------------
47  // Read persons from file.
48  //------------------------------------------------
49  const auto fileName = m_config.get<string>("run.population_file");
50  m_stride_logger->info("Building default population from file {}.", fileName);
51 
52  const auto use_install_dirs = m_config.get<bool>("run.use_install_dirs");
53  const auto filePath = (use_install_dirs) ? FileSys::GetDataDir() /= fileName : filesys::path(fileName);
54  if (!is_regular_file(filePath)) {
55  throw runtime_error(string(__func__) + "> Population file " + filePath.string() + " not present.");
56  }
57 
58  ifstream popFile;
59  popFile.open(filePath.string());
60  if (!popFile.is_open()) {
61  throw runtime_error(string(__func__) + "> Error opening population file " + filePath.string());
62  }
63 
64  string line;
65  getline(popFile, line); // step over file header
66  unsigned int person_id = 0U;
67 
68  while (getline(popFile, line)) {
69  const auto values = Split(line, ",");
70  const auto age = FromString<unsigned int>(values[0]);
71  const auto householdId = FromString<unsigned int>(values[1]);
72  const auto schoolId = FromString<unsigned int>(values[2]);
73  const auto workId = FromString<unsigned int>(values[3]);
74  const auto primaryCommunityId = FromString<unsigned int>(values[4]);
75  const auto secondaryCommunityId = FromString<unsigned int>(values[5]);
76 
77  pop->CreatePerson(person_id, age, householdId, schoolId, 0, workId, primaryCommunityId,
78  secondaryCommunityId);
79  ++person_id;
80  }
81 
82  popFile.close();
83 
84  m_stride_logger->trace("Done building default population.");
85 
86  return pop;
87 }
88 
89 shared_ptr<Population> DefaultPopBuilder::Build(shared_ptr<Population> pop)
90 {
91  //------------------------------------------------
92  // Check validity of input data.
93  //------------------------------------------------
94  boost::optional<float> seeding_rate = m_config.get_optional<float>("run.seeding_rate");
95  if (seeding_rate) {
96  if (*seeding_rate > 1.0) {
97  throw runtime_error(string(__func__) + "> Bad input data for seeding_rate.");
98  }
99  }
100 
101  //------------------------------------------------
102  // Add persons
103  //------------------------------------------------
104  MakePersons(pop);
105 
106 
107  // --------------------------------------------------------------
108  // Determine maximum pool ids in population.
109  // --------------------------------------------------------------
111  for (const auto& p : *pop) {
112  for (Id typ : IdList) {
113  maxIds[typ] = max(maxIds[typ], p.GetPoolId(typ));
114  }
115  }
116  // --------------------------------------------------------------
117  // Initialize poolSys with empty ContactPools (even for Id=0).
118  // --------------------------------------------------------------
119  for (Id typ : IdList) {
120  for (unsigned int i = 1; i < maxIds[typ] + 1; i++) {
121  pop->RefPoolSys().CreateContactPool(typ);
122  }
123  }
124 
125  // --------------------------------------------------------------
126  // Insert persons (pointers) in their contactpools. Having Id 0
127  // means "not belonging pool of that type" (e.g. school/ work -
128  // cannot belong to both, or e.g. out-of-work).
129  //
130  // Pools are uniquely identified by (typ, subscript) and a Person
131  // belongs, for typ, to pool with subscript p.GetPoolId(typ).
132  // Defensive measure: we have a pool for Id 0 and leave it empty.
133  // --------------------------------------------------------------
134  for (auto& p : *pop) {
135  for (Id typ : IdList) {
136  const auto poolId = p.GetPoolId(typ);
137  if (poolId > 0) {
138  pop->RefPoolSys().RefPools(typ)[poolId].AddMember(&p);
139  }
140  }
141  }
142 
143 
144  return pop;
145 }
146 
147 } // namespace stride
static filesys::path GetDataDir()
/// Return data dir (only relevant when use_install_dirs mode is active)
Definition: FileSys.h:88
constexpr std::initializer_list< Id > IdList
To allow iteration over the type ids.
Definition: ContactType.h:75
std::shared_ptr< Population > MakePersons(std::shared_ptr< Population > pop)
Generates pop&#39;s individuals and return pop.
std::shared_ptr< Population > Build(std::shared_ptr< Population > pop) override
Build Population and return it afterwards.
STL namespace.
std::vector< std::string > Split(const std::string &s, const std::string &delimiters)
Split a string (in order of occurence) by splitting it on the given delimiters.
Definition: StringUtils.h:58
Interface of RnMan.
Definition of ContactPool Id Type.
Container for the contact pools of various type (household, work, ...).
Header for the ContactLogMode class.
Header file for the SurveySeeder class.
Header file for the core Population class.
Initialize populations.
Miscellaneous string utilities.
Interface for install directory queries.
Namespace for the simulator and related classes.
Definition: Calendar.cpp:28