RODFNet.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // A DFROUTER-network
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 // included modules
00021 // ===========================================================================
00022 #ifdef _MSC_VER
00023 #include <windows_config.h>
00024 #else
00025 #include <config.h>
00026 #endif
00027 
00028 #include <iostream>
00029 #include <map>
00030 #include <vector>
00031 #include "RODFNet.h"
00032 #include "RODFDetector.h"
00033 #include "RODFRouteDesc.h"
00034 #include "RODFDetectorFlow.h"
00035 #include "RODFEdge.h"
00036 #include <cmath>
00037 #include <utils/common/MsgHandler.h>
00038 #include <utils/common/ToString.h>
00039 #include <utils/common/UtilExceptions.h>
00040 #include <utils/geom/GeomHelper.h>
00041 
00042 #ifdef CHECK_MEMORY_LEAKS
00043 #include <foreign/nvwa/debug_new.h>
00044 #endif // CHECK_MEMORY_LEAKS
00045 
00046 
00047 // ===========================================================================
00048 // method definitions
00049 // ===========================================================================
00050 RODFNet::RODFNet(bool amInHighwayMode) throw()
00051         : RONet(), myAmInHighwayMode(amInHighwayMode),
00052         mySourceNumber(0), mySinkNumber(0), myInBetweenNumber(0), myInvalidNumber(0) {
00053     myDisallowedEdges = OptionsCont::getOptions().getStringVector("disallowed-edges");
00054     myKeepTurnarounds = OptionsCont::getOptions().getBool("keep-turnarounds");
00055 }
00056 
00057 
00058 RODFNet::~RODFNet() throw() {
00059 }
00060 
00061 
00062 void
00063 RODFNet::buildApproachList() {
00064     const std::map<std::string, ROEdge*> &edges = getEdgeMap();
00065     for (std::map<std::string, ROEdge*>::const_iterator rit = edges.begin(); rit != edges.end(); ++rit) {
00066         ROEdge *ce = (*rit).second;
00067         unsigned int i = 0;
00068         unsigned int length_size = ce->getNoFollowing();
00069         for (i=0; i<length_size; i++) {
00070             ROEdge *help = ce->getFollower(i);
00071             if (find(myDisallowedEdges.begin(), myDisallowedEdges.end(), help->getID())!=myDisallowedEdges.end()) {
00072                 // edges in sinks will not be used
00073                 continue;
00074             }
00075             if (!myKeepTurnarounds && help->getToNode()==ce->getFromNode()) {
00076                 // do not use turnarounds
00077                 continue;
00078             }
00079             // add the connection help->ce to myApproachingEdges
00080             if (myApproachingEdges.find(help)==myApproachingEdges.end()) {
00081                 myApproachingEdges[help] = std::vector<ROEdge*>();
00082             }
00083             myApproachingEdges[help].push_back(ce);
00084             // add the connection ce->help to myApproachingEdges
00085             if (myApproachedEdges.find(ce)==myApproachedEdges.end()) {
00086                 myApproachedEdges[ce] = std::vector<ROEdge*>();
00087             }
00088             myApproachedEdges[ce].push_back(help);
00089         }
00090     }
00091 }
00092 
00093 
00094 void
00095 RODFNet::buildDetectorEdgeDependencies(RODFDetectorCon &detcont) const {
00096     myDetectorsOnEdges.clear();
00097     myDetectorEdges.clear();
00098     const std::vector<RODFDetector*> &dets = detcont.getDetectors();
00099     {
00100         for (std::vector<RODFDetector*>::const_iterator i=dets.begin(); i!=dets.end(); ++i) {
00101             ROEdge *e = getDetectorEdge(**i);
00102 
00103             if (myDetectorsOnEdges.find(e)==myDetectorsOnEdges.end()) {
00104                 myDetectorsOnEdges[e] = std::vector<std::string>();
00105             }
00106             myDetectorsOnEdges[e].push_back((*i)->getID());
00107 
00108             myDetectorEdges[(*i)->getID()] = e;
00109         }
00110     }
00111 }
00112 
00113 
00114 void
00115 RODFNet::computeTypes(RODFDetectorCon &detcont,
00116                       bool sourcesStrict) const {
00117     MsgHandler::getMessageInstance()->beginProcessMsg("Computing detector types...");
00118     const std::vector< RODFDetector*> &dets = detcont.getDetectors();
00119     // build needed information. first
00120     buildDetectorEdgeDependencies(detcont);
00121     // compute detector types then
00122     {
00123         for (std::vector< RODFDetector*>::const_iterator i=dets.begin(); i!=dets.end(); ++i) {
00124             if (isSource(**i, detcont, sourcesStrict)) {
00125                 (*i)->setType(SOURCE_DETECTOR);
00126                 mySourceNumber++;
00127             }
00128             if (isDestination(**i, detcont)) {
00129                 (*i)->setType(SINK_DETECTOR);
00130                 mySinkNumber++;
00131             }
00132             if ((*i)->getType()==TYPE_NOT_DEFINED) {
00133                 (*i)->setType(BETWEEN_DETECTOR);
00134                 myInBetweenNumber++;
00135             }
00136         }
00137     }
00138     // recheck sources
00139     {
00140         for (std::vector< RODFDetector*>::const_iterator i=dets.begin(); i!=dets.end(); ++i) {
00141             if ((*i)->getType()==SOURCE_DETECTOR&&isFalseSource(**i, detcont)) {
00142                 (*i)->setType(DISCARDED_DETECTOR);
00143                 myInvalidNumber++;
00144                 mySourceNumber--;
00145             }
00146         }
00147     }
00148     // print results
00149     MsgHandler::getMessageInstance()->endProcessMsg("done.");
00150     MsgHandler::getMessageInstance()->inform("Computed detector types:");
00151     MsgHandler::getMessageInstance()->inform(" " + toString(mySourceNumber) + " source detectors");
00152     MsgHandler::getMessageInstance()->inform(" " + toString(mySinkNumber) + " sink detectors");
00153     MsgHandler::getMessageInstance()->inform(" " + toString(myInBetweenNumber) + " in-between detectors");
00154     MsgHandler::getMessageInstance()->inform(" " + toString(myInvalidNumber) + " invalid detectors");
00155 }
00156 
00157 
00158 bool
00159 RODFNet::hasInBetweenDetectorsOnly(ROEdge *edge,
00160                                    const RODFDetectorCon &detectors) const {
00161     assert(myDetectorsOnEdges.find(edge)!=myDetectorsOnEdges.end());
00162     const std::vector<std::string> &detIDs = myDetectorsOnEdges.find(edge)->second;
00163     std::vector<std::string>::const_iterator i;
00164     for (i=detIDs.begin(); i!=detIDs.end(); ++i) {
00165         const RODFDetector &det = detectors.getDetector(*i);
00166         if (det.getType()!=BETWEEN_DETECTOR) {
00167             return false;
00168         }
00169     }
00170     return true;
00171 }
00172 
00173 
00174 bool
00175 RODFNet::hasSourceDetector(ROEdge *edge,
00176                            const RODFDetectorCon &detectors) const {
00177     assert(myDetectorsOnEdges.find(edge)!=myDetectorsOnEdges.end());
00178     const std::vector<std::string> &detIDs = myDetectorsOnEdges.find(edge)->second;
00179     std::vector<std::string>::const_iterator i;
00180     for (i=detIDs.begin(); i!=detIDs.end(); ++i) {
00181         const RODFDetector &det = detectors.getDetector(*i);
00182         if (det.getType()==SOURCE_DETECTOR) {
00183             return true;
00184         }
00185     }
00186     return false;
00187 }
00188 
00189 
00190 
00191 void
00192 RODFNet::computeRoutesFor(ROEdge *edge, RODFRouteDesc &base, int /*no*/,
00193                           bool keepUnfoundEnds,
00194                           bool keepShortestOnly,
00195                           std::vector<ROEdge*> &/*visited*/,
00196                           const RODFDetector &det, RODFRouteCont &into,
00197                           const RODFDetectorCon &detectors,
00198                           int maxFollowingLength,
00199                           std::vector<ROEdge*> &seen) const {
00200     std::vector<RODFRouteDesc> unfoundEnds;
00201     std::priority_queue<RODFRouteDesc, std::vector<RODFRouteDesc>, DFRouteDescByTimeComperator> toSolve;
00202     std::map<ROEdge*, std::vector<ROEdge*> > dets2Follow;
00203     dets2Follow[edge] = std::vector<ROEdge*>();
00204     base.passedNo = 0;
00205     SUMOReal minDist = OptionsCont::getOptions().getFloat("min-distance");
00206     toSolve.push(base);
00207     while (!toSolve.empty()) {
00208         RODFRouteDesc current = toSolve.top();
00209         toSolve.pop();
00210         ROEdge *last = *(current.edges2Pass.end()-1);
00211         if (hasDetector(last)) {
00212             if (dets2Follow.find(last)==dets2Follow.end()) {
00213                 dets2Follow[last] = std::vector<ROEdge*>();
00214             }
00215             for (std::vector<ROEdge*>::reverse_iterator i=current.edges2Pass.rbegin()+1; i!=current.edges2Pass.rend(); ++i) {
00216                 if (hasDetector(*i)) {
00217                     dets2Follow[*i].push_back(last);
00218                     break;
00219                 }
00220             }
00221         }
00222 
00223         // do not process an edge twice
00224         if (find(seen.begin(), seen.end(), last)!=seen.end() && keepShortestOnly) {
00225             continue;
00226         }
00227         seen.push_back(last);
00228         // end if the edge has no further connections
00229         if (!hasApproached(last)) {
00230             // ok, no further connections to follow
00231             current.factor = 1.;
00232             SUMOReal cdist = current.edges2Pass[0]->getFromNode()->getPosition().distanceTo(current.edges2Pass.back()->getToNode()->getPosition());
00233             if (minDist<cdist) {
00234                 into.addRouteDesc(current);
00235             }
00236             continue;
00237         }
00238         // check for passing detectors:
00239         //  if the current last edge is not the one the detector is placed on ...
00240         bool addNextNoFurther = false;
00241         if (last!=getDetectorEdge(det)) {
00242             // ... if there is a detector ...
00243             if (hasDetector(last)) {
00244                 if (!hasInBetweenDetectorsOnly(last, detectors)) {
00245                     // ... and it's not an in-between-detector
00246                     // -> let's add this edge and the following, but not any further
00247                     addNextNoFurther = true;
00248                     current.lastDetectorEdge = last;
00249                     current.duration2Last = (SUMOTime) current.duration_2;
00250                     current.distance2Last = current.distance;
00251                     current.endDetectorEdge = last;
00252                     if (hasSourceDetector(last, detectors)) {
00254                     }
00255                     current.factor = 1.;
00256                     SUMOReal cdist = current.edges2Pass[0]->getFromNode()->getPosition().distanceTo(current.edges2Pass.back()->getToNode()->getPosition());
00257                     if (minDist<cdist) {
00258                         into.addRouteDesc(current);
00259                     }
00260                     continue;
00261                 } else {
00262                     // ... if it's an in-between-detector
00263                     // -> mark the current route as to be continued
00264                     current.passedNo = 0;
00265                     current.duration2Last = (SUMOTime) current.duration_2;
00266                     current.distance2Last = current.distance;
00267                     current.lastDetectorEdge = last;
00268                 }
00269             }
00270         }
00271         // check for highway off-ramps
00272         if (myAmInHighwayMode) {
00273             // if it's beside the highway...
00274             if (last->getSpeed()<19.4&&last!=getDetectorEdge(det)) {
00275                 // ... and has more than one following edge
00276                 if (myApproachedEdges.find(last)->second.size()>1) {
00277                     // -> let's add this edge and the following, but not any further
00278                     addNextNoFurther = true;
00279                 }
00280 
00281             }
00282         }
00283         // check for missing end connections
00284         if (!addNextNoFurther) {
00285             // ... if this one would be processed, but already too many edge
00286             //  without a detector occured
00287             if (current.passedNo>maxFollowingLength) {
00288                 // mark not to process any further
00289                 MsgHandler::getWarningInstance()->inform("Could not close route for '" + det.getID() + "'");
00290                 unfoundEnds.push_back(current);
00291                 current.factor = 1.;
00292                 SUMOReal cdist = current.edges2Pass[0]->getFromNode()->getPosition().distanceTo(current.edges2Pass.back()->getToNode()->getPosition());
00293                 if (minDist<cdist) {
00294                     into.addRouteDesc(current);
00295                 }
00296                 continue;
00297             }
00298         }
00299         // ... else: loop over the next edges
00300         const std::vector<ROEdge*> &appr  = myApproachedEdges.find(last)->second;
00301         bool hadOne = false;
00302         for (size_t i=0; i<appr.size(); i++) {
00303             if (find(current.edges2Pass.begin(), current.edges2Pass.end(), appr[i])!=current.edges2Pass.end()) {
00304                 // do not append an edge twice (do not build loops)
00305                 continue;
00306             }
00307             RODFRouteDesc t(current);
00308             t.duration_2 += (appr[i]->getLength()/appr[i]->getSpeed());
00309             t.distance += appr[i]->getLength();
00310             t.edges2Pass.push_back(appr[i]);
00311             if (!addNextNoFurther) {
00312                 t.passedNo = t.passedNo + 1;
00313                 toSolve.push(t);
00314             } else {
00315                 if (!hadOne) {
00316                     t.factor = (SUMOReal) 1. / (SUMOReal) appr.size();
00317                     SUMOReal cdist = current.edges2Pass[0]->getFromNode()->getPosition().distanceTo(current.edges2Pass.back()->getToNode()->getPosition());
00318                     if (minDist<cdist) {
00319                         into.addRouteDesc(t);
00320                     }
00321                     hadOne = true;
00322                 }
00323             }
00324         }
00325     }
00326     //
00327     if (!keepUnfoundEnds) {
00328         std::vector<RODFRouteDesc>::iterator i;
00329         std::vector<const ROEdge*> lastDetEdges;
00330         for (i=unfoundEnds.begin(); i!=unfoundEnds.end(); ++i) {
00331             if (find(lastDetEdges.begin(), lastDetEdges.end(), (*i).lastDetectorEdge)==lastDetEdges.end()) {
00332                 lastDetEdges.push_back((*i).lastDetectorEdge);
00333             } else {
00334                 bool ok = into.removeRouteDesc(*i);
00335                 assert(ok);
00336             }
00337         }
00338     } else {
00339         // !!! patch the factors
00340     }
00341     while (!toSolve.empty()) {
00342 //        RODFRouteDesc d = toSolve.top();
00343         toSolve.pop();
00344 //        delete d;
00345     }
00346 }
00347 
00348 
00349 void
00350 RODFNet::buildRoutes(RODFDetectorCon &detcont, bool allEndFollower,
00351                      bool keepUnfoundEnds, bool includeInBetween,
00352                      bool keepShortestOnly, int maxFollowingLength) const {
00353     /*
00354     std::vector<std::vector<ROEdge*> > illegals;
00355     std::vector<ROEdge*> i1;
00356     i1.push_back(getEdge("-51140604"));
00357     i1.push_back(getEdge("-51140594"));
00358     i1.push_back(getEdge("51140072"));
00359     i1.push_back(getEdge("51140612"));
00360     illegals.push_back(i1);
00361     std::vector<ROEdge*> i2;
00362     i2.push_back(getEdge("-51047761"));
00363     i2.push_back(getEdge("-51047760"));
00364     i2.push_back(getEdge("51047759"));
00365     i2.push_back(getEdge("51047758"));
00366     illegals.push_back(i2);
00367     std::vector<ROEdge*> i3;
00368     i3.push_back(getEdge("-51049672"));
00369     i3.push_back(getEdge("-51049675"));
00370     i3.push_back(getEdge("51049662"));
00371     i3.push_back(getEdge("51049676"));
00372     illegals.push_back(i3);
00373     std::vector<ROEdge*> i4;
00374     i4.push_back(getEdge("-51069817"));
00375     i4.push_back(getEdge("-51069812"));
00376     i4.push_back(getEdge("51069813"));
00377     i4.push_back(getEdge("51069815"));
00378     illegals.push_back(i4);
00379     std::vector<ROEdge*> i5;
00380     i5.push_back(getEdge("50872831"));
00381     i5.push_back(getEdge("-50872833"));
00382     i5.push_back(getEdge("-50872829"));
00383     i5.push_back(getEdge("572267133"));
00384     illegals.push_back(i5);
00385     std::vector<ROEdge*> i6;
00386     i6.push_back(getEdge("-51066847"));
00387     i6.push_back(getEdge("-51066836"));
00388     i6.push_back(getEdge("51066830"));
00389     i6.push_back(getEdge("51066846"));
00390     illegals.push_back(i6);
00391     */
00392     // build needed information first
00393     buildDetectorEdgeDependencies(detcont);
00394     // then build the routes
00395     std::map<ROEdge*, RODFRouteCont * > doneEdges;
00396     const std::vector< RODFDetector*> &dets = detcont.getDetectors();
00397     for (std::vector< RODFDetector*>::const_iterator i=dets.begin(); i!=dets.end(); ++i) {
00398         if ((*i)->getType()!=SOURCE_DETECTOR) {
00399             // do not build routes for other than sources
00400             //continue;
00401         }
00402         ROEdge *e = getDetectorEdge(**i);
00403         if (doneEdges.find(e)!=doneEdges.end()) {
00404             // use previously build routes
00405             (*i)->addRoutes(new RODFRouteCont(*doneEdges[e]));
00406             continue;
00407         }
00408         std::vector<ROEdge*> seen;
00409         RODFRouteCont *routes = new RODFRouteCont();
00410         doneEdges[e] = routes;
00411         RODFRouteDesc rd;
00412         rd.edges2Pass.push_back(e);
00413         rd.duration_2 = (e->getLength()/e->getSpeed());
00414         rd.endDetectorEdge = 0;
00415         rd.lastDetectorEdge = 0;
00416         rd.distance = e->getLength();
00417         rd.distance2Last = 0;
00418         rd.duration2Last = 0;
00419 
00420         rd.overallProb = 0;
00421 
00422         std::vector<ROEdge*> visited;
00423         visited.push_back(e);
00424         computeRoutesFor(e, rd, 0, keepUnfoundEnds, keepShortestOnly,
00425                          visited, **i, *routes, detcont, maxFollowingLength, seen);
00426         if (allEndFollower) {
00427             routes->addAllEndFollower();
00428         }
00430         (*i)->addRoutes(routes);
00431 
00432         // add routes to in-between detectors if wished
00433         if (includeInBetween) {
00434             // go through the routes
00435             const std::vector<RODFRouteDesc> &r = routes->get();
00436             for (std::vector<RODFRouteDesc>::const_iterator j=r.begin(); j!=r.end(); ++j) {
00437                 const RODFRouteDesc &mrd = *j;
00438                 SUMOReal duration = mrd.duration_2;
00439                 SUMOReal distance = mrd.distance;
00440                 // go through each route's edges
00441                 std::vector<ROEdge*>::const_iterator routeend = mrd.edges2Pass.end();
00442                 for (std::vector<ROEdge*>::const_iterator k=mrd.edges2Pass.begin(); k!=routeend; ++k) {
00443                     // check whether any detectors lies on the current edge
00444                     if (myDetectorsOnEdges.find(*k)==myDetectorsOnEdges.end()) {
00445                         duration -= (*k)->getLength()/(*k)->getSpeed();
00446                         distance -= (*k)->getLength();
00447                         continue;
00448                     }
00449                     // get the detectors
00450                     const std::vector<std::string> &dets = myDetectorsOnEdges.find(*k)->second;
00451                     // go through the detectors
00452                     for (std::vector<std::string>::const_iterator l=dets.begin(); l!=dets.end(); ++l) {
00453                         const RODFDetector &m = detcont.getDetector(*l);
00454                         if (m.getType()==BETWEEN_DETECTOR) {
00455                             RODFRouteDesc nrd;
00456                             copy(k, routeend, back_inserter(nrd.edges2Pass));
00457                             nrd.duration_2 = duration;
00458                             nrd.endDetectorEdge = mrd.endDetectorEdge;
00459                             nrd.lastDetectorEdge = mrd.lastDetectorEdge;
00460                             nrd.distance = distance;
00461                             nrd.distance2Last = mrd.distance2Last;
00462                             nrd.duration2Last = mrd.duration2Last;
00463                             nrd.overallProb = mrd.overallProb;
00464                             nrd.factor = mrd.factor;
00465                             ((RODFDetector&) m).addRoute(nrd);
00466                         }
00467                     }
00468                     duration -= (*k)->getLength()/(*k)->getSpeed();
00469                     distance -= (*k)->getLength();
00470                 }
00471             }
00472         }
00473 
00474     }
00475 }
00476 
00477 
00478 void
00479 RODFNet::revalidateFlows(const RODFDetector *detector,
00480                          RODFDetectorFlows &flows,
00481                          SUMOTime startTime, SUMOTime endTime,
00482                          SUMOTime stepOffset) {
00483     {
00484         if (flows.knows(detector->getID())) {
00485             const std::vector<FlowDef> &detFlows = flows.getFlowDefs(detector->getID());
00486             for (std::vector<FlowDef>::const_iterator j=detFlows.begin(); j!=detFlows.end(); ++j) {
00487                 if ((*j).qPKW>0||(*j).qLKW>0) {
00488                     return;
00489                 }
00490             }
00491         }
00492     }
00493     // ok, there is no information for the whole time;
00494     //  lets find preceding detectors and rebuild the flows if possible
00495     WRITE_WARNING("Detector '" + detector->getID() + "' has no flows.\n Trying to rebuild.");
00496     // go back and collect flows
00497     std::vector<ROEdge*> previous;
00498     {
00499         std::vector<IterationEdge> missing;
00500         IterationEdge ie;
00501         ie.depth = 0;
00502         ie.edge = getDetectorEdge(*detector);
00503         missing.push_back(ie);
00504         bool maxDepthReached = false;
00505         while (!missing.empty()&&!maxDepthReached) {
00506             IterationEdge last = missing.back();
00507             missing.pop_back();
00508             std::vector<ROEdge*> approaching = myApproachingEdges[last.edge];
00509             for (std::vector<ROEdge*>::const_iterator j=approaching.begin(); j!=approaching.end(); ++j) {
00510                 if (hasDetector(*j)) {
00511                     previous.push_back(*j);
00512                 } else {
00513                     ie.depth = last.depth + 1;
00514                     ie.edge = *j;
00515                     missing.push_back(ie);
00516                     if (ie.depth>5) {
00517                         maxDepthReached = true;
00518                     }
00519                 }
00520             }
00521         }
00522         if (maxDepthReached) {
00523             WRITE_WARNING(" Could not build list of previous flows.");
00524         }
00525     }
00526     // Edges with previous detectors are now in "previous";
00527     //  compute following
00528     std::vector<ROEdge*> latter;
00529     {
00530         std::vector<IterationEdge> missing;
00531         for (std::vector<ROEdge*>::const_iterator k=previous.begin(); k!=previous.end(); ++k) {
00532             IterationEdge ie;
00533             ie.depth = 0;
00534             ie.edge = *k;
00535             missing.push_back(ie);
00536         }
00537         bool maxDepthReached = false;
00538         while (!missing.empty()&&!maxDepthReached) {
00539             IterationEdge last = missing.back();
00540             missing.pop_back();
00541             std::vector<ROEdge*> approached = myApproachedEdges[last.edge];
00542             for (std::vector<ROEdge*>::const_iterator j=approached.begin(); j!=approached.end(); ++j) {
00543                 if (*j==getDetectorEdge(*detector)) {
00544                     continue;
00545                 }
00546                 if (hasDetector(*j)) {
00547                     latter.push_back(*j);
00548                 } else {
00549                     IterationEdge ie;
00550                     ie.depth = last.depth + 1;
00551                     ie.edge = *j;
00552                     missing.push_back(ie);
00553                     if (ie.depth>5) {
00554                         maxDepthReached = true;
00555                     }
00556                 }
00557             }
00558         }
00559         if (maxDepthReached) {
00560             WRITE_WARNING(" Could not build list of latter flows.");
00561             return;
00562         }
00563     }
00564     // Edges with latter detectors are now in "latter";
00565 
00566     // lets not validate them by now - surely this should be done
00567     // for each time step: collect incoming flows; collect outgoing;
00568     std::vector<FlowDef> mflows;
00569     for (SUMOTime t=startTime; t<endTime; t+=stepOffset) {
00570         FlowDef inFlow;
00571         inFlow.qLKW = 0;
00572         inFlow.qPKW = 0;
00573         inFlow.vLKW = 0;
00574         inFlow.vPKW = 0;
00575         // collect incoming
00576         {
00577             // !! time difference is missing
00578             for (std::vector<ROEdge*>::iterator i=previous.begin(); i!=previous.end(); ++i) {
00579                 const std::vector<FlowDef> &flows = static_cast<const RODFEdge*>(*i)->getFlows();
00580                 if (flows.size()!=0) {
00581                     const FlowDef &srcFD = flows[(int)(t/stepOffset) - startTime];
00582                     inFlow.qLKW += srcFD.qLKW;
00583                     inFlow.qPKW += srcFD.qPKW;
00584                     inFlow.vLKW += srcFD.vLKW;
00585                     inFlow.vPKW += srcFD.vPKW;
00586                 }
00587             }
00588         }
00589         inFlow.vLKW /= (SUMOReal) previous.size();
00590         inFlow.vPKW /= (SUMOReal) previous.size();
00591         // collect outgoing
00592         FlowDef outFlow;
00593         outFlow.qLKW = 0;
00594         outFlow.qPKW = 0;
00595         outFlow.vLKW = 0;
00596         outFlow.vPKW = 0;
00597         {
00598             // !! time difference is missing
00599             for (std::vector<ROEdge*>::iterator i=latter.begin(); i!=latter.end(); ++i) {
00600                 const std::vector<FlowDef> &flows = static_cast<const RODFEdge*>(*i)->getFlows();
00601                 if (flows.size()!=0) {
00602                     const FlowDef &srcFD = flows[(int)(t/stepOffset) - startTime];
00603                     outFlow.qLKW += srcFD.qLKW;
00604                     outFlow.qPKW += srcFD.qPKW;
00605                     outFlow.vLKW += srcFD.vLKW;
00606                     outFlow.vPKW += srcFD.vPKW;
00607                 }
00608             }
00609         }
00610         outFlow.vLKW /= (SUMOReal) latter.size();
00611         outFlow.vPKW /= (SUMOReal) latter.size();
00612         //
00613         FlowDef mFlow;
00614         mFlow.qLKW = inFlow.qLKW - outFlow.qLKW;
00615         mFlow.qPKW = inFlow.qPKW - outFlow.qPKW;
00616         mFlow.vLKW = (inFlow.vLKW + outFlow.vLKW) / (SUMOReal) 2.;
00617         mFlow.vPKW = (inFlow.vPKW + outFlow.vPKW) / (SUMOReal) 2.;
00618         mflows.push_back(mFlow);
00619     }
00620     static_cast<RODFEdge*>(getDetectorEdge(*detector))->setFlows(mflows);
00621     flows.setFlows(detector->getID(), mflows);
00622 }
00623 
00624 
00625 void
00626 RODFNet::revalidateFlows(const RODFDetectorCon &detectors,
00627                          RODFDetectorFlows &flows,
00628                          SUMOTime startTime, SUMOTime endTime,
00629                          SUMOTime stepOffset) {
00630     const std::vector<RODFDetector*> &dets = detectors.getDetectors();
00631     for (std::vector<RODFDetector*>::const_iterator i=dets.begin(); i!=dets.end(); ++i) {
00632         // check whether there is at least one entry with a flow larger than zero
00633         revalidateFlows(*i, flows, startTime, endTime, stepOffset);
00634     }
00635 }
00636 
00637 
00638 
00639 void
00640 RODFNet::removeEmptyDetectors(RODFDetectorCon &detectors,
00641                               RODFDetectorFlows &flows,
00642                               SUMOTime /*startTime*/, SUMOTime /*endTime*/,
00643                               SUMOTime /*stepOffset*/) {
00644     const std::vector<RODFDetector*> &dets = detectors.getDetectors();
00645     for (std::vector<RODFDetector*>::const_iterator i=dets.begin(); i!=dets.end();) {
00646         bool remove = true;
00647         // check whether there is at least one entry with a flow larger than zero
00648         if (flows.knows((*i)->getID())) {
00649             remove = false;
00650         }
00651         if (remove) {
00652             MsgHandler::getMessageInstance()->inform("Removed detector '" + (*i)->getID() + "' because no flows for him exist.");
00653             flows.removeFlow((*i)->getID());
00654             detectors.removeDetector((*i)->getID());
00655             i = dets.begin();
00656         } else {
00657             i++;
00658         }
00659     }
00660 }
00661 
00662 
00663 
00664 void
00665 RODFNet::reportEmptyDetectors(RODFDetectorCon &detectors,
00666                               RODFDetectorFlows &flows) {
00667     const std::vector<RODFDetector*> &dets = detectors.getDetectors();
00668     for (std::vector<RODFDetector*>::const_iterator i=dets.begin(); i!=dets.end(); ++i) {
00669         bool remove = true;
00670         // check whether there is at least one entry with a flow larger than zero
00671         if (flows.knows((*i)->getID())) {
00672             remove = false;
00673         }
00674         if (remove) {
00675             MsgHandler::getMessageInstance()->inform("Detector '" + (*i)->getID() + "' has no flow.");
00676         }
00677     }
00678 }
00679 
00680 
00681 
00682 ROEdge *
00683 RODFNet::getDetectorEdge(const RODFDetector &det) const {
00684     std::string edgeName = det.getLaneID();
00685     edgeName = edgeName.substr(0, edgeName.rfind('_'));
00686     ROEdge *ret = getEdge(edgeName);
00687     if (ret==0) {
00688         throw ProcessError("Edge '" + edgeName + "' used by detector '" + det.getID() + "' is not known.");
00689     }
00690     return ret;
00691 }
00692 
00693 
00694 bool
00695 RODFNet::hasApproaching(ROEdge *edge) const {
00696     return
00697         myApproachingEdges.find(edge)!=myApproachingEdges.end()
00698         &&
00699         myApproachingEdges.find(edge)->second.size()!=0;
00700 }
00701 
00702 
00703 bool
00704 RODFNet::hasApproached(ROEdge *edge) const {
00705     return
00706         myApproachedEdges.find(edge)!=myApproachedEdges.end()
00707         &&
00708         myApproachedEdges.find(edge)->second.size()!=0;
00709 }
00710 
00711 
00712 bool
00713 RODFNet::hasDetector(ROEdge *edge) const {
00714     return
00715         myDetectorsOnEdges.find(edge)!=myDetectorsOnEdges.end()
00716         &&
00717         myDetectorsOnEdges.find(edge)->second.size()!=0;
00718 }
00719 
00720 
00721 const std::vector<std::string> &
00722 RODFNet::getDetectorList(ROEdge *edge) const {
00723     return myDetectorsOnEdges.find(edge)->second;
00724 }
00725 
00726 
00727 SUMOReal
00728 RODFNet::getAbsPos(const RODFDetector &det) const {
00729     if (det.getPos()>=0) {
00730         return det.getPos();
00731     }
00732     return getDetectorEdge(det)->getLength() + det.getPos();
00733 }
00734 
00735 bool
00736 RODFNet::isSource(const RODFDetector &det, const RODFDetectorCon &detectors,
00737                   bool strict) const {
00738     std::vector<ROEdge*> seen;
00739     return
00740         isSource(det, getDetectorEdge(det), seen, detectors, strict);
00741 }
00742 
00743 bool
00744 RODFNet::isFalseSource(const RODFDetector &det, const RODFDetectorCon &detectors) const {
00745     std::vector<ROEdge*> seen;
00746     return
00747         isFalseSource(det, getDetectorEdge(det), seen, detectors);
00748 }
00749 
00750 bool
00751 RODFNet::isDestination(const RODFDetector &det, const RODFDetectorCon &detectors) const {
00752     std::vector<ROEdge*> seen;
00753     return isDestination(det, getDetectorEdge(det), seen, detectors);
00754 }
00755 
00756 
00757 bool
00758 RODFNet::isSource(const RODFDetector &det, ROEdge *edge,
00759                   std::vector<ROEdge*> &seen,
00760                   const RODFDetectorCon &detectors,
00761                   bool strict) const {
00762     if (seen.size()==1000) { // !!!
00763         MsgHandler::getWarningInstance()->inform("Quitting checking for being a source for detector '" + det.getID() + "' due to seen edge limit.");
00764         return false;
00765     }
00766     if (edge==getDetectorEdge(det)) {
00767         // maybe there is another detector at the same edge
00768         //  get the list of this/these detector(s)
00769         const std::vector<std::string> &detsOnEdge = myDetectorsOnEdges.find(edge)->second;
00770         for (std::vector<std::string>::const_iterator i=detsOnEdge.begin(); i!=detsOnEdge.end(); ++i) {
00771             if ((*i)==det.getID()) {
00772                 continue;
00773             }
00774             const RODFDetector &sec = detectors.getDetector(*i);
00775             if (getAbsPos(sec)<getAbsPos(det)) {
00776                 // ok, there is another detector on the same edge and it is
00777                 //  before this one -> no source
00778                 return false;
00779             }
00780         }
00781     }
00782     // it's a source if no edges are approaching the edge
00783     if (!hasApproaching(edge)) {
00784         if (edge!=getDetectorEdge(det)) {
00785             if (hasDetector(edge)) {
00786                 return false;
00787             }
00788         }
00789         return true;
00790     }
00791     if (edge!=getDetectorEdge(det)) {
00792         // ok, we are at one of the edges in front
00793         if (myAmInHighwayMode) {
00794             if (edge->getSpeed()>=19.4) {
00795                 if (hasDetector(edge)) {
00796                     // we are still on the highway and there is another detector
00797                     return false;
00798                 }
00799                 // the next is a hack for the A100 scenario...
00800                 //  We have to look into further edges herein edges
00801                 const std::vector<ROEdge*> &appr = myApproachingEdges.find(edge)->second;
00802                 size_t noOk = 0;
00803                 size_t noFalse = 0;
00804                 size_t noSkipped = 0;
00805                 for (size_t i=0; i<appr.size(); i++) {
00806                     if (!hasDetector(appr[i])) {
00807                         noOk++;
00808                     } else {
00809                         noFalse++;
00810                     }
00811                 }
00812                 if ((noFalse+noSkipped)==appr.size()) {
00813                     return false;
00814                 }
00815             }
00816         }
00817     }
00818 
00819     if (myAmInHighwayMode) {
00820         if (edge->getSpeed()<19.4&&edge!=getDetectorEdge(det)) {
00821             // we have left the highway already
00822             //  -> the detector will be a highway source
00823             if (!hasDetector(edge)) {
00824                 return true;
00825             }
00826         }
00827     }
00828     if (myDetectorsOnEdges.find(edge)!=myDetectorsOnEdges.end()
00829             &&
00830             myDetectorEdges.find(det.getID())->second!=edge) {
00831         return false;
00832     }
00833 
00834     // let's check the edges in front
00835     const std::vector<ROEdge*> &appr = myApproachingEdges.find(edge)->second;
00836     size_t noOk = 0;
00837     size_t noFalse = 0;
00838     size_t noSkipped = 0;
00839     seen.push_back(edge);
00840     for (size_t i=0; i<appr.size(); i++) {
00841         bool had = std::find(seen.begin(), seen.end(), appr[i])!=seen.end();
00842         if (!had) {
00843             if (isSource(det, appr[i], seen, detectors, strict)) {
00844                 noOk++;
00845             } else {
00846                 noFalse++;
00847             }
00848         } else {
00849             noSkipped++;
00850         }
00851     }
00852     if (!strict) {
00853         return (noFalse+noSkipped)!=appr.size();
00854     } else {
00855         return (noOk+noSkipped)==appr.size();
00856     }
00857 }
00858 
00859 
00860 bool
00861 RODFNet::isDestination(const RODFDetector &det, ROEdge *edge, std::vector<ROEdge*> &seen,
00862                        const RODFDetectorCon &detectors) const {
00863     if (seen.size()==1000) { // !!!
00864         MsgHandler::getWarningInstance()->inform("Quitting checking for being a destination for detector '" + det.getID() + "' due to seen edge limit.");
00865         return false;
00866     }
00867     if (edge==getDetectorEdge(det)) {
00868         // maybe there is another detector at the same edge
00869         //  get the list of this/these detector(s)
00870         const std::vector<std::string> &detsOnEdge = myDetectorsOnEdges.find(edge)->second;
00871         for (std::vector<std::string>::const_iterator i=detsOnEdge.begin(); i!=detsOnEdge.end(); ++i) {
00872             if ((*i)==det.getID()) {
00873                 continue;
00874             }
00875             const RODFDetector &sec = detectors.getDetector(*i);
00876             if (getAbsPos(sec)>getAbsPos(det)) {
00877                 // ok, there is another detector on the same edge and it is
00878                 //  after this one -> no destination
00879                 return false;
00880             }
00881         }
00882     }
00883     if (!hasApproached(edge)) {
00884         if (edge!=getDetectorEdge(det)) {
00885             if (hasDetector(edge)) {
00886                 return false;
00887             }
00888         }
00889         return true;
00890     }
00891     if (edge!=getDetectorEdge(det)) {
00892         // ok, we are at one of the edges coming behind
00893         if (myAmInHighwayMode) {
00894             if (edge->getSpeed()>=19.4) {
00895                 if (hasDetector(edge)) {
00896                     // we are still on the highway and there is another detector
00897                     return false;
00898                 }
00899             }
00900         }
00901     }
00902 
00903     if (myAmInHighwayMode) {
00904         if (edge->getSpeed()<19.4&&edge!=getDetectorEdge(det)) {
00905             if (hasDetector(edge)) {
00906                 return true;
00907             }
00908             if (myApproachedEdges.find(edge)->second.size()>1) {
00909                 return true;
00910             }
00911 
00912         }
00913     }
00914 
00915     if (myDetectorsOnEdges.find(edge)!=myDetectorsOnEdges.end()
00916             &&
00917             myDetectorEdges.find(det.getID())->second!=edge) {
00918         return false;
00919     }
00920     const std::vector<ROEdge*> &appr  = myApproachedEdges.find(edge)->second;
00921     bool isall = true;
00922     size_t no = 0;
00923     seen.push_back(edge);
00924     for (size_t i=0; i<appr.size()&&isall; i++) {
00925         bool had = std::find(seen.begin(), seen.end(), appr[i])!=seen.end();
00926         if (!had) {
00927             if (!isDestination(det, appr[i], seen, detectors)) {
00928                 no++;
00929                 isall = false;
00930             }
00931         }
00932     }
00933     return isall;
00934 }
00935 
00936 bool
00937 RODFNet::isFalseSource(const RODFDetector &det, ROEdge *edge, std::vector<ROEdge*> &seen,
00938                        const RODFDetectorCon &detectors) const {
00939     if (seen.size()==1000) { // !!!
00940         MsgHandler::getWarningInstance()->inform("Quitting checking for being a false source for detector '" + det.getID() + "' due to seen edge limit.");
00941         return false;
00942     }
00943     seen.push_back(edge);
00944     if (edge!=getDetectorEdge(det)) {
00945         // ok, we are at one of the edges coming behind
00946         if (hasDetector(edge)) {
00947             const std::vector<std::string> &dets = myDetectorsOnEdges.find(edge)->second;
00948             for (std::vector<std::string>::const_iterator i=dets.begin(); i!=dets.end(); ++i) {
00949                 if (detectors.getDetector(*i).getType()==SINK_DETECTOR) {
00950                     return false;
00951                 }
00952                 if (detectors.getDetector(*i).getType()==BETWEEN_DETECTOR) {
00953                     return false;
00954                 }
00955                 if (detectors.getDetector(*i).getType()==SOURCE_DETECTOR) {
00956                     return true;
00957                 }
00958             }
00959         } else {
00960             if (myAmInHighwayMode&&edge->getSpeed()<19.) {
00961                 return false;
00962             }
00963         }
00964     }
00965 
00966     if (myApproachedEdges.find(edge)==myApproachedEdges.end()) {
00967         return false;
00968     }
00969 
00970     const std::vector<ROEdge*> &appr  = myApproachedEdges.find(edge)->second;
00971     bool isall = false;
00972     for (size_t i=0; i<appr.size()&&!isall; i++) {
00973         //printf("checking %s->\n", appr[i].c_str());
00974         bool had = std::find(seen.begin(), seen.end(), appr[i])!=seen.end();
00975         if (!had) {
00976             if (isFalseSource(det, appr[i], seen, detectors)) {
00977                 isall = true;
00978             }
00979         }
00980     }
00981     return isall;
00982 }
00983 
00984 
00985 void
00986 RODFNet::buildEdgeFlowMap(const RODFDetectorFlows &flows,
00987                           const RODFDetectorCon &detectors,
00988                           SUMOTime startTime, SUMOTime endTime,
00989                           SUMOTime stepOffset) {
00990     std::map<ROEdge*, std::vector<std::string> >::iterator i;
00991     for (i=myDetectorsOnEdges.begin(); i!=myDetectorsOnEdges.end(); ++i) {
00992         ROEdge *into = (*i).first;
00993         const std::vector<std::string> &dets = (*i).second;
00994         std::map<SUMOReal, std::vector<std::string> > cliques;
00995         size_t maxCliqueSize = 0;
00996         for (std::vector<std::string>::const_iterator j=dets.begin(); j!=dets.end(); ++j) {
00997             if (!flows.knows(*j)) {
00998                 continue;
00999             }
01000             const RODFDetector &det = detectors.getDetector(*j);
01001             bool found = false;
01002             for (std::map<SUMOReal, std::vector<std::string> >::iterator k=cliques.begin(); !found&&k!=cliques.end(); ++k) {
01003                 if (fabs((*k).first-det.getPos())<1) {
01004                     (*k).second.push_back(*j);
01005                     maxCliqueSize = MAX2(maxCliqueSize, (*k).second.size());
01006                     found = true;
01007                 }
01008             }
01009             if (!found) {
01010                 cliques[det.getPos()] = std::vector<std::string>();
01011                 cliques[det.getPos()].push_back(*j);
01012                 maxCliqueSize = MAX2(maxCliqueSize, (size_t) 1);
01013             }
01014         }
01015         std::vector<std::string> firstClique;
01016         for (std::map<SUMOReal, std::vector<std::string> >::iterator m=cliques.begin(); firstClique.size()==0&&m!=cliques.end(); ++m) {
01017             if ((*m).second.size()==maxCliqueSize) {
01018                 firstClique = (*m).second;
01019             }
01020         }
01021         std::vector<FlowDef> mflows; // !!! reserve
01022         SUMOTime t;
01023         for (t=startTime; t<endTime; t+=stepOffset) {
01024             FlowDef fd;
01025             fd.qPKW = 0;
01026             fd.qLKW = 0;
01027             fd.vLKW = 0;
01028             fd.vPKW = 0;
01029             fd.fLKW = 0;
01030             fd.isLKW = 0;
01031             mflows.push_back(fd);
01032         }
01033         for (std::vector<std::string>::iterator l=firstClique.begin(); l!=firstClique.end(); ++l) {
01034             const std::vector<FlowDef> &dflows = flows.getFlowDefs(*l);
01035             for (t=startTime; t<endTime; t+=stepOffset) {
01036                 const FlowDef &srcFD = dflows[(int)(t/stepOffset) - startTime];
01037                 FlowDef &fd = mflows[(int)(t/stepOffset) - startTime];
01038                 fd.qPKW += srcFD.qPKW;
01039                 fd.qLKW += srcFD.qLKW;
01040                 fd.vLKW += (srcFD.vLKW / (SUMOReal) firstClique.size());
01041                 fd.vPKW += (srcFD.vPKW / (SUMOReal) firstClique.size());
01042                 fd.fLKW += (srcFD.fLKW / (SUMOReal) firstClique.size());
01043                 fd.isLKW += (srcFD.isLKW / (SUMOReal) firstClique.size());
01044             }
01045         }
01046         static_cast<RODFEdge*>(into)->setFlows(mflows);
01047     }
01048 }
01049 
01050 
01051 void
01052 RODFNet::buildDetectorDependencies(RODFDetectorCon &detectors) {
01053     // !!! this will not work when several detectors are lying on the same edge on different positions
01054 
01055 
01056     buildDetectorEdgeDependencies(detectors);
01057     // for each detector, compute the lists of predecessor and following detectors
01058     std::map<std::string, ROEdge*>::const_iterator i;
01059     for (i=myDetectorEdges.begin(); i!=myDetectorEdges.end(); ++i) {
01060         const RODFDetector &det = detectors.getDetector((*i).first);
01061         if (!det.hasRoutes()) {
01062             continue;
01063         }
01064         // mark current detectors
01065         std::vector<RODFDetector*> last;
01066         {
01067             const std::vector<std::string> &detNames = myDetectorsOnEdges.find((*i).second)->second;
01068             for (std::vector<std::string>::const_iterator j=detNames.begin(); j!=detNames.end(); ++j) {
01069                 last.push_back((RODFDetector*) &detectors.getDetector(*j));
01070             }
01071         }
01072         // iterate over the current detector's routes
01073         const std::vector<RODFRouteDesc> &routes = det.getRouteVector();
01074         for (std::vector<RODFRouteDesc>::const_iterator j=routes.begin(); j!=routes.end(); ++j) {
01075             const std::vector<ROEdge*> &edges2Pass = (*j).edges2Pass;
01076             for (std::vector<ROEdge*>::const_iterator k=edges2Pass.begin()+1; k!=edges2Pass.end(); ++k) {
01077                 if (myDetectorsOnEdges.find(*k)!=myDetectorsOnEdges.end()) {
01078                     const std::vector<std::string> &detNames = myDetectorsOnEdges.find(*k)->second;
01079                     // ok, consecutive detector found
01080                     for (std::vector<RODFDetector*>::iterator l=last.begin(); l!=last.end(); ++l) {
01081                         // mark as follower of current
01082                         for (std::vector<std::string>::const_iterator m=detNames.begin(); m!=detNames.end(); ++m) {
01083                             ((RODFDetector*) &detectors.getDetector(*m))->addPriorDetector((RODFDetector*) &(*l));
01084                             (*l)->addFollowingDetector((RODFDetector*) &detectors.getDetector(*m));
01085                         }
01086                     }
01087                     last.clear();
01088                     for (std::vector<std::string>::const_iterator m=detNames.begin(); m!=detNames.end(); ++m) {
01089                         last.push_back((RODFDetector*) &detectors.getDetector(*m));
01090                     }
01091                 }
01092             }
01093         }
01094     }
01095 }
01096 
01097 
01098 void
01099 RODFNet::mesoJoin(RODFDetectorCon &detectors, RODFDetectorFlows &flows) {
01100     buildDetectorEdgeDependencies(detectors);
01101     std::map<ROEdge*, std::vector<std::string> >::iterator i;
01102     for (i=myDetectorsOnEdges.begin(); i!=myDetectorsOnEdges.end(); ++i) {
01103         ROEdge *into = (*i).first;
01104         const std::vector<std::string> &dets = (*i).second;
01105         std::map<SUMOReal, std::vector<std::string> > cliques;
01106         // compute detector cliques
01107         for (std::vector<std::string>::const_iterator j=dets.begin(); j!=dets.end(); ++j) {
01108             const RODFDetector &det = detectors.getDetector(*j);
01109             bool found = false;
01110             for (std::map<SUMOReal, std::vector<std::string> >::iterator k=cliques.begin(); !found&&k!=cliques.end(); ++k) {
01111                 if (fabs((*k).first-det.getPos())<10.) {
01112                     (*k).second.push_back(*j);
01113                     found = true;
01114                 }
01115             }
01116             if (!found) {
01117                 cliques[det.getPos()] = std::vector<std::string>();
01118                 cliques[det.getPos()].push_back(*j);
01119             }
01120         }
01121         // join detector cliques
01122         for (std::map<SUMOReal, std::vector<std::string> >::iterator m=cliques.begin(); m!=cliques.end(); ++m) {
01123             std::vector<std::string> clique = (*m).second;
01124             // do not join if only one
01125             if (clique.size()==1) {
01126                 continue;
01127             }
01128             std::string nid;
01129             for (std::vector<std::string>::iterator n=clique.begin(); n!=clique.end(); ++n) {
01130                 std::cout << *n << " ";
01131                 if (n!=clique.begin()) {
01132                     nid = nid + "_";
01133                 }
01134                 nid = nid + *n;
01135             }
01136             std::cout << ":" << nid << std::endl;
01137             flows.mesoJoin(nid, (*m).second);
01138             detectors.mesoJoin(nid, (*m).second);
01139         }
01140     }
01141 }
01142 
01143 
01144 
01145 /****************************************************************************/
01146 

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