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