SUMO - Simulation of Urban MObility
AGDataAndStatistics.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // Contains various data, statistical values and functions from input used
11 // by various objects
12 /****************************************************************************/
13 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
14 // Copyright (C) 2001-2014 DLR (http://www.dlr.de/) and contributors
15 // activitygen module
16 // Copyright 2010 TUM (Technische Universitaet Muenchen, http://www.tum.de/)
17 /****************************************************************************/
18 //
19 // This file is part of SUMO.
20 // SUMO is free software: you can redistribute it and/or modify
21 // it under the terms of the GNU General Public License as published by
22 // the Free Software Foundation, either version 3 of the License, or
23 // (at your option) any later version.
24 //
25 /****************************************************************************/
26 
27 
28 // ===========================================================================
29 // included modules
30 // ===========================================================================
31 #ifdef _MSC_VER
32 #include <windows_config.h>
33 #else
34 #include <config.h>
35 #endif
36 
37 #include "AGDataAndStatistics.h"
39 #include <cmath>
40 #include <iomanip>
41 #define LIMIT_CHILDREN_NUMBER 3
42 
43 
44 // ===========================================================================
45 // method definitions
46 // ===========================================================================
49  static AGDataAndStatistics ds;
50  return ds;
51 }
52 
53 int
55  if (m < n) {
56  return 0;
57  }
58  int num = RandHelper::rand(m - n);
59  num += n;
60  return num;
61 }
62 
63 int
65  if (m < n || n >= limitEndAge) {
66  return -1;
67  }
68  if (m > limitEndAge) {
69  m = limitEndAge;
70  }
72  for (int a = n; a < m; ++a) {
73  if (alea < getPropYoungerThan(a + 1)) {
74  return a;
75  }
76  }
77  return -1;
78 }
79 
80 int
82  SUMOReal alea = RandHelper::rand();
83  SUMOReal cumul = 0;
84  for (int nbr = 0; nbr < LIMIT_CHILDREN_NUMBER; ++nbr) {
85  cumul += poisson(mean, nbr);
86  if (cumul > alea) {
87  return nbr;
88  }
89  }
90  return LIMIT_CHILDREN_NUMBER;
91 }
92 
95  return exp(-mean) * pow(mean, occ) / (SUMOReal)factorial(occ);
96 }
97 
98 int
100  if (fact > 0) {
101  return fact * factorial(fact - 1);
102  }
103  return 1;
104 }
105 
106 void
113  limitEndAge = population.rbegin()->first;
114 
118  //cout << " --> oldAgeHhProb = " << setprecision(3) << oldAgeHhProb << " - retAge? " << getPeopleOlderThan(limitAgeRetirement) << " adAge? " << getPeopleOlderThan(limitAgeChildren) << endl;
119  //cout << " --> secondPersProb = " << setprecision(3) << secondPersProb << " - adAge? " << getPeopleOlderThan(limitAgeChildren) << " hh?" << households << endl;
120  //cout << " --> meanNbrChildren = " << setprecision(3) << meanNbrChildren << " - chAge? " << getPeopleYoungerThan(limitAgeChildren) << endl;
121 }
122 
123 SUMOReal
125  std::map<int, SUMOReal>::iterator it;
126  SUMOReal sum = 0;
127  int previousAge = 0;
128  SUMOReal prop = 0;
129 
130  for (it = population.begin(); it != population.end(); ++it) {
131  if (it->first < age) {
132  sum += it->second;
133  } else if (it->first >= age && previousAge < age) {
134  prop = ((SUMOReal)(age - previousAge) / (SUMOReal)(it->first - previousAge));
135  sum += prop * it->second;
136  break;
137  }
138  previousAge = it->first;
139  }
140  return sum;
141 }
142 
143 int
145  return (int)((SUMOReal)inhabitants * getPropYoungerThan(age) + .5);
146 }
147 
148 int
150  return (inhabitants - getPeopleYoungerThan(age));
151 }
152 
153 void
154 AGDataAndStatistics::normalizeMapProb(std::map<int, SUMOReal>* myMap) {
155  SUMOReal sum = 0;
156  std::map<int, SUMOReal>::iterator it;
157  for (it = myMap->begin(); it != myMap->end(); ++it) {
158  sum += it->second;
159  }
160  if (sum == 0) {
161  return;
162  }
163  for (it = myMap->begin(); it != myMap->end(); ++it) {
164  it->second = it->second / sum;
165  }
166 }
167 
168 SUMOReal
170  if (maxVar <= 0) {
171  return mean;
172  }
173  SUMOReal p = RandHelper::rand(static_cast<SUMOReal>(0.0001), static_cast<SUMOReal>(1));
174  //we have to scale the distribution because maxVar is different from INF
175  SUMOReal scale = exp((-1) * maxVar);
176  //new p: scaled
177  p = p * (1 - scale) + scale; // p = [scale; 1) ==> (1-p) = (0; 1-scale]
178 
179  SUMOReal variation = (-1) * log(p);
180  //decide the side of the mean value
181  if (RandHelper::rand(1000) < 500) {
182  return mean + variation;
183  } else {
184  return mean - variation;
185  }
186 
187 }
188 
189 int
191  SUMOReal alea = RandHelper::rand();
192  SUMOReal total = 0;
193  std::map<int, SUMOReal>::iterator it;
194  for (it = incoming.begin(); it != incoming.end(); ++it) {
195  total += it->second;
196  if (alea < total) {
197  return it->first;
198  }
199  }
200  std::cout << "ERROR: incoming at city gates not normalized" << std::endl;
201  return 0;
202 }
203 
204 int
206  SUMOReal alea = RandHelper::rand();
207  SUMOReal total = 0;
208  std::map<int, SUMOReal>::iterator it;
209  for (it = outgoing.begin(); it != outgoing.end(); ++it) {
210  total += it->second;
211  if (alea < total) {
212  return it->first;
213  }
214  }
215  std::cout << "ERROR: outgoing at city gates not normalized" << std::endl;
216  return 0;
217 }
218 
219 
220 
221 /****************************************************************************/
#define LIMIT_CHILDREN_NUMBER
static AGDataAndStatistics & getDataAndStatistics()
static SUMOReal rand()
Returns a random real number in [0, 1)
Definition: RandHelper.h:62
std::map< int, SUMOReal > endWorkHours
int getRandomPopDistributed(int n, int m)
SUMOReal poisson(SUMOReal mean, int occ)
void normalizeMapProb(std::map< int, SUMOReal > *myMap)
std::map< int, SUMOReal > beginWorkHours
std::map< int, SUMOReal > population
int getPoissonsNumberOfChildren(SUMOReal mean)
int getRandom(int n, int m)
std::map< int, SUMOReal > incoming
#define SUMOReal
Definition: config.h:215
SUMOReal getInverseExpRandomValue(SUMOReal mean, SUMOReal maxVar)
SUMOReal getPropYoungerThan(int age)
std::map< int, SUMOReal > outgoing