00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifdef _MSC_VER
00025 #include <windows_config.h>
00026 #else
00027 #include <config.h>
00028 #endif
00029
00030 #include "RODFDetector.h"
00031 #include <utils/common/FileHelpers.h>
00032 #include <utils/common/MsgHandler.h>
00033 #include <utils/common/UtilExceptions.h>
00034 #include <router/ROEdge.h>
00035 #include "RODFEdge.h"
00036 #include "RODFRouteDesc.h"
00037 #include "RODFRouteCont.h"
00038 #include "RODFDetectorFlow.h"
00039 #include <utils/common/RandomDistributor.h>
00040 #include <utils/common/StdDefs.h>
00041 #include <utils/geom/GeomHelper.h>
00042 #include "RODFNet.h"
00043 #include <utils/iodevices/OutputDevice.h>
00044 #include <utils/common/StringUtils.h>
00045
00046 #ifdef CHECK_MEMORY_LEAKS
00047 #include <foreign/nvwa/debug_new.h>
00048 #endif // CHECK_MEMORY_LEAKS
00049
00050
00051
00052
00053
00054 RODFDetector::RODFDetector(const std::string &id, const std::string &laneID,
00055 SUMOReal pos, const RODFDetectorType type) throw()
00056 : myID(id), myLaneID(laneID), myPosition(pos), myType(type), myRoutes(0) {}
00057
00058
00059 RODFDetector::RODFDetector(const std::string &id, const RODFDetector &f) throw()
00060 : myID(id), myLaneID(f.myLaneID), myPosition(f.myPosition),
00061 myType(f.myType), myRoutes(0) {
00062 if (f.myRoutes!=0) {
00063 myRoutes = new RODFRouteCont(*(f.myRoutes));
00064 }
00065 }
00066
00067
00068 RODFDetector::~RODFDetector() throw() {
00069 delete myRoutes;
00070 }
00071
00072
00073 void
00074 RODFDetector::setType(RODFDetectorType type) {
00075 myType = type;
00076 }
00077
00078
00079 SUMOReal
00080 RODFDetector::computeDistanceFactor(const RODFRouteDesc &rd) const {
00081 SUMOReal distance = rd.edges2Pass[0]->getFromNode()->getPosition().distanceTo(rd.edges2Pass.back()->getToNode()->getPosition());
00082 SUMOReal length = 0;
00083 for (std::vector<ROEdge*>::const_iterator i=rd.edges2Pass.begin(); i!=rd.edges2Pass.end(); ++i) {
00084 length += (*i)->getLength();
00085 }
00086 return (distance/length);
00087 }
00088
00089
00090 void
00091 RODFDetector::computeSplitProbabilities(const RODFNet *net, const RODFDetectorCon &detectors,
00092 const RODFDetectorFlows &flows,
00093 SUMOTime startTime, SUMOTime endTime, SUMOTime stepOffset) {
00094 if (myRoutes==0) {
00095 return;
00096 }
00097
00098 const std::vector<RODFRouteDesc> &routes = myRoutes->get();
00099 std::vector<RODFEdge*> nextDetEdges;
00100 for (std::vector<RODFRouteDesc>::const_iterator i=routes.begin(); i!=routes.end(); ++i) {
00101 const RODFRouteDesc &rd = *i;
00102 bool hadSplit = false;
00103 bool hadDetectorAfterSplit = false;
00104 for (std::vector<ROEdge*>::const_iterator j=rd.edges2Pass.begin(); !hadDetectorAfterSplit&&j!=rd.edges2Pass.end(); ++j) {
00105 if (hadSplit&&!hadDetectorAfterSplit&&net->hasDetector(*j)) {
00106 hadDetectorAfterSplit = true;
00107 if (find(nextDetEdges.begin(), nextDetEdges.end(), *j)==nextDetEdges.end()) {
00108 nextDetEdges.push_back(static_cast<RODFEdge*>(*j));
00109 }
00110 myRoute2Edge[rd.routename] = static_cast<RODFEdge*>(*j);
00111 }
00112 if ((*j)->getNoFollowing()>1) {
00113 hadSplit = true;
00114 }
00115 }
00116 }
00117
00118 int index = 0;
00119 for (int time=startTime; time<endTime; time+=stepOffset, ++index) {
00120 mySplitProbabilities.push_back(std::map<RODFEdge*, SUMOReal>());
00121 SUMOReal overallProb = 0;
00122
00123 for (std::vector<RODFEdge*>::const_iterator i=nextDetEdges.begin(); i!=nextDetEdges.end(); ++i) {
00124 SUMOReal flow = detectors.getAggFlowFor(*i, time, 60, flows);
00125 overallProb += flow;
00126 mySplitProbabilities[index][*i] = flow;
00127 }
00128
00129 if (overallProb>0) {
00130 for (std::vector<RODFEdge*>::const_iterator i=nextDetEdges.begin(); i!=nextDetEdges.end(); ++i) {
00131 mySplitProbabilities[index][*i] = mySplitProbabilities[index][*i] / overallProb;
00132 }
00133 }
00134 }
00135 }
00136
00137
00138 void
00139 RODFDetector::buildDestinationDistribution(const RODFDetectorCon &detectors,
00140 const RODFDetectorFlows &flows,
00141 SUMOTime startTime,
00142 SUMOTime endTime,
00143 SUMOTime stepOffset,
00144 const RODFNet &net,
00145 std::map<size_t, RandomDistributor<size_t>* > &into,
00146 int maxFollower) const {
00147 if (myRoutes==0) {
00148 if (myType!=DISCARDED_DETECTOR&&myType!=BETWEEN_DETECTOR) {
00149 MsgHandler::getErrorInstance()->inform("Missing routes for detector '" + myID + "'.");
00150 }
00151 return;
00152 }
00153 std::vector<RODFRouteDesc> &descs = myRoutes->get();
00154 const std::vector<FlowDef> &mflows = flows.getFlowDefs(myID);
00155
00156 for (int time=startTime; time<endTime; time+=stepOffset) {
00157 into[time] = new RandomDistributor<size_t>();
00158 std::map<ROEdge*, SUMOReal> flowMap;
00159
00160 size_t index = 0;
00161 for (std::vector<RODFRouteDesc>::iterator ri=descs.begin(); ri!=descs.end(); ++ri, index++) {
00162 SUMOReal prob = 1.;
00163 for (std::vector<ROEdge*>::iterator j=(*ri).edges2Pass.begin(); j!=(*ri).edges2Pass.end()&&prob>0; ++j) {
00164 if (!net.hasDetector(*j)) {
00165 continue;
00166 }
00167 const RODFDetector &det = detectors.getAnyDetectorForEdge(static_cast<RODFEdge*>(*j));
00168 const std::vector<std::map<RODFEdge*, SUMOReal> > &probs = det.getSplitProbabilities();
00169 if (probs.size()==0) {
00170 prob = 0;
00171 continue;
00172 }
00173 const std::map<RODFEdge*, SUMOReal> &tprobs = probs[time/stepOffset];
00174 for (std::map<RODFEdge*, SUMOReal>::const_iterator k=tprobs.begin(); k!=tprobs.end(); ++k) {
00175 if (find(j, (*ri).edges2Pass.end(), (*k).first)!=(*ri).edges2Pass.end()) {
00176 prob *= (*k).second;
00177 }
00178 }
00179 }
00180 into[time]->add(prob, index);
00181 (*ri).overallProb = prob;
00182 }
00183 }
00184 }
00185
00186
00187 const std::vector<RODFRouteDesc> &
00188 RODFDetector::getRouteVector() const {
00189 return myRoutes->get();
00190 }
00191
00192
00193 void
00194 RODFDetector::addPriorDetector(RODFDetector *det) {
00195 myPriorDetectors.push_back(det);
00196 }
00197
00198
00199 void
00200 RODFDetector::addFollowingDetector(RODFDetector *det) {
00201 myFollowingDetectors.push_back(det);
00202 }
00203
00204
00205 const std::vector<RODFDetector*> &
00206 RODFDetector::getPriorDetectors() const {
00207 return myPriorDetectors;
00208 }
00209
00210
00211 const std::vector<RODFDetector*> &
00212 RODFDetector::getFollowerDetectors() const {
00213 return myFollowingDetectors;
00214 }
00215
00216
00217
00218 void
00219 RODFDetector::addRoutes(RODFRouteCont *routes) {
00220 delete myRoutes;
00221 myRoutes = routes;
00222 }
00223
00224
00225 void
00226 RODFDetector::addRoute(RODFRouteDesc &nrd) {
00227 if (myRoutes==0) {
00228 myRoutes = new RODFRouteCont();
00229 }
00230 myRoutes->addRouteDesc(nrd);
00231 }
00232
00233
00234 bool
00235 RODFDetector::hasRoutes() const {
00236 return myRoutes!=0&&myRoutes->get().size()!=0;
00237 }
00238
00239
00240 bool
00241 RODFDetector::writeEmitterDefinition(const std::string &file,
00242 const std::map<size_t, RandomDistributor<size_t>* > &dists,
00243 const RODFDetectorFlows &flows,
00244 SUMOTime startTime, SUMOTime endTime,
00245 SUMOTime stepOffset,
00246 bool includeUnusedRoutes,
00247 SUMOReal scale,
00248 bool emissionsOnly,
00249 SUMOReal defaultSpeed) const {
00250 OutputDevice& out = OutputDevice::getDevice(file);
00251 if (getType()==SOURCE_DETECTOR) {
00252 out.writeXMLHeader("triggeredsource");
00253 } else {
00254 out.writeXMLHeader("calibrator");
00255 }
00256
00257 if (!emissionsOnly) {
00258 if (myRoutes!=0&&myRoutes->get().size()!=0) {
00259 const std::vector<RODFRouteDesc> &routes = myRoutes->get();
00260 std::vector<RODFRouteDesc>::const_iterator i;
00261
00262 SUMOReal overallSum = 0;
00263 for (i=routes.begin(); i!=routes.end(); ++i) {
00264 overallSum += (*i).overallProb;
00265 }
00266
00267 int writtenRoutes = 0;
00268 if (overallSum!=0) {
00269
00270
00271 for (i=routes.begin(); i!=routes.end(); ++i) {
00272 if ((*i).overallProb>0||includeUnusedRoutes) {
00273 out << " <routedistelem id=\"" << (*i).routename << "\" probability=\"" << ((*i).overallProb/overallSum) << "\"/>\n";
00274 writtenRoutes++;
00275 }
00276 }
00277 }
00278
00279
00280 if (writtenRoutes==0) {
00281 for (i=routes.begin(); i!=routes.end(); ++i) {
00282 out << " <routedistelem id=\"" << (*i).routename << "\" probability=\"1\"/>\n";
00283 }
00284 }
00285 } else {
00286 MsgHandler::getErrorInstance()->inform("Detector '" + getID() + "' has no routes!?");
00287 return false;
00288 }
00289 }
00290
00291 if (emissionsOnly||flows.knows(myID)) {
00292
00293
00294 const std::vector<FlowDef> &mflows = flows.getFlowDefs(myID);
00295
00296 for (SUMOTime time=startTime; time<endTime; time+=stepOffset) {
00297
00298 if ((int) mflows.size()<=(int)((time/stepOffset) - startTime)) {
00299 std::cout << mflows.size() << ":" << (int)(time/stepOffset) - startTime << std::endl;
00300 throw 1;
00301 }
00302 const FlowDef &srcFD = mflows[(int)(time/stepOffset) - startTime];
00303
00304 RandomDistributor<size_t> *destDist = dists.size()>time ? dists.find(time)->second : 0;
00305
00306 size_t carNo = (size_t)((srcFD.qPKW + srcFD.qLKW) * scale);
00307 for (size_t car=0; car<carNo; ++car) {
00308
00309 std::string type = "test";
00310 SUMOReal v = -1;
00311 int destIndex = destDist!=0 && destDist->getOverallProb()>0 ? (int) destDist->get() : -1;
00312 if (srcFD.isLKW>=1) {
00313 srcFD.isLKW = srcFD.isLKW - (SUMOReal) 1.;
00315 v = srcFD.vLKW;
00316 } else {
00318 v = srcFD.vPKW;
00319 }
00320
00321 if (v<0||v>250) {
00322 v = defaultSpeed;
00323 } else {
00324 v = (SUMOReal)(v/3.6);
00325 }
00326
00327 int ctime = (int)(time + ((SUMOReal) stepOffset * (SUMOReal) car / (SUMOReal) carNo));
00328
00329
00330 out << " <emit id=\"";
00331 if (getType()==SOURCE_DETECTOR) {
00332 out << "emitter_" << myID;
00333 } else {
00334 out << "calibrator_" << myID;
00335 }
00336 out << "_" << ctime << "\""
00337 << " time=\"" << ctime << "\""
00338 << " speed=\"" << v << "\"";
00339 if (!emissionsOnly&&destIndex>=0) {
00340 out << " route=\"" << myRoutes->get()[destIndex].routename << "\"";
00341 }
00342 out << "/>\n";
00343 srcFD.isLKW += srcFD.fLKW;
00344 }
00345 }
00346 }
00347
00348 out.close();
00349 return true;
00350 }
00351
00352
00353 bool
00354 RODFDetector::writeRoutes(std::vector<std::string> &saved,
00355 OutputDevice &out) {
00356 if (myRoutes!=0) {
00357 return myRoutes->save(saved, "", out);
00358 }
00359 return false;
00360 }
00361
00362
00363 void
00364 RODFDetector::writeSingleSpeedTrigger(const std::string &file,
00365 const RODFDetectorFlows &flows,
00366 SUMOTime startTime, SUMOTime endTime,
00367 SUMOTime stepOffset, SUMOReal defaultSpeed) {
00368 OutputDevice& out = OutputDevice::getDevice(file);
00369 out.writeXMLHeader("vss");
00370 const std::vector<FlowDef> &mflows = flows.getFlowDefs(myID);
00371 for (SUMOTime t=startTime; t<endTime; t+=stepOffset) {
00372 const FlowDef &srcFD = mflows[(int)(t/stepOffset) - startTime];
00373 SUMOReal speed = MAX2(srcFD.vLKW, srcFD.vPKW);
00374 if (speed<=0||speed>250) {
00375 speed = defaultSpeed;
00376 } else {
00377 speed = (SUMOReal)(speed / 3.6);
00378 }
00379 out << " <step time=\"" << t << "\" speed=\"" << speed << "\"/>\n";
00380 }
00381 out.close();
00382 }
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393 RODFDetectorCon::RODFDetectorCon() {}
00394
00395
00396 RODFDetectorCon::~RODFDetectorCon() {
00397 for (std::vector<RODFDetector*>::iterator i=myDetectors.begin(); i!=myDetectors.end(); ++i) {
00398 delete *i;
00399 }
00400 }
00401
00402
00403 bool
00404 RODFDetectorCon::addDetector(RODFDetector *dfd) {
00405 if (myDetectorMap.find(dfd->getID())!=myDetectorMap.end()) {
00406 return false;
00407 }
00408 myDetectorMap[dfd->getID()] = dfd;
00409 myDetectors.push_back(dfd);
00410 std::string edgeid = dfd->getLaneID().substr(0, dfd->getLaneID().rfind('_'));
00411 if (myDetectorEdgeMap.find(edgeid)==myDetectorEdgeMap.end()) {
00412 myDetectorEdgeMap[edgeid] = std::vector<RODFDetector*>();
00413 }
00414 myDetectorEdgeMap[edgeid].push_back(dfd);
00415 return true;
00416 }
00417
00418
00419 bool
00420 RODFDetectorCon::detectorsHaveCompleteTypes() const {
00421 for (std::vector<RODFDetector*>::const_iterator i=myDetectors.begin(); i!=myDetectors.end(); ++i) {
00422 if ((*i)->getType()==TYPE_NOT_DEFINED) {
00423 return false;
00424 }
00425 }
00426 return true;
00427 }
00428
00429
00430 bool
00431 RODFDetectorCon::detectorsHaveRoutes() const {
00432 for (std::vector<RODFDetector*>::const_iterator i=myDetectors.begin(); i!=myDetectors.end(); ++i) {
00433 if ((*i)->hasRoutes()) {
00434 return true;
00435 }
00436 }
00437 return false;
00438 }
00439
00440
00441 const std::vector< RODFDetector*> &
00442 RODFDetectorCon::getDetectors() const {
00443 return myDetectors;
00444 }
00445
00446
00447 void
00448 RODFDetectorCon::save(const std::string &file) const {
00449 OutputDevice& out = OutputDevice::getDevice(file);
00450 out.writeXMLHeader("detectors");
00451 for (std::vector<RODFDetector*>::const_iterator i=myDetectors.begin(); i!=myDetectors.end(); ++i) {
00452 out << " <detector_definition id=\"" << StringUtils::escapeXML((*i)->getID())
00453 << "\" lane=\"" << (*i)->getLaneID()
00454 << "\" pos=\"" << (*i)->getPos();
00455 switch ((*i)->getType()) {
00456 case BETWEEN_DETECTOR:
00457 out << "\" type=\"between\"";
00458 break;
00459 case SOURCE_DETECTOR:
00460 out << "\" type=\"source\"";
00461 break;
00462 case SINK_DETECTOR:
00463 out << "\" type=\"sink\"";
00464 break;
00465 case DISCARDED_DETECTOR:
00466 out << "\" type=\"discarded\"";
00467 break;
00468 default:
00469 throw 1;
00470 }
00471 out << "/>\n";
00472 }
00473 out.close();
00474 }
00475
00476
00477 void
00478 RODFDetectorCon::saveAsPOIs(const std::string &file) const {
00479 OutputDevice& out = OutputDevice::getDevice(file);
00480 out.writeXMLHeader("pois");
00481 for (std::vector<RODFDetector*>::const_iterator i=myDetectors.begin(); i!=myDetectors.end(); ++i) {
00482 out << " <poi id=\"" << StringUtils::escapeXML((*i)->getID());
00483 switch ((*i)->getType()) {
00484 case BETWEEN_DETECTOR:
00485 out << "\" type=\"between_detector_position\" color=\"0,0,1\"";
00486 break;
00487 case SOURCE_DETECTOR:
00488 out << "\" type=\"source_detector_position\" color=\"0,1,0\"";
00489 break;
00490 case SINK_DETECTOR:
00491 out << "\" type=\"sink_detector_position\" color=\"1,0,0\"";
00492 break;
00493 case DISCARDED_DETECTOR:
00494 out << "\" type=\"discarded_detector_position\" color=\".2,.2,.2\"";
00495 break;
00496 default:
00497 throw 1;
00498 }
00499 out << " lane=\"" << (*i)->getLaneID()<< "\" pos=\""
00500 << (*i)->getPos() << "\"/>\n";
00501 }
00502 out.close();
00503 }
00504
00505
00506 void
00507 RODFDetectorCon::saveRoutes(const std::string &file) const {
00508 OutputDevice& out = OutputDevice::getDevice(file);
00509 out.writeXMLHeader("routes");
00510 std::vector<std::string> saved;
00511
00512 bool lastWasSaved = true;
00513 for (std::vector<RODFDetector*>::const_iterator i=myDetectors.begin(); i!=myDetectors.end(); ++i) {
00514 if ((*i)->getType()!=SOURCE_DETECTOR) {
00515
00516 continue;
00517 }
00518 if (lastWasSaved) {
00519 out << "\n";
00520 }
00521 lastWasSaved = (*i)->writeRoutes(saved, out);
00522 }
00523 out << "\n";
00524 out.close();
00525 }
00526
00527
00528 const RODFDetector &
00529 RODFDetectorCon::getDetector(const std::string &id) const {
00530 return *(myDetectorMap.find(id)->second);
00531 }
00532
00533
00534 bool
00535 RODFDetectorCon::knows(const std::string &id) const {
00536 return myDetectorMap.find(id)!=myDetectorMap.end();
00537 }
00538
00539
00540 void
00541 RODFDetectorCon::writeEmitters(const std::string &file,
00542 const RODFDetectorFlows &flows,
00543 SUMOTime startTime, SUMOTime endTime,
00544 SUMOTime stepOffset, const RODFNet &net,
00545 bool writeCalibrators,
00546 bool includeUnusedRoutes,
00547 SUMOReal scale,
00548 int maxFollower,
00549 bool emissionsOnly) {
00550
00551 for (std::vector<RODFDetector*>::const_iterator i=myDetectors.begin(); i!=myDetectors.end(); ++i) {
00552 (*i)->computeSplitProbabilities(&net, *this, flows, startTime, endTime, stepOffset);
00553 }
00554
00555 OutputDevice& out = OutputDevice::getDevice(file);
00556 out.writeXMLHeader("additional");
00557 for (std::vector<RODFDetector*>::const_iterator i=myDetectors.begin(); i!=myDetectors.end(); ++i) {
00558 RODFDetector *det = *i;
00559
00560 std::string escapedID = StringUtils::escapeXML(det->getID());
00561 std::string defFileName;
00562 if (det->getType()==SOURCE_DETECTOR) {
00563 defFileName = FileHelpers::getFilePath(file) + "emitter_" + escapedID + ".def.xml";
00564 } else if (writeCalibrators&&det->getType()==BETWEEN_DETECTOR) {
00565 defFileName = FileHelpers::getFilePath(file) + "calibrator_" + escapedID + ".def.xml";
00566 } else {
00567 defFileName = FileHelpers::getFilePath(file) + "other_" + escapedID + ".def.xml";
00568 continue;
00569 }
00570
00571 SUMOReal defaultSpeed = net.getEdge(det->getEdgeID())->getSpeed();
00572
00573 std::map<size_t, RandomDistributor<size_t>* > dists;
00574 if (!emissionsOnly&&flows.knows(det->getID())) {
00575 det->buildDestinationDistribution(*this, flows, startTime, endTime, stepOffset, net, dists, maxFollower);
00576 }
00577
00578 if (!det->writeEmitterDefinition(defFileName, dists, flows, startTime, endTime, stepOffset, includeUnusedRoutes, scale, emissionsOnly, defaultSpeed)) {
00579
00580 continue;
00581 }
00582
00583 clearDists(dists);
00584
00585 if (det->getType()==SOURCE_DETECTOR) {
00586 out << " <emitter id=\"source_" << escapedID
00587 << "\" pos=\"" << det->getPos() << "\" "
00588 << "lane=\"" << det->getLaneID() << "\" "
00589 << "friendly_pos=\"x\" "
00590 << "file=\"" << defFileName << "\"/>\n";
00591 } else if (writeCalibrators&&det->getType()==BETWEEN_DETECTOR) {
00592 out << " <calibrator id=\"calibrator_" << escapedID
00593 << "\" pos=\"" << det->getPos() << "\" "
00594 << "lane=\"" << det->getLaneID() << "\" "
00595 << "friendly_pos=\"x\" "
00596 << "file=\"" << defFileName << "\"/>\n";
00597 }
00598 }
00599 out.close();
00600 }
00601
00602
00603 void
00604 RODFDetectorCon::writeEmitterPOIs(const std::string &file,
00605 const RODFDetectorFlows &flows,
00606 SUMOTime startTime, SUMOTime endTime,
00607 SUMOTime stepOffset) {
00608 OutputDevice& out = OutputDevice::getDevice(file);
00609 out.writeXMLHeader("additional");
00610 for (std::vector<RODFDetector*>::const_iterator i=myDetectors.begin(); i!=myDetectors.end(); ++i) {
00611 RODFDetector *det = *i;
00612 SUMOReal flow = flows.getFlowSumSecure(det->getID());
00613 SUMOReal col = flow / flows.getMaxDetectorFlow();
00614 col = (SUMOReal)(col / 2. + .5);
00615 SUMOReal r, g, b;
00616 r = g = b = 0;
00617 out << " <poi id=\"" << StringUtils::escapeXML((*i)->getID()) << ":" << flow;
00618 switch ((*i)->getType()) {
00619 case BETWEEN_DETECTOR:
00620 out << "\" type=\"between_detector_position\" color=\"0,0," << col << "\"";
00621 break;
00622 case SOURCE_DETECTOR:
00623 out << "\" type=\"source_detector_position\" color=\"0," << col << ",0\"";
00624 break;
00625 case SINK_DETECTOR:
00626 out << "\" type=\"sink_detector_position\" color=\"" << col << ",0,0\"";
00627 break;
00628 case DISCARDED_DETECTOR:
00629 out << "\" type=\"discarded_detector_position\" color=\".2,.2,.2\"";
00630 break;
00631 default:
00632 throw 1;
00633 }
00634 out << " lane=\"" << (*i)->getLaneID()<< "\" pos=\"" << (*i)->getPos() << "\"/>\n";
00635 }
00636 out.close();
00637 }
00638
00639
00640 int
00641 RODFDetectorCon::getFlowFor(const ROEdge *edge, SUMOTime time,
00642 const RODFDetectorFlows &) const {
00643 SUMOReal stepOffset = 60;
00644 SUMOReal startTime = 0;
00645 assert(myDetectorEdgeMap.find(edge->getID())!=myDetectorEdgeMap.end());
00646 const std::vector<FlowDef> &flows = static_cast<const RODFEdge*>(edge)->getFlows();
00647 if (flows.size()!=0) {
00648 const FlowDef &srcFD = flows[(int)((time/stepOffset) - startTime)];
00649 return (int)(MAX2(srcFD.qLKW, (SUMOReal) 0.) + MAX2(srcFD.qPKW, (SUMOReal) 0.));
00650 }
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668 return -1;
00669 }
00670
00671
00672 int
00673 RODFDetectorCon::getAggFlowFor(const ROEdge *edge, SUMOTime time, SUMOTime period,
00674 const RODFDetectorFlows &) const {
00675 if (edge==0) {
00676 return 0;
00677 }
00678 SUMOReal stepOffset = 60;
00679 SUMOReal startTime = 0;
00680
00681 assert(myDetectorEdgeMap.find(edge->getID())!=myDetectorEdgeMap.end());
00682 const std::vector<FlowDef> &flows = static_cast<const RODFEdge*>(edge)->getFlows();
00683 SUMOReal agg = 0;
00684 for (std::vector<FlowDef>::const_iterator i=flows.begin(); i!=flows.end(); ++i) {
00685 const FlowDef &srcFD = *i;
00686 if (srcFD.qLKW>=0) {
00687 agg += srcFD.qLKW;
00688 }
00689 if (srcFD.qPKW>=0) {
00690 agg += srcFD.qPKW;
00691 }
00692 }
00693 return (int) agg;
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710 return -1;
00711 }
00712
00713
00714 void
00715 RODFDetectorCon::writeSpeedTrigger(const RODFNet * const net,
00716 const std::string &file,
00717 const RODFDetectorFlows &flows,
00718 SUMOTime startTime, SUMOTime endTime,
00719 SUMOTime stepOffset) {
00720 OutputDevice& out = OutputDevice::getDevice(file);
00721 out.writeXMLHeader("additional");
00722 for (std::vector<RODFDetector*>::const_iterator i=myDetectors.begin(); i!=myDetectors.end(); ++i) {
00723 RODFDetector *det = *i;
00724
00725 if (det->getType()==SINK_DETECTOR&&flows.knows(det->getID())) {
00726 std::string filename = FileHelpers::getFilePath(file) + "vss_" + det->getID() + ".def.xml";
00727 out << " <variableSpeedSign id=\"vss_" << StringUtils::escapeXML(det->getID()) << '\"'
00728 << " lanes=\"" << det->getLaneID() << '\"'
00729 << " file=\"" << filename << "\"/>\n";
00730 SUMOReal defaultSpeed = net!=0 ? net->getEdge(det->getEdgeID())->getSpeed() : (SUMOReal) 200.;
00731 det->writeSingleSpeedTrigger(filename, flows, startTime, endTime, stepOffset, defaultSpeed);
00732 }
00733 }
00734 out.close();
00735 }
00736
00737
00738 void
00739 RODFDetectorCon::writeEndRerouterDetectors(const std::string &file) {
00740 OutputDevice& out = OutputDevice::getDevice(file);
00741 out.writeXMLHeader("additional");
00742 for (std::vector<RODFDetector*>::const_iterator i=myDetectors.begin(); i!=myDetectors.end(); ++i) {
00743 RODFDetector *det = *i;
00744
00745 if (det->getType()==SINK_DETECTOR) {
00746 out << " <rerouter id=\"endrerouter_" << StringUtils::escapeXML(det->getID())
00747 << "\" edges=\"" <<
00748 det->getLaneID() << "\" attr=\"reroute\" pos=\"0\" file=\"endrerouter_"
00749 << det->getID() << ".def.xml\"/>\n";
00750 }
00751 }
00752 out.close();
00753 }
00754
00755
00756 void
00757 RODFDetectorCon::writeValidationDetectors(const std::string &file,
00758 bool includeSources,
00759 bool singleFile, bool friendly) {
00760 OutputDevice& out = OutputDevice::getDevice(file);
00761 out.writeXMLHeader("additional");
00762 for (std::vector<RODFDetector*>::const_iterator i=myDetectors.begin(); i!=myDetectors.end(); ++i) {
00763 RODFDetector *det = *i;
00764
00765 if (det->getType()!=SOURCE_DETECTOR||includeSources) {
00766 SUMOReal pos = det->getPos();
00767 if (det->getType()==SOURCE_DETECTOR) {
00768 pos += 1;
00769 }
00770 out << " <detector id=\"validation_" << StringUtils::escapeXML(det->getID()) << "\" "
00771 << "lane=\"" << det->getLaneID() << "\" "
00772 << "pos=\"" << pos << "\" "
00773 << "freq=\"60\" ";
00774 if (friendly) {
00775 out << "friendly_pos=\"x\" ";
00776 }
00777 if (!singleFile) {
00778 out << "file=\"validation_det_" << StringUtils::escapeXML(det->getID()) << ".xml\"/>\n";
00779 } else {
00780 out << "file=\"validation_dets.xml\"/>\n";
00781 }
00782 }
00783 }
00784 out.close();
00785 }
00786
00787
00788 void
00789 RODFDetectorCon::removeDetector(const std::string &id) {
00790
00791 std::map<std::string, RODFDetector*>::iterator ri1 = myDetectorMap.find(id);
00792 RODFDetector *oldDet = (*ri1).second;
00793 myDetectorMap.erase(ri1);
00794
00795 std::vector<RODFDetector*>::iterator ri2 =
00796 find(myDetectors.begin(), myDetectors.end(), oldDet);
00797 myDetectors.erase(ri2);
00798
00799 bool found = false;
00800 for (std::map<std::string, std::vector<RODFDetector*> >::iterator rr3=myDetectorEdgeMap.begin(); !found&&rr3!=myDetectorEdgeMap.end(); ++rr3) {
00801 std::vector<RODFDetector*> &dets = (*rr3).second;
00802 for (std::vector<RODFDetector*>::iterator ri3=dets.begin(); !found&&ri3!=dets.end();) {
00803 if (*ri3==oldDet) {
00804 found = true;
00805 ri3 = dets.erase(ri3);
00806 } else {
00807 ++ri3;
00808 }
00809 }
00810 }
00811 delete oldDet;
00812 }
00813
00814
00815 void
00816 RODFDetectorCon::guessEmptyFlows(RODFDetectorFlows &flows) {
00817
00818
00819
00820 bool changed = true;
00821 while (changed) {
00822 for (std::vector<RODFDetector*>::const_iterator i=myDetectors.begin(); i!=myDetectors.end(); ++i) {
00823 RODFDetector *det = *i;
00824 const std::vector<RODFDetector*> &prior = det->getPriorDetectors();
00825 const std::vector<RODFDetector*> &follower = det->getFollowerDetectors();
00826 size_t noFollowerWithRoutes = 0;
00827 size_t noPriorWithRoutes = 0;
00828
00829 std::vector<RODFDetector*>::const_iterator j;
00830 for (j=prior.begin(); j!=prior.end(); ++j) {
00831 if (flows.knows((*j)->getID())) {
00832 ++noPriorWithRoutes;
00833 }
00834 }
00835 assert(noPriorWithRoutes<=prior.size());
00836 for (j=follower.begin(); j!=follower.end(); ++j) {
00837 if (flows.knows((*j)->getID())) {
00838 ++noFollowerWithRoutes;
00839 }
00840 }
00841 assert(noFollowerWithRoutes<=follower.size());
00842
00843
00844 if (!flows.knows(det->getID())) {
00845 continue;
00846 }
00847
00848
00849 if (noFollowerWithRoutes==follower.size()) {
00850
00851 continue;
00852 }
00853
00854 }
00855 }
00856 }
00857
00858
00859 const RODFDetector &
00860 RODFDetectorCon::getAnyDetectorForEdge(const RODFEdge * const edge) const {
00861 for (std::vector<RODFDetector*>::const_iterator i=myDetectors.begin(); i!=myDetectors.end(); ++i) {
00862 if ((*i)->getEdgeID()==edge->getID()) {
00863 return **i;
00864 }
00865 }
00866 throw 1;
00867 }
00868
00869
00870 void
00871 RODFDetectorCon::clearDists(std::map<size_t, RandomDistributor<size_t>* > &dists) const throw() {
00872 for (std::map<size_t, RandomDistributor<size_t>* >::iterator i=dists.begin(); i!=dists.end(); ++i) {
00873 delete(*i).second;
00874 }
00875 }
00876
00877
00878 void
00879 RODFDetectorCon::mesoJoin(const std::string &nid,
00880 const std::vector<std::string> &oldids) {
00881
00882 const RODFDetector &first = getDetector(*(oldids.begin()));
00883 RODFDetector *newDet = new RODFDetector(nid, first);
00884 addDetector(newDet);
00885
00886 for (std::vector<std::string>::const_iterator i=oldids.begin(); i!=oldids.end(); ++i) {
00887 removeDetector(*i);
00888 }
00889 }
00890
00891
00892