SUMO - Simulation of Urban MObility

AGActivityGen.cpp

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