SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AGCity.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // City class that contains all other objects of the city: in particular
11 // streets, households, bus lines, work positions and school
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 <iostream>
38 #include <vector>
39 #include <string>
40 #include <map>
41 #include <iomanip>
43 #include <router/RONet.h>
44 #include <router/ROEdge.h>
45 #include "AGStreet.h"
46 #include "AGWorkPosition.h"
47 #include "AGCity.h"
48 //#define DRIVING_LICENSE_AGE 18
49 
50 
51 // ===========================================================================
52 // method definitions
53 // ===========================================================================
54 void
56  if (streetsCompleted) {
57  return;
58  } else {
59  streetsCompleted = true;
60  }
61 
62  SUMOReal pop = 0, work = 0;
63  std::vector<AGStreet>::iterator it;
64 
65  for (it = streets.begin(); it != streets.end(); ++it) {
66  pop += it->getPopulation();
67  work += it->getWorkplaceNumber();
68  }
70  //can be improved with other input data
71  SUMOReal neededWorkPositionsInCity = (1.0 - statData.unemployement)
75  // we generate 5% more work positions that really needed: to avoid any expensive research of random work positions
76  neededWorkPositionsInCity *= SUMOReal(1.05);
77  statData.workPositions = (int)neededWorkPositionsInCity;
78  statData.factorWorkPositions = neededWorkPositionsInCity / (SUMOReal) work;
79 
80  for (it = streets.begin(); it != streets.end(); ++it) {
81  it->setPopulation(it->getPopulation() * statData.factorInhabitants);
82  it->setWorkplaceNumber(it->getWorkplaceNumber() * statData.factorWorkPositions);
83  //it->print();
84  }
85 
86  //completing streets from edges of the network not handled/present in STAT file (no population no work position)
87  std::map<std::string, ROEdge*>::const_iterator itE;
88  std::vector<AGStreet>::iterator itS;
89 
90  for (itE = net->getEdgeMap().begin(); itE != net->getEdgeMap().end(); ++itE) {
91  for (itS = streets.begin(); itS != streets.end(); ++itS) {
92  if (itS->getName() == itE->second->getID()) {
93  break;
94  }
95  }
96  //if this edge isn't represented by a street
97  if (itS == streets.end()) {
98  streets.push_back(AGStreet(itE->second));
99  }
100  }
101 }
102 
103 void
105  std::vector<AGStreet>::iterator it;
106  int workPositionCounter = 0;
107 
108  try {
109  for (it = streets.begin(); it != streets.end(); ++it) {
110  //std::cout << "number of work positions in street: " << it->getWorkplaceNumber() << std::endl;
111  for (int i = 0; i < it->getWorkplaceNumber(); ++i) {
112  workPositions.push_back(AGWorkPosition(&statData, *it));
113  ++workPositionCounter;
114  }
115  }
116  } catch (const std::bad_alloc& e) {
117  std::cout << "Number of work positions at bad_alloc exception: " << workPositionCounter << std::endl;
118  throw(e);
119  }
120  //std::cout << "Inner work positions done. " << workPositionCounter << " generated." << std::endl;
121 
122  // Work positions outside the city
124  std::cout << "--> work position: " << std::endl;
125  std::cout << " |-> in city: " << workPositionCounter << std::endl;
126  std::cout << " |-> out city: " << statData.workPositions - workPositionCounter << std::endl;
127  std::cout << " |-> in+out city: " << statData.workPositions << std::endl;
128 }
129 
130 void
132  // work positions outside the city
134  if (nbrWorkers <= 0) {
135  return;
136  }
137  nbrWorkers *= (1.0 - statData.unemployement);
141  int nbrOutWorkPositions = static_cast<int>(workPositions.size() * (static_cast<SUMOReal>(statData.outgoingTraffic)) / (nbrWorkers - static_cast<SUMOReal>(statData.outgoingTraffic)));
142 
143  if (cityGates.empty()) {
144  return;
145  }
146 
147  for (int i = 0; i < nbrOutWorkPositions; ++i) {
149  workPositions.push_back(AGWorkPosition(&statData, cityGates[posi].getStreet(), cityGates[posi].getPosition()));
150  }
151  //cout << "outgoing traffic: " << statData.outgoingTraffic << std::endl;
152  //cout << "total number of workers in the city: " << nbrWorkers << std::endl;
153  //cout << "work positions out side the city: " << nbrOutWorkPositions << std::endl;
154  //cout << "work positions in and out of the city: " << workPositions.size() << std::endl;
155  statData.workPositions = static_cast<int>(workPositions.size());
156 }
157 
158 void
160  std::list<AGBusLine>::iterator it;
161  for (it = busLines.begin(); it != busLines.end(); ++it) {
162  //it->generateOpositDirection();
163  it->setBusNames();
164  }
165 }
166 
167 void
169  std::vector<AGStreet>::iterator it;
170  SUMOReal people = 0;
171  nbrCars = 0;
172  unsigned int idHouseholds = 0;
173  std::vector<int> numAdults(statData.households);
174  std::vector<int> numChilds(statData.households);
177  for (int i = 0; i < statData.households; i++) {
178  numAdults[i] = 1;
179  numChilds[i] = 0;
180  if (RandHelper::rand() < retiredProb) {
181  numAdults[i] = -numAdults[i];
182  } else if (totalChildrenLeft > 0) {
184  totalChildrenLeft -= numChilds[i];
185  }
186  }
187  //compensate with adults for too many / missing children
188  const int numSecondPers = statData.getPeopleOlderThan(statData.limitAgeChildren) - statData.households + totalChildrenLeft;
189  for (int i = 0; i < numSecondPers; i++) {
190  numAdults[i] *= 2;
191  }
192  for (it = streets.begin(); it != streets.end(); ++it) {
193  people += it->getPopulation();
194  while (people > 0 && idHouseholds < (unsigned int)numAdults.size()) {
195  size_t i = RandHelper::rand(numAdults.size() - idHouseholds);
196  ++idHouseholds;
197  households.push_back(AGHousehold(&*it, this, idHouseholds));
198  households.back().generatePeople(abs(numAdults[i]), numChilds[i], numAdults[i] < 0); //&statData
199  //households.back().generateCars(statData.carRate);
200  people -= households.back().getPeopleNbr();
201  numAdults[i] = numAdults[numAdults.size() - idHouseholds];
202  numChilds[i] = numChilds[numAdults.size() - idHouseholds];
203  }
204  }
205 
206  //people from outside of the city generation:
208 
209  //TEST
210  int nbrSingle = 0;
211  int nbrCouple = 0;
212  int nbrChild = 0;
213  int nbrHH = 0;
214  int workingP = 0;
215  std::list<AGHousehold>::iterator itt;
216  for (itt = households.begin(); itt != households.end(); ++itt) {
217  if (itt->getAdultNbr() == 1) {
218  nbrSingle++;
219  if (itt->getAdults().front().isWorking()) {
220  workingP++;
221  }
222  }
223  if (itt->getAdultNbr() == 2) {
224  nbrCouple += 2;
225  if (itt->getAdults().front().isWorking()) {
226  workingP++;
227  }
228  if (itt->getAdults().back().isWorking()) {
229  workingP++;
230  }
231  }
232  nbrChild += itt->getPeopleNbr() - itt->getAdultNbr();
233  nbrHH++;
234  }
235  //cout << "number hh: " << nbrHH << std::endl;
236  //cout << "number single: " << nbrSingle << std::endl;
237  //cout << "number couple: " << nbrCouple << std::endl;
238  //cout << "number 3 or more: " << nbr3More << std::endl;
239  //cout << "number adults: " << nbrSingle + nbrCouple + nbr3More << std::endl;
240  //cout << "number children: " << nbrChild << std::endl;
241  //cout << "number people: " << nbrSingle + nbrCouple + nbr3More + nbrChild << std::endl;
242  //END TEST
243 
244  std::cout << "--> population: " << std::endl;
245  std::cout << " |-> city households: " << nbrHH << std::endl;
246  std::cout << " |-> city people: " << nbrSingle + nbrCouple + nbrChild << std::endl;
247  std::cout << " |-> city single: " << nbrSingle << " / (in) couple: " << nbrCouple << std::endl;
248  std::cout << " |-> city adults: " << nbrSingle + nbrCouple << std::endl;
249  std::cout << " |-> estimation: " << statData.getPeopleOlderThan(statData.limitAgeChildren) << std::endl;
250  std::cout << " |-> retired: " << statData.getPeopleOlderThan(statData.limitAgeRetirement) << std::endl;
251  std::cout << " |-> city children: " << nbrChild << std::endl;
252  std::cout << " |-> estimation: " << statData.getPeopleYoungerThan(statData.limitAgeChildren) << std::endl;
253 
254 }
255 
256 void
258  for (int i = 0; i < statData.incomingTraffic; ++i) {
260  peopleIncoming.push_back(ad);
261  }
262 }
263 
264 void
266  std::list<AGHousehold>::iterator it;
267  bool shortage;
268  for (it = households.begin(); it != households.end(); ++it) {
269  shortage = !it->allocateChildrenSchool();
270  if (shortage) {
271  /*ofstream fichier("test.txt", ios::app); // ouverture en écriture avec effacement du fichier ouvert
272  if(fichier)
273  {
274  fichier << "===> WARNING: Not enough school places in the city for all children..." << std::endl;
275  fichier.close();
276  }
277  else
278  cerr << "Impossible d'ouvrir le fichier !" << std::endl;*/
279 
280  //std::cout << "===> WARNING: Not enough school places in the city for all children..." << std::endl;
281  }
282  }
283 }
284 
285 void
287  statData.AdultNbr = 0;
288  //end tests
292  std::list<AGHousehold>::iterator it;
293  bool shortage;
294 
295  for (it = households.begin(); it != households.end(); ++it) {
296  if (it->retiredHouseholders()) {
297  continue;
298  }
299  shortage = !it->allocateAdultsWork();
300  if (shortage) {
301  std::cout << "===> ERROR: Not enough work positions in the city for all working people..." << std::endl;
302  }
303  statData.AdultNbr += it->getAdultNbr(); //TESTING
304  }
305 
309  std::list<AGAdult>::iterator itA;
310  for (itA = peopleIncoming.begin(); itA != peopleIncoming.end(); ++itA) {
311  if (statData.workPositions > 0) {
312  itA->tryToWork(1, &workPositions);
313  } else {
314  //shouldn't happen
315  std::cout << "not enough work for incoming people..." << std::endl;
316  }
317  }
318 
319  //BEGIN TESTS
320  int workingP = 0;
321  std::list<AGHousehold>::iterator itt;
322  for (itt = households.begin(); itt != households.end(); ++itt) {
323  if (itt->getAdultNbr() == 1) {
324  if (itt->getAdults().front().isWorking()) {
325  workingP++;
326  }
327  }
328  if (itt->getAdultNbr() == 2) {
329  if (itt->getAdults().front().isWorking()) {
330  workingP++;
331  }
332  if (itt->getAdults().back().isWorking()) {
333  workingP++;
334  }
335  }
336  }
337  std::cout << " |-> working people: " << peopleIncoming.size() + workingP << std::endl;
338  std::cout << " |-> working people in city: " << workingP << std::endl;
339  std::cout << " |-> working people from outside: " << peopleIncoming.size() << std::endl;
340  //END TESTS
341 }
342 
343 void
345  statData.hhFarFromPT = 0;
346  nbrCars = 0;
347  std::list<AGHousehold>::iterator it;
348  for (it = households.begin(); it != households.end(); ++it) {
349  if (!it->isCloseFromPubTransport(&(statData.busStations))) {
351  nbrCars++;
352  it->addACar();
353  }
355  }
356  // new rate: the rate on the people that have'nt any car yet:
357  // nR = (R * Drivers - AlreadyCars) / (Drivers - AlreadyCars)
359  //std::cout << " - " << newRate << std::endl;
360  if (newRate < 0 || newRate >= 1) {
361  newRate = 0;
362  }
363 
364  nbrCars = 0;
365  int nbrAdults = 0;
366  for (it = households.begin(); it != households.end(); ++it) {
367  it->generateCars(newRate);
368  nbrCars += it->getCarNbr();
369  nbrAdults += it->getAdultNbr();
370  }
371  //TEST RESULTS
372  //std::cout << "number of cars: " << nbrCars << std::endl;
373  //std::cout << "number of adults: " << statData.getPeopleOlderThan(statData.limitAgeChildren) << std::endl;
374  //std::cout << "real number of adults: " << nbrAdults << std::endl;
375  //std::cout << "number of people far from public transport: " << statData.hhFarFromPT << std::endl;
376  //std::cout << "original rate: " << setprecision(4) << statData.carRate << std::endl;
377  //std::cout << "new rate: " << setprecision(4) << newRate << std::endl;
378  //std::cout << "real rate: " << setprecision(4) << (SUMOReal)nbrCars / (SUMOReal)statData.getPeopleOlderThan(statData.limitAgeChildren) << std::endl;
379  //END TEST RESULTS
380 }
381 
382 const AGStreet&
383 AGCity::getStreet(const std::string& edge) {
390  if (!streetsCompleted) {
392  completeStreets();
393  std::cout << "first completed in getStreet() of City: Consolidation of data not needed in ActivityGen any more" << std::endl;
394  }
395  //rest of the function
396  std::vector<AGStreet>::iterator it = streets.begin();
397  while (it != streets.end()) {
398  if (it->getName() == edge) {
399  return *it;
400  }
401  ++it;
402  }
403  std::cout << "===> ERROR: WRONG STREET EDGE (" << edge << ") given and not found in street set." << std::endl;
404  throw(std::runtime_error("Street not found with edge id " + edge));
405 }
406 
407 const AGStreet&
409  if (streets.empty()) {
410  throw(std::runtime_error("No street found in this city"));
411  }
412  return streets[RandHelper::rand(streets.size())];
413 }
414 
415 /****************************************************************************/