SUMO - Simulation of Urban MObility
MS_E2_ZS_CollectorOverLanes.cpp
Go to the documentation of this file.
00001 /****************************************************************************/
00008 // A detector which joins E2Collectors over consecutive lanes (backward)
00009 /****************************************************************************/
00010 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00011 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00012 /****************************************************************************/
00013 //
00014 //   This file is part of SUMO.
00015 //   SUMO 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 3 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 <microsim/MSEdge.h>
00033 #include <microsim/MSLane.h>
00034 #include "MS_E2_ZS_CollectorOverLanes.h"
00035 #include "MSE2Collector.h"
00036 
00037 #ifdef CHECK_MEMORY_LEAKS
00038 #include <foreign/nvwa/debug_new.h>
00039 #endif // CHECK_MEMORY_LEAKS
00040 
00041 
00042 // ===========================================================================
00043 // method definitions
00044 // ===========================================================================
00045 MS_E2_ZS_CollectorOverLanes::MS_E2_ZS_CollectorOverLanes(const std::string& id,
00046         DetectorUsage usage,
00047         MSLane* lane,
00048         SUMOReal startPos,
00049         SUMOTime haltingTimeThreshold,
00050         SUMOReal haltingSpeedThreshold,
00051         SUMOReal jamDistThreshold)
00052     : MSDetectorFileOutput(id),
00053       startPosM(startPos), haltingTimeThresholdM(haltingTimeThreshold),
00054       haltingSpeedThresholdM(haltingSpeedThreshold), jamDistThresholdM(jamDistThreshold),
00055       myID(id), myStartLaneID(lane->getID()), myUsage(usage) {}
00056 
00057 
00058 void
00059 MS_E2_ZS_CollectorOverLanes::init(MSLane* lane, SUMOReal detLength) {
00060     myLength = detLength;
00061     if (startPosM == 0) {
00062         startPosM = (SUMOReal) 0.1;
00063     }
00064     SUMOReal length = lane->getLength() - startPosM - (SUMOReal) 0.1;
00065     SUMOReal dlength = detLength;
00066     if (length > dlength) {
00067         length = dlength;
00068     }
00069     myLengths.push_back(length);
00070     myLaneCombinations.push_back(LaneVector());
00071     myLaneCombinations[0].push_back(lane);
00072     myDetectorCombinations.push_back(DetectorVector());
00073     MSE2Collector* c =
00074         buildCollector(0, 0, lane, startPosM, length);
00075     myDetectorCombinations[0].push_back(c);
00076     myAlreadyBuild[lane] = c;
00077     extendTo(detLength);
00078 }
00079 
00080 
00081 MS_E2_ZS_CollectorOverLanes::~MS_E2_ZS_CollectorOverLanes() {}
00082 
00083 
00084 void
00085 MS_E2_ZS_CollectorOverLanes::extendTo(SUMOReal length) {
00086     bool done = false;
00087     while (!done) {
00088         done = true;
00089         LengthVector::iterator leni = myLengths.begin();
00090         LaneVectorVector::iterator lanei = myLaneCombinations.begin();
00091         DetectorVectorVector::iterator deti = myDetectorCombinations.begin();
00092         for (; leni != myLengths.end(); leni++, lanei++, deti++) {
00093             if ((*leni) < length) {
00094                 done = false;
00095                 // copy current values
00096                 LaneVector lv = *lanei;
00097                 DetectorVector dv = *deti;
00098                 SUMOReal clength = *leni;
00099                 assert(lv.size() > 0);
00100                 assert(dv.size() > 0);
00101                 // erase previous elements
00102                 assert(leni != myLengths.end());
00103                 myLengths.erase(leni);
00104                 myLaneCombinations.erase(lanei);
00105                 myDetectorCombinations.erase(deti);
00106                 // get the lane to look before
00107                 MSLane* toExtend = lv.back();
00108                 // and her predecessors
00109                 std::vector<MSLane*> predeccessors = getLanePredeccessorLanes(toExtend);
00110                 if (predeccessors.size() == 0) {
00111                     int off = 1;
00112                     MSEdge& e = toExtend->getEdge();
00113                     const std::vector<MSLane*> &lanes = e.getLanes();
00114                     int idx = (int) distance(lanes.begin(), find(lanes.begin(), lanes.end(), toExtend));
00115                     while (predeccessors.size() == 0) {
00116                         if (idx - off >= 0) {
00117                             MSLane* tryMe = lanes[idx - off];
00118                             predeccessors = getLanePredeccessorLanes(tryMe);
00119                         }
00120                         if (predeccessors.size() == 0 && idx + off < (int) lanes.size()) {
00121                             MSLane* tryMe = lanes[idx + off];
00122                             predeccessors = getLanePredeccessorLanes(tryMe);
00123                         }
00124                         off++;
00125                     }
00126                 }
00127 
00128                 /*                LaneContinuations::const_iterator conts =
00129                                     laneContinuations.find(toExtend->id());
00130                                 assert(conts!=laneContinuations.end());
00131                                 const std::vector<std::string> &predeccessors =
00132                                     (*conts).second;*/
00133                 // go through the predeccessors and extend the detector
00134                 for (std::vector<MSLane*>::const_iterator i = predeccessors.begin(); i != predeccessors.end(); i++) {
00135                     // get the lane
00136                     MSLane* l = *i;
00137                     // compute detector length
00138                     SUMOReal lanelen = length - clength;
00139                     if (lanelen > l->getLength()) {
00140                         lanelen = l->getLength() - (SUMOReal) 0.2;
00141                     }
00142                     // build new info
00143                     LaneVector nlv = lv;
00144                     nlv.push_back(l);
00145                     DetectorVector ndv = dv;
00146                     MSE2Collector* coll = 0;
00147                     if (myAlreadyBuild.find(l) == myAlreadyBuild.end()) {
00148                         coll = buildCollector(0, 0, l, (SUMOReal) 0.1, lanelen);
00149                     } else {
00150                         coll = myAlreadyBuild.find(l)->second;
00151                     }
00152                     myAlreadyBuild[l] = coll;
00153                     ndv.push_back(coll);
00154                     // store new info
00155                     myLaneCombinations.push_back(nlv);
00156                     myDetectorCombinations.push_back(ndv);
00157                     myLengths.push_back(clength + lanelen);
00158                 }
00159                 // restart
00160                 leni = myLengths.end() - 1;
00161             }
00162         }
00163     }
00164 }
00165 
00166 
00167 std::vector<MSLane*>
00168 MS_E2_ZS_CollectorOverLanes::getLanePredeccessorLanes(MSLane* l) {
00169     std::string eid = l->getEdge().getID();
00170     // get predecessing edges
00171     const std::vector<MSEdge*> &predEdges = l->getEdge().getIncomingEdges();
00172     std::vector<MSLane*> ret;
00173     // find predecessing lanes
00174     std::vector<MSEdge*>::const_iterator i = predEdges.begin();
00175     for (; i != predEdges.end(); ++i) {
00176         MSEdge* e = *i;
00177         assert(e != 0);
00178         typedef std::vector<MSLane*> LaneVector;
00179         const LaneVector* cl = e->allowedLanes(l->getEdge(), SVC_UNKNOWN);
00180         bool fastAbort = false;
00181         if (cl != 0) {
00182             for (LaneVector::const_iterator j = cl->begin(); !fastAbort && j != cl->end(); j++) {
00183                 const MSLinkCont& lc = (*j)->getLinkCont();
00184                 for (MSLinkCont::const_iterator k = lc.begin(); !fastAbort && k != lc.end(); k++) {
00185                     if ((*k)->getLane() == l) {
00186                         ret.push_back(*j);
00187                         fastAbort = true;
00188                     }
00189                 }
00190             }
00191         }
00192     }
00193     return ret;
00194 }
00195 
00196 
00197 MSE2Collector*
00198 MS_E2_ZS_CollectorOverLanes::buildCollector(size_t c, size_t r, MSLane* l,
00199         SUMOReal start, SUMOReal end) {
00200     std::string id = makeID(l->getID(), c, r);
00201     if (start + end < l->getLength()) {
00202         start = l->getLength() - end - (SUMOReal) 0.1;
00203     }
00204     return new MSE2Collector(id, myUsage,
00205                              l, start, end, haltingTimeThresholdM,
00206                              haltingSpeedThresholdM, jamDistThresholdM);
00207 }
00208 
00209 
00210 void
00211 MS_E2_ZS_CollectorOverLanes::writeXMLOutput(OutputDevice& /*&dev*/,
00212         SUMOTime /*startTime*/, SUMOTime /*stopTime*/) {
00213     /*
00214     dev<<"<interval begin=\""<<time2string(startTime)<<"\" end=\""<<
00215     time2string(stopTime)<<"\" "<<"id=\""<<myID<<"\" ";
00216     if (hasDetector(E2::QUEUE_LENGTH_AHEAD_OF_TRAFFIC_LIGHTS_IN_VEHICLES)) {
00217         dev<<"collQueueLengthAheadOfTrafficLightsInVehiclesMax=\"";
00218         dev<<toString(getCurrent(E2::QUEUE_LENGTH_AHEAD_OF_TRAFFIC_LIGHTS_IN_VEHICLES));
00219         dev<<"\" ";
00220         resetQueueLengthAheadOfTrafficLights();
00221     }
00222     myDetectorCombinations[0][0]->writeXMLOutput(dev, startTime, stopTime);
00223     dev<<"/>\n";
00224     */
00225 }
00226 
00227 
00228 void
00229 MS_E2_ZS_CollectorOverLanes::writeXMLDetectorProlog(OutputDevice& dev) const {
00230     dev.writeXMLHeader("detector");
00231 }
00232 
00233 
00234 size_t bla = 0;
00235 
00236 std::string
00237 MS_E2_ZS_CollectorOverLanes::makeID(const std::string& baseID ,
00238                                     size_t /*col*/, size_t /*row*/) const {
00239     std::string add;
00240     switch (myUsage) {
00241         case DU_USER_DEFINED:
00242             add = "(u)";
00243             break;
00244         case DU_SUMO_INTERNAL:
00245             add = "(i)";
00246             break;
00247         case DU_TL_CONTROL:
00248             add = "(c)";
00249             break;
00250         default:
00251             break;
00252     }
00253     std::string ret =  baseID + add + toString<size_t>(bla++);
00254     return ret;
00255 }
00256 
00257 
00258 const std::string&
00259 MS_E2_ZS_CollectorOverLanes::getID() const {
00260     return myID;
00261 }
00262 
00263 
00264 const std::string&
00265 MS_E2_ZS_CollectorOverLanes::getStartLaneID() const {
00266     return myStartLaneID;
00267 }
00268 
00269 
00270 /****************************************************************************/
00271 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines