SUMO - Simulation of Urban MObility
RODFDetectorFlow.cpp
Go to the documentation of this file.
00001 /****************************************************************************/
00009 // Storage for flows within the DFROUTER
00010 /****************************************************************************/
00011 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00012 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00013 /****************************************************************************/
00014 //
00015 //   This file is part of SUMO.
00016 //   SUMO is free software: you can redistribute it and/or modify
00017 //   it under the terms of the GNU General Public License as published by
00018 //   the Free Software Foundation, either version 3 of the License, or
00019 //   (at your option) any later version.
00020 //
00021 /****************************************************************************/
00022 
00023 
00024 // ===========================================================================
00025 // included modules
00026 // ===========================================================================
00027 #ifdef _MSC_VER
00028 #include <windows_config.h>
00029 #else
00030 #include <config.h>
00031 #endif
00032 
00033 #include <iostream>
00034 #include <cassert>
00035 #include "RODFDetectorFlow.h"
00036 
00037 #ifdef CHECK_MEMORY_LEAKS
00038 #include <foreign/nvwa/debug_new.h>
00039 #endif // CHECK_MEMORY_LEAKS
00040 
00041 
00042 // ===========================================================================
00043 // method definitions
00044 // ===========================================================================
00045 RODFDetectorFlows::RODFDetectorFlows(SUMOTime startTime, SUMOTime endTime,
00046                                      SUMOTime stepOffset)
00047     : myBeginTime(startTime), myEndTime(endTime), myStepOffset(stepOffset),
00048       myMaxDetectorFlow(-1) {}
00049 
00050 
00051 RODFDetectorFlows::~RODFDetectorFlows() {}
00052 
00053 
00054 void
00055 RODFDetectorFlows::addFlow(const std::string& id, SUMOTime t, const FlowDef& fd) {
00056     if (myFastAccessFlows.find(id) == myFastAccessFlows.end()) {
00057         size_t noItems = (size_t)((myEndTime - myBeginTime) / myStepOffset);
00058         myFastAccessFlows[id] = std::vector<FlowDef>(noItems);
00059         std::vector<FlowDef> &cflows = myFastAccessFlows[id];
00060         // initialise
00061         for (std::vector<FlowDef>::iterator i = cflows.begin(); i < cflows.end(); ++i) {
00062             (*i).qPKW = 0;
00063             (*i).qLKW = 0;
00064             (*i).vPKW = 0;
00065             (*i).vLKW = 0;
00066             (*i).fLKW = 0;
00067             (*i).isLKW = 0;
00068             (*i).firstSet = true;
00069         }
00070     }
00071     assert((t - myBeginTime) / myStepOffset < (int) myFastAccessFlows[id].size());
00072     FlowDef& ofd = myFastAccessFlows[id][(t - myBeginTime) / myStepOffset];
00073     if (ofd.firstSet) {
00074         ofd = fd;
00075         ofd.firstSet = false;
00076     } else {
00077         ofd.qLKW = ofd.qLKW + fd.qLKW;
00078         ofd.qPKW = ofd.qPKW + fd.qPKW;
00079         ofd.vLKW = ofd.vLKW + fd.vLKW; 
00080         ofd.vPKW = ofd.vPKW + fd.vPKW; 
00081     }
00082     if (ofd.qLKW != 0 && ofd.qPKW != 0) {
00083         ofd.fLKW = ofd.qLKW / ofd.qPKW ;
00084     } else if (ofd.qPKW != 0) {
00085         ofd.fLKW = 0;
00086     } else {
00087         ofd.fLKW = 1;
00088         ofd.isLKW = 1;
00089     }
00090 }
00091 
00092 
00093 
00094 
00095 void
00096 RODFDetectorFlows::setFlows(const std::string& detector_id,
00097                             std::vector<FlowDef> &flows) {
00098     for (std::vector<FlowDef>::iterator i = flows.begin(); i < flows.end(); ++i) {
00099         FlowDef& ofd = *i;
00100         if (ofd.qLKW != 0 && ofd.qPKW != 0) {
00101             ofd.fLKW = ofd.qLKW / ofd.qPKW ;
00102         } else {
00103             ofd.fLKW = 0;
00104         }
00105     }
00106     myFastAccessFlows[detector_id] = flows;
00107 }
00108 
00109 
00110 void
00111 RODFDetectorFlows::removeFlow(const std::string& detector_id) {
00112     myFastAccessFlows.erase(detector_id);
00113 }
00114 
00115 
00116 bool
00117 RODFDetectorFlows::knows(const std::string& det_id) const {
00118     return myFastAccessFlows.find(det_id) != myFastAccessFlows.end();
00119 }
00120 
00121 
00122 const std::vector<FlowDef> &
00123 RODFDetectorFlows::getFlowDefs(const std::string& id) const {
00124     assert(myFastAccessFlows.find(id) != myFastAccessFlows.end());
00125     assert(myFastAccessFlows.find(id)->second.size() != 0);
00126     return myFastAccessFlows.find(id)->second;
00127 }
00128 
00129 
00130 SUMOReal
00131 RODFDetectorFlows::getFlowSumSecure(const std::string& id) const {
00132     SUMOReal ret = 0;
00133     if (knows(id)) {
00134         const std::vector<FlowDef> &flows = getFlowDefs(id);
00135         for (std::vector<FlowDef>::const_iterator i = flows.begin(); i != flows.end(); ++i) {
00136             ret += (*i).qPKW;
00137             ret += (*i).qLKW;
00138         }
00139     }
00140     return ret;
00141 }
00142 
00143 
00144 SUMOReal
00145 RODFDetectorFlows::getMaxDetectorFlow() const {
00146     if (myMaxDetectorFlow < 0) {
00147         SUMOReal max = 0;
00148         std::map<std::string, std::vector<FlowDef> >::const_iterator j;
00149         for (j = myFastAccessFlows.begin(); j != myFastAccessFlows.end(); ++j) {
00150             SUMOReal curr = 0;
00151             const std::vector<FlowDef> &flows = (*j).second;
00152             for (std::vector<FlowDef>::const_iterator i = flows.begin(); i != flows.end(); ++i) {
00153                 curr += (*i).qPKW;
00154                 curr += (*i).qLKW;
00155             }
00156             if (max < curr) {
00157                 max = curr;
00158             }
00159         }
00160         myMaxDetectorFlow = max;
00161     }
00162     return myMaxDetectorFlow;
00163 }
00164 
00165 
00166 void
00167 RODFDetectorFlows::mesoJoin(const std::string& nid,
00168                             const std::vector<std::string> &oldids) {
00169     for (std::vector<std::string>::const_iterator i = oldids.begin(); i != oldids.end(); ++i) {
00170         if (!knows(*i)) {
00171             continue;
00172         }
00173         std::vector<FlowDef> &flows = myFastAccessFlows[*i];
00174         size_t index = 0;
00175         for (SUMOTime t = myBeginTime; t != myEndTime; t += myStepOffset) {
00176             addFlow(nid, t, flows[index++]); // !!!
00177         }
00178         myFastAccessFlows.erase(*i);
00179     }
00180 }
00181 
00182 
00183 void
00184 RODFDetectorFlows::printAbsolute() const {
00185     for (std::map<std::string, std::vector<FlowDef> >::const_iterator i = myFastAccessFlows.begin(); i != myFastAccessFlows.end(); ++i) {
00186         std::cout << (*i).first << ":";
00187         const std::vector<FlowDef> &flows = (*i).second;
00188         SUMOReal qPKW = 0;
00189         SUMOReal qLKW = 0;
00190         for (std::vector<FlowDef>::const_iterator j = flows.begin(); j != flows.end(); ++j) {
00191             qPKW += (*j).qPKW;
00192             qLKW += (*j).qLKW;
00193         }
00194         std::cout << qPKW << "/" << qLKW << std::endl;
00195     }
00196 }
00197 
00198 /****************************************************************************/
00199 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines