Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2010-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 : /****************************************************************************/
14 : /// @file MSSOTLE2Sensors.cpp
15 : /// @author Gianfilippo Slager
16 : /// @author Alessio Bonfietti
17 : /// @author Federico Caselli
18 : /// @date 2010-02-25
19 : ///
20 : // The class for SOTL sensors of "E2" type
21 : /****************************************************************************/
22 :
23 : #include <utils/common/StringUtils.h>
24 : #include <microsim/output/MSDetectorControl.h>
25 : #include <microsim/MSEdge.h>
26 : #include <microsim/MSVehicleType.h>
27 : #include "MSSOTLE2Sensors.h"
28 :
29 : #define INVALID_POSITION std::numeric_limits<double>::max()
30 :
31 192 : MSSOTLE2Sensors::MSSOTLE2Sensors(std::string tlLogicID,
32 192 : const MSTrafficLightLogic::Phases* phases) :
33 384 : MSSOTLSensors(tlLogicID, phases) {
34 192 : }
35 :
36 192 : MSSOTLE2Sensors::~MSSOTLE2Sensors(void) {
37 : //Deleting sensors takes place in the detector control
38 192 : }
39 :
40 0 : void MSSOTLE2Sensors::buildSensors(
41 : MSTrafficLightLogic::LaneVectorVector controlledLanes,
42 : NLDetectorBuilder& nb) {
43 0 : buildSensors(controlledLanes, nb, INPUT_SENSOR_LENGTH);
44 0 : }
45 :
46 96 : void MSSOTLE2Sensors::buildSensors(
47 : MSTrafficLightLogic::LaneVectorVector controlledLanes,
48 : NLDetectorBuilder& nb, double sensorLength) {
49 : //for each lane build an appropriate sensor on it
50 : MSLane* currentLane = nullptr;
51 :
52 : //input and ouput lanes
53 96 : for (MSTrafficLightLogic::LaneVectorVector::const_iterator laneVector =
54 480 : controlledLanes.begin(); laneVector != controlledLanes.end();
55 : laneVector++) {
56 384 : for (MSTrafficLightLogic::LaneVector::const_iterator lane =
57 768 : laneVector->begin(); lane != laneVector->end(); lane++) {
58 384 : currentLane = (*lane);
59 384 : buildSensorForLane(currentLane, nb, sensorLength);
60 : }
61 : }
62 96 : }
63 : /****************************************************************************/
64 : /*
65 : * Count Sensors. Should be refactor to make a new class.
66 : */
67 96 : void MSSOTLE2Sensors::buildCountSensors(
68 : MSTrafficLightLogic::LaneVectorVector controlledLanes,
69 : NLDetectorBuilder& nb) {
70 : //for each lane build an appropriate sensor on it
71 : MSLane* currentLane = nullptr;
72 : //input and ouput lanes
73 96 : for (MSTrafficLightLogic::LaneVectorVector::const_iterator laneVector =
74 480 : controlledLanes.begin(); laneVector != controlledLanes.end();
75 : laneVector++) {
76 384 : for (MSTrafficLightLogic::LaneVector::const_iterator lane =
77 768 : laneVector->begin(); lane != laneVector->end(); lane++) {
78 384 : currentLane = (*lane);
79 384 : buildCountSensorForLane(currentLane, nb);
80 : }
81 : }
82 96 : }
83 :
84 96 : void MSSOTLE2Sensors::buildCountOutSensors(
85 : MSTrafficLightLogic::LaneVectorVector controlledLanes,
86 : NLDetectorBuilder& nb) {
87 : //for each lane build an appropriate sensor on it
88 : MSLane* currentLane = nullptr;
89 :
90 : //input and ouput lanes
91 96 : for (MSTrafficLightLogic::LaneVectorVector::const_iterator laneVector =
92 192 : controlledLanes.begin(); laneVector != controlledLanes.end();
93 : laneVector++) {
94 96 : for (MSTrafficLightLogic::LaneVector::const_iterator lane =
95 480 : laneVector->begin(); lane != laneVector->end(); lane++) {
96 384 : currentLane = (*lane);
97 384 : buildCountSensorForOutLane(currentLane, nb);
98 : }
99 : }
100 96 : }
101 :
102 0 : void MSSOTLE2Sensors::buildOutSensors(
103 : MSTrafficLightLogic::LaneVectorVector controlledLanes,
104 : NLDetectorBuilder& nb) {
105 0 : buildOutSensors(controlledLanes, nb, OUTPUT_SENSOR_LENGTH);
106 0 : }
107 96 : void MSSOTLE2Sensors::buildOutSensors(
108 : MSTrafficLightLogic::LaneVectorVector controlledLanes,
109 : NLDetectorBuilder& nb, double sensorLength) {
110 : //for each lane build an appropriate sensor on it
111 : MSLane* currentLane = nullptr;
112 :
113 : //input and ouput lanes
114 96 : for (MSTrafficLightLogic::LaneVectorVector::const_iterator laneVector =
115 192 : controlledLanes.begin(); laneVector != controlledLanes.end();
116 : laneVector++) {
117 96 : for (MSTrafficLightLogic::LaneVector::const_iterator lane =
118 480 : laneVector->begin(); lane != laneVector->end(); lane++) {
119 384 : currentLane = (*lane);
120 384 : buildSensorForOutLane(currentLane, nb, sensorLength);
121 : }
122 : }
123 96 : }
124 :
125 384 : void MSSOTLE2Sensors::buildCountSensorForLane(MSLane* lane, NLDetectorBuilder& nb) {
126 : double sensorPos;
127 : double lensorLength;
128 : MSE2Collector* newSensor = nullptr;
129 : //Check not to have more than a sensor for lane
130 384 : if (m_sensorMap.find(lane->getID()) == m_sensorMap.end()) {
131 :
132 : //Check and set zero if the lane is not long enough for the specified sensor start
133 384 : sensorPos = COUNT_SENSOR_START <= lane->getLength() ? COUNT_SENSOR_START : 0;
134 :
135 : //Original:
136 : double sensorLength = INPUT_COUNT_SENSOR_LENGTH;
137 :
138 : //Check and trim if the lane is not long enough for the specified sensor length
139 384 : lensorLength = sensorLength <= (lane->getLength() - sensorPos) ? sensorLength : (lane->getLength() - sensorPos);
140 :
141 : //TODO check this lengths
142 : // DBG(
143 : // std::ostringstream phero_str;
144 : // phero_str << " lane " << lane->getID() << " sensorPos= " << sensorPos
145 : // << " ,SENSOR_START " << SENSOR_START << "; lane->getLength = "
146 : // << lane->getLength() << " ,lensorLength= " << lensorLength
147 : // << " ,SENSOR_LENGTH= " << INPUT_SENSOR_LENGTH;
148 : // WRITE_MESSAGE(
149 : // "MSSOTLE2Sensors::buildSensorForLane::" + phero_str.str());
150 : // )
151 :
152 : //Create sensor for lane and insert it into the map<MSLane*, MSE2Collector*>
153 1152 : newSensor = nb.createE2Detector(
154 384 : "SOTL_E2_lane:" + lane->getID() + "_tl:" + tlLogicID,
155 : DU_TL_CONTROL, lane,
156 384 : (lane->getLength() - sensorPos - lensorLength), INVALID_POSITION, lensorLength,
157 : HALTING_TIME_THRS, HALTING_SPEED_THRS, DIST_THRS, "", "", "", (int)PersonMode::NONE, true);
158 :
159 384 : MSNet::getInstance()->getDetectorControl().add(
160 : SUMO_TAG_LANE_AREA_DETECTOR, newSensor);
161 :
162 384 : m_sensorMap.insert(MSLaneID_MSE2Collector(lane->getID(), newSensor));
163 384 : m_maxSpeedMap.insert(MSLaneID_MaxSpeed(lane->getID(), lane->getSpeedLimit()));
164 : }
165 384 : }
166 :
167 384 : void MSSOTLE2Sensors::buildCountSensorForOutLane(MSLane* lane, NLDetectorBuilder& nb) {
168 : double sensorPos;
169 : double lensorLength;
170 : MSE2Collector* newSensor = nullptr;
171 : //Check not to have more than a sensor for lane
172 384 : if (m_sensorMap.find(lane->getID()) == m_sensorMap.end()) {
173 :
174 : //Original:
175 : double sensorLength = OUTPUT_COUNT_SENSOR_LENGTH;
176 : //Check and set zero if the lane is not long enough for the specified sensor start
177 384 : sensorPos = (lane->getLength() - sensorLength)
178 : - (SENSOR_START <= lane->getLength() ? SENSOR_START : 0);
179 :
180 : //Check and trim if the lane is not long enough for the specified sensor length
181 : lensorLength =
182 384 : sensorLength <= (lane->getLength() - sensorPos) ?
183 : sensorLength : (lane->getLength() - sensorPos);
184 :
185 : //TODO check this lengths
186 : // DBG(
187 : // std::ostringstream phero_str;
188 : // phero_str << " lane " << lane->getID() << " sensorPos= " << sensorPos
189 : // << " ,SENSOR_START " << SENSOR_START << "; lane->getLength = "
190 : // << lane->getLength() << " ,lensorLength= " << lensorLength
191 : // << " ,SENSOR_LENGTH= " << INPUT_SENSOR_LENGTH;
192 : // WRITE_MESSAGE(
193 : // "MSSOTLE2Sensors::buildSensorForLane::" + phero_str.str());
194 : // )
195 :
196 : //Create sensor for lane and insert it into the map<MSLane*, MSE2Collector*>
197 1152 : newSensor = nb.createE2Detector(
198 384 : "SOTL_E2_lane:" + lane->getID() + "_tl:" + tlLogicID,
199 : DU_TL_CONTROL, lane,
200 384 : (lane->getLength() - sensorPos - lensorLength), INVALID_POSITION, lensorLength,
201 : HALTING_TIME_THRS, HALTING_SPEED_THRS, DIST_THRS, "", "", "", (int)PersonMode::NONE, true);
202 :
203 384 : MSNet::getInstance()->getDetectorControl().add(
204 : SUMO_TAG_LANE_AREA_DETECTOR, newSensor);
205 :
206 384 : m_sensorMap.insert(MSLaneID_MSE2Collector(lane->getID(), newSensor));
207 384 : m_maxSpeedMap.insert(MSLaneID_MaxSpeed(lane->getID(), lane->getSpeedLimit()));
208 : }
209 384 : }
210 :
211 0 : int MSSOTLE2Sensors::getPassedVeh(std::string laneId, bool /* out */) {
212 : MSLaneID_MSE2CollectorMap::const_iterator sensorsIterator;
213 : sensorsIterator = m_sensorMap.find(laneId);
214 0 : if (sensorsIterator == m_sensorMap.end()) {
215 : assert(0);
216 : return 0;
217 : } else {
218 : int additional = 0;
219 0 : if (m_continueSensorOnLanes.find(laneId) != m_continueSensorOnLanes.end())
220 0 : for (std::vector<std::string>::iterator it = m_continueSensorOnLanes[laneId].begin(); it != m_continueSensorOnLanes[laneId].end(); ++ it) {
221 0 : int tmp = 0;
222 0 : if (getVelueFromSensor(*it, &MSE2Collector::getPassedVeh, tmp)) {
223 0 : additional += tmp;
224 : }
225 : }
226 0 : return sensorsIterator->second->getPassedVeh() + additional;
227 : }
228 : }
229 :
230 : void
231 0 : MSSOTLE2Sensors::subtractPassedVeh(std::string laneId, int passed) {
232 : //TODO Ivan: ciclare tutti i sensori di count e resettare passedVeh
233 : MSLaneID_MSE2CollectorMap::const_iterator sensorsIterator;
234 :
235 : sensorsIterator = m_sensorMap.find(laneId);
236 0 : if (sensorsIterator != m_sensorMap.end()) {
237 0 : sensorsIterator->second->subtractPassedVeh(passed);
238 : }
239 0 : }
240 :
241 : /*******************************************************************************/
242 :
243 0 : void MSSOTLE2Sensors::buildSensorForLane(MSLane* lane, NLDetectorBuilder& nb) {
244 0 : buildSensorForLane(lane, nb, INPUT_SENSOR_LENGTH);
245 0 : }
246 :
247 :
248 :
249 384 : void MSSOTLE2Sensors::buildSensorForLane(MSLane* lane, NLDetectorBuilder& nb, double sensorLength) {
250 : const MSEdge* edge = &lane->getEdge();
251 384 : if (edge->isInternal() || edge->isWalkingArea() || edge->isCrossing()) {
252 : return;
253 : }
254 : double sensorPos;
255 : double lensorLength;
256 : MSE2Collector* newSensor = nullptr;
257 : //Check not to have more than a sensor for lane
258 384 : if (m_sensorMap.find(lane->getID()) == m_sensorMap.end()) {
259 :
260 : //Check and set zero if the lane is not long enough for the specified sensor start
261 : sensorPos = SENSOR_START <= lane->getLength() ? SENSOR_START : 0;
262 : //Check and trim if the lane is not long enough for the specified sensor length
263 384 : lensorLength = sensorLength <= (lane->getLength() - sensorPos) ? sensorLength : (lane->getLength() - sensorPos);
264 : //TODO check this lengths
265 : // DBG(
266 : // std::ostringstream phero_str;
267 : // phero_str << " lane " << lane->getID() << " sensorPos= " << sensorPos
268 : // << " ,SENSOR_START " << SENSOR_START << "; lane->getLength = "
269 : // << lane->getLength() << " ,lensorLength= " << lensorLength
270 : // << " ,SENSOR_LENGTH= " << INPUT_SENSOR_LENGTH;
271 : // WRITE_MESSAGE(
272 : // "MSSOTLE2Sensors::buildSensorForLane::" + phero_str.str());
273 : // )
274 :
275 : //Create sensor for lane and insert it into the map<MSLane*, MSE2Collector*>
276 1152 : newSensor = nb.createE2Detector(
277 768 : "SOTL_E2_lane:" + lane->getID() + "_tl:" + tlLogicID,
278 : DU_TL_CONTROL, lane,
279 : (lane->getLength() - sensorPos - lensorLength), INVALID_POSITION, lensorLength,
280 : HALTING_TIME_THRS, HALTING_SPEED_THRS, DIST_THRS, "", "", "", (int)PersonMode::NONE, true);
281 :
282 384 : MSNet::getInstance()->getDetectorControl().add(SUMO_TAG_LANE_AREA_DETECTOR, newSensor);
283 :
284 384 : m_sensorMap.insert(MSLaneID_MSE2Collector(lane->getID(), newSensor));
285 384 : m_maxSpeedMap.insert(MSLaneID_MaxSpeed(lane->getID(), lane->getSpeedLimit()));
286 : //Avoid creating really small sensors. Only add a further sensors if the one created is less than 90% the requested length
287 384 : if (lensorLength < sensorLength * 0.9) {
288 0 : std::ostringstream oss;
289 0 : oss << "Sensor on lane " << lane->getID() << " is long " << lensorLength << ", while it should be " << sensorLength << ". Continuing it on the other lanes if possible";
290 0 : WRITE_MESSAGE(oss.str())
291 0 : for (const MSLane::IncomingLaneInfo& it : lane->getIncomingLanes()) {
292 0 : const MSEdge& inEdge = it.lane->getEdge();
293 : if (!inEdge.isInternal() && !inEdge.isWalkingArea() && !inEdge.isCrossing()) {
294 0 : buildContinueSensior(lane, nb, sensorLength, it.lane, lensorLength);
295 : }
296 : }
297 0 : }
298 : }
299 : }
300 :
301 0 : void MSSOTLE2Sensors::buildContinueSensior(MSLane* lane, NLDetectorBuilder& nb, double sensorLength, MSLane* continueOnLane, double usedLength) {
302 0 : double availableLength = sensorLength - usedLength;
303 0 : if (m_sensorMap.find(continueOnLane->getID()) == m_sensorMap.end()) {
304 0 : double length = availableLength <= continueOnLane->getLength() ? availableLength : continueOnLane->getLength();
305 0 : MSE2Collector* newSensor = nb.createE2Detector(
306 0 : "SOTL_E2_lane:" + continueOnLane->getID() + "_tl:" + tlLogicID,
307 : DU_TL_CONTROL, continueOnLane,
308 : (continueOnLane->getLength() - length), INVALID_POSITION, length,
309 : HALTING_TIME_THRS, HALTING_SPEED_THRS, DIST_THRS, "", "", "", (int)PersonMode::NONE, true);
310 0 : MSNet::getInstance()->getDetectorControl().add(SUMO_TAG_LANE_AREA_DETECTOR, newSensor);
311 0 : m_sensorMap.insert(MSLaneID_MSE2Collector(continueOnLane->getID(), newSensor));
312 0 : m_continueSensorOnLanes[lane->getID()].push_back(continueOnLane->getID());
313 0 : std::ostringstream oss;
314 0 : oss << "Continue sensor on lane " << continueOnLane->getID() << ". Current length " << (length + usedLength);
315 0 : WRITE_MESSAGE(oss.str())
316 : //Continue other line if needed.
317 0 : if (length + usedLength < sensorLength * 0.9) {
318 0 : for (std::vector<MSLane::IncomingLaneInfo>::const_iterator it = continueOnLane->getIncomingLanes().begin(); it != continueOnLane->getIncomingLanes().end(); ++it) {
319 0 : const MSEdge* edge = &it->lane->getEdge();
320 : if (!edge->isInternal() && !edge->isWalkingArea() && !edge->isCrossing()) {
321 0 : buildContinueSensior(lane, nb, sensorLength, it->lane, length + usedLength);
322 : }
323 : }
324 : }
325 0 : }
326 0 : }
327 :
328 0 : void MSSOTLE2Sensors::buildSensorForOutLane(MSLane* lane,
329 : NLDetectorBuilder& nb) {
330 0 : buildSensorForOutLane(lane, nb, OUTPUT_SENSOR_LENGTH);
331 0 : }
332 :
333 384 : void MSSOTLE2Sensors::buildSensorForOutLane(MSLane* lane,
334 : NLDetectorBuilder& nb, double sensorLength) {
335 : double sensorPos;
336 : double lensorLength;
337 : MSE2Collector* newSensor = nullptr;
338 : //Check not to have more than a sensor for lane
339 384 : if (m_sensorMap.find(lane->getID()) == m_sensorMap.end()) {
340 :
341 : //Original:
342 : //double sensorLength = OUTPUT_SENSOR_LENGTH;
343 : //Check and set zero if the lane is not long enough for the specified sensor start
344 384 : sensorPos = (lane->getLength() - sensorLength)
345 : - (SENSOR_START <= lane->getLength() ? SENSOR_START : 0);
346 :
347 : //Check and trim if the lane is not long enough for the specified sensor length
348 : lensorLength =
349 384 : sensorLength <= (lane->getLength() - sensorPos) ?
350 : sensorLength : (lane->getLength() - sensorPos);
351 :
352 : //TODO check this lengths
353 : // DBG(
354 : // std::ostringstream phero_str;
355 : // phero_str << " lane " << lane->getID() << " sensorPos= " << sensorPos
356 : // << " ,SENSOR_START " << SENSOR_START << "; lane->getLength = "
357 : // << lane->getLength() << " ,lensorLength= " << lensorLength
358 : // << " ,SENSOR_LENGTH= " << INPUT_SENSOR_LENGTH;
359 : // WRITE_MESSAGE(
360 : // "MSSOTLE2Sensors::buildSensorForLane::" + phero_str.str());
361 : // )
362 :
363 : //Create sensor for lane and insert it into the map<MSLane*, MSE2Collector*>
364 1152 : newSensor = nb.createE2Detector(
365 384 : "SOTL_E2_lane:" + lane->getID() + "_tl:" + tlLogicID,
366 : DU_TL_CONTROL, lane,
367 384 : (lane->getLength() - sensorPos - lensorLength), INVALID_POSITION, lensorLength,
368 : HALTING_TIME_THRS, HALTING_SPEED_THRS, DIST_THRS, "", "", "", (int)PersonMode::NONE, true);
369 :
370 384 : MSNet::getInstance()->getDetectorControl().add(
371 : SUMO_TAG_LANE_AREA_DETECTOR, newSensor);
372 :
373 384 : m_sensorMap.insert(MSLaneID_MSE2Collector(lane->getID(), newSensor));
374 384 : m_maxSpeedMap.insert(MSLaneID_MaxSpeed(lane->getID(), lane->getSpeedLimit()));
375 : }
376 384 : }
377 :
378 3628 : int MSSOTLE2Sensors::countVehicles(MSLane* lane) {
379 7256 : return countVehicles(lane->getID());
380 : }
381 :
382 : /*
383 : * Estimate queue length according to the distance of the last vehicles
384 : */
385 :
386 0 : double MSSOTLE2Sensors::getEstimateQueueLength(std::string laneId) {
387 : MSLaneID_MSE2CollectorMap::const_iterator sensorsIterator = m_sensorMap.find(laneId);
388 0 : if (sensorsIterator == m_sensorMap.end()) {
389 : assert(0);
390 : return 0;
391 : } else {
392 0 : double estQL = sensorsIterator->second->getEstimateQueueLength();
393 0 : if (estQL == -1) {
394 : return 0;
395 : } else
396 : // DBG(
397 : // std::ostringstream str;
398 : // str << "MSSOTLE2Sensors::getEstimateQueueLength lane " << sensorsIterator->second->getLane()->getID()
399 : // << " laneLength " << sensorsIterator->second->getLane()->getLength() << " estimateQueueLength " << estQL;
400 : // WRITE_MESSAGE(str.str());
401 : // )
402 : {
403 : return estQL;
404 : }
405 : }
406 : }
407 :
408 : /*
409 : * Estimate queue length according to the distance of the last vehicles that exceed a threshold
410 : */
411 :
412 0 : int MSSOTLE2Sensors::estimateVehicles(std::string laneId) {
413 : MSLaneID_MSE2CollectorMap::const_iterator sensorsIterator = m_sensorMap.find(laneId);
414 0 : if (sensorsIterator == m_sensorMap.end()) {
415 : assert(0);
416 : return 0;
417 : }
418 : int additional = 0;
419 0 : if (m_continueSensorOnLanes.find(laneId) != m_continueSensorOnLanes.end())
420 0 : for (std::vector<std::string>::iterator it = m_continueSensorOnLanes[laneId].begin(); it != m_continueSensorOnLanes[laneId].end(); ++ it) {
421 0 : if (m_sensorMap.find(*it) != m_sensorMap.end()) {
422 0 : additional += m_sensorMap[*it]->getEstimatedCurrentVehicleNumber(speedThresholdParam);
423 : }
424 : }
425 0 : return sensorsIterator->second->getEstimatedCurrentVehicleNumber(speedThresholdParam) + additional;
426 : }
427 :
428 354980 : int MSSOTLE2Sensors::countVehicles(std::string laneId) {
429 : MSLaneID_MSE2CollectorMap::const_iterator sensorsIterator = m_sensorMap.find(laneId);
430 354980 : if (sensorsIterator == m_sensorMap.end()) {
431 : assert(0);
432 : return 0;
433 : }
434 : int additional = 0;
435 354980 : if (m_continueSensorOnLanes.find(laneId) != m_continueSensorOnLanes.end()) {
436 0 : for (std::vector<std::string>::iterator it = m_continueSensorOnLanes[laneId].begin(); it != m_continueSensorOnLanes[laneId].end(); ++ it) {
437 0 : if (m_sensorMap.find(*it) != m_sensorMap.end()) {
438 0 : additional += count(m_sensorMap[*it]);
439 : }
440 : }
441 : }
442 354980 : return count(sensorsIterator->second) + additional;
443 : }
444 :
445 127808 : double MSSOTLE2Sensors::getMaxSpeed(std::string laneId) {
446 : MSLaneID_MaxSpeedMap::const_iterator sensorsIteratorIn = m_maxSpeedMap.find(laneId);
447 127808 : if (sensorsIteratorIn == m_maxSpeedMap.end()) {
448 : assert(0);
449 0 : WRITE_ERROR("MSSOTLE2Sensors::meanVehiclesSpeed:: No lane found " + laneId);
450 0 : return 0;
451 : }
452 127808 : return sensorsIteratorIn->second;
453 : }
454 :
455 0 : double MSSOTLE2Sensors::meanVehiclesSpeed(MSLane* lane) {
456 0 : return meanVehiclesSpeed(lane->getID());
457 : }
458 :
459 127808 : double MSSOTLE2Sensors::meanVehiclesSpeed(std::string laneId) {
460 : MSLaneID_MSE2CollectorMap::const_iterator sensorsIteratorOut = m_sensorMap.find(laneId);
461 127808 : if (sensorsIteratorOut == m_sensorMap.end()) {
462 : assert(0);
463 0 : WRITE_ERROR("MSSOTLE2Sensors::meanVehiclesSpeed:: No lane found " + laneId);
464 0 : return 0;
465 : }
466 : double meanSpeedAcc = 0;
467 : int totalCarNumer = 0;
468 127808 : if (m_continueSensorOnLanes.find(laneId) != m_continueSensorOnLanes.end())
469 0 : for (std::vector<std::string>::iterator it = m_continueSensorOnLanes[laneId].begin(); it != m_continueSensorOnLanes[laneId].end(); ++ it) {
470 0 : int number = 0;
471 0 : double mean = -1;
472 0 : if (!getVelueFromSensor(*it, &MSE2Collector::getCurrentVehicleNumber, number)) {
473 0 : continue;
474 : }
475 0 : totalCarNumer += number;
476 0 : getVelueFromSensor(*it, &MSE2Collector::getCurrentMeanSpeed, mean);
477 0 : meanSpeedAcc += mean * (double) number;
478 : }
479 127808 : int number = sensorsIteratorOut->second->getCurrentVehicleNumber();
480 127808 : totalCarNumer += number;
481 127808 : double mean = sensorsIteratorOut->second->getCurrentMeanSpeed();
482 127808 : meanSpeedAcc += mean * (double) number;
483 127808 : return totalCarNumer == 0 ? -1 : meanSpeedAcc / (double) totalCarNumer;
484 : }
485 :
486 0 : std::string trim(std::string& str) {
487 0 : int first = (int)str.find_first_not_of(' ');
488 0 : int last = (int)str.find_last_not_of(' ');
489 0 : return str.substr(first, (last - first + 1));
490 : }
491 :
492 0 : std::vector<std::string>& split(const std::string& s, char delim, std::vector<std::string>& elems) {
493 0 : std::stringstream ss(s);
494 : std::string item;
495 0 : while (std::getline(ss, item, delim)) {
496 0 : if (!item.empty()) {
497 0 : elems.push_back(item);
498 : }
499 : }
500 0 : return elems;
501 0 : }
502 :
503 0 : void MSSOTLE2Sensors::setVehicleWeigths(const std::string& weightString) {
504 : std::vector<std::string> types;
505 0 : split(weightString, ';', types);
506 0 : std::ostringstream logstr;
507 0 : logstr << "[MSSOTLE2Sensors::setVehicleWeigths] ";
508 0 : for (std::vector<std::string>::iterator typesIt = types.begin(); typesIt != types.end(); ++typesIt) {
509 : std::vector<std::string> typeWeight;
510 0 : split(*typesIt, '=', typeWeight);
511 0 : if (typeWeight.size() == 2) {
512 0 : std::string type = trim(typeWeight[0]);
513 0 : int value = StringUtils::toInt(typeWeight[1]);
514 0 : logstr << type << "=" << value << " ";
515 0 : m_typeWeightMap[type] = value;
516 : }
517 0 : }
518 0 : WRITE_MESSAGE(logstr.str());
519 0 : }
520 :
521 354980 : int MSSOTLE2Sensors::count(MSE2Collector* sensor) {
522 354980 : int totCars = sensor->getCurrentVehicleNumber();
523 354980 : if (m_typeWeightMap.size() == 0) {
524 : return totCars;
525 : }
526 : int number = 0;
527 0 : const std::vector<MSE2Collector::VehicleInfo*> vehicles = sensor->getCurrentVehicles();
528 0 : std::ostringstream logstr;
529 0 : logstr << "[MSSOTLE2Sensors::count]";
530 0 : for (std::vector<MSE2Collector::VehicleInfo*>::const_iterator vit = vehicles.begin(); vit != vehicles.end(); ++vit) {
531 0 : if ((*vit)->onDetector) {
532 : const std::string vtype = (*vit)->type;
533 0 : if (m_typeWeightMap.find(vtype) != m_typeWeightMap.end()) {
534 0 : number += m_typeWeightMap[vtype];
535 : // DBG(logstr << " Added " << m_typeWeightMap[vtype] << " for vtype " << vtype;)
536 : } else {
537 0 : ++number;
538 : }
539 : }
540 : }
541 : // DBG(if (totCars != number) {
542 : // logstr << ". Real number " << totCars << "; weighted " << number;
543 : // WRITE_MESSAGE(logstr.str());
544 : // })
545 : return number;
546 0 : }
|