SUMO - Simulation of Urban MObility
RODFDetectorFlow.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // Storage for flows within the DFROUTER
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
13 // Copyright (C) 2006-2014 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 /****************************************************************************/
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include <iostream>
35 #include <cassert>
36 #include "RODFDetectorFlow.h"
37 
38 #ifdef CHECK_MEMORY_LEAKS
39 #include <foreign/nvwa/debug_new.h>
40 #endif // CHECK_MEMORY_LEAKS
41 
42 
43 // ===========================================================================
44 // method definitions
45 // ===========================================================================
47  SUMOTime stepOffset)
48  : myBeginTime(startTime), myEndTime(endTime), myStepOffset(stepOffset),
49  myMaxDetectorFlow(-1) {}
50 
51 
53 
54 
55 void
56 RODFDetectorFlows::addFlow(const std::string& id, SUMOTime t, const FlowDef& fd) {
57  if (myFastAccessFlows.find(id) == myFastAccessFlows.end()) {
58  size_t noItems = (size_t)((myEndTime - myBeginTime) / myStepOffset);
59  myFastAccessFlows[id] = std::vector<FlowDef>(noItems);
60  std::vector<FlowDef>& cflows = myFastAccessFlows[id];
61  // initialise
62  for (std::vector<FlowDef>::iterator i = cflows.begin(); i < cflows.end(); ++i) {
63  (*i).qPKW = 0;
64  (*i).qLKW = 0;
65  (*i).vPKW = 0;
66  (*i).vLKW = 0;
67  (*i).fLKW = 0;
68  (*i).isLKW = 0;
69  (*i).firstSet = true;
70  }
71  }
72  assert((t - myBeginTime) / myStepOffset < (int) myFastAccessFlows[id].size());
74  if (ofd.firstSet) {
75  ofd = fd;
76  ofd.firstSet = false;
77  } else {
78  ofd.qLKW = ofd.qLKW + fd.qLKW;
79  ofd.qPKW = ofd.qPKW + fd.qPKW;
80  ofd.vLKW = ofd.vLKW + fd.vLKW;
81  ofd.vPKW = ofd.vPKW + fd.vPKW;
82  }
83  if (ofd.qPKW != 0) {
84  ofd.fLKW = ofd.qLKW / (ofd.qLKW + ofd.qPKW);
85  } else {
86  ofd.fLKW = 1;
87  ofd.isLKW = 1;
88  }
89 }
90 
91 
92 
93 
94 void
95 RODFDetectorFlows::setFlows(const std::string& detector_id,
96  std::vector<FlowDef>& flows) {
97  for (std::vector<FlowDef>::iterator i = flows.begin(); i < flows.end(); ++i) {
98  FlowDef& ofd = *i;
99  if (ofd.qLKW != 0 && ofd.qPKW != 0) {
100  ofd.fLKW = ofd.qLKW / ofd.qPKW;
101  } else {
102  ofd.fLKW = 0;
103  }
104  }
105  myFastAccessFlows[detector_id] = flows;
106 }
107 
108 
109 void
110 RODFDetectorFlows::removeFlow(const std::string& detector_id) {
111  myFastAccessFlows.erase(detector_id);
112 }
113 
114 
115 bool
116 RODFDetectorFlows::knows(const std::string& det_id) const {
117  return myFastAccessFlows.find(det_id) != myFastAccessFlows.end();
118 }
119 
120 
121 const std::vector<FlowDef>&
122 RODFDetectorFlows::getFlowDefs(const std::string& id) const {
123  assert(myFastAccessFlows.find(id) != myFastAccessFlows.end());
124  assert(myFastAccessFlows.find(id)->second.size() != 0);
125  return myFastAccessFlows.find(id)->second;
126 }
127 
128 
129 SUMOReal
130 RODFDetectorFlows::getFlowSumSecure(const std::string& id) const {
131  SUMOReal ret = 0;
132  if (knows(id)) {
133  const std::vector<FlowDef>& flows = getFlowDefs(id);
134  for (std::vector<FlowDef>::const_iterator i = flows.begin(); i != flows.end(); ++i) {
135  ret += (*i).qPKW;
136  ret += (*i).qLKW;
137  }
138  }
139  return ret;
140 }
141 
142 
143 SUMOReal
145  if (myMaxDetectorFlow < 0) {
146  SUMOReal max = 0;
147  std::map<std::string, std::vector<FlowDef> >::const_iterator j;
148  for (j = myFastAccessFlows.begin(); j != myFastAccessFlows.end(); ++j) {
149  SUMOReal curr = 0;
150  const std::vector<FlowDef>& flows = (*j).second;
151  for (std::vector<FlowDef>::const_iterator i = flows.begin(); i != flows.end(); ++i) {
152  curr += (*i).qPKW;
153  curr += (*i).qLKW;
154  }
155  if (max < curr) {
156  max = curr;
157  }
158  }
160  }
161  return myMaxDetectorFlow;
162 }
163 
164 
165 void
166 RODFDetectorFlows::mesoJoin(const std::string& nid,
167  const std::vector<std::string>& oldids) {
168  for (std::vector<std::string>::const_iterator i = oldids.begin(); i != oldids.end(); ++i) {
169  if (!knows(*i)) {
170  continue;
171  }
172  std::vector<FlowDef>& flows = myFastAccessFlows[*i];
173  size_t index = 0;
174  for (SUMOTime t = myBeginTime; t != myEndTime; t += myStepOffset) {
175  addFlow(nid, t, flows[index++]); // !!!
176  }
177  myFastAccessFlows.erase(*i);
178  }
179 }
180 
181 
182 void
184  for (std::map<std::string, std::vector<FlowDef> >::const_iterator i = myFastAccessFlows.begin(); i != myFastAccessFlows.end(); ++i) {
185  std::cout << (*i).first << ":";
186  const std::vector<FlowDef>& flows = (*i).second;
187  SUMOReal qPKW = 0;
188  SUMOReal qLKW = 0;
189  for (std::vector<FlowDef>::const_iterator j = flows.begin(); j != flows.end(); ++j) {
190  qPKW += (*j).qPKW;
191  qLKW += (*j).qLKW;
192  }
193  std::cout << qPKW << "/" << qLKW << std::endl;
194  }
195 }
196 
197 /****************************************************************************/
198 
SUMOReal getFlowSumSecure(const std::string &id) const
std::map< std::string, std::vector< FlowDef > > myFastAccessFlows
void removeFlow(const std::string &detector_id)
SUMOReal getMaxDetectorFlow() const
SUMOReal isLKW
void printAbsolute() const
SUMOReal fLKW
#define max(a, b)
Definition: polyfonts.c:65
void setFlows(const std::string &detector_id, std::vector< FlowDef > &)
bool knows(const std::string &det_id) const
Definition of the traffic during a certain time containing the flows and speeds.
SUMOReal vPKW
void addFlow(const std::string &detector_id, SUMOTime timestamp, const FlowDef &fd)
SUMOReal qPKW
const std::vector< FlowDef > & getFlowDefs(const std::string &id) const
#define SUMOReal
Definition: config.h:215
void mesoJoin(const std::string &nid, const std::vector< std::string > &oldids)
SUMOReal myMaxDetectorFlow
SUMOReal vLKW
SUMOReal qLKW
RODFDetectorFlows(SUMOTime startTime, SUMOTime endTime, SUMOTime stepOffset)