1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2007-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
10 /// @file    MSDevice_Routing.h
11 /// @author  Michael Behrisch
12 /// @author  Daniel Krajzewicz
13 /// @author  Jakob Erdmann
14 /// @date    Tue, 04 Dec 2007
15 /// @version $Id$
16 ///
17 // A device that performs vehicle rerouting based on current edge speeds
18 /****************************************************************************/
19 #ifndef MSDevice_Routing_h
20 #define MSDevice_Routing_h
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #include <config.h>
27 
28 #include <set>
29 #include <vector>
30 #include <map>
31 #include <utils/common/SUMOTime.h>
32 #include <utils/common/WrappingCommand.h>
33 #include <microsim/MSVehicle.h>
34 #include "MSVehicleDevice.h"
35 
36 
37 // ===========================================================================
38 // class declarations
39 // ===========================================================================
40 class MSLane;
41 
42 
43 // ===========================================================================
44 // class definitions
45 // ===========================================================================
46 /**
47  * @class MSDevice_Routing
48  * @brief A device that performs vehicle rerouting based on current edge speeds
49  *
50  * The routing-device system consists of in-vehicle devices that perform the routing
51  *  and simulation-wide static methods for collecting edge weights and
52  *  parallelizing in MSRoutingEngine.
53  *
54  * A device is assigned to a vehicle using the common explicit/probability - procedure.
55  *
56  * A device computes a new route for a vehicle as soon as the vehicle is inserted
57  *  (within "enterLaneAtInsertion") - and, if the given period is larger than 0 - each
58  *  x time steps where x is the period. This is triggered by an event that executes
59  *  "wrappedRerouteCommandExecute".
60  */
61 class MSDevice_Routing : public MSVehicleDevice {
62 public:
63     /** @brief Inserts MSDevice_Routing-options
64      * @param[filled] oc The options container to add the options to
65      */
66     static void insertOptions(OptionsCont& oc);
67 
68     /** @brief checks MSDevice_Routing-options
69      * @param[filled] oc The options container with the user-defined options
70      */
71     static bool checkOptions(OptionsCont& oc);
72 
73 
74     /** @brief Build devices for the given vehicle, if needed
75      *
76      * The options are read and evaluated whether rerouting-devices shall be built
77      *  for the given vehicle.
78      *
79      * When the first device is built, the static container of edge weights
80      *  used for routing is initialised with the mean speed the edges allow.
81      *  In addition, an event is generated which updates these weights is
82      *  built and added to the list of events to execute at a simulation end.
83      *
84      * The built device is stored in the given vector.
85      *
86      * @param[in] v The vehicle for which a device may be built
87      * @param[filled] into The vector to store the built device in
88      */
89     static void buildVehicleDevices(SUMOVehicle& v, std::vector<MSVehicleDevice*>& into);
90 
91 
92     /// @brief Destructor.
93     ~MSDevice_Routing();
94 
95 
96 
97     /// @name Methods called on vehicle movement / state change, overwriting MSDevice
98     /// @{
99 
100     /** @brief Computes a new route on vehicle insertion
101      *
102      * A new route is computed by calling the vehicle's "reroute" method, supplying
103      *  "getEffort" as the edge effort retrieval method.
104      *
105      * If the reroute period is larger than 0, an event is generated and added
106      *  to the list of simulation step begin events which executes
107      *  "wrappedRerouteCommandExecute".
108      *
109      * @param[in] veh The entering vehicle.
110      * @param[in] reason how the vehicle enters the lane
111      * @return Always false
112      * @see MSMoveReminder::notifyEnter
113      * @see MSMoveReminder::Notification
114      * @see MSVehicle::reroute
115      * @see MSEventHandler
116      * @see WrappingCommand
117      */
118     bool notifyEnter(SUMOTrafficObject& veh, MSMoveReminder::Notification reason, const MSLane* enteredLane = 0);
119     /// @}
120 
121     /// @brief return the name for this type of device
deviceName()122     const std::string deviceName() const {
123         return "rerouting";
124     }
125 
126     /** @brief Saves the state of the device
127      *
128      * @param[in] out The OutputDevice to write the information into
129      */
130     void saveState(OutputDevice& out) const;
131 
132     /** @brief Loads the state of the device from the given description
133      *
134      * @param[in] attrs XML attributes describing the current state
135      */
136     void loadState(const SUMOSAXAttributes& attrs);
137 
138     /// @brief initiate the rerouting, create router / thread pool on first use
139     void reroute(const SUMOTime currentTime, const bool onInit = false);
140 
141 
142     /** @brief Labels the current time step as "unroutable".
143      *
144      * Sets mySkipRouting to the current time in order to skip rerouting.
145      * This is useful for pre insertion routing when we know in advance
146      * we cannot insert.
147      *
148      * @param[in] currentTime The current simulation time
149      */
skipRouting(const SUMOTime currentTime)150     void skipRouting(const SUMOTime currentTime) {
151         mySkipRouting = currentTime;
152     }
153 
getPeriod()154     SUMOTime getPeriod() const {
155         return myPeriod;
156     }
157 
158     /// @brief try to retrieve the given parameter from this device. Throw exception for unsupported key
159     std::string getParameter(const std::string& key) const;
160 
161     /// @brief try to set the given parameter for this device. Throw exception for unsupported key
162     void setParameter(const std::string& key, const std::string& value);
163 
164 
165 private:
166 
167     /** @brief Constructor
168      *
169      * @param[in] holder The vehicle that holds this device
170      * @param[in] id The ID of the device
171      * @param[in] period The period with which a new route shall be searched
172      * @param[in] preInsertionPeriod The route search period before insertion
173      */
174     MSDevice_Routing(SUMOVehicle& holder, const std::string& id, SUMOTime period, SUMOTime preInsertionPeriod);
175 
176     /** @brief Performs rerouting before insertion into the network
177      *
178      * A new route is computed by calling the reroute method. If the routing
179      *  involves taz the internal route cache is asked beforehand.
180      *
181      * @param[in] currentTime The current simulation time
182      * @return The offset to the next call (the rerouting period "myPreInsertionPeriod")
183      * @see MSVehicle::reroute
184      * @see MSEventHandler
185      * @see WrappingCommand
186      */
187     SUMOTime preInsertionReroute(const SUMOTime currentTime);
188 
189     /** @brief Performs rerouting after a period
190      *
191      * A new route is computed by calling the vehicle's "reroute" method, supplying
192      *  "getEffort" as the edge effort retrieval method.
193      *
194      * This method is called from the event handler at the begin of a simulation
195      *  step after the rerouting period is over. The reroute period is returned.
196      *
197      * @param[in] currentTime The current simulation time
198      * @return The offset to the next call (the rerouting period "myPeriod")
199      * @see MSVehicle::reroute
200      * @see MSEventHandler
201      * @see WrappingCommand
202      */
203     SUMOTime wrappedRerouteCommandExecute(SUMOTime currentTime);
204 
205 
206 private:
207     /// @brief The period with which a vehicle shall be rerouted
208     SUMOTime myPeriod;
209 
210     /// @brief The period with which a vehicle shall be rerouted before insertion
211     SUMOTime myPreInsertionPeriod;
212 
213     /// @brief The last time a routing took place
214     SUMOTime myLastRouting;
215 
216     /// @brief The time for which routing may be skipped because we cannot be inserted
217     SUMOTime mySkipRouting;
218 
219     /// @brief The (optional) command responsible for rerouting
220     WrappingCommand< MSDevice_Routing >* myRerouteCommand;
221 
222 private:
223     /// @brief Invalidated copy constructor.
224     MSDevice_Routing(const MSDevice_Routing&);
225 
226     /// @brief Invalidated assignment operator.
227     MSDevice_Routing& operator=(const MSDevice_Routing&);
228 
229 
230 };
231 
232 
233 #endif
234 
235 /****************************************************************************/
236 
237