SUMO - Simulation of Urban MObility
RODFDetectorFlow.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2006-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
20 // Storage for flows within the DFROUTER
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 
38 // ===========================================================================
39 // method definitions
40 // ===========================================================================
42  SUMOTime stepOffset)
43  : myBeginTime(startTime), myEndTime(endTime), myStepOffset(stepOffset),
44  myMaxDetectorFlow(-1) {}
45 
46 
48 
49 
50 void
51 RODFDetectorFlows::addFlow(const std::string& id, SUMOTime t, const FlowDef& fd) {
52  if (myFastAccessFlows.find(id) == myFastAccessFlows.end()) {
53  int noItems = (int)((myEndTime - myBeginTime) / myStepOffset);
54  myFastAccessFlows[id] = std::vector<FlowDef>(noItems);
55  std::vector<FlowDef>& cflows = myFastAccessFlows[id];
56  // initialise
57  for (std::vector<FlowDef>::iterator i = cflows.begin(); i < cflows.end(); ++i) {
58  (*i).qPKW = 0;
59  (*i).qLKW = 0;
60  (*i).vPKW = 0;
61  (*i).vLKW = 0;
62  (*i).fLKW = 0;
63  (*i).isLKW = 0;
64  (*i).firstSet = true;
65  }
66  }
67  const int index = (int)((t - myBeginTime) / myStepOffset);
68  assert(index < (int) myFastAccessFlows[id].size());
69  FlowDef& ofd = myFastAccessFlows[id][index];
70  if (ofd.firstSet) {
71  ofd = fd;
72  ofd.firstSet = false;
73  } else {
74  ofd.qLKW = ofd.qLKW + fd.qLKW;
75  ofd.qPKW = ofd.qPKW + fd.qPKW;
76  ofd.vLKW = ofd.vLKW + fd.vLKW;
77  ofd.vPKW = ofd.vPKW + fd.vPKW;
78  }
79  if (ofd.qPKW != 0) {
80  ofd.fLKW = ofd.qLKW / (ofd.qLKW + ofd.qPKW);
81  } else {
82  ofd.fLKW = 1;
83  ofd.isLKW = 1;
84  }
85 }
86 
87 
88 
89 
90 void
91 RODFDetectorFlows::setFlows(const std::string& detector_id,
92  std::vector<FlowDef>& flows) {
93  for (std::vector<FlowDef>::iterator i = flows.begin(); i < flows.end(); ++i) {
94  FlowDef& ofd = *i;
95  if (ofd.qLKW != 0 && ofd.qPKW != 0) {
96  ofd.fLKW = ofd.qLKW / ofd.qPKW;
97  } else {
98  ofd.fLKW = 0;
99  }
100  }
101  myFastAccessFlows[detector_id] = flows;
102 }
103 
104 
105 void
106 RODFDetectorFlows::removeFlow(const std::string& detector_id) {
107  myFastAccessFlows.erase(detector_id);
108 }
109 
110 
111 bool
112 RODFDetectorFlows::knows(const std::string& det_id) const {
113  return myFastAccessFlows.find(det_id) != myFastAccessFlows.end();
114 }
115 
116 
117 const std::vector<FlowDef>&
118 RODFDetectorFlows::getFlowDefs(const std::string& id) const {
119  assert(myFastAccessFlows.find(id) != myFastAccessFlows.end());
120  assert(myFastAccessFlows.find(id)->second.size() != 0);
121  return myFastAccessFlows.find(id)->second;
122 }
123 
124 
125 double
126 RODFDetectorFlows::getFlowSumSecure(const std::string& id) const {
127  double ret = 0;
128  if (knows(id)) {
129  const std::vector<FlowDef>& flows = getFlowDefs(id);
130  for (std::vector<FlowDef>::const_iterator i = flows.begin(); i != flows.end(); ++i) {
131  ret += (*i).qPKW;
132  ret += (*i).qLKW;
133  }
134  }
135  return ret;
136 }
137 
138 
139 double
141  if (myMaxDetectorFlow < 0) {
142  double max = 0;
143  std::map<std::string, std::vector<FlowDef> >::const_iterator j;
144  for (j = myFastAccessFlows.begin(); j != myFastAccessFlows.end(); ++j) {
145  double curr = 0;
146  const std::vector<FlowDef>& flows = (*j).second;
147  for (std::vector<FlowDef>::const_iterator i = flows.begin(); i != flows.end(); ++i) {
148  curr += (*i).qPKW;
149  curr += (*i).qLKW;
150  }
151  if (max < curr) {
152  max = curr;
153  }
154  }
155  myMaxDetectorFlow = max;
156  }
157  return myMaxDetectorFlow;
158 }
159 
160 
161 void
162 RODFDetectorFlows::mesoJoin(const std::string& nid,
163  const std::vector<std::string>& oldids) {
164  for (std::vector<std::string>::const_iterator i = oldids.begin(); i != oldids.end(); ++i) {
165  if (!knows(*i)) {
166  continue;
167  }
168  std::vector<FlowDef>& flows = myFastAccessFlows[*i];
169  int index = 0;
170  for (SUMOTime t = myBeginTime; t != myEndTime; t += myStepOffset) {
171  addFlow(nid, t, flows[index++]); // !!!
172  }
173  myFastAccessFlows.erase(*i);
174  }
175 }
176 
177 
178 void
180  for (std::map<std::string, std::vector<FlowDef> >::const_iterator i = myFastAccessFlows.begin(); i != myFastAccessFlows.end(); ++i) {
181  std::cout << (*i).first << ":";
182  const std::vector<FlowDef>& flows = (*i).second;
183  double qPKW = 0;
184  double qLKW = 0;
185  for (std::vector<FlowDef>::const_iterator j = flows.begin(); j != flows.end(); ++j) {
186  qPKW += (*j).qPKW;
187  qLKW += (*j).qLKW;
188  }
189  std::cout << qPKW << "/" << qLKW << std::endl;
190  }
191 }
192 
193 /****************************************************************************/
194 
double getFlowSumSecure(const std::string &id) const
double fLKW
std::map< std::string, std::vector< FlowDef > > myFastAccessFlows
void removeFlow(const std::string &detector_id)
static double fd[10]
Definition: odrSpiral.cpp:94
bool knows(const std::string &det_id) const
void printAbsolute() const
double vPKW
double getMaxDetectorFlow() const
double vLKW
void setFlows(const std::string &detector_id, std::vector< FlowDef > &)
Definition of the traffic during a certain time containing the flows and speeds.
double qPKW
void addFlow(const std::string &detector_id, SUMOTime timestamp, const FlowDef &fd)
double isLKW
double qLKW
const std::vector< FlowDef > & getFlowDefs(const std::string &id) const
void mesoJoin(const std::string &nid, const std::vector< std::string > &oldids)
long long int SUMOTime
Definition: TraCIDefs.h:51
RODFDetectorFlows(SUMOTime startTime, SUMOTime endTime, SUMOTime stepOffset)