SUMO - Simulation of Urban MObility
AGActivityGen.cpp
Go to the documentation of this file.
00001 /****************************************************************************/
00009 // Main class that handles City, Activities and Trips
00010 /****************************************************************************/
00011 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00012 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00013 // activitygen module
00014 // Copyright 2010 TUM (Technische Universitaet Muenchen, http://www.tum.de/)
00015 /****************************************************************************/
00016 //
00017 //   This file is part of SUMO.
00018 //   SUMO is free software: you can redistribute it and/or modify
00019 //   it under the terms of the GNU General Public License as published by
00020 //   the Free Software Foundation, either version 3 of the License, or
00021 //   (at your option) any later version.
00022 //
00023 /****************************************************************************/
00024 
00025 
00026 // ===========================================================================
00027 // included modules
00028 // ===========================================================================
00029 #ifdef _MSC_VER
00030 #include <windows_config.h>
00031 #else
00032 #include <config.h>
00033 #endif
00034 
00035 #include <iostream>
00036 #include <utils/xml/XMLSubSys.h>
00037 #include <utils/common/MsgHandler.h>
00038 #include <utils/common/RandHelper.h>
00039 #include <sstream>
00040 #include "AGActivityGen.h"
00041 #include "AGActivityGenHandler.h"
00042 #include "city/AGPosition.h"
00043 #include "activities/AGActivities.h"
00044 #include "AGActivityTripWriter.h"
00045 #include "city/AGTime.h"
00046 
00047 #ifdef CHECK_MEMORY_LEAKS
00048 #include <foreign/nvwa/debug_new.h>
00049 #endif // CHECK_MEMORY_LEAKS
00050 
00051 
00052 // ===========================================================================
00053 // method definitions
00054 // ===========================================================================
00055 void
00056 AGActivityGen::importInfoCity() {
00057     AGActivityGenHandler handler(city, net);
00058     handler.setFileName(inputFile);
00059     XMLSubSys::init(false);
00060     MsgHandler::initOutputOptions();
00061     PROGRESS_BEGIN_MESSAGE("Reading input");
00062     if (!XMLSubSys::runParser(handler, inputFile)) {
00063         PROGRESS_FAILED_MESSAGE();
00064         throw ProcessError();
00065     } else {
00066         PROGRESS_DONE_MESSAGE();
00067     }
00068 
00069     PROGRESS_BEGIN_MESSAGE("Consolidating statistics");
00070     city.statData.consolidateStat(); //some maps are still not
00071     PROGRESS_DONE_MESSAGE();
00072 
00073     PROGRESS_BEGIN_MESSAGE("Building street representation");
00074     city.completeStreets();
00075     PROGRESS_DONE_MESSAGE();
00076 
00077     PROGRESS_BEGIN_MESSAGE("Generating work positions");
00078     city.generateWorkPositions();
00079     PROGRESS_DONE_MESSAGE();
00080 
00081     PROGRESS_BEGIN_MESSAGE("Building bus lines");
00082     city.completeBusLines();
00083     PROGRESS_DONE_MESSAGE();
00084 
00085 
00086     PROGRESS_BEGIN_MESSAGE("Generating population");
00087     city.generatePopulation();
00088     PROGRESS_DONE_MESSAGE();
00089 
00090     PROGRESS_BEGIN_MESSAGE("Allocating schools");
00091     city.schoolAllocation();
00092     PROGRESS_DONE_MESSAGE();
00093 
00094     PROGRESS_BEGIN_MESSAGE("Allocating work places");
00095     city.workAllocation();
00096     PROGRESS_DONE_MESSAGE();
00097 
00098     PROGRESS_BEGIN_MESSAGE("Allocating car places");
00099     city.carAllocation();
00100     PROGRESS_DONE_MESSAGE();
00101 }
00102 
00103 bool
00104 AGActivityGen::timeTripValidation(AGTrip trip) {
00105     if (trip.getDay() > durationInDays + 1) {
00106         return false;
00107     }
00108     if (trip.getDay() == 1) { //first day
00109         if (trip.getTime() < beginTime) {
00110             return false;
00111         }
00112         if (durationInDays == 0 && trip.getTime() > endTime) {
00113             return false;
00114         }
00115     }
00116     if (trip.getDay() == durationInDays + 1) { //last day
00117         if (trip.getTime() > endTime) {
00118             return false;
00119         }
00120         if (durationInDays == 0 && trip.getTime() < beginTime) {
00121             return false;
00122         }
00123     }
00124     return true;
00125 }
00126 
00127 void
00128 AGActivityGen::varDepTime(AGTrip& trip) {
00129     if (trip.getType() != "default") {
00130         return;
00131     }
00132     //buses are on time and random are already spread
00133     int variation = (int)RandHelper::randNorm(0, city.statData.departureVariation);
00134     AGTime depTime(trip.getDay(), 0, 0, trip.getTime());
00135     depTime += variation;
00136     if (depTime.getDay() > 0) {
00137         trip.setDay(depTime.getDay());
00138         trip.setDepTime(depTime.getSecondsInCurrentDay());
00139     } else {
00140         trip.setDay(1);
00141         trip.setDepTime(0);
00142     }
00143 }
00144 
00145 void
00146 AGActivityGen::generateOutputFile(std::list<AGTrip>& trips) {
00147     AGActivityTripWriter atw(outputFile);
00148     std::list<AGTrip>::iterator it;
00149     //variables for TESTS:
00150     int firstTrip = trips.front().getTime() + trips.front().getDay() * 86400;
00151     int lastTrip = trips.front().getTime() + trips.front().getDay() * 86400;
00152     std::map<int, int> histogram;
00153     for (int i = 0 ; i < 100 ; ++i) {
00154         histogram[i] = 0;
00155     }
00156     //END var TESTS
00157     for (it = trips.begin() ; it != trips.end() ; ++it) {
00158         atw.addTrip(*it);
00159         //TEST
00160         if (it->getTime() + 86400 * it->getDay() > lastTrip) {
00161             lastTrip = it->getTime() + 86400 * it->getDay();
00162         }
00163         if (it->getTime() + 86400 * it->getDay() < firstTrip) {
00164             firstTrip = it->getTime() + 86400 * it->getDay();
00165         }
00166         //++histogram[((it->getDay()-1)*86400 + it->getTime())/3600];
00167         ++histogram[(it->getTime()) / 3600];
00168         //END TEST
00169     }
00170     //PRINT TEST
00171     AGTime first(firstTrip);
00172     AGTime last(lastTrip);
00173     std::cout << "first real trip: " << first.getDay() << ", " << first.getHour() << ":" << first.getMinute() << ":" << first.getSecond() << std::endl;
00174     std::cout << "last real trip: " << last.getDay() << ", " << last.getHour() << ":" << last.getMinute() << ":" << last.getSecond() << std::endl;
00175     for (int i = 0 ; i < 100 ; ++i) {
00176         if (histogram[i] > 0) {
00177             std::cout << "histogram[ hour " << i << " ] = " << histogram[i] << std::endl;
00178         }
00179     }
00180     //END TEST
00181     atw.writeOutputFile();
00182 }
00183 
00184 void
00185 AGActivityGen::makeActivityTrips(int days, int beginSec, int endSec) {
00186     durationInDays = days;
00187     beginTime = beginSec;
00188     endTime = endSec;
00192     AGActivities acts(&city, durationInDays + 1);
00193     acts.generateActivityTrips();
00194 
00198     //list<Trip>* trips = &(acts.trips);
00199     std::list<AGTrip> expTrips;
00200     std::map<std::string, int> carUsed;
00201     std::list<AGTrip>::iterator it;
00202     //multiplication of days
00203     for (it = acts.trips.begin() ; it != acts.trips.end() ; ++it) {
00204         if (it->isDaily()) {
00205             for (int currday = 1 ; currday < durationInDays + 2 ; ++currday) {
00206                 AGTrip tr(it->getDep(), it->getArr(), it->getVehicleName(), it->getTime(), currday);
00207                 tr.setType(it->getType());
00208                 if (carUsed.find(tr.getVehicleName()) != carUsed.end()) {
00209                     ++carUsed.find(tr.getVehicleName())->second;
00210                 } else {
00211                     carUsed[tr.getVehicleName()] = 1;
00212                 }
00213                 std::ostringstream os;
00214                 os << tr.getVehicleName() << ":" << carUsed.find(tr.getVehicleName())->second;
00215                 tr.setVehicleName(os.str());
00216                 tr.addLayOverWithoutDestination(*it); //intermediate destinations are taken in account too
00217                 varDepTime(tr); //slight variation on each "default" car
00218                 if (timeTripValidation(tr)) {
00219                     expTrips.push_back(tr);
00220                 }
00221                 //else
00222                 //  std::cout << "trop tard 1 pour " << tr.getVehicleName() << " " << tr.getTime() << " day: " << tr.getDay() << std::endl;
00223             }
00224         } else {
00225             AGTrip tr(it->getDep(), it->getArr(), it->getVehicleName(), it->getTime(), it->getDay());
00226             tr.setType(it->getType());
00227             if (carUsed.find(tr.getVehicleName()) != carUsed.end()) {
00228                 ++carUsed.find(tr.getVehicleName())->second;
00229             } else {
00230                 carUsed[tr.getVehicleName()] = 1;
00231             }
00232             std::ostringstream os;
00233             os << tr.getVehicleName() << ":" << carUsed.find(tr.getVehicleName())->second;
00234             tr.setVehicleName(os.str());
00235             tr.addLayOverWithoutDestination(*it); //intermediate destinations are taken in account too
00236             varDepTime(tr); //slight variation on each "default" car
00237             if (timeTripValidation(tr)) {
00238                 expTrips.push_back(tr);
00239             }
00240             //else
00241             //  std::cout << "trop tard 2 pour " << tr.getVehicleName() << " " << tr.getTime() << " day: " << tr.getDay() << std::endl;
00242         }
00243     }
00244 
00245     std::cout << "total trips generated: " << acts.trips.size() << std::endl;
00246     std::cout << "total trips finally taken: " << expTrips.size() << std::endl;
00247 
00251     expTrips.sort(); //natural order of trips
00252     std::cout << "...sorted by departure time.\n" << std::endl;
00253 
00257     generateOutputFile(expTrips);
00258 }
00259 
00260 /****************************************************************************/
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines