Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
libsumo/Calibrator.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2017-2024 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>
24#include <microsim/MSLane.h>
29#include "Helper.h"
30#include "Calibrator.h"
31
32
33namespace libsumo {
34// ===========================================================================
35// static member initializations
36// ===========================================================================
37SubscriptionResults Calibrator::mySubscriptionResults;
38ContextSubscriptionResults Calibrator::myContextSubscriptionResults;
39
40
41// ===========================================================================
42// static member definitions
43// ===========================================================================
44std::vector<std::string>
45Calibrator::getIDList() {
46 MSNet::getInstance(); // just to check that we actually have a network
47 std::vector<std::string> ids;
48 for (const auto& item : MSCalibrator::getInstances()) {
49 ids.push_back(item.first);
50 }
51 return ids;
52}
53
54int
55Calibrator::getIDCount() {
56 return (int)getIDList().size();
57}
58
59std::string
60Calibrator::getEdgeID(const std::string& calibratorID) {
61 return getCalibrator(calibratorID)->getEdge()->getID();
62}
63
64std::string
65Calibrator::getLaneID(const std::string& calibratorID) {
66 const MSLane* lane = getCalibrator(calibratorID)->getLane();
67 if (lane == nullptr) {
68 return "";
69 } else {
70 return lane->getID();
71 }
72}
73
74double
75Calibrator::getVehsPerHour(const std::string& calibratorID) {
76 return Helper::getCalibratorState(getCalibrator(calibratorID)).q;
77}
78
79double
80Calibrator::getSpeed(const std::string& calibratorID) {
81 return Helper::getCalibratorState(getCalibrator(calibratorID)).v;
82}
83
84std::string
85Calibrator::getTypeID(const std::string& calibratorID) {
86 return Helper::getCalibratorState(getCalibrator(calibratorID)).vehicleParameter->vtypeid;
87}
88
89double
90Calibrator::getBegin(const std::string& calibratorID) {
91 return STEPS2TIME(Helper::getCalibratorState(getCalibrator(calibratorID)).begin);
92}
93
94double
95Calibrator::getEnd(const std::string& calibratorID) {
96 return STEPS2TIME(Helper::getCalibratorState(getCalibrator(calibratorID)).end);
97}
98
99std::string
100Calibrator::getRouteID(const std::string& calibratorID) {
101 return Helper::getCalibratorState(getCalibrator(calibratorID)).vehicleParameter->routeid;
102}
103
104std::string
105Calibrator::getRouteProbeID(const std::string& calibratorID) {
106 const MSRouteProbe* rp = getCalibrator(calibratorID)->getRouteProbe();
107 if (rp == nullptr) {
108 return "";
109 } else {
110 return rp->getID();
111 }
112}
113
114std::vector<std::string>
115Calibrator::getVTypes(const std::string& calibratorID) {
116 std::vector<std::string> result;
117 const std::set<std::string>& vTypes = getCalibrator(calibratorID)->getVehicleTypes();
118 result.insert(result.begin(), vTypes.begin(), vTypes.end());
119 std::sort(result.begin(), result.end());
120 return result;
121}
122
123
124int
125Calibrator::getPassed(const std::string& calibratorID) {
126 return getCalibrator(calibratorID)->passed();
127}
128
129int
130Calibrator::getInserted(const std::string& calibratorID) {
131 return getCalibrator(calibratorID)->getInserted();
132}
133
134int
135Calibrator::getRemoved(const std::string& calibratorID) {
136 return getCalibrator(calibratorID)->getRemoved();
137}
138
139std::string
140Calibrator::getParameter(const std::string& calibratorID, const std::string& param) {
141 const MSCalibrator* c = getCalibrator(calibratorID);
142 return c->getParameter(param, "");
143}
144
146
147void
148Calibrator::setParameter(const std::string& calibratorID, const std::string& key, const std::string& value) {
149 MSCalibrator* c = getCalibrator(calibratorID);
150 c->setParameter(key, value);
151}
152
153void
154Calibrator::setFlow(const std::string& calibratorID, double begin, double end, double vehsPerHour, double speed, const std::string& typeID,
155 const std::string& routeID,
156 const std::string& departLane,
157 const std::string& departSpeed) {
158 std::string error;
159 SUMOVehicleParameter vehicleParams;
160 vehicleParams.vtypeid = typeID;
161 vehicleParams.routeid = routeID;
163 if (t == nullptr) {
164 throw TraCIException("Vehicle type '" + typeID + "' is not known");
165 }
166 if (!SUMOVehicleParameter::parseDepartLane(departLane, "calibrator", calibratorID, vehicleParams.departLane, vehicleParams.departLaneProcedure, error)) {
167 throw TraCIException(error);
168 }
169 if (!SUMOVehicleParameter::parseDepartSpeed(departSpeed, "calibrator", calibratorID, vehicleParams.departSpeed, vehicleParams.departSpeedProcedure, error)) {
170 throw TraCIException(error);
171 }
172 getCalibrator(calibratorID)->setFlow(TIME2STEPS(begin), TIME2STEPS(end), vehsPerHour, speed, vehicleParams);
173}
174
175
177
178
180Calibrator::getCalibrator(const std::string& id) {
181 const auto& dict = MSCalibrator::getInstances();
182 auto it = dict.find(id);
183 if (it == dict.end()) {
184 throw TraCIException("Calibrator '" + id + "' is not known");
185 }
186 return it->second;
187}
188
189
190std::shared_ptr<VariableWrapper>
191Calibrator::makeWrapper() {
192 return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
193}
194
195
196bool
197Calibrator::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData) {
198 switch (variable) {
199 case TRACI_ID_LIST:
200 return wrapper->wrapStringList(objID, variable, getIDList());
201 case ID_COUNT:
202 return wrapper->wrapInt(objID, variable, getIDCount());
203 case VAR_ROAD_ID:
204 return wrapper->wrapString(objID, variable, getEdgeID(objID));
205 case VAR_LANE_ID:
206 return wrapper->wrapString(objID, variable, getLaneID(objID));
207 case VAR_VEHSPERHOUR:
208 return wrapper->wrapDouble(objID, variable, getVehsPerHour(objID));
209 case VAR_SPEED:
210 return wrapper->wrapDouble(objID, variable, getSpeed(objID));
211 case VAR_TYPE:
212 return wrapper->wrapString(objID, variable, getTypeID(objID));
213 case VAR_BEGIN:
214 return wrapper->wrapDouble(objID, variable, getBegin(objID));
215 case VAR_END:
216 return wrapper->wrapDouble(objID, variable, getEnd(objID));
217 case VAR_ROUTE_ID:
218 return wrapper->wrapString(objID, variable, getRouteID(objID));
219 case VAR_ROUTE_PROBE:
220 return wrapper->wrapString(objID, variable, getRouteProbeID(objID));
221 case VAR_VTYPES:
222 return wrapper->wrapStringList(objID, variable, getVTypes(objID));
223 case VAR_PASSED:
224 return wrapper->wrapInt(objID, variable, getPassed(objID));
225 case VAR_INSERTED:
226 return wrapper->wrapInt(objID, variable, getInserted(objID));
227 case VAR_REMOVED:
228 return wrapper->wrapInt(objID, variable, getRemoved(objID));
230 paramData->readUnsignedByte();
231 return wrapper->wrapString(objID, variable, getParameter(objID, paramData->readString()));
233 paramData->readUnsignedByte();
234 return wrapper->wrapStringPair(objID, variable, getParameterWithKey(objID, paramData->readString()));
235 default:
236 return false;
237 }
238}
239
240}
241
242
243/****************************************************************************/
#define STEPS2TIME(x)
Definition SUMOTime.h:55
#define TIME2STEPS(x)
Definition SUMOTime.h:57
#define LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOM)
Definition TraCIDefs.h:76
#define LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(CLASS)
Definition TraCIDefs.h:123
C++ TraCI client API implementation.
Calibrates the flow on a segment to a specified one.
static const std::map< std::string, MSCalibrator * > & getInstances()
return all calibrator instances
Representation of a lane in the micro simulation.
Definition MSLane.h:84
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition MSNet.cpp:185
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition MSNet.h:378
Writes routes of vehicles passing a certain edge.
MSVehicleType * getVType(const std::string &id=DEFAULT_VTYPE_ID, SumoRNG *rng=nullptr, bool readOnly=false)
Returns the named vehicle type or a sample from the named distribution.
The car-following model and parameter.
const std::string & getID() const
Returns the id.
Definition Named.h:74
virtual 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 MSCalibrator::AspiredState getCalibratorState(const MSCalibrator *c)
Definition Helper.cpp:730
virtual std::string readString()
Definition storage.cpp:180
virtual int readUnsignedByte()
Definition storage.cpp:155
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
std::map< std::string, libsumo::SubscriptionResults > ContextSubscriptionResults
Definition TraCIDefs.h:338
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, libsumo::TraCIResults > SubscriptionResults
{object->{variable->value}}
Definition TraCIDefs.h:337
TRACI_CONST int ID_COUNT
TRACI_CONST int VAR_PARAMETER
TRACI_CONST int VAR_LANE_ID
TRACI_CONST int VAR_SPEED
TRACI_CONST int VAR_PARAMETER_WITH_KEY
TRACI_CONST int VAR_INSERTED
TRACI_CONST int VAR_PASSED
TRACI_CONST int VAR_ROUTE_ID
SUMOVehicleParameter * vehicleParameter