SUMO - Simulation of Urban MObility

RODFDetectorFlow.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // Storage for flows 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 <iostream>
00031 #include <cassert>
00032 #include "RODFDetectorFlow.h"
00033 
00034 #ifdef CHECK_MEMORY_LEAKS
00035 #include <foreign/nvwa/debug_new.h>
00036 #endif // CHECK_MEMORY_LEAKS
00037 
00038 
00039 // ===========================================================================
00040 // method definitions
00041 // ===========================================================================
00042 RODFDetectorFlows::RODFDetectorFlows(SUMOTime startTime, SUMOTime endTime,
00043                                      SUMOTime stepOffset)
00044     : myBeginTime(startTime), myEndTime(endTime), myStepOffset(stepOffset),
00045       myMaxDetectorFlow(-1) {}
00046 
00047 
00048 RODFDetectorFlows::~RODFDetectorFlows() {}
00049 
00050 
00051 void
00052 RODFDetectorFlows::addFlow(const std::string& id, SUMOTime t, const FlowDef& fd) {
00053     if (myFastAccessFlows.find(id)==myFastAccessFlows.end()) {
00054         size_t noItems = (size_t)((myEndTime-myBeginTime)/myStepOffset);
00055         myFastAccessFlows[id] = std::vector<FlowDef>(noItems);
00056         std::vector<FlowDef> &cflows = myFastAccessFlows[id];
00057         // initialise
00058         for (std::vector<FlowDef>::iterator i=cflows.begin(); i<cflows.end(); ++i) {
00059             (*i).qPKW = 0;
00060             (*i).qLKW = 0;
00061             (*i).vPKW = 0;
00062             (*i).vLKW = 0;
00063             (*i).fLKW = 0;
00064             (*i).isLKW = 0;
00065             (*i).firstSet = true;
00066         }
00067     }
00068     assert((t-myBeginTime)/myStepOffset<(int) myFastAccessFlows[id].size());
00069     FlowDef& ofd = myFastAccessFlows[id][(t-myBeginTime)/myStepOffset];
00070     if (ofd.firstSet) {
00071         ofd = fd;
00072         ofd.firstSet = false;
00073     } else {
00074         ofd.qLKW = ofd.qLKW + fd.qLKW;
00075         ofd.qPKW = ofd.qPKW + fd.qPKW;
00076         ofd.vLKW = ofd.vLKW + fd.vLKW; 
00077         ofd.vPKW = ofd.vPKW + fd.vPKW; 
00078     }
00079     if (ofd.qLKW!=0 && ofd.qPKW!=0) {
00080         ofd.fLKW = ofd.qLKW / ofd.qPKW ;
00081     } else if (ofd.qPKW!=0) {
00082         ofd.fLKW = 0;
00083     } else {
00084         ofd.fLKW = 1;
00085         ofd.isLKW = 1;
00086     }
00087 }
00088 
00089 
00090 
00091 
00092 void
00093 RODFDetectorFlows::setFlows(const std::string& detector_id,
00094                             std::vector<FlowDef> &flows) {
00095     for (std::vector<FlowDef>::iterator i=flows.begin(); i<flows.end(); ++i) {
00096         FlowDef& ofd = *i;
00097         if (ofd.qLKW!=0 && ofd.qPKW!=0) {
00098             ofd.fLKW = ofd.qLKW / ofd.qPKW ;
00099         } else {
00100             ofd.fLKW = 0;
00101         }
00102     }
00103     myFastAccessFlows[detector_id] = flows;
00104 }
00105 
00106 
00107 void
00108 RODFDetectorFlows::removeFlow(const std::string& detector_id) {
00109     myFastAccessFlows.erase(detector_id);
00110 }
00111 
00112 
00113 bool
00114 RODFDetectorFlows::knows(const std::string& det_id) const {
00115     return myFastAccessFlows.find(det_id)!=myFastAccessFlows.end();
00116 }
00117 
00118 
00119 const std::vector<FlowDef> &
00120 RODFDetectorFlows::getFlowDefs(const std::string& id) const {
00121     assert(myFastAccessFlows.find(id)!=myFastAccessFlows.end());
00122     assert(myFastAccessFlows.find(id)->second.size()!=0);
00123     return myFastAccessFlows.find(id)->second;
00124 }
00125 
00126 
00127 SUMOReal
00128 RODFDetectorFlows::getFlowSumSecure(const std::string& id) const {
00129     SUMOReal ret = 0;
00130     if (knows(id)) {
00131         const std::vector<FlowDef> &flows = getFlowDefs(id);
00132         for (std::vector<FlowDef>::const_iterator i=flows.begin(); i!=flows.end(); ++i) {
00133             ret += (*i).qPKW;
00134             ret += (*i).qLKW;
00135         }
00136     }
00137     return ret;
00138 }
00139 
00140 
00141 SUMOReal
00142 RODFDetectorFlows::getMaxDetectorFlow() const {
00143     if (myMaxDetectorFlow<0) {
00144         SUMOReal max = 0;
00145         std::map<std::string, std::vector<FlowDef> >::const_iterator j;
00146         for (j=myFastAccessFlows.begin(); j!=myFastAccessFlows.end(); ++j) {
00147             SUMOReal curr = 0;
00148             const std::vector<FlowDef> &flows = (*j).second;
00149             for (std::vector<FlowDef>::const_iterator i=flows.begin(); i!=flows.end(); ++i) {
00150                 curr += (*i).qPKW;
00151                 curr += (*i).qLKW;
00152             }
00153             if (max<curr) {
00154                 max = curr;
00155             }
00156         }
00157         myMaxDetectorFlow = max;
00158     }
00159     return myMaxDetectorFlow;
00160 }
00161 
00162 
00163 void
00164 RODFDetectorFlows::mesoJoin(const std::string& nid,
00165                             const std::vector<std::string> &oldids) {
00166     for (std::vector<std::string>::const_iterator i=oldids.begin(); i!=oldids.end(); ++i) {
00167         if (!knows(*i)) {
00168             continue;
00169         }
00170         std::vector<FlowDef> &flows = myFastAccessFlows[*i];
00171         size_t index = 0;
00172         for (SUMOTime t=myBeginTime; t!=myEndTime; t+=myStepOffset) {
00173             addFlow(nid, t, flows[index++]); // !!!
00174         }
00175         myFastAccessFlows.erase(*i);
00176     }
00177 }
00178 
00179 
00180 void
00181 RODFDetectorFlows::printAbsolute() const {
00182     for (std::map<std::string, std::vector<FlowDef> >::const_iterator i=myFastAccessFlows.begin(); i!=myFastAccessFlows.end(); ++i) {
00183         std::cout << (*i).first << ":";
00184         const std::vector<FlowDef> &flows = (*i).second;
00185         SUMOReal qPKW = 0;
00186         SUMOReal qLKW = 0;
00187         for (std::vector<FlowDef>::const_iterator j=flows.begin(); j!=flows.end(); ++j) {
00188             qPKW += (*j).qPKW;
00189             qLKW += (*j).qLKW;
00190         }
00191         std::cout << qPKW << "/" << qLKW << std::endl;
00192     }
00193 }
00194 
00195 /****************************************************************************/
00196 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines