Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/microsim/MSInsertionControl.h
185785 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2001-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 MSInsertionControl.h
15
/// @author Christian Roessel
16
/// @author Daniel Krajzewicz
17
/// @author Michael Behrisch
18
/// @author Jakob Erdmann
19
/// @date Mon, 12 Mar 2001
20
///
21
// Inserts vehicles into the network when their departure time is reached
22
/****************************************************************************/
23
#pragma once
24
#include <config.h>
25
26
#include <vector>
27
#include <map>
28
#include <string>
29
#include <set>
30
#include <utils/foxtools/MFXSynchSet.h>
31
#include <microsim/MSRouterDefs.h>
32
#include "MSVehicleContainer.h"
33
34
35
// ===========================================================================
36
// class declarations
37
// ===========================================================================
38
class MSVehicle;
39
class MSVehicleControl;
40
class SUMOVehicle;
41
class SUMOVehicleParameter;
42
43
44
// ===========================================================================
45
// class definitions
46
// ===========================================================================
47
/**
48
* @class MSInsertionControl
49
* @brief Inserts vehicles into the network when their departure time is reached
50
*
51
* Holds a list of vehicles which may be filled by vehicles
52
* read by SUMORouteLoaders. Tries to emit vehicles departing at a time into the
53
* network as soon this time is reached and keeps them as long the insertion
54
* fails.
55
*
56
* If a vehicle is emitted, the control about it is given to the lanes.
57
*
58
* Vehicles are not controlled (created, deleted) by this class.
59
*
60
* @todo When a vehicle is deleted due to waiting too long or because of vaporizing, this is not reported anywhere
61
*/
62
class MSInsertionControl {
63
public:
64
/** @brief Constructor
65
*
66
* @param[in] vc The assigned vehicle control (needed for vehicle re-insertion and deletion)
67
* @param[in] maxDepartDelay Vehicles waiting for insertion longer than this time are deleted (-1: no deletion)
68
* @param[in] checkEdgesOnce Whether an edge on which a vehicle could not depart should be ignored in the same step
69
* @param[in] maxVehicleNumber The maximum number of vehicles that should not be exceeded
70
*/
71
MSInsertionControl(MSVehicleControl& vc, SUMOTime maxDepartDelay, bool checkEdgesOnce, int maxVehicleNumber, SUMOTime randomDepartOffset);
72
73
74
/// @brief Destructor.
75
~MSInsertionControl();
76
77
78
/** @brief Emits vehicles that want to depart at the given time
79
*
80
* All vehicles scheduled for this time are tried to be emitted. This
81
* includes those with a depart time as the given time and those that
82
* wait for being emitted due they could not be inserted in previous
83
* steps.
84
*
85
* For each vehicle, tryInsert is called. If this fails, a vehicle
86
* keeps within the refused emit containers ("myRefusedEmits1",
87
* "myRefusedEmits2") so that it may be emitted within the next steps.
88
*
89
* Returns the number of vehicles that could be inserted into the net.
90
*
91
* @param[in] time The current simulation time
92
* @return The number of vehicles that could be inserted into the net
93
*/
94
int emitVehicles(SUMOTime time);
95
96
97
/** @brief Adds a single vehicle for departure
98
*
99
* The vehicle is added to "myAllVeh".
100
*
101
* @param[in] veh The vehicle to add for later insertion
102
*/
103
void add(SUMOVehicle* veh);
104
105
106
/** @brief Adds parameter for a vehicle flow for departure
107
*
108
* @param[in] pars The flow parameters to add for later insertion
109
* @param[in] index The current index when loading this flow from a simulation state
110
* @return whether it could be added (no other flow with the same id was present)
111
*/
112
bool addFlow(SUMOVehicleParameter* const pars, int index = -1);
113
114
115
/** @brief Returns the number of waiting vehicles
116
*
117
* The sizes of refused emits (sum of vehicles in "myRefusedEmits1" and
118
* "myRefusedEmits2") is returned.
119
*
120
* @return The number of vehicles that could not (yet) be inserted into the net
121
*/
122
int getWaitingVehicleNo() const;
123
124
/// @brief retrieve vehicles waiting for insertion
125
const MSVehicleContainer::VehicleVector& getPendingVehicles() const {
126
return myPendingEmits;
127
}
128
129
/** @brief Returns the number of flows that are still active
130
*
131
* @return number of active flows
132
*/
133
int getPendingFlowCount() const;
134
135
/// @brief stops trying to emit the given vehicle (because it already departed)
136
void alreadyDeparted(SUMOVehicle* veh);
137
138
/// @brief stops trying to emit the given vehicle (and delete it)
139
void descheduleDeparture(const SUMOVehicle* veh);
140
141
/// @brief reverts a previous call to descheduleDeparture (only needed for departPos="random_free")
142
void retractDescheduleDeparture(const SUMOVehicle* veh);
143
144
/// @brief clears out all pending vehicles from a route, "" for all routes
145
void clearPendingVehicles(const std::string& route);
146
147
148
/** @brief Checks for all vehicles whether they can be emitted
149
*
150
* @param[in] time The current simulation time
151
*/
152
void determineCandidates(SUMOTime time);
153
154
/// @brief return the number of pending emits for the given lane
155
int getPendingEmits(const MSLane* lane);
156
157
void adaptIntermodalRouter(MSTransportableRouter& router) const;
158
159
/// @brief compute (optional) random offset to the departure time
160
SUMOTime computeRandomDepartOffset() const;
161
162
/** @brief Saves the current state into the given stream
163
*/
164
void saveState(OutputDevice& out);
165
166
/** @brief Remove all vehicles before quick-loading state */
167
void clearState();
168
169
/// @brief retrieve internal RNG
170
SumoRNG* getFlowRNG() {
171
return &myFlowRNG;
172
}
173
174
/// @brief checks whether the given flow still exists
175
bool hasFlow(const std::string& id) const {
176
return myFlowIDs.count(id) != 0;
177
}
178
179
/// @brief return parameters for the given flow
180
const SUMOVehicleParameter* getFlowPars(const std::string& id) const;
181
182
/// @brief return the last vehicle for the given flow
183
SUMOVehicle* getLastFlowVehicle(const std::string& id) const;
184
185
/// @brief updates the flow scale value to keep track of TraCI-induced change
186
void updateScale(const std::string vtypeid);
187
188
bool hasTaxiFlow() const;
189
190
private:
191
/** @brief Tries to emit the vehicle
192
*
193
* If the insertion fails, it is examined whether the reason was a vaporizing
194
* edge. If so, the vehicle is deleted. Otherwise, it is checked whether the
195
* time the vehicle had to wait so far is larger than the maximum allowed
196
* waiting time. If so, the vehicle is deleted, too. If both does not match,
197
* the vehicle is reinserted to "refusedEmits" in order to be emitted in
198
* next steps.
199
*
200
* @param[in] time The current simulation time
201
* @param[in] veh The vehicle to emit
202
* @param[in] refusedEmits Container to insert vehicles that could not be emitted into
203
* @return The number of emitted vehicles (0 or 1)
204
*/
205
int tryInsert(SUMOTime time, SUMOVehicle* veh,
206
MSVehicleContainer::VehicleVector& refusedEmits);
207
208
209
/** @brief Adds all vehicles that should have been emitted earlier to the refuse container
210
*
211
* @param[in] time The current simulation time
212
* @todo recheck
213
*/
214
void checkCandidates(SUMOTime time, const bool preCheck);
215
216
217
private:
218
219
/// @brief init scale value of flow
220
static double initScale(const std::string vtypeid);
221
222
static bool hasTaxiDeviceType(const std::string& vtypeId, SumoRNG& rng);
223
224
private:
225
/// @brief The assigned vehicle control (needed for vehicle re-insertion and deletion)
226
MSVehicleControl& myVehicleControl;
227
228
/// @brief All loaded vehicles sorted by their departure time
229
MSVehicleContainer myAllVeh;
230
231
/// @brief Buffers for vehicles that could not be inserted
232
MSVehicleContainer::VehicleVector myPendingEmits;
233
234
/// @brief Buffer for vehicles that may be inserted in the current step
235
std::set<SUMOVehicle*> myEmitCandidates;
236
237
/// @brief Set of vehicles which shall not be inserted anymore
238
239
#ifdef HAVE_FOX
240
MFXSynchSet<const SUMOVehicle*> myAbortedEmits;
241
#else
242
std::set<const SUMOVehicle*> myAbortedEmits;
243
#endif
244
245
/** @struct Flow
246
* @brief Definition of vehicle flow with the current index for vehicle numbering
247
*/
248
struct Flow {
249
/// @brief The parameters
250
SUMOVehicleParameter* pars;
251
/// @brief the running index
252
int index;
253
/// @brief the type scaling of this flow. Negative value indicates inhomogenous type distribution
254
double scale;
255
};
256
257
/// @brief Container for periodical vehicle parameters
258
std::vector<Flow> myFlows;
259
260
/// @brief Cache for periodical vehicle ids and their most recent index for quicker checking
261
std::map<std::string, int> myFlowIDs;
262
263
/// @brief The maximum waiting time; vehicles waiting longer are deleted (-1: no deletion)
264
SUMOTime myMaxDepartDelay;
265
266
/// @brief Whether an edge on which a vehicle could not depart should be ignored in the same step
267
bool myEagerInsertionCheck;
268
269
/// @brief Storage for maximum vehicle number
270
int myMaxVehicleNumber;
271
272
/// @brief Last time at which pending emits for each edge where counted
273
SUMOTime myPendingEmitsUpdateTime;
274
275
/// @brief the number of pending emits for each edge in the current time step
276
std::map<const MSLane*, int> myPendingEmitsForLane;
277
278
/// @brief The maximum random offset to be added to vehicles departure times (non-negative)
279
SUMOTime myMaxRandomDepartOffset;
280
281
private:
282
/// @brief Invalidated copy constructor.
283
MSInsertionControl(const MSInsertionControl&);
284
285
/// @brief Invalidated assignment operator.
286
MSInsertionControl& operator=(const MSInsertionControl&);
287
288
/// @brief A random number generator for probabilistic flows
289
SumoRNG myFlowRNG;
290
291
};
292
293