SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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.sourceforge.net/
14 // Copyright (C) 2001-2012 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  }
71  SUMOReal alea = RandHelper::rand();
72  SUMOReal beginProp = getPropYoungerThan(n);
73  SUMOReal total = getPropYoungerThan(m) - beginProp;
74  if (total <= 0) {
75  return -1;
76  }
81  alea = alea * total + beginProp;
82  for (int a = n ; a < m ; ++a) {
83  if (alea < getPropYoungerThan(a + 1)) {
84  return a;
85  }
86  }
87  return -1;
88 }
89 
90 int
92  SUMOReal alea = RandHelper::rand();
93  SUMOReal cumul = 0;
94  for (int nbr = 0 ; nbr < LIMIT_CHILDREN_NUMBER ; ++nbr) {
95  cumul += poisson(mean, nbr);
96  if (cumul > alea) {
97  return nbr;
98  }
99  }
100  return LIMIT_CHILDREN_NUMBER;
101 }
102 
103 SUMOReal
105  SUMOReal proba = exp(-mean);
106  proba *= pow(mean, occ);
107  proba /= (SUMOReal)factorial(occ);
108  return proba;
109 }
110 
111 int
113  if (fact > 0) {
114  return fact * factorial(fact - 1);
115  }
116  return 1;
117 }
118 
119 void
126  limitEndAge = population.rbegin()->first;
127 
131  //cout << " --> oldAgeHhProb = " << setprecision(3) << oldAgeHhProb << " - retAge? " << getPeopleOlderThan(limitAgeRetirement) << " adAge? " << getPeopleOlderThan(limitAgeChildren) << endl;
132  //cout << " --> secondPersProb = " << setprecision(3) << secondPersProb << " - adAge? " << getPeopleOlderThan(limitAgeChildren) << " hh?" << households << endl;
133  //cout << " --> meanNbrChildren = " << setprecision(3) << meanNbrChildren << " - chAge? " << getPeopleYoungerThan(limitAgeChildren) << endl;
134 }
135 
136 SUMOReal
138  std::map<int, SUMOReal>::iterator it;
139  SUMOReal sum = 0;
140  int previousAge = 0;
141  SUMOReal prop = 0;
142 
143  for (it = population.begin() ; it != population.end() ; ++it) {
144  if (it->first < age) {
145  sum += it->second;
146  } else if (it->first >= age && previousAge < age) {
147  prop = ((SUMOReal)(age - previousAge) / (SUMOReal)(it->first - previousAge));
148  sum += prop * it->second;
149  break;
150  }
151  previousAge = it->first;
152  }
153  return sum;
154 }
155 
156 int
158  return (int)((SUMOReal)inhabitants * getPropYoungerThan(age));
159 }
160 
161 int
163  return (inhabitants - getPeopleYoungerThan(age));
164 }
165 
166 void
167 AGDataAndStatistics::normalizeMapProb(std::map<int, SUMOReal> *myMap) {
168  SUMOReal sum = 0;
169  std::map<int, SUMOReal>::iterator it;
170  for (it = myMap->begin() ; it != myMap->end() ; ++it) {
171  sum += it->second;
172  }
173  if (sum == 0) {
174  return;
175  }
176  for (it = myMap->begin() ; it != myMap->end() ; ++it) {
177  it->second = it->second / sum;
178  }
179 }
180 
181 SUMOReal
183  if (maxVar <= 0) {
184  return mean;
185  }
186  SUMOReal p = RandHelper::rand(static_cast<SUMOReal>(0.0001), static_cast<SUMOReal>(1));
187  //we have to scale the distribution because maxVar is different from INF
188  SUMOReal scale = exp((-1) * maxVar);
189  //new p: scaled
190  p = p * (1 - scale) + scale; // p = [scale ; 1) ==> (1-p) = (0 ; 1-scale]
191 
192  SUMOReal variation = (-1) * log(p);
193  //decide the side of the mean value
194  if (RandHelper::rand(1000) < 500) {
195  return mean + variation;
196  } else {
197  return mean - variation;
198  }
199 
200 }
201 
202 int
204  SUMOReal alea = RandHelper::rand();
205  SUMOReal total = 0;
206  std::map<int, SUMOReal>::iterator it;
207  for (it = incoming.begin() ; it != incoming.end() ; ++it) {
208  total += it->second;
209  if (alea < total) {
210  return it->first;
211  }
212  }
213  std::cout << "ERROR: incoming at city gates not normalized" << std::endl;
214  return 0;
215 }
216 
217 int
219  SUMOReal alea = RandHelper::rand();
220  SUMOReal total = 0;
221  std::map<int, SUMOReal>::iterator it;
222  for (it = outgoing.begin() ; it != outgoing.end() ; ++it) {
223  total += it->second;
224  if (alea < total) {
225  return it->first;
226  }
227  }
228  std::cout << "ERROR: outgoing at city gates not normalized" << std::endl;
229  return 0;
230 }
231 
232 
233 
234 /****************************************************************************/