Eclipse SUMO - Simulation of Urban MObility
MSRouteProbe.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 // Copyright (C) 2008-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 /****************************************************************************/
21 // Writes route distributions at a certain edge
22 /****************************************************************************/
23 #include <config.h>
24 
25 #include <string>
26 #include <microsim/MSEdge.h>
27 #include <microsim/MSLane.h>
28 #include <microsim/MSGlobals.h>
29 #include <microsim/MSRoute.h>
30 #include <microsim/MSVehicle.h>
31 #include <utils/common/ToString.h>
33 #include <mesosim/MELoop.h>
34 #include <mesosim/MESegment.h>
35 #include "MSRouteProbe.h"
36 
37 // ===========================================================================
38 // static members
39 // ===========================================================================
40 
41 // ===========================================================================
42 // method definitions
43 // ===========================================================================
44 MSRouteProbe::MSRouteProbe(const std::string& id, const MSEdge* edge, const std::string& distID, const std::string& lastID,
45  const std::string& vTypes) :
46  MSDetectorFileOutput(id, vTypes), MSMoveReminder(id),
47  myDistID(distID),
48  myLastID(lastID),
49  myLastRouteDistribution(nullptr),
50  myCurrentRouteDistribution(nullptr),
51  myEdge(edge) {
54  while (seg != nullptr) {
55  seg->addDetector(this);
56  seg = seg->getNextSegment();
57  }
58  return;
59  }
60  for (std::vector<MSLane*>::const_iterator it = edge->getLanes().begin(); it != edge->getLanes().end(); ++it) {
61  (*it)->addMoveReminder(this);
62  }
63 }
64 
65 
67 }
68 
69 void
71  UNUSED_PARAMETER(step);
73  myLastRouteDistribution = nullptr;
74 }
75 
76 void
78  if (myCurrentRouteDistribution == nullptr) {
80  if (myCurrentRouteDistribution == 0) {
83  }
85  }
86 }
87 
88 bool
90  if (!vehicleApplies(veh)) {
91  return false;
92  }
94  SUMOVehicle* vehicle = dynamic_cast<SUMOVehicle*>(&veh);
95  if (vehicle != nullptr) {
98  }
99  }
100  return false;
101 }
102 
103 
104 void
106  SUMOTime startTime, SUMOTime stopTime) {
108  dev.openTag("routeDistribution") << " id=\"" << getID() + "_" + time2string(startTime) << "\"";
109  const std::vector<ConstMSRoutePtr>& routes = myCurrentRouteDistribution->getVals();
110  const std::vector<double>& probs = myCurrentRouteDistribution->getProbs();
111  for (int j = 0; j < (int)routes.size(); ++j) {
112  ConstMSRoutePtr r = routes[j];
113  dev.openTag("route") << " id=\"" << r->getID() + "_" + time2string(startTime) << "\" edges=\"";
114  for (MSRouteIterator i = r->begin(); i != r->end(); ++i) {
115  if (i != r->begin()) {
116  dev << " ";
117  }
118  dev << (*i)->getID();
119  }
120  dev << "\" probability=\"" << probs[j] << "\"";
121  dev.closeTag();
122  }
123  dev.closeTag();
124  if (myLastRouteDistribution != 0) {
126  }
128  myLastID = myDistID;
129  myDistID = getID() + "_" + toString(stopTime);
132  }
133 }
134 
135 
136 void
138  dev.writeXMLHeader("routes", "routes_file.xsd");
139 }
140 
141 
143 MSRouteProbe::sampleRoute(bool last) const {
144  if (myLastRouteDistribution == 0 || !last) {
147  }
148  return nullptr;
149  }
150  return myLastRouteDistribution->get();
151 }
long long int SUMOTime
Definition: GUI.h:35
ConstMSEdgeVector::const_iterator MSRouteIterator
Definition: MSRoute.h:56
std::shared_ptr< const MSRoute > ConstMSRoutePtr
Definition: Route.h:31
std::string time2string(SUMOTime t, bool humanReadable)
convert SUMOTime to string (independently of global format setting)
Definition: SUMOTime.cpp:69
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:30
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
MESegment * getSegmentForEdge(const MSEdge &e, double pos=0)
Get the segment for a given edge at a given position.
Definition: MELoop.cpp:325
A single mesoscopic segment (cell)
Definition: MESegment.h:49
void addDetector(MSMoveReminder *data, int queueIndex=-1)
Adds a data collector for a detector to this segment.
Definition: MESegment.cpp:250
MESegment * getNextSegment() const
Returns the following segment on the same edge (0 if it is the last).
Definition: MESegment.h:234
Base of value-generating classes (detectors)
bool vehicleApplies(const SUMOTrafficObject &veh) const
Checks whether the detector measures vehicles of the given type.
A road/street connecting two junctions.
Definition: MSEdge.h:77
const std::vector< MSLane * > & getLanes() const
Returns this edge's lanes.
Definition: MSEdge.h:168
static bool gUseMesoSim
Definition: MSGlobals.h:103
static MELoop * gMesoNet
mesoscopic simulation infrastructure
Definition: MSGlobals.h:109
Representation of a lane in the micro simulation.
Definition: MSLane.h:84
Something on a lane to be noticed about vehicle movement.
Notification
Definition of a vehicle state.
@ NOTIFICATION_SEGMENT
The vehicle changes the segment (meso only)
@ NOTIFICATION_LANE_CHANGE
The vehicle changes lanes (micro only)
static bool dictionary(const std::string &id, ConstMSRoutePtr route)
Adds a route to the dictionary.
Definition: MSRoute.cpp:109
static RandomDistributor< ConstMSRoutePtr > * distDictionary(const std::string &id)
Returns the named route distribution.
Definition: MSRoute.cpp:161
static void checkDist(const std::string &id)
Checks the distribution whether it is permanent and deletes it if not.
Definition: MSRoute.cpp:187
void clearState(SUMOTime step) override
Remove all vehicles before quick-loading state.
std::string myLastID
Definition: MSRouteProbe.h:139
void writeXMLDetectorProlog(OutputDevice &dev) const override
Opens the XML-output using "detector" as root element.
virtual ~MSRouteProbe()
Destructor.
RandomDistributor< ConstMSRoutePtr > * myCurrentRouteDistribution
The current distribution of routes (probability->route)
Definition: MSRouteProbe.h:145
MSRouteProbe(const std::string &id, const MSEdge *edge, const std::string &distID, const std::string &lastID, const std::string &vTypes)
Constructor.
RandomDistributor< ConstMSRoutePtr > * myLastRouteDistribution
The previous distribution of routes (probability->route)
Definition: MSRouteProbe.h:142
void initDistributions()
std::string myDistID
Definition: MSRouteProbe.h:138
ConstMSRoutePtr sampleRoute(bool last=true) const
bool notifyEnter(SUMOTrafficObject &veh, MSMoveReminder::Notification reason, const MSLane *enteredLane=0) override
Returns whether the vehicle shall be aware of this entry.
void writeXMLOutput(OutputDevice &dev, SUMOTime startTime, SUMOTime stopTime) override
Writes values into the given stream.
const std::string & getID() const
Returns the id.
Definition: Named.h:74
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:61
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
bool writeXMLHeader(const std::string &rootElement, const std::string &schemaFile, std::map< SumoXMLAttr, std::string > attrs=std::map< SumoXMLAttr, std::string >(), bool includeConfig=true)
Writes an XML header with optional configuration.
double getOverallProb() const
Return the sum of the probabilites assigned to the members.
const std::vector< double > & getProbs() const
Returns the probabilities assigned to the members of the distribution.
const std::vector< T > & getVals() const
Returns the members of the distribution.
T get(SumoRNG *which=nullptr) const
Draw a sample of the distribution.
bool add(T val, double prob, bool checkDuplicates=true)
Adds a value with an assigned probability to the distribution.
Representation of a vehicle, person, or container.
Representation of a vehicle.
Definition: SUMOVehicle.h:60
virtual ConstMSRoutePtr getRoutePtr() const =0
Returns the current route.