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 /****************************************************************************/
10 /// @file    RODFDetectorFlow.cpp
11 /// @author  Daniel Krajzewicz
12 /// @author  Eric Nicolay
13 /// @author  Michael Behrisch
14 /// @author  Melanie Knocke
15 /// @date    Thu, 16.03.2006
16 /// @version $Id$
17 ///
18 // Storage for flows within the DFROUTER
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #include <iostream>
28 #include <cassert>
29 #include "RODFDetectorFlow.h"
30 
31 
32 // ===========================================================================
33 // method definitions
34 // ===========================================================================
RODFDetectorFlows(SUMOTime startTime,SUMOTime endTime,SUMOTime stepOffset)35 RODFDetectorFlows::RODFDetectorFlows(SUMOTime startTime, SUMOTime endTime,
36                                      SUMOTime stepOffset)
37     : myBeginTime(startTime), myEndTime(endTime), myStepOffset(stepOffset),
38       myMaxDetectorFlow(-1) {}
39 
40 
~RODFDetectorFlows()41 RODFDetectorFlows::~RODFDetectorFlows() {}
42 
43 
44 void
addFlow(const std::string & id,SUMOTime t,const FlowDef & fd)45 RODFDetectorFlows::addFlow(const std::string& id, SUMOTime t, const FlowDef& fd) {
46     if (myFastAccessFlows.find(id) == myFastAccessFlows.end()) {
47         int noItems = (int)((myEndTime - myBeginTime) / myStepOffset);
48         myFastAccessFlows[id] = std::vector<FlowDef>(noItems);
49         std::vector<FlowDef>& cflows = myFastAccessFlows[id];
50         // initialise
51         for (std::vector<FlowDef>::iterator i = cflows.begin(); i < cflows.end(); ++i) {
52             (*i).qPKW = 0;
53             (*i).qLKW = 0;
54             (*i).vPKW = 0;
55             (*i).vLKW = 0;
56             (*i).fLKW = 0;
57             (*i).isLKW = 0;
58             (*i).firstSet = true;
59         }
60     }
61     const int index = (int)((t - myBeginTime) / myStepOffset);
62     assert(index < (int) myFastAccessFlows[id].size());
63     FlowDef& ofd = myFastAccessFlows[id][index];
64     if (ofd.firstSet) {
65         ofd = fd;
66         ofd.firstSet = false;
67     } else {
68         ofd.qLKW = ofd.qLKW + fd.qLKW;
69         ofd.qPKW = ofd.qPKW + fd.qPKW;
70         ofd.vLKW = ofd.vLKW + fd.vLKW; //!!! mean value?
71         ofd.vPKW = ofd.vPKW + fd.vPKW; //!!! mean value?
72     }
73     if (ofd.qPKW != 0) {
74         ofd.fLKW = ofd.qLKW / (ofd.qLKW + ofd.qPKW);
75     } else {
76         ofd.fLKW = 1;
77         ofd.isLKW = 1;
78     }
79 }
80 
81 
82 
83 
84 void
setFlows(const std::string & detector_id,std::vector<FlowDef> & flows)85 RODFDetectorFlows::setFlows(const std::string& detector_id,
86                             std::vector<FlowDef>& flows) {
87     for (std::vector<FlowDef>::iterator i = flows.begin(); i < flows.end(); ++i) {
88         FlowDef& ofd = *i;
89         if (ofd.qLKW != 0 && ofd.qPKW != 0) {
90             ofd.fLKW = ofd.qLKW / ofd.qPKW;
91         } else {
92             ofd.fLKW = 0;
93         }
94     }
95     myFastAccessFlows[detector_id] = flows;
96 }
97 
98 
99 void
removeFlow(const std::string & detector_id)100 RODFDetectorFlows::removeFlow(const std::string& detector_id) {
101     myFastAccessFlows.erase(detector_id);
102 }
103 
104 
105 bool
knows(const std::string & det_id) const106 RODFDetectorFlows::knows(const std::string& det_id) const {
107     return myFastAccessFlows.find(det_id) != myFastAccessFlows.end();
108 }
109 
110 
111 const std::vector<FlowDef>&
getFlowDefs(const std::string & id) const112 RODFDetectorFlows::getFlowDefs(const std::string& id) const {
113     assert(myFastAccessFlows.find(id) != myFastAccessFlows.end());
114     assert(myFastAccessFlows.find(id)->second.size() != 0);
115     return myFastAccessFlows.find(id)->second;
116 }
117 
118 
119 double
getFlowSumSecure(const std::string & id) const120 RODFDetectorFlows::getFlowSumSecure(const std::string& id) const {
121     double ret = 0;
122     if (knows(id)) {
123         const std::vector<FlowDef>& flows = getFlowDefs(id);
124         for (std::vector<FlowDef>::const_iterator i = flows.begin(); i != flows.end(); ++i) {
125             ret += (*i).qPKW;
126             ret += (*i).qLKW;
127         }
128     }
129     return ret;
130 }
131 
132 
133 double
getMaxDetectorFlow() const134 RODFDetectorFlows::getMaxDetectorFlow() const {
135     if (myMaxDetectorFlow < 0) {
136         double max = 0;
137         std::map<std::string, std::vector<FlowDef> >::const_iterator j;
138         for (j = myFastAccessFlows.begin(); j != myFastAccessFlows.end(); ++j) {
139             double curr = 0;
140             const std::vector<FlowDef>& flows = (*j).second;
141             for (std::vector<FlowDef>::const_iterator i = flows.begin(); i != flows.end(); ++i) {
142                 curr += (*i).qPKW;
143                 curr += (*i).qLKW;
144             }
145             if (max < curr) {
146                 max = curr;
147             }
148         }
149         myMaxDetectorFlow = max;
150     }
151     return myMaxDetectorFlow;
152 }
153 
154 
155 void
mesoJoin(const std::string & nid,const std::vector<std::string> & oldids)156 RODFDetectorFlows::mesoJoin(const std::string& nid,
157                             const std::vector<std::string>& oldids) {
158     for (std::vector<std::string>::const_iterator i = oldids.begin(); i != oldids.end(); ++i) {
159         if (!knows(*i)) {
160             continue;
161         }
162         std::vector<FlowDef>& flows = myFastAccessFlows[*i];
163         int index = 0;
164         for (SUMOTime t = myBeginTime; t != myEndTime; t += myStepOffset) {
165             addFlow(nid, t, flows[index++]); // !!!
166         }
167         myFastAccessFlows.erase(*i);
168     }
169 }
170 
171 
172 void
printAbsolute() const173 RODFDetectorFlows::printAbsolute() const {
174     for (std::map<std::string, std::vector<FlowDef> >::const_iterator i = myFastAccessFlows.begin(); i != myFastAccessFlows.end(); ++i) {
175         std::cout << (*i).first << ":";
176         const std::vector<FlowDef>& flows = (*i).second;
177         double qPKW = 0;
178         double qLKW = 0;
179         for (std::vector<FlowDef>::const_iterator j = flows.begin(); j != flows.end(); ++j) {
180             qPKW += (*j).qPKW;
181             qLKW += (*j).qLKW;
182         }
183         std::cout << qPKW << "/" << qLKW << std::endl;
184     }
185 }
186 
187 /****************************************************************************/
188 
189