SUMO - Simulation of Urban MObility

AGBusLine.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // Bus line of the city: contains all the buses of this line
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 <utility>
00034 #include <sstream>
00035 #include <string>
00036 #include <list>
00037 #include "AGBusLine.h"
00038 #include "AGBus.h"
00039 #include "AGPosition.h"
00040 #include "AGTime.h"
00041 #include <utils/common/StdDefs.h>
00042 
00043 #define PAUSE_TIME 15       //time (in minutes) a bus waits before going in the opposite direction.
00044 
00045 
00046 // ===========================================================================
00047 // method definitions
00048 // ===========================================================================
00049 void
00050 AGBusLine::setMaxTripTime(int time) {
00051     this->maxTripTime = time;
00052 }
00053 
00054 void
00055 AGBusLine::setBusNames() {
00056     busNbr = 0;
00057     std::list<AGBus>::iterator it1 = buses.begin(); //iterator on buses in the first direction
00058     std::list<AGBus>::iterator it2 = revBuses.begin();  //iterator on buses in the second direction
00059 
00060     std::list<std::pair<int, std::string> > drivingBuses1, drivingBuses2;   //buses on the road or in the parking of the corresponding end: int: the time of availability
00061 
00062     while (it1!= buses.end() && it2!=revBuses.end()) {
00063         if (it1->getDeparture() > it2->getDeparture()) {
00064             if (drivingBuses2.size() == 0) {
00065                 drivingBuses2.push_front(make_pair(it2->getDeparture(), createName()));
00066             } else if (drivingBuses2.front().first > it2->getDeparture()) {
00067                 drivingBuses2.push_front(make_pair(it2->getDeparture(), createName()));
00068             }
00069             //here the first in drivingBuses2 is available for the trip
00070             it2->setName(drivingBuses2.front().second);
00071             drivingBuses2.pop_front();
00072             //the same bus will be available for the main direction after some time (see function getReady):
00073             drivingBuses1.push_back(make_pair(getReady(it2->getDeparture()), it2->getName()));
00074             it2++;
00075         } else {
00076             if (drivingBuses1.size() == 0) {
00077                 drivingBuses1.push_front(make_pair(it1->getDeparture(), createName()));
00078             } else if (drivingBuses1.front().first > it1->getDeparture()) {
00079                 drivingBuses1.push_front(make_pair(it1->getDeparture(), createName()));
00080             }
00081             //here the first in drivingBuses1 is available for the trip
00082             it1->setName(drivingBuses1.front().second);
00083             drivingBuses1.pop_front();
00084             //the same bus will be available for the return way after some time (see function getReady):
00085             drivingBuses2.push_back(make_pair(getReady(it1->getDeparture()), it1->getName()));
00086             it1++;
00087         }
00088     }
00089     if (it1 != buses.end()) {
00090         if (drivingBuses1.size() == 0) {
00091             it1->setName(createName());
00092         } else if (drivingBuses1.front().first > it1->getDeparture()) {
00093             it1->setName(createName());
00094         } else {
00095             it1->setName(drivingBuses1.front().second);
00096             drivingBuses1.pop_front();
00097         }
00098         it1++;
00099     }
00100     if (it2 != revBuses.end()) {
00101         if (drivingBuses2.size() == 0) {
00102             it2->setName(createName());
00103         } else if (drivingBuses2.front().first > it2->getDeparture()) {
00104             it2->setName(createName());
00105         } else {
00106             it2->setName(drivingBuses2.front().second);
00107             drivingBuses2.pop_front();
00108         }
00109         it2++;
00110     }
00111 }
00112 
00113 std::string
00114 AGBusLine::createName() {
00115     ++busNbr; //initialized in setBusNames()
00116     std::ostringstream os;
00117     os << busNbr;
00118     return "bl" + lineNumber + "b" + os.str();
00119 }
00120 
00121 int
00122 AGBusLine::getReady(int time) {
00123     AGTime current(time);
00124     current.addSeconds(maxTripTime);
00125     current.addMinutes(PAUSE_TIME);
00126     return current.getTime();
00127 }
00128 
00129 int
00130 AGBusLine::nbrBuses() {
00131     return static_cast<int>(buses.size());
00132 }
00133 
00134 void
00135 AGBusLine::locateStation(AGPosition pos) {
00136     stations.push_back(pos);
00137 }
00138 
00139 void
00140 AGBusLine::locateRevStation(AGPosition pos) {
00141     revStations.push_back(pos);
00142 }
00143 
00144 void
00145 AGBusLine::generateBuses(int start, int stop, int rate) {
00146     int t = start;
00147     AGBus* bus;
00148     UNUSED_PARAMETER(bus);
00149     while (t < stop) {
00150         buses.push_back(AGBus(t)); //one direction
00151         revBuses.push_back(AGBus(t)); //return direction
00152         t += rate;
00153     }
00154 }
00155 
00156 
00157 void
00158 AGBusLine::printBuses() {
00159     std::list<AGBus>::iterator it;
00160     std::cout << "\n ----------- BUS LINE " << lineNumber << " PRINTING -------------\n" << std::endl;
00161     std::cout << "\n -------------------------- First way ---------------------------\n" << std::endl;
00162     for (it=buses.begin() ; it!=buses.end() ; ++it) {
00163         it->print();
00164     }
00165     std::cout << "\n -------------------------- Second way --------------------------\n" << std::endl;
00166     for (it=revBuses.begin() ; it!=revBuses.end() ; ++it) {
00167         it->print();
00168     }
00169     std::cout << "\n ----------------------------------------------------------------\n" << std::endl;
00170 }
00171 
00172 /****************************************************************************/
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines