Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/libsumo/Calibrator.cpp
169666 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2017-2025 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
/****************************************************************************/
14
/// @file Calibrator.cpp
15
/// @author Jakob Erdmann
16
/// @date 16.03.2020
17
///
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>
25
#include <microsim/output/MSRouteProbe.h>
26
#include <microsim/trigger/MSCalibrator.h>
27
#include <microsim/MSVehicleControl.h>
28
#include <libsumo/TraCIConstants.h>
29
#include "Helper.h"
30
#include "Calibrator.h"
31
32
33
namespace libsumo {
34
// ===========================================================================
35
// static member initializations
36
// ===========================================================================
37
SubscriptionResults Calibrator::mySubscriptionResults;
38
ContextSubscriptionResults Calibrator::myContextSubscriptionResults;
39
40
41
// ===========================================================================
42
// static member definitions
43
// ===========================================================================
44
std::vector<std::string>
45
Calibrator::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
54
int
55
Calibrator::getIDCount() {
56
return (int)getIDList().size();
57
}
58
59
std::string
60
Calibrator::getEdgeID(const std::string& calibratorID) {
61
return getCalibrator(calibratorID)->getEdge()->getID();
62
}
63
64
std::string
65
Calibrator::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
74
double
75
Calibrator::getVehsPerHour(const std::string& calibratorID) {
76
return Helper::getCalibratorState(getCalibrator(calibratorID)).q;
77
}
78
79
double
80
Calibrator::getSpeed(const std::string& calibratorID) {
81
return Helper::getCalibratorState(getCalibrator(calibratorID)).v;
82
}
83
84
std::string
85
Calibrator::getTypeID(const std::string& calibratorID) {
86
return Helper::getCalibratorState(getCalibrator(calibratorID)).vehicleParameter->vtypeid;
87
}
88
89
double
90
Calibrator::getBegin(const std::string& calibratorID) {
91
return STEPS2TIME(Helper::getCalibratorState(getCalibrator(calibratorID)).begin);
92
}
93
94
double
95
Calibrator::getEnd(const std::string& calibratorID) {
96
return STEPS2TIME(Helper::getCalibratorState(getCalibrator(calibratorID)).end);
97
}
98
99
std::string
100
Calibrator::getRouteID(const std::string& calibratorID) {
101
return Helper::getCalibratorState(getCalibrator(calibratorID)).vehicleParameter->routeid;
102
}
103
104
std::string
105
Calibrator::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
114
std::vector<std::string>
115
Calibrator::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
124
int
125
Calibrator::getPassed(const std::string& calibratorID) {
126
return getCalibrator(calibratorID)->passed();
127
}
128
129
int
130
Calibrator::getInserted(const std::string& calibratorID) {
131
return getCalibrator(calibratorID)->getInserted();
132
}
133
134
int
135
Calibrator::getRemoved(const std::string& calibratorID) {
136
return getCalibrator(calibratorID)->getRemoved();
137
}
138
139
std::string
140
Calibrator::getParameter(const std::string& calibratorID, const std::string& param) {
141
const MSCalibrator* c = getCalibrator(calibratorID);
142
return c->getParameter(param, "");
143
}
144
145
LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(Calibrator)
146
147
void
148
Calibrator::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
153
void
154
Calibrator::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;
162
MSVehicleType* t = MSNet::getInstance()->getVehicleControl().getVType(typeID);
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
176
LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(Calibrator, CALIBRATOR)
177
178
179
MSCalibrator*
180
Calibrator::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
190
std::shared_ptr<VariableWrapper>
191
Calibrator::makeWrapper() {
192
return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
193
}
194
195
196
bool
197
Calibrator::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));
229
case libsumo::VAR_PARAMETER:
230
paramData->readUnsignedByte();
231
return wrapper->wrapString(objID, variable, getParameter(objID, paramData->readString()));
232
case libsumo::VAR_PARAMETER_WITH_KEY:
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
/****************************************************************************/
244
245