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