Eclipse SUMO - Simulation of Urban MObility
AGDataAndStatistics.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2019 German Aerospace Center (DLR) and others.
4 // activitygen module
5 // Copyright 2010 TUM (Technische Universitaet Muenchen, http://www.tum.de/)
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 // SPDX-License-Identifier: EPL-2.0
11 /****************************************************************************/
20 // Contains various data, statistical values and functions from input used
21 // by various objects
22 /****************************************************************************/
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #include <config.h>
29 
30 #include "AGDataAndStatistics.h"
32 #include <cmath>
33 #include <iomanip>
34 #define LIMIT_CHILDREN_NUMBER 3
35 
36 
37 // ===========================================================================
38 // method definitions
39 // ===========================================================================
42  static AGDataAndStatistics ds;
43  return ds;
44 }
45 
46 int
48  if (m < n) {
49  return 0;
50  }
51  int num = RandHelper::rand(m - n);
52  num += n;
53  return num;
54 }
55 
56 int
58  if (m < n || n >= limitEndAge) {
59  return -1;
60  }
61  if (m > limitEndAge) {
62  m = limitEndAge;
63  }
64  const double alea = RandHelper::rand(getPropYoungerThan(n), getPropYoungerThan(m));
65  for (int a = n; a < m; ++a) {
66  if (alea < getPropYoungerThan(a + 1)) {
67  return a;
68  }
69  }
70  return -1;
71 }
72 
73 int
75  double alea = RandHelper::rand();
76  double cumul = 0;
77  for (int nbr = 0; nbr < LIMIT_CHILDREN_NUMBER; ++nbr) {
78  cumul += poisson(mean, nbr);
79  if (cumul > alea) {
80  return nbr;
81  }
82  }
83  return LIMIT_CHILDREN_NUMBER;
84 }
85 
86 double
87 AGDataAndStatistics::poisson(double mean, int occ) {
88  return exp(-mean) * pow(mean, occ) / (double)factorial(occ);
89 }
90 
91 int
93  if (fact > 0) {
94  return fact * factorial(fact - 1);
95  }
96  return 1;
97 }
98 
99 void
106  limitEndAge = population.rbegin()->first;
107 
111  //cout << " --> oldAgeHhProb = " << setprecision(3) << oldAgeHhProb << " - retAge? " << getPeopleOlderThan(limitAgeRetirement) << " adAge? " << getPeopleOlderThan(limitAgeChildren) << endl;
112  //cout << " --> secondPersProb = " << setprecision(3) << secondPersProb << " - adAge? " << getPeopleOlderThan(limitAgeChildren) << " hh?" << households << endl;
113  //cout << " --> meanNbrChildren = " << setprecision(3) << meanNbrChildren << " - chAge? " << getPeopleYoungerThan(limitAgeChildren) << endl;
114 }
115 
116 double
118  std::map<int, double>::iterator it;
119  double sum = 0;
120  int previousAge = 0;
121  double prop = 0;
122 
123  for (it = population.begin(); it != population.end(); ++it) {
124  if (it->first < age) {
125  sum += it->second;
126  } else if (it->first >= age && previousAge < age) {
127  prop = ((double)(age - previousAge) / (double)(it->first - previousAge));
128  sum += prop * it->second;
129  break;
130  }
131  previousAge = it->first;
132  }
133  return sum;
134 }
135 
136 int
138  return (int)((double)inhabitants * getPropYoungerThan(age) + .5);
139 }
140 
141 int
143  return (inhabitants - getPeopleYoungerThan(age));
144 }
145 
146 void
147 AGDataAndStatistics::normalizeMapProb(std::map<int, double>* myMap) {
148  double sum = 0;
149  std::map<int, double>::iterator it;
150  for (it = myMap->begin(); it != myMap->end(); ++it) {
151  sum += it->second;
152  }
153  if (sum == 0) {
154  return;
155  }
156  for (it = myMap->begin(); it != myMap->end(); ++it) {
157  it->second = it->second / sum;
158  }
159 }
160 
161 double
163  if (maxVar <= 0) {
164  return mean;
165  }
166  double p = RandHelper::rand(static_cast<double>(0.0001), static_cast<double>(1));
167  //we have to scale the distribution because maxVar is different from INF
168  double scale = exp((-1) * maxVar);
169  //new p: scaled
170  p = p * (1 - scale) + scale; // p = [scale; 1) ==> (1-p) = (0; 1-scale]
171 
172  double variation = (-1) * log(p);
173  //decide the side of the mean value
174  if (RandHelper::rand(1000) < 500) {
175  return mean + variation;
176  } else {
177  return mean - variation;
178  }
179 
180 }
181 
182 int
184  double alea = RandHelper::rand();
185  double total = 0;
186  std::map<int, double>::iterator it;
187  for (it = incoming.begin(); it != incoming.end(); ++it) {
188  total += it->second;
189  if (alea < total) {
190  return it->first;
191  }
192  }
193  std::cout << "ERROR: incoming at city gates not normalized" << std::endl;
194  return 0;
195 }
196 
197 int
199  double alea = RandHelper::rand();
200  double total = 0;
201  std::map<int, double>::iterator it;
202  for (it = outgoing.begin(); it != outgoing.end(); ++it) {
203  total += it->second;
204  if (alea < total) {
205  return it->first;
206  }
207  }
208  std::cout << "ERROR: outgoing at city gates not normalized" << std::endl;
209  return 0;
210 }
211 
212 
213 
214 /****************************************************************************/
AGDataAndStatistics::meanNbrChildren
double meanNbrChildren
Definition: AGDataAndStatistics.h:96
AGDataAndStatistics::consolidateStat
void consolidateStat()
Definition: AGDataAndStatistics.cpp:100
AGDataAndStatistics::limitAgeRetirement
int limitAgeRetirement
Definition: AGDataAndStatistics.h:55
AGDataAndStatistics::population
std::map< int, double > population
Definition: AGDataAndStatistics.h:71
AGDataAndStatistics::getPeopleOlderThan
int getPeopleOlderThan(int age)
Definition: AGDataAndStatistics.cpp:142
AGDataAndStatistics
Definition: AGDataAndStatistics.h:41
AGDataAndStatistics::getInverseExpRandomValue
double getInverseExpRandomValue(double mean, double maxVar)
Definition: AGDataAndStatistics.cpp:162
AGDataAndStatistics::secondPersProb
double secondPersProb
Definition: AGDataAndStatistics.h:94
RandHelper::rand
static double rand(std::mt19937 *rng=0)
Returns a random real number in [0, 1)
Definition: RandHelper.h:60
AGDataAndStatistics::beginWorkHours
std::map< int, double > beginWorkHours
Definition: AGDataAndStatistics.h:67
AGDataAndStatistics::outgoing
std::map< int, double > outgoing
Definition: AGDataAndStatistics.h:78
AGDataAndStatistics::getRandomCityGateByOutgoing
int getRandomCityGateByOutgoing()
Definition: AGDataAndStatistics.cpp:198
AGDataAndStatistics::limitEndAge
int limitEndAge
Definition: AGDataAndStatistics.h:56
AGDataAndStatistics::getRandomPopDistributed
int getRandomPopDistributed(int n, int m)
Definition: AGDataAndStatistics.cpp:57
LIMIT_CHILDREN_NUMBER
#define LIMIT_CHILDREN_NUMBER
Definition: AGDataAndStatistics.cpp:34
AGDataAndStatistics::getPeopleYoungerThan
int getPeopleYoungerThan(int age)
Definition: AGDataAndStatistics.cpp:137
AGDataAndStatistics::getRandom
int getRandom(int n, int m)
Definition: AGDataAndStatistics.cpp:47
AGDataAndStatistics::normalizeMapProb
void normalizeMapProb(std::map< int, double > *myMap)
Definition: AGDataAndStatistics.cpp:147
AGDataAndStatistics::factorial
int factorial(int n)
Definition: AGDataAndStatistics.cpp:92
AGDataAndStatistics::endWorkHours
std::map< int, double > endWorkHours
Definition: AGDataAndStatistics.h:68
AGDataAndStatistics::incoming
std::map< int, double > incoming
Definition: AGDataAndStatistics.h:77
AGDataAndStatistics::oldAgeHhProb
double oldAgeHhProb
Definition: AGDataAndStatistics.h:92
AGDataAndStatistics::inhabitants
int inhabitants
Definition: AGDataAndStatistics.h:52
AGDataAndStatistics::getPropYoungerThan
double getPropYoungerThan(int age)
Definition: AGDataAndStatistics.cpp:117
config.h
RandHelper.h
AGDataAndStatistics::getPoissonsNumberOfChildren
int getPoissonsNumberOfChildren(double mean)
Definition: AGDataAndStatistics.cpp:74
AGDataAndStatistics::poisson
double poisson(double mean, int occ)
Definition: AGDataAndStatistics.cpp:87
AGDataAndStatistics::getDataAndStatistics
static AGDataAndStatistics & getDataAndStatistics()
Definition: AGDataAndStatistics.cpp:41
AGDataAndStatistics::limitAgeChildren
int limitAgeChildren
Definition: AGDataAndStatistics.h:54
AGDataAndStatistics::getRandomCityGateByIncoming
int getRandomCityGateByIncoming()
Definition: AGDataAndStatistics.cpp:183
AGDataAndStatistics.h
AGDataAndStatistics::households
int households
Definition: AGDataAndStatistics.h:53