Stride Reference Manual  - generated for commit 9643b11
PrimaryCommunityGenerator.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 2019, Jan Broeckhove.
14  */
15 
16 #include "Generator.h"
17 
18 #include "util/Assert.h"
19 
20 #include <cmath>
21 #include <iostream>
22 #include <stdexcept>
23 
24 namespace geopop {
25 
26 using namespace std;
27 using namespace stride;
28 using namespace stride::ContactType;
29 
30 template<>
32 {
33  // 1. calculate number of communities
34  // 2. assign communities to a location using a discrete distribution reflecting
35  // the relative number of people at that location
36 
37  const auto popCount = ggConfig.param.pop_size;
38  //const auto communitySize = PoolParams<Id::PrimaryCommunity>::people;
39  const auto communitySize = ggConfig.people[Id::PrimaryCommunity];
40  const auto communityCount = static_cast<unsigned int>(ceil(popCount / static_cast<double>(communitySize)));
41 
42  vector<double> weights;
43  for (const auto& loc : geoGrid) {
44  const auto weight = static_cast<double>(loc->GetPopCount()) / static_cast<double>(popCount);
45  AssertThrow(weight >= 0 && weight <= 1 && !std::isnan(weight),
46  "CommunityGenerator> Invalid weight: " + to_string(weight), m_logger);
47  weights.push_back(weight);
48  }
49 
50  if (weights.empty()) {
51  // trng can't handle empty vectors
52  return;
53  }
54 
55  const auto dist = m_rn_man.GetDiscreteGenerator(weights, 0U);
56  auto pop = geoGrid.GetPopulation();
57 
58  for (auto i = 0U; i < communityCount; i++) {
59  const auto loc = geoGrid[dist()];
60  AddPools(*loc, pop, ggConfig);
61  }
62 }
63 
64 } // namespace geopop
Configuration data mostly for generating a population, but also for computing the required number of ...
Definition: GeoGridConfig.h:35
#define AssertThrow(CONDITION, MESSAGE, LOGGER)
Definition: Assert.h:68
STL namespace.
A Geographic grid of simulation region contains Locations that in turn contain an index to the Contac...
Definition: GeoGrid.h:41
unsigned int pop_size
Target population size for the generated population.
Definition: GeoGridConfig.h:73
Namespace for the geographic and demograhic classes.
Definition: Coordinate.h:21
stride::ContactType::IdSubscriptArray< unsigned int > people
People per unit (= Household, K12School, College, etc.) for each of the ContactTypes.
Definition: GeoGridConfig.h:47
struct geopop::GeoGridConfig::@0 param
void Apply(GeoGrid &, const GeoGridConfig &)
Generate ContactPools for ContactType::Id as sepcified by data in GeoGridConfig.
Definition: Generator.h:52
Definition of .
Namespace for the simulator and related classes.
Definition: Calendar.cpp:28
Namespace to manage types of ContactPool.
Definition: ContactType.cpp:28