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