MS_E2_ZS_CollectorOverLanes.cpp

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

Generated on Wed May 5 00:06:30 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6