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.dlr.de/
13 // Copyright (C) 2006-2017 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 
39 // ===========================================================================
40 // method definitions
41 // ===========================================================================
43  SUMOTime stepOffset)
44  : myBeginTime(startTime), myEndTime(endTime), myStepOffset(stepOffset),
45  myMaxDetectorFlow(-1) {}
46 
47 
49 
50 
51 void
52 RODFDetectorFlows::addFlow(const std::string& id, SUMOTime t, const FlowDef& fd) {
53  if (myFastAccessFlows.find(id) == myFastAccessFlows.end()) {
54  int noItems = (int)((myEndTime - myBeginTime) / myStepOffset);
55  myFastAccessFlows[id] = std::vector<FlowDef>(noItems);
56  std::vector<FlowDef>& cflows = myFastAccessFlows[id];
57  // initialise
58  for (std::vector<FlowDef>::iterator i = cflows.begin(); i < cflows.end(); ++i) {
59  (*i).qPKW = 0;
60  (*i).qLKW = 0;
61  (*i).vPKW = 0;
62  (*i).vLKW = 0;
63  (*i).fLKW = 0;
64  (*i).isLKW = 0;
65  (*i).firstSet = true;
66  }
67  }
68  const int index = (int)((t - myBeginTime) / myStepOffset);
69  assert(index < (int) myFastAccessFlows[id].size());
70  FlowDef& ofd = myFastAccessFlows[id][index];
71  if (ofd.firstSet) {
72  ofd = fd;
73  ofd.firstSet = false;
74  } else {
75  ofd.qLKW = ofd.qLKW + fd.qLKW;
76  ofd.qPKW = ofd.qPKW + fd.qPKW;
77  ofd.vLKW = ofd.vLKW + fd.vLKW;
78  ofd.vPKW = ofd.vPKW + fd.vPKW;
79  }
80  if (ofd.qPKW != 0) {
81  ofd.fLKW = ofd.qLKW / (ofd.qLKW + ofd.qPKW);
82  } else {
83  ofd.fLKW = 1;
84  ofd.isLKW = 1;
85  }
86 }
87 
88 
89 
90 
91 void
92 RODFDetectorFlows::setFlows(const std::string& detector_id,
93  std::vector<FlowDef>& flows) {
94  for (std::vector<FlowDef>::iterator i = flows.begin(); i < flows.end(); ++i) {
95  FlowDef& ofd = *i;
96  if (ofd.qLKW != 0 && ofd.qPKW != 0) {
97  ofd.fLKW = ofd.qLKW / ofd.qPKW;
98  } else {
99  ofd.fLKW = 0;
100  }
101  }
102  myFastAccessFlows[detector_id] = flows;
103 }
104 
105 
106 void
107 RODFDetectorFlows::removeFlow(const std::string& detector_id) {
108  myFastAccessFlows.erase(detector_id);
109 }
110 
111 
112 bool
113 RODFDetectorFlows::knows(const std::string& det_id) const {
114  return myFastAccessFlows.find(det_id) != myFastAccessFlows.end();
115 }
116 
117 
118 const std::vector<FlowDef>&
119 RODFDetectorFlows::getFlowDefs(const std::string& id) const {
120  assert(myFastAccessFlows.find(id) != myFastAccessFlows.end());
121  assert(myFastAccessFlows.find(id)->second.size() != 0);
122  return myFastAccessFlows.find(id)->second;
123 }
124 
125 
126 double
127 RODFDetectorFlows::getFlowSumSecure(const std::string& id) const {
128  double ret = 0;
129  if (knows(id)) {
130  const std::vector<FlowDef>& flows = getFlowDefs(id);
131  for (std::vector<FlowDef>::const_iterator i = flows.begin(); i != flows.end(); ++i) {
132  ret += (*i).qPKW;
133  ret += (*i).qLKW;
134  }
135  }
136  return ret;
137 }
138 
139 
140 double
142  if (myMaxDetectorFlow < 0) {
143  double max = 0;
144  std::map<std::string, std::vector<FlowDef> >::const_iterator j;
145  for (j = myFastAccessFlows.begin(); j != myFastAccessFlows.end(); ++j) {
146  double curr = 0;
147  const std::vector<FlowDef>& flows = (*j).second;
148  for (std::vector<FlowDef>::const_iterator i = flows.begin(); i != flows.end(); ++i) {
149  curr += (*i).qPKW;
150  curr += (*i).qLKW;
151  }
152  if (max < curr) {
153  max = curr;
154  }
155  }
157  }
158  return myMaxDetectorFlow;
159 }
160 
161 
162 void
163 RODFDetectorFlows::mesoJoin(const std::string& nid,
164  const std::vector<std::string>& oldids) {
165  for (std::vector<std::string>::const_iterator i = oldids.begin(); i != oldids.end(); ++i) {
166  if (!knows(*i)) {
167  continue;
168  }
169  std::vector<FlowDef>& flows = myFastAccessFlows[*i];
170  int index = 0;
171  for (SUMOTime t = myBeginTime; t != myEndTime; t += myStepOffset) {
172  addFlow(nid, t, flows[index++]); // !!!
173  }
174  myFastAccessFlows.erase(*i);
175  }
176 }
177 
178 
179 void
181  for (std::map<std::string, std::vector<FlowDef> >::const_iterator i = myFastAccessFlows.begin(); i != myFastAccessFlows.end(); ++i) {
182  std::cout << (*i).first << ":";
183  const std::vector<FlowDef>& flows = (*i).second;
184  double qPKW = 0;
185  double qLKW = 0;
186  for (std::vector<FlowDef>::const_iterator j = flows.begin(); j != flows.end(); ++j) {
187  qPKW += (*j).qPKW;
188  qLKW += (*j).qLKW;
189  }
190  std::cout << qPKW << "/" << qLKW << std::endl;
191  }
192 }
193 
194 /****************************************************************************/
195 
double getFlowSumSecure(const std::string &id) const
double fLKW
std::map< std::string, std::vector< FlowDef > > myFastAccessFlows
void removeFlow(const std::string &detector_id)
bool knows(const std::string &det_id) const
void printAbsolute() const
double vPKW
double getMaxDetectorFlow() const
#define max(a, b)
Definition: polyfonts.c:65
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:52
RODFDetectorFlows(SUMOTime startTime, SUMOTime endTime, SUMOTime stepOffset)