LCOV - code coverage report
Current view: top level - src/microsim/devices - MSDispatch.h (source / functions) Coverage Total Hit
Test: lcov.info Lines: 100.0 % 21 21
Test Date: 2026-03-02 16:00:03 Functions: 100.0 % 1 1

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2007-2026 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    MSDispatch.h
      15              : /// @author  Jakob Erdmann
      16              : /// @date    16.12.2019
      17              : ///
      18              : // An algorithm that performs dispatch for the taxi device
      19              : /****************************************************************************/
      20              : #pragma once
      21              : #include <config.h>
      22              : 
      23              : #include <set>
      24              : #include <vector>
      25              : #include <map>
      26              : #include <utils/common/Parameterised.h>
      27              : #include <utils/common/SUMOTime.h>
      28              : #include "MSDevice_Taxi.h"
      29              : 
      30              : // ===========================================================================
      31              : // class declarations
      32              : // ===========================================================================
      33              : class MSTransportable;
      34              : class MSStoppingPlace;
      35              : 
      36              : // ===========================================================================
      37              : // class definitions
      38              : // ===========================================================================
      39              : struct Reservation {
      40              :     enum ReservationState {
      41              :         NEW = 1, // new reservation (not yet retrieved)
      42              :         RETRIEVED = 2, // retrieved at least once via MSDispatch_TraCI
      43              :         ASSIGNED = 4, // a taxi was dispatched to service this reservation
      44              :         ONBOARD = 8, // a taxi has picked up the persons belonging to this reservation
      45              :         FULFILLED = 16, // the persons belonging to this reservation have been dropped off
      46              :     };
      47              : 
      48         2676 :     Reservation(const std::string& _id,
      49              :                 const std::vector<const MSTransportable*>& _persons,
      50              :                 SUMOTime _reservationTime,
      51              :                 SUMOTime _pickupTime,
      52              :                 SUMOTime _earliestPickupTime,
      53              :                 const MSEdge* _from, double _fromPos,
      54              :                 const MSStoppingPlace* _fromStop,
      55              :                 const MSEdge* _to, double _toPos,
      56              :                 const MSStoppingPlace* _toStop,
      57              :                 const std::string& _group,
      58         2676 :                 const std::string& _line) :
      59         2676 :         id(_id),
      60         2676 :         persons(_persons.begin(), _persons.end()),
      61         2676 :         reservationTime(_reservationTime),
      62         2676 :         pickupTime(_pickupTime),
      63         2676 :         earliestPickupTime(_earliestPickupTime),
      64         2676 :         from(_from),
      65         2676 :         fromPos(_fromPos),
      66         2676 :         fromStop(_fromStop),
      67         2676 :         to(_to),
      68         2676 :         toPos(_toPos),
      69         2676 :         toStop(_toStop),
      70         2676 :         group(_group),
      71         2676 :         line(_line),
      72         2676 :         recheck(_reservationTime),
      73         2676 :         state(NEW)
      74         2676 :     {}
      75              : 
      76              :     std::string id;
      77              :     std::set<const MSTransportable*> persons;
      78              :     SUMOTime reservationTime;
      79              :     SUMOTime pickupTime;
      80              :     SUMOTime earliestPickupTime;
      81              :     const MSEdge* from;
      82              :     double fromPos;
      83              :     const MSStoppingPlace* fromStop;
      84              :     const MSEdge* to;
      85              :     double toPos;
      86              :     const MSStoppingPlace* toStop;
      87              :     std::string group;
      88              :     std::string line;
      89              :     SUMOTime recheck;
      90              :     ReservationState state;
      91              : 
      92              :     bool operator==(const Reservation& other) const {
      93              :         return persons == other.persons
      94              :                && reservationTime == other.reservationTime
      95              :                && pickupTime == other.pickupTime
      96              :                && from == other.from
      97              :                && fromPos == other.fromPos
      98              :                && to == other.to
      99              :                && toPos == other.toPos
     100              :                && group == other.group
     101              :                && line == other.line;
     102              :     }
     103              : 
     104              :     /// @brief for sorting by id
     105              :     std::string getID() const {
     106         4701 :         return id;
     107              :     }
     108              : };
     109              : 
     110              : /**
     111              :  * @class MSDispatch
     112              :  * @brief An algorithm that performs distpach for a taxi fleet
     113              :  */
     114              : class MSDispatch : public Parameterised {
     115              : public:
     116              : 
     117              :     /// @brief sorts reservations by time
     118              :     class time_sorter {
     119              :     public:
     120              :         /// @brief Constructor
     121              :         explicit time_sorter() {}
     122              : 
     123              :         /// @brief Comparing operator
     124              :         int operator()(const Reservation* r1, const Reservation* r2) const {
     125        91758 :             return MAX2(r1->reservationTime, r1->earliestPickupTime) < MAX2(r2->reservationTime, r2->earliestPickupTime);
     126              :         }
     127              :     };
     128              : 
     129              :     /// @brief Constructor;
     130              :     MSDispatch(const Parameterised::Map& params);
     131              : 
     132              :     /// @brief Destructor
     133              :     virtual ~MSDispatch();
     134              : 
     135              :     /// @brief add a new reservation
     136              :     virtual Reservation* addReservation(MSTransportable* person,
     137              :                                         SUMOTime reservationTime,
     138              :                                         SUMOTime pickupTime,
     139              :                                         SUMOTime earliestPickupTime,
     140              :                                         const MSEdge* from, double fromPos,
     141              :                                         const MSStoppingPlace* fromStop,
     142              :                                         const MSEdge* to, double toPos,
     143              :                                         const MSStoppingPlace* tostop,
     144              :                                         std::string group,
     145              :                                         const std::string& line,
     146              :                                         int maxCapacity,
     147              :                                         int maxContainerCapacity);
     148              : 
     149              :     /// @brief remove person from reservation. If the whole reservation is removed, return its id
     150              :     virtual std::string removeReservation(MSTransportable* person,
     151              :                                           const MSEdge* from, double fromPos,
     152              :                                           const MSEdge* to, double toPos,
     153              :                                           std::string group);
     154              : 
     155              :     /// @brief update fromPos of the person's reservation.
     156              :     /// TODO: if there is already a reservation with the newFromPos, add to this reservation
     157              :     /// TODO: if there are other persons in this reservation, create a new reservation for the updated one
     158              :     virtual Reservation* updateReservationFromPos(MSTransportable* person,
     159              :             const MSEdge* from, double fromPos,
     160              :             const MSEdge* to, double toPos,
     161              :             std::string group, double newFromPos);
     162              : 
     163              :     /// @brief erase reservation from storage
     164              :     virtual void fulfilledReservation(const Reservation* res);
     165              : 
     166              :     /// @brief computes dispatch and updates reservations
     167              :     virtual void computeDispatch(SUMOTime now, const std::vector<MSDevice_Taxi*>& fleet) = 0;
     168              : 
     169              :     /// @brief retrieve all reservations
     170              :     std::vector<Reservation*> getReservations();
     171              : 
     172              :     /// @brief retrieve all reservations that were already dispatched and are still active
     173              :     virtual std::vector<const Reservation*> getRunningReservations();
     174              : 
     175              :     /// @brief check whether there are still (servable) reservations in the system
     176              :     bool hasServableReservations() {
     177        76413 :         return myHasServableReservations;
     178              :     }
     179              : 
     180              :     virtual SUMOAbstractRouter<MSEdge, SUMOVehicle>& getRouter() const;
     181              : 
     182              :     ///@brief compute time to pick up the given reservation
     183              :     static SUMOTime computePickupTime(SUMOTime t, const MSDevice_Taxi* taxi, const Reservation& res, SUMOAbstractRouter<MSEdge, SUMOVehicle>& router);
     184              : 
     185              :     ///@brief compute whether the reservation is servable
     186              :     bool isReachable(SUMOTime t, const MSDevice_Taxi* taxi, const Reservation& res, SUMOAbstractRouter<MSEdge, SUMOVehicle>& router);
     187              : 
     188              :     ///@brief compute directTime and detourTime
     189              :     static double computeDetourTime(SUMOTime t, SUMOTime viaTime, const MSDevice_Taxi* taxi,
     190              :                                     const MSEdge* from, double fromPos,
     191              :                                     const MSEdge* via, double viaPos,
     192              :                                     const MSEdge* to, double toPos,
     193              :                                     SUMOAbstractRouter<MSEdge, SUMOVehicle>& router,
     194              :                                     double& timeDirect) ;
     195              : 
     196              : 
     197              :     /// @brief whether the last call to computeDispatch has left servable reservations
     198              :     bool myHasServableReservations = false;
     199              : 
     200              :     void swappedRunning(const Reservation* res, MSDevice_Taxi* taxi);
     201              : 
     202              : protected:
     203              :     void servedReservation(const Reservation* res, MSDevice_Taxi* taxi);
     204              : 
     205              :     /// @brief whether the given taxi has sufficient capacity to serve the reservation
     206              :     int remainingCapacity(const MSDevice_Taxi* taxi, const Reservation* res);
     207              : 
     208              :     // reservations that are currently being served (could still be used during re-dispatch)
     209              :     std::map<std::string, std::map<const Reservation*, MSDevice_Taxi*, ComparatorIdLess> > myRunningReservations;
     210              : 
     211              :     /// @brief optional file output for dispatch information
     212              :     OutputDevice* myOutput;
     213              : 
     214              :     int myReservationCount;
     215              : 
     216              :     /// @brief the duration before canceling unreachable reservations
     217              :     SUMOTime myKeepUnreachableResTime;
     218              : 
     219              :     std::map<std::string, std::vector<Reservation*> > myGroupReservations;
     220              : 
     221              :     /// @brief which router/edge weights to use
     222              :     const int myRoutingMode;
     223              : 
     224              : };
        

Generated by: LCOV version 2.0-1