Stride Reference Manual  - generated for commit 9643b11
GeoPopBuilder.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 2018, 2019 Jan Broeckhove and Bistromatics group.
14  */
15 
22 #include "GeoPopBuilder.h"
23 
24 #include "geopop/GeoGrid.h"
25 #include "geopop/GeoGridConfig.h"
29 #include "pop/Population.h"
30 #include "pop/SurveySeeder.h"
31 #include "util/FileSys.h"
32 
33 #include "util/RnMan.h"
34 #include "util/StringUtils.h"
35 
36 #include <boost/property_tree/ptree.hpp>
37 #include <contact/ContactType.h>
38 #include <spdlog/logger.h>
39 
40 namespace stride {
41 
42 using namespace std;
43 using namespace util;
44 using namespace ContactType;
45 using namespace boost::property_tree;
46 using namespace geopop;
47 
48 GeoPopBuilder::GeoPopBuilder(const ptree& config, RnMan& rnMan, shared_ptr<spdlog::logger> strideLogger)
49  : AbstractPopBuilder(config, rnMan, move(strideLogger))
50 {
51 }
52 
53 shared_ptr<Population> GeoPopBuilder::Build(shared_ptr<Population> pop)
54 {
55  m_stride_logger->trace("Building geopop.");
56 
57  // ------------------------------------------------------------
58  // Set the GeoGridConfig.
59  // ------------------------------------------------------------
60  GeoGridConfig ggConfig(m_config);
61  ggConfig.SetData(m_config.get<string>("run.geopop_gen.household_file"));
62 
63  // ------------------------------------------------------------
64  // Get GeoGrid associated with 'pop'.
65  // ------------------------------------------------------------
66  auto& geoGrid = pop->RefGeoGrid();
67 
68  // ------------------------------------------------------------
69  // Read locations file (commute file only if present).
70  // ------------------------------------------------------------
71  string commutesFile;
72  auto geopop_gen = m_config.get_child("run.geopop_gen");
73  if (geopop_gen.count("commuting_file")) {
74  commutesFile = m_config.get<string>("run.geopop_gen.commuting_file");
75  }
76 
77  m_stride_logger->trace("Starting MakeLocations");
78  MakeLocations(geoGrid, ggConfig, m_config.get<string>("run.geopop_gen.cities_file"), commutesFile);
79  m_stride_logger->trace("Finished MakeLocations");
80 
81  // ------------------------------------------------------------
82  // Generate Geo.
83  // ------------------------------------------------------------
84  m_stride_logger->trace("Starting MakePools");
85  MakePools(geoGrid, ggConfig);
86  m_stride_logger->trace("Finished MakePools");
87 
88  // ------------------------------------------------------------
89  // Generate Pop.
90  // ------------------------------------------------------------
91  m_stride_logger->trace("Starting MakePersons");
92  MakePersons(geoGrid, ggConfig);
93  m_stride_logger->trace("Finished MakePersons");
94 
95  // ------------------------------------------------------------
96  // Done.
97  // ------------------------------------------------------------
98  m_stride_logger->trace("Done building geopop.");
99 
100  return pop;
101 }
102 
103 void GeoPopBuilder::MakeLocations(GeoGrid& geoGrid, const GeoGridConfig& geoGridConfig, const string& citiesFileName,
104  const string& commutingFileName)
105 {
106  const auto locationsReader = ReaderFactory::CreateLocationsReader(citiesFileName);
107  locationsReader->FillGeoGrid(geoGrid);
108 
109  if (!commutingFileName.empty()) {
110  const auto commutesReader = ReaderFactory::CreateCommutesReader(commutingFileName);
111  commutesReader->FillGeoGrid(geoGrid);
112  }
113 
114  for (const shared_ptr<Location>& loc : geoGrid) {
115  loc->SetPopCount(geoGridConfig.param.pop_size);
116  }
117  geoGrid.Finalize();
118 }
119 
120 void GeoPopBuilder::MakePools(GeoGrid& geoGrid, const GeoGridConfig& geoGridConfig)
121 {
122  K12SchoolGenerator(m_rn_man, m_stride_logger).Apply(geoGrid, geoGridConfig);
123 
124  CollegeGenerator(m_rn_man, m_stride_logger).Apply(geoGrid, geoGridConfig);
125 
126  WorkplaceGenerator(m_rn_man, m_stride_logger).Apply(geoGrid, geoGridConfig);
127 
128  PrimaryCommunityGenerator(m_rn_man, m_stride_logger).Apply(geoGrid, geoGridConfig);
129 
130  SecondaryCommunityGenerator(m_rn_man, m_stride_logger).Apply(geoGrid, geoGridConfig);
131 
132  HouseholdGenerator(m_rn_man, m_stride_logger).Apply(geoGrid, geoGridConfig);
133 }
134 
135 void GeoPopBuilder::MakePersons(GeoGrid& geoGrid, const GeoGridConfig& geoGridConfig)
136 {
137  // NOTICE: WorkplacePopulator logic requires that CollegePopulator
138  // has been executed prior to WorkplacePopulator.
139 
140  HouseholdPopulator(m_rn_man, m_stride_logger).Apply(geoGrid, geoGridConfig);
141 
142  K12SchoolPopulator(m_rn_man, m_stride_logger).Apply(geoGrid, geoGridConfig);
143 
144  CollegePopulator(m_rn_man, m_stride_logger).Apply(geoGrid, geoGridConfig);
145 
146  PrimaryCommunityPopulator(m_rn_man, m_stride_logger).Apply(geoGrid, geoGridConfig);
147 
148  SecondaryCommunityPopulator(m_rn_man, m_stride_logger).Apply(geoGrid, geoGridConfig);
149 
150  WorkplacePopulator(m_rn_man, m_stride_logger).Apply(geoGrid, geoGridConfig);
151 }
152 
153 } // namespace stride
std::shared_ptr< Population > Build(std::shared_ptr< Population > pop) override
Generates a synthetic population.
Initialize populations.
void MakeLocations(geopop::GeoGrid &geoGrid, const geopop::GeoGridConfig &geoGridConfig, const std::string &citiesFileName, const std::string &commutingFileName)
Read files with location data and constructs GeoGrid config.
Configuration data mostly for generating a population, but also for computing the required number of ...
Definition: GeoGridConfig.h:35
void SetData(const std::string &householdsFileName)
Read the househould data file, parse it and set data.
Populator< stride::ContactType::Id::SecondaryCommunity > SecondaryCommunityPopulator
Definition: Populator.h:87
Base Class for PopBuilders.
Generator< stride::ContactType::Id::K12School > K12SchoolGenerator
Definition: Generator.h:93
Populator< stride::ContactType::Id::K12School > K12SchoolPopulator
Definition: Populator.h:82
Generator< stride::ContactType::Id::College > CollegeGenerator
Definition: Generator.h:94
void MakePools(geopop::GeoGrid &geoGrid, const geopop::GeoGridConfig &geoGridConfig)
Build and store the Geo part of the GeoGrid.
static std::shared_ptr< CommutesReader > CreateCommutesReader(const std::string &filename)
Create a CommutesReader based on the filename relative to the data directory.
STL namespace.
A Geographic grid of simulation region contains Locations that in turn contain an index to the Contac...
Definition: GeoGrid.h:41
static std::shared_ptr< LocationsReader > CreateLocationsReader(const std::string &filename)
Create a Reader based on the filename relative to the data directory.
Interface of RnMan.
Definition of ContactPool Id Type.
unsigned int pop_size
Target population size for the generated population.
Definition: GeoGridConfig.h:73
Populator< stride::ContactType::Id::PrimaryCommunity > PrimaryCommunityPopulator
Definition: Populator.h:86
Namespace for the geographic and demograhic classes.
Definition: Coordinate.h:21
Generator< stride::ContactType::Id::SecondaryCommunity > SecondaryCommunityGenerator
Definition: Generator.h:98
struct geopop::GeoGridConfig::@0 param
util::RnMan & m_rn_man
Random number generation management.
Header file for the SurveySeeder class.
Populator< stride::ContactType::Id::Workplace > WorkplacePopulator
Definition: Populator.h:84
Header file for the core Population class.
void MakePersons(geopop::GeoGrid &geoGrid, const geopop::GeoGridConfig &geoGridConfig)
Build and store the Pop part of the GeoGrid.
std::shared_ptr< spdlog::logger > m_stride_logger
Miscellaneous string utilities.
Interface for install directory queries.
Populator< stride::ContactType::Id::Household > HouseholdPopulator
Definition: Populator.h:85
GeoPopBuilder(const boost::property_tree::ptree &config, util::RnMan &rnMan, std::shared_ptr< spdlog::logger > strideLogger=nullptr)
Use constructor of base.
const boost::property_tree::ptree & m_config
Configuration property tree.
Namespace for the simulator and related classes.
Definition: Calendar.cpp:28
Generator< stride::ContactType::Id::Workplace > WorkplaceGenerator
Definition: Generator.h:95
Populator< stride::ContactType::Id::College > CollegePopulator
Definition: Populator.h:83
Generator< stride::ContactType::Id::Household > HouseholdGenerator
Definition: Generator.h:96
Generator< stride::ContactType::Id::PrimaryCommunity > PrimaryCommunityGenerator
Definition: Generator.h:97