Eclipse SUMO - Simulation of Urban MObility
Calibrator.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-2020 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 /****************************************************************************/
18 // C++ TraCI client API implementation
19 /****************************************************************************/
20 #include <config.h>
21 
22 #include <microsim/MSNet.h>
23 #include <microsim/MSEdge.h>
26 #include <libsumo/TraCIConstants.h>
27 #include "Helper.h"
28 #include "Calibrator.h"
29 
30 
31 namespace libsumo {
32 // ===========================================================================
33 // static member initializations
34 // ===========================================================================
37 
38 
39 // ===========================================================================
40 // static member definitions
41 // ===========================================================================
42 std::vector<std::string>
43 Calibrator::getIDList() {
44  std::vector<std::string> ids;
45  for (auto& item : MSCalibrator::getInstances()) {
46  ids.push_back(item.first);
47  }
48  std::sort(ids.begin(), ids.end());
49  return ids;
50 }
51 
52 int
53 Calibrator::getIDCount() {
54  return (int)getIDList().size();
55 }
56 
57 std::string
58 Calibrator::getEdgeID(const std::string& calibratorID) {
59  return getCalibrator(calibratorID)->getEdge()->getID();
60 }
61 
62 std::string
63 Calibrator::getLaneID(const std::string& calibratorID) {
64  const MSLane* lane = getCalibrator(calibratorID)->getLane();
65  if (lane == nullptr) {
66  return "";
67  } else {
68  return lane->getID();
69  }
70 }
71 
72 double
73 Calibrator::getVehsPerHour(const std::string& calibratorID) {
74  return getCalibratorState(getCalibrator(calibratorID)).q;
75 }
76 
77 double
78 Calibrator::getSpeed(const std::string& calibratorID) {
79  return getCalibratorState(getCalibrator(calibratorID)).v;
80 }
81 
82 std::string
83 Calibrator::getTypeID(const std::string& calibratorID) {
85 }
86 
87 double
88 Calibrator::getBegin(const std::string& calibratorID) {
89  return STEPS2TIME(getCalibratorState(getCalibrator(calibratorID)).begin);
90 }
91 
92 double
93 Calibrator::getEnd(const std::string& calibratorID) {
94  return STEPS2TIME(getCalibratorState(getCalibrator(calibratorID)).end);
95 }
96 
97 std::string
98 Calibrator::getRouteID(const std::string& calibratorID) {
100 }
101 
102 std::string
103 Calibrator::getRouteProbeID(const std::string& calibratorID) {
104  const MSRouteProbe* rp = getCalibrator(calibratorID)->getRouteProbe();
105  if (rp == nullptr) {
106  return "";
107  } else {
108  return rp->getID();
109  }
110 }
111 
112 std::vector<std::string>
113 Calibrator::getVTypes(const std::string& calibratorID) {
114  std::vector<std::string> result;
115  const std::set<std::string>& vTypes = getCalibrator(calibratorID)->getVehicleTypes();
116  result.insert(result.begin(), vTypes.begin(), vTypes.end());
117  std::sort(result.begin(), result.end());
118  return result;
119 }
120 
121 
122 int
123 Calibrator::getPassed(const std::string& calibratorID) {
124  return getCalibrator(calibratorID)->passed();
125 }
126 
127 int
128 Calibrator::getInserted(const std::string& calibratorID) {
129  return getCalibrator(calibratorID)->getInserted();
130 }
131 
132 int
133 Calibrator::getRemoved(const std::string& calibratorID) {
134  return getCalibrator(calibratorID)->getRemoved();
135 }
136 
137 std::string
138 Calibrator::getParameter(const std::string& calibratorID, const std::string& param) {
139  const MSCalibrator* c = getCalibrator(calibratorID);
140  return c->getParameter(param, "");
141 }
142 
144 
145 void
146 Calibrator::setParameter(const std::string& calibratorID, const std::string& key, const std::string& value) {
147  MSCalibrator* c = getCalibrator(calibratorID);
148  c->setParameter(key, value);
149 }
150 
151 void
152 Calibrator::setFlow(const std::string& calibratorID, double begin, double end, double vehsPerHour, double speed, const std::string& typeID,
153  const std::string& routeID,
154  const std::string& departLane,
155  const std::string& departSpeed) {
156  std::string error;
157  SUMOVehicleParameter vehicleParams;
158  vehicleParams.vtypeid = typeID;
159  vehicleParams.routeid = routeID;
161  if (t == nullptr) {
162  throw TraCIException("Vehicle type '" + typeID + "' is not known");
163  }
164  if (!SUMOVehicleParameter::parseDepartLane(departLane, "calibrator", calibratorID, vehicleParams.departLane, vehicleParams.departLaneProcedure, error)) {
165  throw TraCIException(error);
166  }
167  if (!SUMOVehicleParameter::parseDepartSpeed(departSpeed, "calibrator", calibratorID, vehicleParams.departSpeed, vehicleParams.departSpeedProcedure, error)) {
168  throw TraCIException(error);
169  }
170  getCalibrator(calibratorID)->setFlow(TIME2STEPS(begin), TIME2STEPS(end), vehsPerHour, speed, vehicleParams);
171 }
172 
173 
175 
176 
178 Calibrator::getCalibrator(const std::string& id) {
179  const auto& dict = MSCalibrator::getInstances();
180  auto it = dict.find(id);
181  if (it == dict.end()) {
182  throw TraCIException("Calibrator '" + id + "' is not known");
183  }
184  return it->second;
185 }
186 
189  try {
190  return c->getCurrentStateInterval();
191  } catch (ProcessError& e) {
192  throw TraCIException(e.what());
193  }
194 }
195 
196 std::shared_ptr<VariableWrapper>
198  return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
199 }
200 
201 
202 bool
203 Calibrator::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper) {
204  switch (variable) {
205  case TRACI_ID_LIST:
206  return wrapper->wrapStringList(objID, variable, getIDList());
207  case ID_COUNT:
208  return wrapper->wrapInt(objID, variable, getIDCount());
209  case VAR_ROAD_ID:
210  return wrapper->wrapString(objID, variable, getEdgeID(objID));
211  case VAR_LANE_ID:
212  return wrapper->wrapString(objID, variable, getLaneID(objID));
213  case VAR_VEHSPERHOUR:
214  return wrapper->wrapDouble(objID, variable, getVehsPerHour(objID));
215  case VAR_SPEED:
216  return wrapper->wrapDouble(objID, variable, getSpeed(objID));
217  case VAR_TYPE:
218  return wrapper->wrapString(objID, variable, getTypeID(objID));
219  case VAR_BEGIN:
220  return wrapper->wrapDouble(objID, variable, getBegin(objID));
221  case VAR_END:
222  return wrapper->wrapDouble(objID, variable, getEnd(objID));
223  case VAR_ROUTE_ID:
224  return wrapper->wrapString(objID, variable, getRouteID(objID));
225  case VAR_ROUTE_PROBE:
226  return wrapper->wrapString(objID, variable, getRouteProbeID(objID));
227  case VAR_VTYPES:
228  return wrapper->wrapStringList(objID, variable, getVTypes(objID));
229  case VAR_PASSED:
230  return wrapper->wrapInt(objID, variable, getPassed(objID));
231  case VAR_INSERTED:
232  return wrapper->wrapInt(objID, variable, getInserted(objID));
233  case VAR_REMOVED:
234  return wrapper->wrapInt(objID, variable, getRemoved(objID));
235  default:
236  return false;
237  }
238 }
239 
240 }
241 
242 
243 /****************************************************************************/
#define STEPS2TIME(x)
Definition: SUMOTime.h:53
#define TIME2STEPS(x)
Definition: SUMOTime.h:55
#define LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOMAIN)
Definition: TraCIDefs.h:57
#define LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(CLASS)
Definition: TraCIDefs.h:104
C++ TraCI client API implementation.
Calibrates the flow on a segment to a specified one.
Definition: MSCalibrator.h:48
int getInserted() const
Definition: MSCalibrator.h:134
static const std::map< std::string, MSCalibrator * > & getInstances()
return all calibrator instances
Definition: MSCalibrator.h:98
const MSLane * getLane() const
Definition: MSCalibrator.h:117
virtual int passed() const
Definition: MSCalibrator.h:125
void setFlow(SUMOTime begin, SUMOTime end, double vehsPerHour, double speed, SUMOVehicleParameter vehicleParameter)
const MSRouteProbe * getRouteProbe() const
Definition: MSCalibrator.h:121
int getRemoved() const
Definition: MSCalibrator.h:138
AspiredState getCurrentStateInterval() const
const MSEdge * getEdge() const
Definition: MSCalibrator.h:113
const std::set< std::string > & getVehicleTypes() const
Representation of a lane in the micro simulation.
Definition: MSLane.h:82
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:171
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:371
Writes routes of vehicles passing a certain edge.
Definition: MSRouteProbe.h:58
MSVehicleType * getVType(const std::string &id=DEFAULT_VTYPE_ID, std::mt19937 *rng=nullptr)
Returns the named vehicle type or a sample from the named distribution.
The car-following model and parameter.
Definition: MSVehicleType.h:62
const std::string & getID() const
Returns the id.
Definition: Named.h:73
const std::string getParameter(const std::string &key, const std::string defaultValue="") const
Returns the value for a given key.
virtual void setParameter(const std::string &key, const std::string &value)
Sets a parameter.
Structure representing possible vehicle parameter.
int departLane
(optional) The lane the vehicle shall depart from (index in edge)
double departSpeed
(optional) The initial speed of the vehicle
std::string vtypeid
The vehicle's type id.
DepartLaneDefinition departLaneProcedure
Information how the vehicle shall choose the lane to depart from.
static bool parseDepartSpeed(const std::string &val, const std::string &element, const std::string &id, double &speed, DepartSpeedDefinition &dsd, std::string &error)
Validates a given departSpeed value.
DepartSpeedDefinition departSpeedProcedure
Information how the vehicle's initial speed shall be chosen.
static bool parseDepartLane(const std::string &val, const std::string &element, const std::string &id, int &lane, DepartLaneDefinition &dld, std::string &error)
Validates a given departLane value.
std::string routeid
The vehicle's route id.
static int getPassed(const std::string &calibratorID)
Definition: Calibrator.cpp:123
static MSCalibrator * getCalibrator(const std::string &id)
Definition: Calibrator.cpp:178
static double getSpeed(const std::string &calibratorID)
Definition: Calibrator.cpp:78
static int getRemoved(const std::string &calibratorID)
Definition: Calibrator.cpp:133
static MSCalibrator::AspiredState getCalibratorState(const MSCalibrator *c)
Definition: Calibrator.cpp:188
static std::string getRouteID(const std::string &calibratorID)
Definition: Calibrator.cpp:98
static double getEnd(const std::string &calibratorID)
Definition: Calibrator.cpp:93
static std::string getEdgeID(const std::string &calibratorID)
Definition: Calibrator.cpp:58
static double getVehsPerHour(const std::string &calibratorID)
Definition: Calibrator.cpp:73
static std::vector< std::string > getVTypes(const std::string &routeID)
Definition: Calibrator.cpp:113
static std::string getRouteProbeID(const std::string &calibratorID)
Definition: Calibrator.cpp:103
static SubscriptionResults mySubscriptionResults
Definition: Calibrator.h:78
static std::string getTypeID(const std::string &calibratorID)
Definition: Calibrator.cpp:83
static double getBegin(const std::string &calibratorID)
Definition: Calibrator.cpp:88
static std::string getLaneID(const std::string &calibratorID)
Definition: Calibrator.cpp:63
static std::shared_ptr< VariableWrapper > makeWrapper()
Definition: Calibrator.cpp:197
static bool handleVariable(const std::string &objID, const int variable, VariableWrapper *wrapper)
Definition: Calibrator.cpp:203
LIBSUMO_ID_PARAMETER_API static LIBSUMO_SUBSCRIPTION_API void setFlow(const std::string &calibratorID, double begin, double end, double vehsPerHour, double speed, const std::string &typeID, const std::string &routeID, const std::string &departLane="first", const std::string &departSpeed="max")
Definition: Calibrator.cpp:152
static ContextSubscriptionResults myContextSubscriptionResults
Definition: Calibrator.h:79
static int getInserted(const std::string &calibratorID)
Definition: Calibrator.cpp:128
virtual bool wrapStringList(const std::string &objID, const int variable, const std::vector< std::string > &value)=0
virtual bool wrapString(const std::string &objID, const int variable, const std::string &value)=0
virtual bool wrapInt(const std::string &objID, const int variable, const int value)=0
virtual bool wrapDouble(const std::string &objID, const int variable, const double value)=0
TRACI_CONST int VAR_VEHSPERHOUR
TRACI_CONST int VAR_BEGIN
TRACI_CONST int VAR_ROUTE_PROBE
TRACI_CONST int TRACI_ID_LIST
TRACI_CONST int VAR_TYPE
TRACI_CONST int VAR_REMOVED
TRACI_CONST int VAR_ROAD_ID
TRACI_CONST int VAR_VTYPES
TRACI_CONST int VAR_END
std::map< std::string, TraCIResults > SubscriptionResults
{object->{variable->value}}
Definition: TraCIDefs.h:250
TRACI_CONST int ID_COUNT
TRACI_CONST int VAR_LANE_ID
TRACI_CONST int VAR_SPEED
TRACI_CONST int VAR_INSERTED
TRACI_CONST int VAR_PASSED
TRACI_CONST int VAR_ROUTE_ID
std::map< std::string, SubscriptionResults > ContextSubscriptionResults
Definition: TraCIDefs.h:251
SUMOVehicleParameter * vehicleParameter
Definition: MSCalibrator.h:108