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