Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2002-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 : /****************************************************************************/
14 : /// @file MSRoute.h
15 : /// @author Daniel Krajzewicz
16 : /// @author Friedemann Wesner
17 : /// @author Sascha Krieg
18 : /// @author Michael Behrisch
19 : /// @author Jakob Erdmann
20 : /// @date Sept 2002
21 : ///
22 : // A vehicle route
23 : /****************************************************************************/
24 : #pragma once
25 : #include <config.h>
26 :
27 : #include <string>
28 : #include <map>
29 : #include <vector>
30 : #include <algorithm>
31 : #include <memory>
32 : #include <utils/common/Named.h>
33 : #include <utils/distribution/RandomDistributor.h>
34 : #include <utils/common/RGBColor.h>
35 : #include <utils/vehicle/SUMOVehicleParameter.h>
36 : #include <utils/common/Parameterised.h>
37 : #ifdef HAVE_FOX
38 : #include <utils/foxtools/fxheader.h>
39 : #include <FXThread.h>
40 : #endif
41 :
42 :
43 : // ===========================================================================
44 : // class declarations
45 : // ===========================================================================
46 : class MSEdge;
47 : class MSLane;
48 : class MSRoute;
49 : class OutputDevice;
50 :
51 :
52 : // ===========================================================================
53 : // types definitions
54 : // ===========================================================================
55 : typedef std::vector<const MSEdge*> ConstMSEdgeVector;
56 : typedef std::vector<MSEdge*> MSEdgeVector;
57 : typedef ConstMSEdgeVector::const_iterator MSRouteIterator;
58 : typedef std::shared_ptr<const MSRoute> ConstMSRoutePtr;
59 :
60 :
61 : // ===========================================================================
62 : // class definitions
63 : // ===========================================================================
64 : /**
65 : * @class MSRoute
66 : */
67 : class MSRoute : public Named, public Parameterised {
68 : public:
69 : /// Constructor
70 : MSRoute(const std::string& id, const ConstMSEdgeVector& edges,
71 : const bool isPermanent, const RGBColor* const c,
72 : const std::vector<SUMOVehicleParameter::Stop>& stops,
73 : SUMOTime replacedTime = -1,
74 : int replacedIndex = 0);
75 :
76 : /// Destructor
77 : virtual ~MSRoute();
78 :
79 : /// Returns the begin of the list of edges to pass
80 : MSRouteIterator begin() const;
81 :
82 : /// Returns the end of the list of edges to pass
83 : MSRouteIterator end() const;
84 :
85 : /// Returns the number of edges to pass
86 : int size() const;
87 :
88 : /// returns the destination edge
89 : const MSEdge* getLastEdge() const;
90 :
91 : /** @brief removes the route from the internal dict if it is not marked as permanent */
92 : void checkRemoval() const;
93 :
94 : /** @brief Output the edge ids up to but not including the id of the given edge
95 : * @param[in] os The stream to write the routes into (binary)
96 : * @param[in] firstIndex index of the first edge to be written
97 : * @param[in] lastIndex index of the first edge that shall not be written (-1 writes all remaining)
98 : * @param[in] withInternal Whether internal edges shall be included
99 : * @param[in] svc The vClass for determining internal edges
100 : * @return The number of edges written
101 : */
102 : int writeEdgeIDs(OutputDevice& os, int firstIndex = 0, int lastIndex = -1, bool withInternal = false, SUMOVehicleClass svc = SVC_IGNORING) const;
103 :
104 : bool contains(const MSEdge* const edge) const {
105 115497 : return std::find(myEdges.begin(), myEdges.end(), edge) != myEdges.end();
106 : }
107 :
108 : bool containsAnyOf(const MSEdgeVector& edgelist) const;
109 :
110 : const MSEdge* operator[](int index) const;
111 :
112 : /// @name State I/O
113 : /// @{
114 :
115 : /** @brief Saves all known routes into the given stream
116 : *
117 : * @param[in] os The stream to write the routes into (binary)
118 : */
119 : static void dict_saveState(OutputDevice& out);
120 :
121 : /** @brief Decrement all route references before quick-loading state */
122 : static void dict_clearState();
123 : /// @}
124 :
125 : const ConstMSEdgeVector& getEdges() const {
126 2781806 : return myEdges;
127 : }
128 :
129 : /** @brief Compute the distance between 2 given edges on this route, optionally including the length of internal lanes.
130 : * Note, that for routes which contain loops:
131 : * - the first occurance of fromEdge after routePosition will be used
132 : * - the first occurance of toEdge after the above occurance of fromEdge will be used
133 : * If fromEdge or toEdge are not in the route or the toPos on toEdge is not downstream of fromPos on fromEdge
134 : * std::numeric_limits<double>::max() will be returned.
135 : *
136 : * The code will work with internal edges as fromEdge and toEdge and also the route may contain internal edges
137 : * but only at the start or the end, not between two regular edges. If the route contains internal edges at the begin
138 : * routePosition needs to be 0.
139 : *
140 : * @param[in] fromPos position on the first edge, at wich the computed distance begins
141 : * @param[in] toPos position on the last edge, at which the computed distance ends
142 : * @param[in] fromLane lane at wich computation begins
143 : * @param[in] toLane lane at which distance computation shall stop
144 : * @param[in] routePosition Optional offset when searching for the fromEdge within the route
145 : * @return distance between the position fromPos on fromEdge and toPos on toEdge
146 : */
147 : double getDistanceBetween(double fromPos, double toPos, const MSLane* fromLane, const MSLane* toLane, int routePosition = 0) const;
148 :
149 : /** @brief Compute the distance between 2 given edges on this route, optionally including the length of internal lanes.
150 : * This has the same semantics as above but uses iterators instead of edge
151 : * points so looping routes are not an issue.
152 : *
153 : * @param[in] fromPos position on the first edge, at wich the computed distance begins
154 : * @param[in] toPos position on the last edge, at which the coumputed distance endsance
155 : * @param[in] fromEdge edge at wich computation begins
156 : * @param[in] toEdge edge at which distance computation shall stop
157 : * @param[in] includeInternal Whether the lengths of internal edges shall be counted
158 : * @return distance between the position fromPos on fromEdge and toPos on toEdge
159 : */
160 : double getDistanceBetween(double fromPos, double toPos, const MSRouteIterator& fromEdge, const MSRouteIterator& toEdge, bool includeInternal = true) const;
161 :
162 :
163 : /// @brief Returns the color
164 : const RGBColor& getColor() const;
165 :
166 : /// @brief returns the period
167 : SUMOTime getPeriod() const {
168 1941587 : return myPeriod;
169 : }
170 :
171 : /** @brief Returns the costs of the route
172 : *
173 : * @return The route's costs (normally the time needed to pass it)
174 : */
175 : double getCosts() const {
176 33113 : return myCosts;
177 : }
178 :
179 : /** @brief Returns the estimated savings due to using this route (compare to the route before rerouting)
180 : *
181 : * @return The route's estimated savings (the difference in costs of this route to the previous one)
182 : */
183 : double getSavings() const {
184 33095 : return mySavings;
185 : }
186 :
187 : /// @brief Returns the time at which this route was replaced (or -1)
188 : SUMOTime getReplacedTime() const {
189 1791 : return myReplacedTime;
190 : }
191 :
192 : /// @brief Returns the index at which this route was replaced
193 : int getReplacedIndex() const {
194 1791 : return myReplacedIndex;
195 : }
196 :
197 : /// @brief sets the period
198 : void setPeriod(SUMOTime period) {
199 196760 : myPeriod = period;
200 : }
201 :
202 : /** @brief Sets the costs of the route
203 : *
204 : * @param[in] costs The new route costs
205 : */
206 : void setCosts(double costs) {
207 1644520 : myCosts = costs;
208 : }
209 : /** @brief Sets the savings of the route
210 : *
211 : * @param[in] costs The new route costs
212 : */
213 : void setSavings(double savings) {
214 1447760 : mySavings = savings;
215 : }
216 :
217 : bool mustReroute() const {
218 314056 : return myReroute;
219 : }
220 :
221 : void setReroute(bool reroute = true) {
222 196760 : myReroute = reroute;
223 : }
224 :
225 : /// Returns the stops
226 : const std::vector<SUMOVehicleParameter::Stop>& getStops() const;
227 :
228 : public:
229 : /** @brief Adds a route to the dictionary.
230 : *
231 : * Returns true if the route could be added,
232 : * false if a route (distribution) with the same name already exists.
233 : *
234 : * @param[in] id the id for the new route
235 : * @param[in] route pointer to the route object
236 : * @return whether adding was successful
237 : */
238 : static bool dictionary(const std::string& id, ConstMSRoutePtr route);
239 :
240 : /** @brief Adds a route distribution to the dictionary.
241 : *
242 : * Returns true if the distribution could be added,
243 : * false if a route (distribution) with the same name already exists.
244 : *
245 : * @param[in] id the id for the new route distribution
246 : * @param[in] routeDist pointer to the distribution object
247 : * @param[in] permanent whether the new route distribution survives more than one vehicle / flow
248 : * @return whether adding was successful
249 : */
250 : static bool dictionary(const std::string& id, RandomDistributor<ConstMSRoutePtr>* const routeDist, const bool permanent = true);
251 :
252 : /** @brief Returns the named route or a sample from the named distribution.
253 : *
254 : * Returns 0 if no route and no distribution with the given name exists
255 : * or if the distribution exists and is empty.
256 : *
257 : * @param[in] id the id of the route or the distribution
258 : * @return the route (sample)
259 : */
260 : static ConstMSRoutePtr dictionary(const std::string& id, SumoRNG* rng = 0);
261 :
262 : /// @brief returns whether a route with the given id exists
263 : static bool hasRoute(const std::string& id);
264 :
265 : /** @brief Returns the named route distribution.
266 : *
267 : * Returns 0 if no route distribution with the given name exists.
268 : *
269 : * @param[in] id the id of the route distribution
270 : * @return the route distribution
271 : */
272 : static RandomDistributor<ConstMSRoutePtr>* distDictionary(const std::string& id);
273 :
274 : /// Clears the dictionary (delete all known routes, too)
275 : static void clear();
276 :
277 : /// Checks the distribution whether it is permanent and deletes it if not
278 : static void checkDist(const std::string& id);
279 :
280 : static void insertIDs(std::vector<std::string>& into);
281 :
282 : private:
283 : /// The list of edges to pass
284 : ConstMSEdgeVector myEdges;
285 :
286 : /// whether the route may be deleted after the last vehicle abandoned it
287 : const bool myAmPermanent;
288 :
289 : /// The color
290 : const RGBColor* const myColor;
291 :
292 : /// The period when repeating this route
293 : SUMOTime myPeriod;
294 :
295 : /// @brief The assigned or calculated costs
296 : double myCosts;
297 :
298 : /// @brief The estimated savings when rerouting
299 : double mySavings;
300 :
301 : /// @brief Whether this route is incomplete and requires rerouting
302 : bool myReroute;
303 :
304 : /// @brief List of the stops on the parsed route
305 : std::vector<SUMOVehicleParameter::Stop> myStops;
306 :
307 : /// The time where this route was replaced with an alternative route (or -1)
308 : SUMOTime myReplacedTime;
309 :
310 : /// The index where this route was replaced with an alternative route
311 : int myReplacedIndex;
312 :
313 : private:
314 : /// Definition of the dictionary container
315 : typedef std::map<std::string, ConstMSRoutePtr> RouteDict;
316 :
317 : /// The dictionary container
318 : static RouteDict myDict;
319 :
320 : /// Definition of the dictionary container
321 : typedef std::map<std::string, std::pair<RandomDistributor<ConstMSRoutePtr>*, bool> > RouteDistDict;
322 :
323 : /// The dictionary container
324 : static RouteDistDict myDistDict;
325 :
326 : #ifdef HAVE_FOX
327 : /// @brief the mutex for the route dictionaries
328 : static FXMutex myDictMutex;
329 : #endif
330 : private:
331 : /** invalid assignment operator */
332 : MSRoute& operator=(const MSRoute& s);
333 :
334 : };
|