/
MSInductLoop.cpp
366 lines (324 loc) · 13.4 KB
/
MSInductLoop.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/****************************************************************************/
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
// Copyright (C) 2001-2021 German Aerospace Center (DLR) and others.
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0/
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License 2.0 are satisfied: GNU General Public License, version 2
// or later which is available at
// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
/****************************************************************************/
/// @file MSInductLoop.cpp
/// @author Christian Roessel
/// @author Daniel Krajzewicz
/// @author Jakob Erdmann
/// @author Sascha Krieg
/// @author Michael Behrisch
/// @author Laura Bieker
/// @date 2004-11-23
///
// An unextended detector measuring at a fixed position on a fixed lane.
/****************************************************************************/
#include <config.h>
#include "MSInductLoop.h"
#include <cassert>
#include <numeric>
#include <utility>
#include <utils/common/WrappingCommand.h>
#include <utils/common/ToString.h>
#include <microsim/MSEventControl.h>
#include <microsim/MSLane.h>
#include <microsim/MSEdge.h>
#include <microsim/MSVehicle.h>
#include <microsim/MSNet.h>
#include <microsim/transportables/MSTransportable.h>
#include <microsim/transportables/MSPModel.h>
#include <utils/common/MsgHandler.h>
#include <utils/common/UtilExceptions.h>
#include <utils/common/StringUtils.h>
#include <utils/iodevices/OutputDevice.h>
#define HAS_NOT_LEFT_DETECTOR -1
// ===========================================================================
// method definitions
// ===========================================================================
MSInductLoop::MSInductLoop(const std::string& id, MSLane* const lane,
double positionInMeters,
const std::string& vTypes,
int detectPersons,
const bool needLocking) :
MSMoveReminder(id, lane),
MSDetectorFileOutput(id, vTypes, detectPersons),
myPosition(positionInMeters),
myNeedLock(needLocking || MSGlobals::gNumSimThreads > 1),
myLastLeaveTime(SIMTIME),
myVehicleDataCont(),
myVehiclesOnDet() {
assert(myPosition >= 0 && myPosition <= myLane->getLength());
reset();
}
MSInductLoop::~MSInductLoop() {
}
void
MSInductLoop::reset() {
#ifdef HAVE_FOX
FXConditionalLock lock(myNotificationMutex, myNeedLock);
#endif
myEnteredVehicleNumber = 0;
myLastVehicleDataCont = myVehicleDataCont;
myVehicleDataCont.clear();
}
bool
MSInductLoop::notifyEnter(SUMOTrafficObject& veh, Notification reason, const MSLane* /* enteredLane */) {
// vehicles must be kept if the "inductionloop" wants to detect passeengers
if (!vehicleApplies(veh) && (veh.isPerson() || myDetectPersons <= (int)PersonMode::WALK)) {
return false;
}
if (reason != NOTIFICATION_JUNCTION) { // the junction case is handled in notifyMove
if (veh.getBackPositionOnLane(myLane) >= myPosition) {
return false;
}
if (veh.getPositionOnLane() >= myPosition) {
#ifdef HAVE_FOX
FXConditionalLock lock(myNotificationMutex, myNeedLock);
#endif
myVehiclesOnDet[&veh] = SIMTIME;
myEnteredVehicleNumber++;
}
}
return true;
}
bool
MSInductLoop::notifyMove(SUMOTrafficObject& veh, double oldPos,
double newPos, double newSpeed) {
if (newPos < myPosition) {
// detector not reached yet
return true;
}
if (myDetectPersons > (int)PersonMode::WALK && !veh.isPerson()) {
bool keep = false;
MSBaseVehicle& v = dynamic_cast<MSBaseVehicle&>(veh);
for (MSTransportable* p : v.getPersons()) {
keep = notifyMove(*p, oldPos, newPos, newSpeed);
}
return keep;
}
#ifdef HAVE_FOX
FXConditionalLock lock(myNotificationMutex, myNeedLock);
#endif
const double oldSpeed = veh.getPreviousSpeed();
if (newPos >= myPosition && oldPos < myPosition) {
// entered the detector by move
const double timeBeforeEnter = MSCFModel::passingTime(oldPos, myPosition, newPos, oldSpeed, newSpeed);
myVehiclesOnDet[&veh] = SIMTIME + timeBeforeEnter;
myEnteredVehicleNumber++;
}
double oldBackPos = oldPos - veh.getVehicleType().getLength();
double newBackPos = newPos - veh.getVehicleType().getLength();
if (newBackPos > myPosition) {
// vehicle passed the detector (it may have changed onto this lane somewhere past the detector)
// assert(!MSGlobals::gSemiImplicitEulerUpdate || newSpeed > 0 || myVehiclesOnDet.find(&veh) == myVehiclesOnDet.end());
// assertion is invalid in case of teleportation
if (oldBackPos <= myPosition) {
const std::map<SUMOTrafficObject*, double>::iterator it = myVehiclesOnDet.find(&veh);
if (it != myVehiclesOnDet.end()) {
const double entryTime = it->second;
const double leaveTime = SIMTIME + MSCFModel::passingTime(oldBackPos, myPosition, newBackPos, oldSpeed, newSpeed);
myVehiclesOnDet.erase(it);
assert(entryTime <= leaveTime);
myVehicleDataCont.push_back(VehicleData(veh, entryTime, leaveTime, false));
myLastLeaveTime = leaveTime;
}
} else {
// vehicle is already beyond the detector...
// This can happen even if it is still registered in myVehiclesOnDet, e.g., after teleport.
myVehiclesOnDet.erase(&veh);
}
return false;
}
// vehicle stays on the detector
return true;
}
bool
MSInductLoop::notifyLeave(SUMOTrafficObject& veh, double lastPos, MSMoveReminder::Notification reason, const MSLane* /* enteredLane */) {
if (veh.isPerson() && myDetectPersons != (int)PersonMode::NONE) {
const int lastDir = lastPos < 0 ? MSPModel::BACKWARD : MSPModel::FORWARD;
notifyMovePerson(dynamic_cast<MSTransportable*>(&veh), lastDir, lastPos);
}
if (reason != MSMoveReminder::NOTIFICATION_JUNCTION || (veh.isPerson() && myDetectPersons != (int)PersonMode::NONE)) {
#ifdef HAVE_FOX
FXConditionalLock lock(myNotificationMutex, myNeedLock);
#endif
const std::map<SUMOTrafficObject*, double>::iterator it = myVehiclesOnDet.find(&veh);
if (it != myVehiclesOnDet.end()) {
const double entryTime = it->second;
const double leaveTime = SIMTIME + TS;
myVehiclesOnDet.erase(it);
myVehicleDataCont.push_back(VehicleData(veh, entryTime, leaveTime, true));
myLastLeaveTime = leaveTime;
}
return false;
}
return true;
}
double
MSInductLoop::getSpeed(const int offset) const {
const std::vector<VehicleData>& d = collectVehiclesOnDet(SIMSTEP - offset);
return d.empty() ? -1. : std::accumulate(d.begin(), d.end(), 0.0, speedSum) / (double) d.size();
}
double
MSInductLoop::getVehicleLength(const int offset) const {
const std::vector<VehicleData>& d = collectVehiclesOnDet(SIMSTEP - offset);
return d.empty() ? -1. : std::accumulate(d.begin(), d.end(), 0.0, lengthSum) / (double)d.size();
}
double
MSInductLoop::getOccupancy() const {
const SUMOTime tbeg = SIMSTEP - DELTA_T;
double occupancy = 0;
const double csecond = SIMTIME;
for (const VehicleData& i : collectVehiclesOnDet(tbeg, false, false, true)) {
const double leaveTime = i.leaveTimeM == HAS_NOT_LEFT_DETECTOR ? csecond : MIN2(i.leaveTimeM, csecond);
const double entryTime = MAX2(i.entryTimeM, STEPS2TIME(tbeg));
occupancy += MIN2(leaveTime - entryTime, TS);
}
return occupancy / TS * 100.;
}
double
MSInductLoop::getEnteredNumber(const int offset) const {
return (double)collectVehiclesOnDet(SIMSTEP - offset, true, true).size();
}
std::vector<std::string>
MSInductLoop::getVehicleIDs(const int offset) const {
std::vector<std::string> ret;
for (const VehicleData& i : collectVehiclesOnDet(SIMSTEP - offset, true, true)) {
ret.push_back(i.idM);
}
return ret;
}
double
MSInductLoop::getTimeSinceLastDetection() const {
if (myVehiclesOnDet.size() != 0) {
// detector is occupied
return 0;
}
return SIMTIME - myLastLeaveTime;
}
SUMOTime
MSInductLoop::getLastDetectionTime() const {
if (myVehiclesOnDet.size() != 0) {
return MSNet::getInstance()->getCurrentTimeStep();
}
return TIME2STEPS(myLastLeaveTime);
}
void
MSInductLoop::writeXMLDetectorProlog(OutputDevice& dev) const {
dev.writeXMLHeader("detector", "det_e1_file.xsd");
}
void
MSInductLoop::writeXMLOutput(OutputDevice& dev, SUMOTime startTime, SUMOTime stopTime) {
const double t(STEPS2TIME(stopTime - startTime));
double occupancy = 0.;
double speedSum = 0.;
double lengthSum = 0.;
int contrib = 0;
// to approximate the space mean speed
double inverseSpeedSum = 0.;
for (const VehicleData& vData : myVehicleDataCont) {
const double timeOnDetDuringInterval = vData.leaveTimeM - MAX2(STEPS2TIME(startTime), vData.entryTimeM);
occupancy += MIN2(timeOnDetDuringInterval, t);
if (!vData.leftEarlyM) {
speedSum += vData.speedM;
assert(vData.speedM > 0.);
inverseSpeedSum += 1. / vData.speedM;
lengthSum += vData.lengthM;
contrib++;
}
}
const double flow = (double)contrib / t * 3600.;
for (std::map< SUMOTrafficObject*, double >::const_iterator i = myVehiclesOnDet.begin(); i != myVehiclesOnDet.end(); ++i) {
occupancy += STEPS2TIME(stopTime) - MAX2(STEPS2TIME(startTime), i->second);
}
occupancy *= 100. / t;
const double meanSpeed = contrib != 0 ? speedSum / (double)contrib : -1;
const double harmonicMeanSpeed = contrib != 0 ? (double)contrib / inverseSpeedSum : -1;
const double meanLength = contrib != 0 ? lengthSum / (double)contrib : -1;
dev.openTag(SUMO_TAG_INTERVAL).writeAttr(SUMO_ATTR_BEGIN, STEPS2TIME(startTime)).writeAttr(SUMO_ATTR_END, STEPS2TIME(stopTime));
dev.writeAttr(SUMO_ATTR_ID, StringUtils::escapeXML(getID())).writeAttr("nVehContrib", contrib);
dev.writeAttr("flow", flow).writeAttr("occupancy", occupancy).writeAttr("speed", meanSpeed).writeAttr("harmonicMeanSpeed", harmonicMeanSpeed);
dev.writeAttr("length", meanLength).writeAttr("nVehEntered", myEnteredVehicleNumber).closeTag();
reset();
}
void
MSInductLoop::detectorUpdate(const SUMOTime /* step */) {
if (myDetectPersons == (int)PersonMode::NONE) {
return;
}
if (myLane->hasPedestrians()) {
for (MSTransportable* p : myLane->getEdge().getPersons()) {
if (p->getLane() != myLane) {
continue;
}
notifyMovePerson(p, p->getDirection(), p->getPositionOnLane());
}
}
}
void
MSInductLoop::notifyMovePerson(MSTransportable* p, int dir, double pos) {
if (personApplies(*p, dir)) {
const double newSpeed = p->getSpeed();
const double newPos = (dir == MSPModel::FORWARD
? pos
// position relative to detector
: myPosition - (pos - myPosition));
const double oldPos = newPos - SPEED2DIST(newSpeed);
if (oldPos - p->getVehicleType().getLength() <= myPosition) {
notifyMove(*p, oldPos, newPos, newSpeed);
}
}
}
std::vector<MSInductLoop::VehicleData>
MSInductLoop::collectVehiclesOnDet(SUMOTime tMS, bool includeEarly, bool leaveTime, bool forOccupancy) const {
#ifdef HAVE_FOX
FXConditionalLock lock(myNotificationMutex, myNeedLock);
#endif
const double t = STEPS2TIME(tMS);
std::vector<VehicleData> ret;
for (const VehicleData& i : myVehicleDataCont) {
if (includeEarly || !i.leftEarlyM) {
if (i.entryTimeM >= t || (leaveTime && i.leaveTimeM >= t)) {
ret.push_back(i);
}
}
}
for (const VehicleData& i : myLastVehicleDataCont) {
if (includeEarly || !i.leftEarlyM) {
if (i.entryTimeM >= t || (leaveTime && i.leaveTimeM >= t)) {
ret.push_back(i);
}
}
}
for (const auto& i : myVehiclesOnDet) {
if (i.second >= t || leaveTime || forOccupancy) { // no need to check leave time, they are still on the detector
SUMOTrafficObject* const v = i.first;
VehicleData d(*v, i.second, HAS_NOT_LEFT_DETECTOR, false);
d.speedM = v->getSpeed();
ret.push_back(d);
}
}
return ret;
}
MSInductLoop::VehicleData::VehicleData(const SUMOTrafficObject& v, double entryTimestep,
double leaveTimestep, const bool leftEarly)
: idM(v.getID()), lengthM(v.getVehicleType().getLength()), entryTimeM(entryTimestep), leaveTimeM(leaveTimestep),
speedM(v.getVehicleType().getLength() / MAX2(leaveTimestep - entryTimestep, NUMERICAL_EPS)), typeIDM(v.getVehicleType().getID()),
leftEarlyM(leftEarly) {}
void
MSInductLoop::clearState() {
myEnteredVehicleNumber = 0;
myLastVehicleDataCont.clear();
myVehicleDataCont.clear();
myVehiclesOnDet.clear();
}
/****************************************************************************/