SUMO - Simulation of Urban MObility
|
00001 /****************************************************************************/ 00011 // Bus line of the city: contains all the buses of this line 00012 /****************************************************************************/ 00013 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/ 00014 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors 00015 // activitygen module 00016 // Copyright 2010 TUM (Technische Universitaet Muenchen, http://www.tum.de/) 00017 /****************************************************************************/ 00018 // 00019 // This file is part of SUMO. 00020 // SUMO is free software: you can redistribute it and/or modify 00021 // it under the terms of the GNU General Public License as published by 00022 // the Free Software Foundation, either version 3 of the License, or 00023 // (at your option) any later version. 00024 // 00025 /****************************************************************************/ 00026 00027 00028 // =========================================================================== 00029 // included modules 00030 // =========================================================================== 00031 #ifdef _MSC_VER 00032 #include <windows_config.h> 00033 #else 00034 #include <config.h> 00035 #endif 00036 00037 #include <iostream> 00038 #include <utility> 00039 #include <sstream> 00040 #include <string> 00041 #include <list> 00042 #include "AGBusLine.h" 00043 #include "AGBus.h" 00044 #include "AGPosition.h" 00045 #include "AGTime.h" 00046 #include <utils/common/StdDefs.h> 00047 00048 #define PAUSE_TIME 15 //time (in minutes) a bus waits before going in the opposite direction. 00049 00050 00051 // =========================================================================== 00052 // method definitions 00053 // =========================================================================== 00054 void 00055 AGBusLine::setMaxTripTime(int time) { 00056 this->maxTripTime = time; 00057 } 00058 00059 void 00060 AGBusLine::setBusNames() { 00061 busNbr = 0; 00062 std::list<AGBus>::iterator it1 = buses.begin(); //iterator on buses in the first direction 00063 std::list<AGBus>::iterator it2 = revBuses.begin(); //iterator on buses in the second direction 00064 00065 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 00066 00067 while (it1 != buses.end() && it2 != revBuses.end()) { 00068 if (it1->getDeparture() > it2->getDeparture()) { 00069 if (drivingBuses2.size() == 0) { 00070 drivingBuses2.push_front(make_pair(it2->getDeparture(), createName())); 00071 } else if (drivingBuses2.front().first > it2->getDeparture()) { 00072 drivingBuses2.push_front(make_pair(it2->getDeparture(), createName())); 00073 } 00074 //here the first in drivingBuses2 is available for the trip 00075 it2->setName(drivingBuses2.front().second); 00076 drivingBuses2.pop_front(); 00077 //the same bus will be available for the main direction after some time (see function getReady): 00078 drivingBuses1.push_back(make_pair(getReady(it2->getDeparture()), it2->getName())); 00079 it2++; 00080 } else { 00081 if (drivingBuses1.size() == 0) { 00082 drivingBuses1.push_front(make_pair(it1->getDeparture(), createName())); 00083 } else if (drivingBuses1.front().first > it1->getDeparture()) { 00084 drivingBuses1.push_front(make_pair(it1->getDeparture(), createName())); 00085 } 00086 //here the first in drivingBuses1 is available for the trip 00087 it1->setName(drivingBuses1.front().second); 00088 drivingBuses1.pop_front(); 00089 //the same bus will be available for the return way after some time (see function getReady): 00090 drivingBuses2.push_back(make_pair(getReady(it1->getDeparture()), it1->getName())); 00091 it1++; 00092 } 00093 } 00094 if (it1 != buses.end()) { 00095 if (drivingBuses1.size() == 0) { 00096 it1->setName(createName()); 00097 } else if (drivingBuses1.front().first > it1->getDeparture()) { 00098 it1->setName(createName()); 00099 } else { 00100 it1->setName(drivingBuses1.front().second); 00101 drivingBuses1.pop_front(); 00102 } 00103 it1++; 00104 } 00105 if (it2 != revBuses.end()) { 00106 if (drivingBuses2.size() == 0) { 00107 it2->setName(createName()); 00108 } else if (drivingBuses2.front().first > it2->getDeparture()) { 00109 it2->setName(createName()); 00110 } else { 00111 it2->setName(drivingBuses2.front().second); 00112 drivingBuses2.pop_front(); 00113 } 00114 it2++; 00115 } 00116 } 00117 00118 std::string 00119 AGBusLine::createName() { 00120 ++busNbr; //initialized in setBusNames() 00121 std::ostringstream os; 00122 os << busNbr; 00123 return "bl" + lineNumber + "b" + os.str(); 00124 } 00125 00126 int 00127 AGBusLine::getReady(int time) { 00128 AGTime current(time); 00129 current.addSeconds(maxTripTime); 00130 current.addMinutes(PAUSE_TIME); 00131 return current.getTime(); 00132 } 00133 00134 int 00135 AGBusLine::nbrBuses() { 00136 return static_cast<int>(buses.size()); 00137 } 00138 00139 void 00140 AGBusLine::locateStation(AGPosition pos) { 00141 stations.push_back(pos); 00142 } 00143 00144 void 00145 AGBusLine::locateRevStation(AGPosition pos) { 00146 revStations.push_back(pos); 00147 } 00148 00149 void 00150 AGBusLine::generateBuses(int start, int stop, int rate) { 00151 int t = start; 00152 AGBus* bus; 00153 UNUSED_PARAMETER(bus); 00154 while (t < stop) { 00155 buses.push_back(AGBus(t)); //one direction 00156 revBuses.push_back(AGBus(t)); //return direction 00157 t += rate; 00158 } 00159 } 00160 00161 00162 void 00163 AGBusLine::printBuses() { 00164 std::list<AGBus>::iterator it; 00165 std::cout << "\n ----------- BUS LINE " << lineNumber << " PRINTING -------------\n" << std::endl; 00166 std::cout << "\n -------------------------- First way ---------------------------\n" << std::endl; 00167 for (it = buses.begin() ; it != buses.end() ; ++it) { 00168 it->print(); 00169 } 00170 std::cout << "\n -------------------------- Second way --------------------------\n" << std::endl; 00171 for (it = revBuses.begin() ; it != revBuses.end() ; ++it) { 00172 it->print(); 00173 } 00174 std::cout << "\n ----------------------------------------------------------------\n" << std::endl; 00175 } 00176 00177 /****************************************************************************/