Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2001-2026 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 MSMeanData_Amitran.cpp
15 : /// @author Daniel Krajzewicz
16 : /// @author Michael Behrisch
17 : /// @date Mon, 10.05.2004
18 : ///
19 : // Network state mean data collector for edges/lanes
20 : /****************************************************************************/
21 : #include <config.h>
22 :
23 : #include <microsim/MSEdgeControl.h>
24 : #include <microsim/MSEdge.h>
25 : #include <microsim/MSLane.h>
26 : #include <microsim/MSVehicle.h>
27 : #include <utils/common/SUMOTime.h>
28 : #include <utils/common/ToString.h>
29 : #include <utils/iodevices/OutputDevice.h>
30 : #include "MSMeanData_Amitran.h"
31 : #include <limits>
32 :
33 :
34 : // ===========================================================================
35 : // method definitions
36 : // ===========================================================================
37 : // ---------------------------------------------------------------------------
38 : // MSMeanData_Amitran::MSLaneMeanDataValues - methods
39 : // ---------------------------------------------------------------------------
40 2492 : MSMeanData_Amitran::MSLaneMeanDataValues::MSLaneMeanDataValues(MSLane* const lane,
41 : const double length,
42 : const bool doAdd,
43 2492 : const MSMeanData_Amitran* parent)
44 2492 : : MSMeanData::MeanDataValues(lane, length, doAdd, parent), amount(0) {}
45 :
46 :
47 4984 : MSMeanData_Amitran::MSLaneMeanDataValues::~MSLaneMeanDataValues() {
48 4984 : }
49 :
50 :
51 : void
52 4608 : MSMeanData_Amitran::MSLaneMeanDataValues::reset(bool) {
53 4608 : amount = 0;
54 : typedAmount.clear();
55 : typedSamples.clear();
56 : typedTravelDistance.clear();
57 4608 : resetTime = SIMSTEP;
58 4608 : }
59 :
60 :
61 : void
62 3384 : MSMeanData_Amitran::MSLaneMeanDataValues::addTo(MSMeanData::MeanDataValues& val) const {
63 : MSLaneMeanDataValues& v = (MSLaneMeanDataValues&) val;
64 3384 : v.amount += amount;
65 3384 : v.sampleSeconds += sampleSeconds;
66 3384 : v.travelledDistance += travelledDistance;
67 6043 : for (std::map<const MSVehicleType*, int>::const_iterator it = typedAmount.begin(); it != typedAmount.end(); ++it) {
68 2659 : v.typedAmount[it->first] += it->second;
69 : }
70 6179 : for (std::map<const MSVehicleType*, double>::const_iterator it = typedSamples.begin(); it != typedSamples.end(); ++it) {
71 2795 : v.typedSamples[it->first] += it->second;
72 : }
73 6179 : for (std::map<const MSVehicleType*, double>::const_iterator it = typedTravelDistance.begin(); it != typedTravelDistance.end(); ++it) {
74 2795 : v.typedTravelDistance[it->first] += it->second;
75 : }
76 3384 : }
77 :
78 :
79 : void
80 1779438 : MSMeanData_Amitran::MSLaneMeanDataValues::notifyMoveInternal(const SUMOTrafficObject& veh, const double /* frontOnLane */, const double timeOnLane, const double /*meanSpeedFrontOnLane*/, const double /*meanSpeedVehicleOnLane*/, const double /*travelledDistanceFrontOnLane*/, const double travelledDistanceVehicleOnLane, const double /* meanLengthOnLane */) {
81 1779438 : sampleSeconds += timeOnLane;
82 1779438 : travelledDistance += travelledDistanceVehicleOnLane;
83 1779438 : typedSamples[&veh.getVehicleType()] += timeOnLane;
84 1779438 : typedTravelDistance[&veh.getVehicleType()] += travelledDistanceVehicleOnLane;
85 1779438 : }
86 :
87 :
88 : bool
89 118632 : MSMeanData_Amitran::MSLaneMeanDataValues::notifyEnter(SUMOTrafficObject& veh, MSMoveReminder::Notification reason, const MSLane* /* enteredLane */) {
90 118632 : if (myParent->vehicleApplies(veh)) {
91 118632 : if (getLane() == nullptr || getLane() == static_cast<MSVehicle&>(veh).getLane()) {
92 118632 : if (reason == MSMoveReminder::NOTIFICATION_DEPARTED || reason == MSMoveReminder::NOTIFICATION_JUNCTION) {
93 63264 : ++amount;
94 63264 : typedAmount[&veh.getVehicleType()]++;
95 : }
96 : }
97 118632 : return true;
98 : }
99 : return false;
100 : }
101 :
102 :
103 : bool
104 2448 : MSMeanData_Amitran::MSLaneMeanDataValues::isEmpty() const {
105 2448 : return sampleSeconds == 0 && amount == 0;
106 : }
107 :
108 :
109 : void
110 2756 : MSMeanData_Amitran::MSLaneMeanDataValues::write(OutputDevice& dev, const SumoXMLAttrMask& attributeMask, const SUMOTime /* period */,
111 : const int /* numLanes */, const double /*speedLimit*/, const double defaultTravelTime, const int /* numVehicles */) const {
112 : int averageSpeed;
113 2756 : if (sampleSeconds > 0) {
114 2726 : averageSpeed = int(100 * travelledDistance / sampleSeconds);
115 30 : } else if (defaultTravelTime >= 0.) {
116 0 : averageSpeed = int(100 * myLaneLength / defaultTravelTime);
117 : } else {
118 30 : averageSpeed = -1;
119 : }
120 2756 : dev.writeOptionalAttr(SUMO_ATTR_AMOUNT, amount, attributeMask);
121 2756 : dev.writeOptionalAttr(SUMO_ATTR_AVERAGESPEED, averageSpeed, attributeMask);
122 :
123 2756 : if (myParent->isTyped()) {
124 1152 : for (std::map<const MSVehicleType*, int>::const_iterator it = typedAmount.begin(); it != typedAmount.end(); ++it) {
125 1152 : dev.openTag("actorConfig");
126 576 : const int typedAvgSpeed = int(100 * typedTravelDistance.find(it->first)->second / typedSamples.find(it->first)->second);
127 576 : dev.writeAttr(SUMO_ATTR_ID, it->first->getNumericalID());
128 576 : dev.writeOptionalAttr(SUMO_ATTR_AMOUNT, it->second, attributeMask);
129 576 : dev.writeOptionalAttr(SUMO_ATTR_AVERAGESPEED, typedAvgSpeed, attributeMask);
130 1152 : dev.closeTag();
131 : }
132 : }
133 2756 : dev.closeTag();
134 2756 : }
135 :
136 : // ---------------------------------------------------------------------------
137 : // MSMeanData_Amitran - methods
138 : // ---------------------------------------------------------------------------
139 26 : MSMeanData_Amitran::MSMeanData_Amitran(const std::string& id,
140 : const SUMOTime dumpBegin,
141 : const SUMOTime dumpEnd, const bool useLanes,
142 : const std::string& excludeEmpty,
143 : const bool withInternal,
144 : const bool trackVehicles,
145 : const int detectPersons,
146 : const double maxTravelTime,
147 : const double minSamples,
148 : const double haltSpeed,
149 : const std::string& vTypes,
150 : const std::string& writeAttributes,
151 : const std::vector<MSEdge*>& edges,
152 26 : AggregateType aggregate) :
153 : MSMeanData(id, dumpBegin, dumpEnd, useLanes, excludeEmpty,
154 : withInternal, trackVehicles, detectPersons, maxTravelTime, minSamples, vTypes, writeAttributes, edges, aggregate),
155 26 : myHaltSpeed(haltSpeed) {
156 26 : }
157 :
158 :
159 52 : MSMeanData_Amitran::~MSMeanData_Amitran() {}
160 :
161 :
162 : void
163 26 : MSMeanData_Amitran::writeXMLDetectorProlog(OutputDevice& dev) const {
164 52 : dev.writeXMLHeader("linkData", "amitran/linkdata.xsd");
165 26 : }
166 :
167 :
168 : std::string
169 2732 : MSMeanData_Amitran::getEdgeID(const MSEdge* const edge) {
170 2732 : return toString(edge->getNumericalID());
171 : }
172 :
173 :
174 : void
175 254 : MSMeanData_Amitran::openInterval(OutputDevice& dev, const SUMOTime startTime, const SUMOTime stopTime) {
176 254 : const int duration = int(1000 * STEPS2TIME(stopTime - startTime) + 0.5);
177 254 : dev.openTag(SUMO_TAG_TIMESLICE).writeAttr(SUMO_ATTR_STARTTIME, int(1000 * STEPS2TIME(startTime) + 0.5)).writeAttr(SUMO_ATTR_DURATION, duration);
178 254 : }
179 :
180 :
181 : void
182 2756 : MSMeanData_Amitran::writePrefix(OutputDevice& dev, const MeanDataValues& /*values*/, const SumoXMLTag /* tag */, const std::string id) const {
183 2756 : dev.openTag("link").writeAttr(SUMO_ATTR_ID, id);
184 2756 : }
185 :
186 :
187 : MSMeanData::MeanDataValues*
188 2492 : MSMeanData_Amitran::createValues(MSLane* const lane, const double length, const bool doAdd) const {
189 2492 : return new MSLaneMeanDataValues(lane, length, doAdd, this);
190 : }
191 :
192 :
193 : /****************************************************************************/
|