SUMO - Simulation of Urban MObility

RODFDetector.cpp

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