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.org/sumo
3 // Copyright (C) 2008-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 /****************************************************************************/
17 // Writes route distributions at a certain edge
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <string>
27 #include <microsim/MSEdge.h>
28 #include <microsim/MSLane.h>
29 #include <microsim/MSGlobals.h>
30 #include <microsim/MSRoute.h>
31 #include <microsim/MSVehicle.h>
32 #include <utils/common/ToString.h>
34 #include <mesosim/MELoop.h>
35 #include <mesosim/MESegment.h>
36 #include "MSRouteProbe.h"
37 
38 
39 // ===========================================================================
40 // method definitions
41 // ===========================================================================
42 MSRouteProbe::MSRouteProbe(const std::string& id, const MSEdge* edge, const std::string& distID, const std::string& lastID,
43  const std::string& vTypes) :
44  MSDetectorFileOutput(id, vTypes), MSMoveReminder(id) {
45  myCurrentRouteDistribution = std::make_pair(distID, MSRoute::distDictionary(distID));
46  if (myCurrentRouteDistribution.second == 0) {
48  MSRoute::dictionary(distID, myCurrentRouteDistribution.second, false);
49  }
50  myLastRouteDistribution = std::make_pair(lastID, MSRoute::distDictionary(lastID));
53  while (seg != nullptr) {
54  seg->addDetector(this);
55  seg = seg->getNextSegment();
56  }
57  return;
58  }
59  for (std::vector<MSLane*>::const_iterator it = edge->getLanes().begin(); it != edge->getLanes().end(); ++it) {
60  (*it)->addMoveReminder(this);
61  }
62 }
63 
64 
66 }
67 
68 
69 bool
71  if (!vehicleApplies(veh)) {
72  return false;
73  }
75  SUMOVehicle* vehicle = dynamic_cast<SUMOVehicle*>(&veh);
76  if (vehicle != nullptr) {
77  if (myCurrentRouteDistribution.second->add(&vehicle->getRoute(), 1.)) {
78  vehicle->getRoute().addReference();
79  }
80  }
81  }
82  return false;
83 }
84 
85 
86 void
88  SUMOTime startTime, SUMOTime stopTime) {
89  if (myCurrentRouteDistribution.second->getOverallProb() > 0) {
90  dev.openTag("routeDistribution") << " id=\"" << getID() + "_" + time2string(startTime) << "\"";
91  const std::vector<const MSRoute*>& routes = myCurrentRouteDistribution.second->getVals();
92  const std::vector<double>& probs = myCurrentRouteDistribution.second->getProbs();
93  for (int j = 0; j < (int)routes.size(); ++j) {
94  const MSRoute* r = routes[j];
95  dev.openTag("route") << " id=\"" << r->getID() + "_" + time2string(startTime) << "\" edges=\"";
96  for (MSRouteIterator i = r->begin(); i != r->end(); ++i) {
97  if (i != r->begin()) {
98  dev << " ";
99  }
100  dev << (*i)->getID();
101  }
102  dev << "\" probability=\"" << probs[j] << "\"";
103  dev.closeTag();
104  }
105  dev.closeTag();
106  if (myLastRouteDistribution.second != 0) {
108  }
110  myCurrentRouteDistribution.first = getID() + "_" + toString(stopTime);
113  }
114 }
115 
116 
117 void
119  dev.writeXMLHeader("routes", "routes_file.xsd");
120 }
121 
122 
123 const MSRoute*
125  if (myLastRouteDistribution.second == 0) {
126  if (myCurrentRouteDistribution.second->getOverallProb() > 0) {
127  return myCurrentRouteDistribution.second->get();
128  }
129  return nullptr;
130  }
131  return myLastRouteDistribution.second->get();
132 }
MSRoute::checkDist
static void checkDist(const std::string &id)
Checks the distribution whether it is permanent and deletes it if not.
Definition: MSRoute.cpp:185
MSMoveReminder::NOTIFICATION_LANE_CHANGE
The vehicle changes lanes (micro only)
Definition: MSMoveReminder.h:99
SUMOTrafficObject
Representation of a vehicle or person.
Definition: SUMOTrafficObject.h:47
ToString.h
MSDetectorFileOutput
Base of value-generating classes (detectors)
Definition: MSDetectorFileOutput.h:63
MSLane
Representation of a lane in the micro simulation.
Definition: MSLane.h:82
MESegment
A single mesoscopic segment (cell)
Definition: MESegment.h:49
OutputDevice
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:63
MSRoute::end
MSRouteIterator end() const
Returns the end of the list of edges to pass.
Definition: MSRoute.cpp:75
MSRouteProbe::writeXMLOutput
void writeXMLOutput(OutputDevice &dev, SUMOTime startTime, SUMOTime stopTime)
Writes values into the given stream.
Definition: MSRouteProbe.cpp:87
MSRouteIterator
ConstMSEdgeVector::const_iterator MSRouteIterator
Definition: MSRoute.h:57
MESegment::addDetector
void addDetector(MSMoveReminder *data)
Adds a data collector for a detector to this segment.
Definition: MESegment.cpp:211
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:34
SUMOVehicle
Representation of a vehicle.
Definition: SUMOVehicle.h:60
MSGlobals::gUseMesoSim
static bool gUseMesoSim
Definition: MSGlobals.h:90
MSEdge.h
MSRouteProbe::writeXMLDetectorProlog
void writeXMLDetectorProlog(OutputDevice &dev) const
Opens the XML-output using "detector" as root element.
Definition: MSRouteProbe.cpp:118
MSRoute
Definition: MSRoute.h:66
MSVehicle.h
MSMoveReminder
Something on a lane to be noticed about vehicle movement.
Definition: MSMoveReminder.h:66
OutputDevice::closeTag
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
Definition: OutputDevice.cpp:253
MESegment.h
MSRouteProbe::myCurrentRouteDistribution
std::pair< std::string, RandomDistributor< const MSRoute * > * > myCurrentRouteDistribution
The current distribution of routes (probability->route)
Definition: MSRouteProbe.h:133
SUMOVehicle::getRoute
virtual const MSRoute & getRoute() const =0
Returns the current route.
MSRoute::distDictionary
static RandomDistributor< const MSRoute * > * distDictionary(const std::string &id)
Returns the named route distribution.
Definition: MSRoute.cpp:156
MSRouteProbe::myLastRouteDistribution
std::pair< std::string, RandomDistributor< const MSRoute * > * > myLastRouteDistribution
The previous distribution of routes (probability->route)
Definition: MSRouteProbe.h:130
MELoop::getSegmentForEdge
MESegment * getSegmentForEdge(const MSEdge &e, double pos=0)
Get the segment for a given edge at a given position.
Definition: MELoop.cpp:292
OutputDevice.h
time2string
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:67
MSRouteProbe::MSRouteProbe
MSRouteProbe(const std::string &id, const MSEdge *edge, const std::string &distID, const std::string &lastID, const std::string &vTypes)
Constructor.
Definition: MSRouteProbe.cpp:42
MSGlobals.h
MESegment::getNextSegment
MESegment * getNextSegment() const
Returns the following segment on the same edge (0 if it is the last).
Definition: MESegment.h:151
MSEdge
A road/street connecting two junctions.
Definition: MSEdge.h:78
MSRouteProbe::getRoute
const MSRoute * getRoute() const
Definition: MSRouteProbe.cpp:124
MSRouteProbe::~MSRouteProbe
virtual ~MSRouteProbe()
Destructor.
Definition: MSRouteProbe.cpp:65
RandomDistributor< const MSRoute * >
MSMoveReminder::NOTIFICATION_SEGMENT
The vehicle changes the segment (meso only)
Definition: MSMoveReminder.h:97
OutputDevice::openTag
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
Definition: OutputDevice.cpp:239
toString
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:47
MSRouteProbe::notifyEnter
bool notifyEnter(SUMOTrafficObject &veh, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Returns whether the vehicle shall be aware of this entry.
Definition: MSRouteProbe.cpp:70
MSGlobals::gMesoNet
static MELoop * gMesoNet
mesoscopic simulation infrastructure
Definition: MSGlobals.h:105
MSRoute.h
MSRouteProbe.h
MSEdge::getLanes
const std::vector< MSLane * > & getLanes() const
Returns this edge's lanes.
Definition: MSEdge.h:167
MSRoute::begin
MSRouteIterator begin() const
Returns the begin of the list of edges to pass.
Definition: MSRoute.cpp:69
MSDetectorFileOutput::vehicleApplies
bool vehicleApplies(const SUMOTrafficObject &veh) const
Checks whether the detector measures vehicles of the given type.
Definition: MSDetectorFileOutput.h:141
MSRoute::dictionary
static bool dictionary(const std::string &id, const MSRoute *route)
Adds a route to the dictionary.
Definition: MSRoute.cpp:113
config.h
MELoop.h
MSRoute::addReference
void addReference() const
increments the reference counter for the route
Definition: MSRoute.cpp:94
MSLane.h
OutputDevice::writeXMLHeader
bool writeXMLHeader(const std::string &rootElement, const std::string &schemaFile, std::map< SumoXMLAttr, std::string > attrs=std::map< SumoXMLAttr, std::string >())
Writes an XML header with optional configuration.
Definition: OutputDevice.cpp:227
Named::getID
const std::string & getID() const
Returns the id.
Definition: Named.h:76
MSMoveReminder::Notification
Notification
Definition of a vehicle state.
Definition: MSMoveReminder.h:91