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