SUMO - Simulation of Urban MObility
Simulation.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2017-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
18 // C++ TraCI client API implementation
19 /****************************************************************************/
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #ifdef _MSC_VER
25 #include <windows_config.h>
26 #else
27 #include <config.h>
28 #endif
29 
30 #include <utils/common/StdDefs.h>
35 #include <utils/xml/XMLSubSys.h>
36 #include <microsim/MSNet.h>
37 #include <microsim/MSEdgeControl.h>
39 #include <microsim/MSEdge.h>
40 #include <microsim/MSLane.h>
41 #include <microsim/MSVehicle.h>
45 #include <netload/NLBuilder.h>
46 #include "Simulation.h"
47 #include <libsumo/TraCIDefs.h>
48 
49 
50 // ===========================================================================
51 // member definitions
52 // ===========================================================================
53 namespace libsumo {
54 /* void
55 Simulation::connect(const std::string& host, int port) {
56 }*/
57 
58 void
59 Simulation::load(const std::vector<std::string>& args) {
60  XMLSubSys::init(); // this may be not good for multiple loads
61  OptionsIO::setArgs(args);
63 }
64 
65 
66 void
68  if (time == 0) {
70  } else {
71  while (MSNet::getInstance()->getCurrentTimeStep() < time) {
73  }
74  }
75 }
76 
77 
78 void
80 }
81 
82 
83 /* void
84 Simulation::subscribe(int domID, const std::string& objID, SUMOTime beginTime, SUMOTime endTime, const std::vector<int>& vars) const {
85 }
86 
87 void
88 Simulation::subscribeContext(int domID, const std::string& objID, SUMOTime beginTime, SUMOTime endTime, int domain, double range, const std::vector<
89 int>& vars) const {
90 } */
91 
92 
95  return mySubscribedValues;
96 }
97 
98 
100 Simulation::getSubscriptionResults(const std::string& objID) const {
101  if (mySubscribedValues.find(objID) != mySubscribedValues.end()) {
102  return mySubscribedValues.find(objID)->second;
103  } else {
104  throw; // Something?
105  }
106 }
107 
108 
112 }
113 
114 
116 Simulation::getContextSubscriptionResults(const std::string& objID) const {
117  if (mySubscribedContextValues.find(objID) != mySubscribedContextValues.end()) {
118  return mySubscribedContextValues.find(objID)->second;
119  } else {
120  throw; // Something?
121  }
122 }
123 
124 
125 SUMOTime
128 }
129 
130 
131 SUMOTime
133  return DELTA_T;
134 }
135 
136 
140  TraCIBoundary tb;
141  tb.xMin = b.xmin();
142  tb.xMax = b.xmax();
143  tb.yMin = b.ymin();
144  tb.yMax = b.ymax();
145  tb.zMin = b.zmin();
146  tb.zMax = b.zmax();
147  return tb;
148 }
149 
150 
151 int
154 }
155 
156 
158 Simulation::findRoute(const std::string& from, const std::string& to, const std::string& typeID, const SUMOTime depart, const int routingMode) {
159  UNUSED_PARAMETER(routingMode);
161  const MSEdge* const fromEdge = MSEdge::dictionary(from);
162  if (fromEdge == 0) {
163  throw TraCIException("Unknown from edge '" + from + "'.");
164  }
165  const MSEdge* const toEdge = MSEdge::dictionary(to);
166  if (toEdge == 0) {
167  throw TraCIException("Unknown to edge '" + from + "'.");
168  }
169  SUMOVehicle* vehicle = 0;
170  if (typeID != "") {
173  if (type == 0) {
174  throw TraCIException("The vehicle type '" + typeID + "' is not known.");
175  }
176  const MSRoute* const routeDummy = new MSRoute("", ConstMSEdgeVector({ fromEdge }), false, 0, std::vector<SUMOVehicleParameter::Stop>());
177  vehicle = MSNet::getInstance()->getVehicleControl().buildVehicle(pars, routeDummy, type, false);
178  }
179  ConstMSEdgeVector edges;
180  const SUMOTime dep = depart < 0 ? MSNet::getInstance()->getCurrentTimeStep() : depart;
181  MSNet::getInstance()->getRouterTT().compute(fromEdge, toEdge, vehicle, dep, edges);
182  for (const MSEdge* e : edges) {
183  result.edges.push_back(e->getID());
184  }
185  result.travelTime = result.cost = MSNet::getInstance()->getRouterTT().recomputeCosts(edges, vehicle, dep);
186  if (vehicle != 0) {
188  }
189  return result;
190 }
191 
192 
193 std::vector<TraCIStage>
194 Simulation::findIntermodalRoute(const std::string& from, const std::string& to,
195  const std::string& modes, const SUMOTime depart, const int routingMode, const double speed, const double walkFactor,
196  const double departPos, const double arrivalPos, const double departPosLat,
197  const std::string& pType, const std::string& vehType) {
198  UNUSED_PARAMETER(routingMode);
199  UNUSED_PARAMETER(departPosLat);
200  std::vector<TraCIStage> result;
201  const MSEdge* const fromEdge = MSEdge::dictionary(from);
202  if (fromEdge == 0) {
203  throw TraCIException("Unknown from edge '" + from + "'.");
204  }
205  const MSEdge* const toEdge = MSEdge::dictionary(to);
206  if (toEdge == 0) {
207  throw TraCIException("Unknown to edge '" + to + "'.");
208  }
210  SVCPermissions modeSet = 0;
211  for (StringTokenizer st(modes); st.hasNext();) {
212  const std::string mode = st.next();
213  if (mode == "car") {
214  modeSet |= SVC_PASSENGER;
215  } else if (mode == "bicycle") {
216  modeSet |= SVC_BICYCLE;
217  } else if (mode == "public") {
218  modeSet |= SVC_BUS;
219  } else {
220  throw TraCIException("Unknown person mode '" + mode + "'.");
221  }
222  }
223  const MSVehicleType* pedType = vehControl.hasVType(pType) ? vehControl.getVType(pType) : vehControl.getVType(DEFAULT_PEDTYPE_ID);
224  const SUMOTime dep = depart < 0 ? MSNet::getInstance()->getCurrentTimeStep() : depart;
225  if (modeSet == 0) {
226  ConstMSEdgeVector edges;
227  const double cost = MSNet::getInstance()->getPedestrianRouter().compute(fromEdge, toEdge, departPos, arrivalPos, speed > 0 ? speed : pedType->getMaxSpeed(), dep, 0, edges);
228  if (cost < 0) {
229  return result;
230  }
232  for (const MSEdge* e : edges) {
233  result.back().edges.push_back(e->getID());
234  }
235  result.back().travelTime = result.back().cost = cost;
236  } else {
238  SUMOVehicle* vehicle = 0;
239  if (vehType != "") {
241  if (type->getVehicleClass() != SVC_IGNORING && (fromEdge->getPermissions() & type->getVehicleClass()) == 0) {
242  throw TraCIException("Invalid vehicle type '" + type->getID() + "', it is not allowed on the start edge.");
243  }
244  const MSRoute* const routeDummy = new MSRoute("", ConstMSEdgeVector({ fromEdge }), false, 0, std::vector<SUMOVehicleParameter::Stop>());
245  vehicle = vehControl.buildVehicle(pars, routeDummy, type, !MSGlobals::gCheckRoutes);
246  }
247  std::vector<MSNet::MSIntermodalRouter::TripItem> items;
248  if (MSNet::getInstance()->getIntermodalRouter().compute(fromEdge, toEdge, departPos, arrivalPos,
249  pedType->getMaxSpeed() * walkFactor, vehicle, modeSet, dep, items)) {
250  for (std::vector<MSNet::MSIntermodalRouter::TripItem>::iterator it = items.begin(); it != items.end(); ++it) {
251  if (!it->edges.empty()) {
252  if (it->line == "") {
254  } else if (vehicle != 0 && it->line == vehicle->getID()) {
255  result.emplace_back(TraCIStage(MSTransportable::DRIVING));
256  }
257  result.back().destStop = it->destStop;
258  result.back().line = it->line;
259  for (const MSEdge* e : it->edges) {
260  result.back().edges.push_back(e->getID());
261  }
262  result.back().travelTime = result.back().cost = it->cost;
263  }
264  }
265  }
266  if (vehicle != 0) {
267  vehControl.deleteVehicle(vehicle, true);
268  }
269  }
270  return result;
271 }
272 
273 
274 std::string
275 Simulation::getParameter(const std::string& objectID, const std::string& key) {
276  if (StringUtils::startsWith(key, "chargingStation.")) {
277  const std::string attrName = key.substr(16);
279  if (cs == 0) {
280  throw TraCIException("Invalid chargingStation '" + objectID + "'");
281  }
282  if (attrName == toString(SUMO_ATTR_TOTALENERGYCHARGED)) {
283  return toString(cs->getTotalCharged());
284  } else {
285  throw TraCIException("Invalid chargingStation parameter '" + attrName + "'");
286  }
287  } else if (StringUtils::startsWith(key, "parkingArea.")) {
288  const std::string attrName = key.substr(12);
290  if (pa == 0) {
291  throw TraCIException("Invalid parkingArea '" + objectID + "'");
292  }
293  if (attrName == "capacity") {
294  return toString(pa->getCapacity());
295  } else if (attrName == "occupancy") {
296  return toString(pa->getOccupancy());
297  } else {
298  throw TraCIException("Invalid parkingArea parameter '" + attrName + "'");
299  }
300  } else {
301  throw TraCIException("Parameter '" + key + "' is not supported.");
302  }
303 }
304 }
305 
306 
307 /****************************************************************************/
A lane area vehicles can halt at.
Definition: MSParkingArea.h:65
std::map< std::string, SubscribedValues > SubscribedContextValues
Definition: Simulation.h:67
double ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:137
static void init()
Initialises the xml-subsystem.
Definition: XMLSubSys.cpp:53
const SubscribedContextValues & getContextSubscriptionResults() const
Definition: Simulation.cpp:110
double xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:131
virtual void deleteVehicle(SUMOVehicle *v, bool discard=false)
Deletes the vehicle.
const Boundary & getConvBoundary() const
Returns the converted boundary.
MSStoppingPlace * getStoppingPlace(const std::string &id, const SumoXMLTag category) const
Returns the named stopping place of the given category.
Definition: MSNet.cpp:863
virtual double recomputeCosts(const std::vector< const E *> &edges, const V *const v, SUMOTime msTime) const =0
static void simulationStep(const SUMOTime time=0)
Advances by one step (or up to the given time)
Definition: Simulation.cpp:67
vehicle is a bicycle
static void load(const std::vector< std::string > &args)
load a simulation with the given arguments
Definition: Simulation.cpp:59
int SVCPermissions
bitset where each bit declares whether a certain SVC may use this edge/lane
static SUMOTime getCurrentTime()
Definition: Simulation.cpp:126
virtual bool compute(const E *from, const E *to, const V *const vehicle, SUMOTime msTime, std::vector< const E *> &into)=0
Builds the route between the given edges using the minimum effort at the given time The definition of...
A 3D-bounding box.
Definition: TraCIDefs.h:90
double zmax() const
Returns maximum z-coordinate.
Definition: Boundary.cpp:155
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:167
MSPedestrianRouterDijkstra & getPedestrianRouter(const MSEdgeVector &prohibited=MSEdgeVector()) const
Definition: MSNet.cpp:928
static std::string getParameter(const std::string &objectID, const std::string &key)
Definition: Simulation.cpp:275
SUMOTime DELTA_T
Definition: SUMOTime.cpp:39
static bool dictionary(const std::string &id, MSEdge *edge)
Inserts edge into the static dictionary Returns true if the key id isn&#39;t already in the dictionary...
Definition: MSEdge.cpp:744
int getActiveVehicleCount() const
Returns the number of build vehicles that have not been removed or need to wait for a passenger or a ...
std::map< std::string, TraCIValues > SubscribedValues
Definition: Simulation.h:66
std::vector< const MSEdge * > ConstMSEdgeVector
Definition: MSEdge.h:78
const std::string & getID() const
Returns the id.
Definition: Named.h:65
static TraCIStage findRoute(const std::string &from, const std::string &to, const std::string &typeID, const SUMOTime depart, const int routingMode)
Definition: Simulation.cpp:158
bool hasVType(const std::string &id) const
Asks for existence of a vehicle type.
double zmin() const
Returns minimum z-coordinate.
Definition: Boundary.cpp:149
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:39
double cost
effort needed
Definition: TraCIDefs.h:222
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:47
The car-following model and parameter.
Definition: MSVehicleType.h:72
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:61
const SubscribedValues & getSubscriptionResults() const
Definition: Simulation.cpp:94
A road/street connecting two junctions.
Definition: MSEdge.h:80
static bool startsWith(const std::string &str, const std::string prefix)
Checks whether a given string starts with the prefix.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:55
Representation of a vehicle.
Definition: SUMOVehicle.h:66
static bool gCheckRoutes
Definition: MSGlobals.h:85
std::vector< std::string > edges
The sequence of edges to travel.
Definition: TraCIDefs.h:218
void close()
Connects to the specified SUMO server.
Definition: Simulation.cpp:79
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:306
SubscribedValues mySubscribedValues
Definition: Simulation.h:96
int getCapacity() const
Returns the area capacity.
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:253
double getMaxSpeed() const
Get vehicle&#39;s maximum speed [m/s].
double xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:125
int getOccupancy() const
Returns the area occupancy.
vehicle is a passenger car (a "normal" car)
Definition: Edge.cpp:31
MSVehicleType * getVType(const std::string &id=DEFAULT_VTYPE_ID, std::mt19937 *rng=0)
Returns the named vehicle type or a sample from the named distribution.
vehicle is a bus
static TraCIBoundary getNetBoundary()
Definition: Simulation.cpp:138
Structure representing possible vehicle parameter.
double compute(const E *from, const E *to, double departPos, double arrivalPos, double speed, SUMOTime msTime, const N *onlyNode, std::vector< const E *> &into, bool allEdges=false)
Builds the route between the given edges using the minimum effort at the given time The definition of...
MSInsertionControl & getInsertionControl()
Returns the insertion control.
Definition: MSNet.h:359
const std::string DEFAULT_PEDTYPE_ID
int getPendingFlowCount() const
Returns the number of flows that are still active.
const std::string & getID() const
Returns the name of the vehicle type.
double travelTime
duration of the stage
Definition: TraCIDefs.h:220
static const GeoConvHelper & getFinal()
the coordinate transformation for writing the location element and for tracking the original coordina...
static std::vector< TraCIStage > findIntermodalRoute(const std::string &from, const std::string &to, const std::string &modes, const SUMOTime depart, const int routingMode, const double speed, const double walkFactor, const double departPos, const double arrivalPos, const double departPosLat, const std::string &pType, const std::string &vehType)
Definition: Simulation.cpp:194
static int getMinExpectedNumber()
Definition: Simulation.cpp:152
std::map< int, TraCIValue > TraCIValues
{object->{variable->value}}
Definition: Simulation.h:65
SubscribedContextValues mySubscribedContextValues
Definition: Simulation.h:97
long long int SUMOTime
Definition: TraCIDefs.h:51
SUMOAbstractRouter< MSEdge, SUMOVehicle > & getRouterTT(const MSEdgeVector &prohibited=MSEdgeVector()) const
Definition: MSNet.cpp:897
The class responsible for building and deletion of vehicles.
SVCPermissions getPermissions() const
Definition: MSEdge.h:540
virtual SUMOVehicle * buildVehicle(SUMOVehicleParameter *defs, const MSRoute *route, MSVehicleType *type, const bool ignoreStopErrors, const bool fromRouteFile=true)
Builds a vehicle, increases the number of built vehicles.
void simulationStep()
Performs a single simulation step.
Definition: MSNet.cpp:439
static MSNet * init()
Definition: NLBuilder.cpp:226
double ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:143
static SUMOTime getDeltaT()
Definition: Simulation.cpp:132
vehicles ignoring classes
SUMOVehicleClass getVehicleClass() const
Get this vehicle type&#39;s vehicle class.
double getTotalCharged() const