SUMO - Simulation of Urban MObility
TraCI_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 #ifndef NO_TRACI
31 
32 #include <utils/common/StdDefs.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 <traci-server/lib/TraCI.h>
46 #include "TraCI_Simulation.h"
47 #include <traci-server/TraCIDefs.h>
48 
49 // ===========================================================================
50 // member definitions
51 // ===========================================================================
52 SUMOTime
55 }
56 
57 
58 SUMOTime
60  return DELTA_T;
61 }
62 
63 
67  TraCIBoundary tb;
68  tb.xMin = b.xmin();
69  tb.xMax = b.xmax();
70  tb.yMin = b.ymin();
71  tb.yMax = b.ymax();
72  tb.zMin = b.zmin();
73  tb.zMax = b.zmax();
74  return tb;
75 }
76 
77 
78 int
81 }
82 
83 
85 TraCI_Simulation::findRoute(const std::string& from, const std::string& to, const std::string& typeID, const SUMOTime depart, const int routingMode) {
86  UNUSED_PARAMETER(routingMode);
88  const MSEdge* const fromEdge = MSEdge::dictionary(from);
89  if (fromEdge == 0) {
90  throw TraCIException("Unknown from edge '" + from + "'.");
91  }
92  const MSEdge* const toEdge = MSEdge::dictionary(to);
93  if (toEdge == 0) {
94  throw TraCIException("Unknown to edge '" + from + "'.");
95  }
96  SUMOVehicle* vehicle = 0;
97  if (typeID != "") {
100  if (type == 0) {
101  throw TraCIException("The vehicle type '" + typeID + "' is not known.");
102  }
103  const MSRoute* const routeDummy = new MSRoute("", ConstMSEdgeVector({ fromEdge }), false, 0, std::vector<SUMOVehicleParameter::Stop>());
104  vehicle = MSNet::getInstance()->getVehicleControl().buildVehicle(pars, routeDummy, type, false);
105  }
106  ConstMSEdgeVector edges;
107  const SUMOTime dep = depart < 0 ? MSNet::getInstance()->getCurrentTimeStep() : depart;
108  MSNet::getInstance()->getRouterTT().compute(fromEdge, toEdge, vehicle, dep, edges);
109  for (const MSEdge* e : edges) {
110  result.edges.push_back(e->getID());
111  }
112  result.travelTime = result.cost = MSNet::getInstance()->getRouterTT().recomputeCosts(edges, vehicle, dep);
113  if (vehicle != 0) {
115  }
116  return result;
117 }
118 
119 
120 std::vector<TraCIStage>
121 TraCI_Simulation::findIntermodalRoute(const std::string& from, const std::string& to,
122  const std::string modes, const SUMOTime depart, const int routingMode, const double speed, const double walkFactor,
123  const double departPos, const double arrivalPos, const double departPosLat,
124  const std::string& pType, const std::string& vehType) {
125  UNUSED_PARAMETER(routingMode);
126  UNUSED_PARAMETER(departPosLat);
127  std::vector<TraCIStage> result;
128  const MSEdge* const fromEdge = MSEdge::dictionary(from);
129  if (fromEdge == 0) {
130  throw TraCIException("Unknown from edge '" + from + "'.");
131  }
132  const MSEdge* const toEdge = MSEdge::dictionary(to);
133  if (toEdge == 0) {
134  throw TraCIException("Unknown to edge '" + from + "'.");
135  }
137  SVCPermissions modeSet = 0;
138  for (StringTokenizer st(modes); st.hasNext();) {
139  const std::string mode = st.next();
140  if (mode == "car") {
141  modeSet |= SVC_PASSENGER;
142  } else if (mode == "bicycle") {
143  modeSet |= SVC_BICYCLE;
144  } else if (mode == "public") {
145  modeSet |= SVC_BUS;
146  } else {
147  throw TraCIException("Unknown person mode '" + mode + "'.");
148  }
149  }
150  const MSVehicleType* pedType = vehControl.hasVType(pType) ? vehControl.getVType(pType) : vehControl.getVType(DEFAULT_PEDTYPE_ID);
151  const SUMOTime dep = depart < 0 ? MSNet::getInstance()->getCurrentTimeStep() : depart;
152  if (modeSet == 0) {
153  ConstMSEdgeVector edges;
154  const double cost = MSNet::getInstance()->getPedestrianRouter().compute(fromEdge, toEdge, departPos, arrivalPos, speed > 0 ? speed : pedType->getMaxSpeed(), dep, 0, edges);
155  if (cost < 0) {
156  return result;
157  }
159  for (const MSEdge* e : edges) {
160  result.back().edges.push_back(e->getID());
161  }
162  result.back().travelTime = result.back().cost = cost;
163  } else {
165  SUMOVehicle* vehicle = 0;
166  if (vehType != "") {
168  if (type->getVehicleClass() != SVC_IGNORING && (fromEdge->getPermissions() & type->getVehicleClass()) == 0) {
169  throw TraCIException("Invalid vehicle type '" + type->getID() + "', it is not allowed on the start edge.");
170  }
171  const MSRoute* const routeDummy = new MSRoute("", ConstMSEdgeVector({ fromEdge }), false, 0, std::vector<SUMOVehicleParameter::Stop>());
172  vehicle = vehControl.buildVehicle(pars, routeDummy, type, !MSGlobals::gCheckRoutes);
173  }
174  std::vector<MSNet::MSIntermodalRouter::TripItem> items;
175  if (MSNet::getInstance()->getIntermodalRouter().compute(fromEdge, toEdge, departPos, arrivalPos,
176  pedType->getMaxSpeed() * walkFactor, vehicle, modeSet, dep, items)) {
177  for (std::vector<MSNet::MSIntermodalRouter::TripItem>::iterator it = items.begin(); it != items.end(); ++it) {
178  if (!it->edges.empty()) {
179  if (it->line == "") {
181  } else if (vehicle != 0 && it->line == vehicle->getID()) {
182  result.emplace_back(TraCIStage(MSTransportable::DRIVING));
183  }
184  result.back().destStop = it->destStop;
185  result.back().line = it->line;
186  for (const MSEdge* e : it->edges) {
187  result.back().edges.push_back(e->getID());
188  }
189  result.back().travelTime = result.back().cost = it->cost;
190  }
191  }
192  }
193  if (vehicle != 0) {
194  vehControl.deleteVehicle(vehicle, true);
195  }
196  }
197  return result;
198 }
199 
200 
201 std::string
202 TraCI_Simulation::getParameter(const std::string& objectID, const std::string& key) {
203  if (StringUtils::startsWith(key, "chargingStation.")) {
204  const std::string attrName = key.substr(16);
206  if (cs == 0) {
207  throw TraCIException("Invalid chargingStation '" + objectID + "'");
208  }
209  if (attrName == toString(SUMO_ATTR_TOTALENERGYCHARGED)) {
210  return toString(cs->getTotalCharged());
211  } else {
212  throw TraCIException("Invalid chargingStation parameter '" + attrName + "'");
213  }
214  } else if (StringUtils::startsWith(key, "parkingArea.")) {
215  const std::string attrName = key.substr(12);
217  if (pa == 0) {
218  throw TraCIException("Invalid parkingArea '" + objectID + "'");
219  }
220  if (attrName == "capacity") {
221  return toString(pa->getCapacity());
222  } else if (attrName == "occupancy") {
223  return toString(pa->getOccupancy());
224  } else {
225  throw TraCIException("Invalid parkingArea parameter '" + attrName + "'");
226  }
227  } else {
228  throw TraCIException("Parameter '" + key + "' is not supported.");
229  }
230 }
231 
232 
233 #endif
234 
235 
236 /****************************************************************************/
237 
A lane area vehicles can halt at.
Definition: MSParkingArea.h:65
int getCapacity() const
Returns the area capacity.
int getOccupancy() const
Returns the area occupancy.
double xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:131
double xMin
Definition: TraCIDefs.h:90
int getPendingFlowCount() const
Returns the number of flows that are still active.
virtual void deleteVehicle(SUMOVehicle *v, bool discard=false)
Deletes the vehicle.
SUMOVehicleClass getVehicleClass() const
Get this vehicle type&#39;s vehicle class.
double zmax() const
Returns maximum z-coordinate.
Definition: Boundary.cpp:155
double ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:143
vehicle is a bicycle
int SVCPermissions
bitset where each bit declares whether a certain SVC may use this edge/lane
MSPedestrianRouterDijkstra & getPedestrianRouter(const MSEdgeVector &prohibited=MSEdgeVector()) const
Definition: MSNet.cpp:925
static SUMOTime getDeltaT()
int getActiveVehicleCount() const
Returns the number of build vehicles that have not been removed or need to wait for a passenger or a ...
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:167
double getMaxSpeed() const
Get vehicle&#39;s maximum speed [m/s].
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...
SUMOTime DELTA_T
Definition: SUMOTime.cpp:39
SVCPermissions getPermissions() const
Definition: MSEdge.h:540
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
static int getMinExpectedNumber()
std::vector< const MSEdge * > ConstMSEdgeVector
Definition: MSEdge.h:78
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...
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:253
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:39
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)
double zMin
Definition: TraCIDefs.h:90
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:47
The car-following model and parameter.
Definition: MSVehicleType.h:72
double zMax
Definition: TraCIDefs.h:91
MSStoppingPlace * getStoppingPlace(const std::string &id, const SumoXMLTag category) const
Returns the named stopping place of the given category.
Definition: MSNet.cpp:860
const std::string & getID() const
Returns the id.
Definition: Named.h:65
A road/street connecting two junctions.
Definition: MSEdge.h:80
double travelTime
duration of the stage
Definition: TraCIDefs.h:218
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
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:306
double getTotalCharged() const
bool hasVType(const std::string &id) const
Asks for existence of a vehicle type.
const Boundary & getConvBoundary() const
Returns the converted boundary.
double cost
effort needed
Definition: TraCIDefs.h:220
vehicle is a passenger car (a "normal" car)
static SUMOTime getCurrentTime()
double yMax
Definition: TraCIDefs.h:91
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.
SUMOAbstractRouter< MSEdge, SUMOVehicle > & getRouterTT(const MSEdgeVector &prohibited=MSEdgeVector()) const
Definition: MSNet.cpp:894
vehicle is a bus
Structure representing possible vehicle parameter.
MSInsertionControl & getInsertionControl()
Returns the insertion control.
Definition: MSNet.h:359
const std::string DEFAULT_PEDTYPE_ID
A 3D-bounding box.
Definition: TraCIDefs.h:89
double xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:125
virtual double recomputeCosts(const std::vector< const E * > &edges, const V *const v, SUMOTime msTime) const =0
const std::string & getID() const
Returns the name of the vehicle type.
static const GeoConvHelper & getFinal()
the coordinate transformation for writing the location element and for tracking the original coordina...
double ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:137
static TraCIStage findRoute(const std::string &from, const std::string &to, const std::string &typeID, const SUMOTime depart, const int routingMode)
long long int SUMOTime
Definition: TraCIDefs.h:51
The class responsible for building and deletion of vehicles.
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.
static std::string getParameter(const std::string &objectID, const std::string &key)
static TraCIBoundary getNetBoundary()
vehicles ignoring classes
double yMin
Definition: TraCIDefs.h:90
double zmin() const
Returns minimum z-coordinate.
Definition: Boundary.cpp:149
double xMax
Definition: TraCIDefs.h:91
std::vector< std::string > edges
The sequence of edges to travel.
Definition: TraCIDefs.h:216