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