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