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