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