RODFDetectorFlow.cpp
Go to the documentation of this file.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 <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
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, int 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.find(id)->second;
00057
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<(int) myFastAccessFlows[id].size());
00069 FlowDef &ofd = myFastAccessFlows[id][t];
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 if (myFastAccessFlows.find(detector_id)!=myFastAccessFlows.end()) {
00110 myFastAccessFlows.erase(myFastAccessFlows.find(detector_id));
00111 }
00112 }
00113
00114
00115 bool
00116 RODFDetectorFlows::knows(const std::string &det_id) const {
00117 return myFastAccessFlows.find(det_id)!=myFastAccessFlows.end();
00118 }
00119
00120
00121 bool
00122 RODFDetectorFlows::knows(const std::string &det_id, SUMOTime ) const {
00123 if (myFastAccessFlows.find(det_id)==myFastAccessFlows.end()) {
00124 return false;
00125 }
00126 return true;
00127 }
00128
00129
00130 const std::vector<FlowDef> &
00131 RODFDetectorFlows::getFlowDefs(const std::string &id) const {
00132 assert(myFastAccessFlows.find(id)!=myFastAccessFlows.end());
00133 assert(myFastAccessFlows.find(id)->second.size()!=0);
00134 return myFastAccessFlows.find(id)->second;
00135 }
00136
00137
00138 SUMOReal
00139 RODFDetectorFlows::getFlowSumSecure(const std::string &id) const {
00140 SUMOReal ret = 0;
00141 if (knows(id)) {
00142 const std::vector<FlowDef> &flows = getFlowDefs(id);
00143 for (std::vector<FlowDef>::const_iterator i=flows.begin(); i!=flows.end(); ++i) {
00144 ret += (*i).qPKW;
00145 ret += (*i).qLKW;
00146 }
00147 }
00148 return ret;
00149 }
00150
00151
00152 SUMOReal
00153 RODFDetectorFlows::getMaxDetectorFlow() const {
00154 if (myMaxDetectorFlow<0) {
00155 SUMOReal max = 0;
00156 std::map<std::string, std::vector<FlowDef> >::const_iterator j;
00157 for (j=myFastAccessFlows.begin(); j!=myFastAccessFlows.end(); ++j) {
00158 SUMOReal curr = 0;
00159 const std::vector<FlowDef> &flows = (*j).second;
00160 for (std::vector<FlowDef>::const_iterator i=flows.begin(); i!=flows.end(); ++i) {
00161 curr += (*i).qPKW;
00162 curr += (*i).qLKW;
00163 }
00164 if (max<curr) {
00165 max = curr;
00166 }
00167 }
00168 myMaxDetectorFlow = max;
00169 }
00170 return myMaxDetectorFlow;
00171 }
00172
00173
00174 void
00175 RODFDetectorFlows::mesoJoin(const std::string &nid,
00176 const std::vector<std::string> &oldids) {
00177 for (std::vector<std::string>::const_iterator i=oldids.begin(); i!=oldids.end(); ++i) {
00178 if (!knows(*i)) {
00179 continue;
00180 }
00181 std::map<std::string, std::vector<FlowDef> >::iterator j = myFastAccessFlows.find(*i);
00182 std::vector<FlowDef> &flows = (*j).second;
00183 size_t index = 0;
00184 for (SUMOTime t=myBeginTime; t!=myEndTime; t+=myStepOffset) {
00185 addFlow(nid, t/myStepOffset, flows[index++]);
00186 }
00187 myFastAccessFlows.erase(j);
00188 }
00189 }
00190
00191
00192 void
00193 RODFDetectorFlows::printAbsolute() const {
00194 for (std::map<std::string, std::vector<FlowDef> >::const_iterator i=myFastAccessFlows.begin(); i!=myFastAccessFlows.end(); ++i) {
00195 std::cout << (*i).first << ":";
00196 const std::vector<FlowDef> &flows = (*i).second;
00197 SUMOReal qPKW = 0;
00198 SUMOReal qLKW = 0;
00199 for (std::vector<FlowDef>::const_iterator j=flows.begin(); j!=flows.end(); ++j) {
00200 qPKW += (*j).qPKW;
00201 qLKW += (*j).qLKW;
00202 }
00203 std::cout << qPKW << "/" << qLKW << std::endl;
00204 }
00205 }
00206
00207
00208