Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
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-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/****************************************************************************/
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>
33#include <mesosim/MELoop.h>
34#include <mesosim/MESegment.h>
35#include "MSRouteProbe.h"
36
37
38// ===========================================================================
39// method definitions
40// ===========================================================================
41MSRouteProbe::MSRouteProbe(const std::string& id, const MSEdge* edge, const std::string& distID, const std::string& lastID,
42 const std::string& vTypes) :
43 MSDetectorFileOutput(id, vTypes), MSMoveReminder(id),
44 myDistID(distID),
45 myLastID(lastID),
46 myLastRouteDistribution(nullptr),
47 myCurrentRouteDistribution(nullptr),
48 myEdge(edge) {
51 while (seg != nullptr) {
52 seg->addDetector(this);
53 seg = seg->getNextSegment();
54 }
55 return;
56 }
57 for (std::vector<MSLane*>::const_iterator it = edge->getLanes().begin(); it != edge->getLanes().end(); ++it) {
58 (*it)->addMoveReminder(this);
59 }
60}
61
62
65
66
67void
73
74
75void
86
87
88bool
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
104void
106 SUMOTime startTime, SUMOTime stopTime) {
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 dev.openTag(SUMO_TAG_ROUTE).writeAttr(SUMO_ATTR_ID, routes[j]->getID() + "_" + time2string(startTime, false))
113 .writeAttr(SUMO_ATTR_EDGES, toString(routes[j]->getEdges())).writeAttr(SUMO_ATTR_PROB, probs[j]).closeTag();
114 }
115 dev.closeTag();
116 if (myLastRouteDistribution != 0) {
118 }
121 myDistID = getID() + "_" + toString(stopTime);
124 }
125}
126
127
128void
130 dev.writeXMLHeader("routes", "routes_file.xsd");
131}
132
133
136 if (myLastRouteDistribution == 0 || !last) {
139 }
140 return nullptr;
141 }
143}
long long int SUMOTime
Definition GUI.h:36
std::shared_ptr< const MSRoute > ConstMSRoutePtr
Definition Route.h:32
std::string time2string(SUMOTime t, bool humanReadable)
convert SUMOTime to string (independently of global format setting)
Definition SUMOTime.cpp:91
@ SUMO_TAG_ROUTE_DISTRIBUTION
distribution of a route
@ SUMO_TAG_ROUTE
description of a route
@ SUMO_ATTR_EDGES
the edges of a route
@ SUMO_ATTR_PROB
@ SUMO_ATTR_ID
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:49
MESegment * getSegmentForEdge(const MSEdge &e, double pos=0)
Get the segment for a given edge at a given position.
Definition MELoop.cpp:330
A single mesoscopic segment (cell)
Definition MESegment.h:50
void addDetector(MSMoveReminder *data, int queueIndex=-1)
Adds a data collector for a detector to this segment.
MESegment * getNextSegment() const
Returns the following segment on the same edge (0 if it is the last).
Definition MESegment.h:238
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:106
static MELoop * gMesoNet
mesoscopic simulation infrastructure
Definition MSGlobals.h:112
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:116
static RandomDistributor< ConstMSRoutePtr > * distDictionary(const std::string &id)
Returns the named route distribution.
Definition MSRoute.cpp:168
static void checkDist(const std::string &id)
Checks the distribution whether it is permanent and deletes it if not.
Definition MSRoute.cpp:194
void clearState(SUMOTime step) override
Remove all vehicles before quick-loading state.
std::string myLastID
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)
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)
void initDistributions()
std::string myDistID
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.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
OutputDevice & writeAttr(const ATTR_TYPE &attr, const T &val, const bool isNull=false)
writes a named attribute
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.
Represents a generic random distribution.
double getOverallProb() const
Return the sum of the probabilites assigned to the members.
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.
const std::vector< T > & getVals() const
Returns the members of the distribution.
const std::vector< double > & getProbs() const
Returns the probabilities assigned to the members of the distribution.
Representation of a vehicle, person, or container.
Representation of a vehicle.
Definition SUMOVehicle.h:63
virtual ConstMSRoutePtr getRoutePtr() const =0
Returns the current route.
#define UNUSED_PARAMETER(x)