LCOV - code coverage report
Current view: top level - src/microsim/devices - MSDispatch.h (source / functions) Hit Total Coverage
Test: lcov.info Lines: 20 20 100.0 %
Date: 2024-04-29 15:38:36 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /****************************************************************************/
       2             : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3             : // Copyright (C) 2007-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    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        2849 :     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        2849 :                 const std::string& _line) :
      59        2849 :         id(_id),
      60        2849 :         persons(_persons.begin(), _persons.end()),
      61        2849 :         reservationTime(_reservationTime),
      62        2849 :         pickupTime(_pickupTime),
      63        2849 :         earliestPickupTime(_earliestPickupTime),
      64        2849 :         from(_from),
      65        2849 :         fromPos(_fromPos),
      66        2849 :         fromStop(_fromStop),
      67        2849 :         to(_to),
      68        2849 :         toPos(_toPos),
      69        2849 :         toStop(_toStop),
      70        2849 :         group(_group),
      71        2849 :         line(_line),
      72        2849 :         recheck(_reservationTime),
      73        2849 :         state(NEW)
      74        2849 :     {}
      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 debug identification
     105             :     std::string getID() const;
     106             : };
     107             : 
     108             : /**
     109             :  * @class MSDispatch
     110             :  * @brief An algorithm that performs distpach for a taxi fleet
     111             :  */
     112             : class MSDispatch : public Parameterised {
     113             : public:
     114             : 
     115             :     /// @brief sorts reservations by time
     116             :     class time_sorter {
     117             :     public:
     118             :         /// @brief Constructor
     119             :         explicit time_sorter() {}
     120             : 
     121             :         /// @brief Comparing operator
     122             :         int operator()(const Reservation* r1, const Reservation* r2) const {
     123      175771 :             return MAX2(r1->reservationTime, r1->earliestPickupTime) < MAX2(r2->reservationTime, r2->earliestPickupTime);
     124             :         }
     125             :     };
     126             : 
     127             :     /// @brief Constructor;
     128             :     MSDispatch(const Parameterised::Map& params);
     129             : 
     130             :     /// @brief Destructor
     131             :     virtual ~MSDispatch();
     132             : 
     133             :     /// @brief add a new reservation
     134             :     virtual Reservation* addReservation(MSTransportable* person,
     135             :                                         SUMOTime reservationTime,
     136             :                                         SUMOTime pickupTime,
     137             :                                         SUMOTime earliestPickupTime,
     138             :                                         const MSEdge* from, double fromPos,
     139             :                                         const MSStoppingPlace* fromStop,
     140             :                                         const MSEdge* to, double toPos,
     141             :                                         const MSStoppingPlace* tostop,
     142             :                                         std::string group,
     143             :                                         const std::string& line,
     144             :                                         int maxCapacity,
     145             :                                         int maxContainerCapacity);
     146             : 
     147             :     /// @brief remove person from reservation. If the whole reservation is removed, return its id
     148             :     virtual std::string removeReservation(MSTransportable* person,
     149             :                                           const MSEdge* from, double fromPos,
     150             :                                           const MSEdge* to, double toPos,
     151             :                                           std::string group);
     152             : 
     153             :     /// @brief update fromPos of the person's reservation.
     154             :     /// TODO: if there is already a reservation with the newFromPos, add to this reservation
     155             :     /// TODO: if there are other persons in this reservation, create a new reservation for the updated one
     156             :     virtual Reservation* updateReservationFromPos(MSTransportable* person,
     157             :             const MSEdge* from, double fromPos,
     158             :             const MSEdge* to, double toPos,
     159             :             std::string group, double newFromPos);
     160             : 
     161             :     /// @brief erase reservation from storage
     162             :     virtual void fulfilledReservation(const Reservation* res);
     163             : 
     164             :     /// @brief computes dispatch and updates reservations
     165             :     virtual void computeDispatch(SUMOTime now, const std::vector<MSDevice_Taxi*>& fleet) = 0;
     166             : 
     167             :     /// @brief retrieve all reservations
     168             :     std::vector<Reservation*> getReservations();
     169             : 
     170             :     /// @brief retrieve all reservations that were already dispatched and are still active
     171             :     virtual std::vector<const Reservation*> getRunningReservations();
     172             : 
     173             :     /// @brief check whether there are still (servable) reservations in the system
     174             :     bool hasServableReservations() {
     175        4900 :         return myHasServableReservations;
     176             :     }
     177             : 
     178             :     ///@brief compute time to pick up the given reservation
     179             :     static SUMOTime computePickupTime(SUMOTime t, const MSDevice_Taxi* taxi, const Reservation& res, SUMOAbstractRouter<MSEdge, SUMOVehicle>& router);
     180             : 
     181             :     ///@brief compute directTime and detourTime
     182             :     static double computeDetourTime(SUMOTime t, SUMOTime viaTime, const MSDevice_Taxi* taxi,
     183             :                                     const MSEdge* from, double fromPos,
     184             :                                     const MSEdge* via, double viaPos,
     185             :                                     const MSEdge* to, double toPos,
     186             :                                     SUMOAbstractRouter<MSEdge, SUMOVehicle>& router,
     187             :                                     double& timeDirect) ;
     188             : 
     189             : 
     190             :     /// @brief whether the last call to computeDispatch has left servable reservations
     191             :     bool myHasServableReservations = false;
     192             : 
     193             : protected:
     194             :     void servedReservation(const Reservation* res);
     195             : 
     196             :     /// @brief whether the given taxi has sufficient capacity to serve the reservation
     197             :     int remainingCapacity(const MSDevice_Taxi* taxi, const Reservation* res);
     198             : 
     199             :     // reservations that are currently being served (could still be used during re-dispatch)
     200             :     std::set<const Reservation*> myRunningReservations;
     201             : 
     202             :     /// @brief optional file output for dispatch information
     203             :     OutputDevice* myOutput;
     204             : 
     205             :     int myReservationCount;
     206             :     std::map<std::string, std::vector<Reservation*> > myGroupReservations;
     207             : 
     208             : };

Generated by: LCOV version 1.14