LCOV - code coverage report
Current view: top level - src/microsim/devices - MSDispatch_TraCI.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 42 49 85.7 %
Date: 2024-04-30 15:40:33 Functions: 5 5 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_TraCI.cpp
      15             : /// @author  Jakob Erdmann
      16             : /// @date    16.12.2019
      17             : ///
      18             : // An algorithm that performs dispatch for the taxi device
      19             : /****************************************************************************/
      20             : #include <config.h>
      21             : 
      22             : #include <limits>
      23             : #include <microsim/transportables/MSTransportable.h>
      24             : #include "MSDispatch_TraCI.h"
      25             : 
      26             : //#define DEBUG_RESERVATION
      27             : //#define DEBUG_DISPATCH
      28             : //#define DEBUG_SERVABLE
      29             : //#define DEBUG_TRAVELTIME
      30             : //#define DEBUG_DETOUR
      31             : //#define DEBUG_COND2(obj) (obj->getID() == "p0")
      32             : #define DEBUG_COND2(obj) (true)
      33             : 
      34             : // ===========================================================================
      35             : // MSDispatch_TraCI methods
      36             : // ===========================================================================
      37             : 
      38             : Reservation*
      39         551 : MSDispatch_TraCI::addReservation(MSTransportable* person,
      40             :                                  SUMOTime reservationTime,
      41             :                                  SUMOTime pickupTime,
      42             :                                  SUMOTime earliestPickupTime,
      43             :                                  const MSEdge* from, double fromPos,
      44             :                                  const MSStoppingPlace* fromStop,
      45             :                                  const MSEdge* to, double toPos,
      46             :                                  const MSStoppingPlace* toStop,
      47             :                                  std::string group,
      48             :                                  const std::string& line,
      49             :                                  int maxCapacity,
      50             :                                  int maxContainerCapacity) {
      51        1102 :     Reservation* res = MSDispatch::addReservation(person, reservationTime, pickupTime, earliestPickupTime, from, fromPos, fromStop, to, toPos, toStop, group, line, maxCapacity, maxContainerCapacity);
      52         551 :     if (!myReservationLookup.has(res)) {
      53        1066 :         myReservationLookup.insert(res->id, res);
      54             :     }
      55         551 :     return res;
      56             : }
      57             : 
      58             : std::string
      59         113 : MSDispatch_TraCI::removeReservation(MSTransportable* person,
      60             :                                     const MSEdge* from, double fromPos,
      61             :                                     const MSEdge* to, double toPos,
      62             :                                     std::string group) {
      63         226 :     const std::string removedID = MSDispatch::removeReservation(person, from, fromPos, to, toPos, group);
      64         113 :     if (myReservationLookup.hasString(removedID)) {
      65             :         // warning! res is already deleted
      66           0 :         const Reservation* res = myReservationLookup.get(removedID);
      67           0 :         myReservationLookup.remove(removedID, res);
      68             :     }
      69         113 :     return removedID;
      70             : }
      71             : 
      72             : 
      73             : void
      74         487 : MSDispatch_TraCI::fulfilledReservation(const Reservation* res) {
      75         487 :     myReservationLookup.remove(res->id, res);
      76         487 :     MSDispatch::fulfilledReservation(res);
      77         487 : }
      78             : 
      79             : void
      80         285 : MSDispatch_TraCI::interpretDispatch(MSDevice_Taxi* taxi, const std::vector<std::string>& reservationsIDs) {
      81             :     std::vector<const Reservation*> reservations;
      82        1354 :     for (std::string resID : reservationsIDs) {
      83        1069 :         if (myReservationLookup.hasString(resID)) {
      84        1069 :             reservations.push_back(myReservationLookup.get(resID));
      85             :         } else {
      86           0 :             throw InvalidArgument("Reservation id '" + resID + "' is not known");
      87             :         }
      88             :     }
      89             :     try {
      90         285 :         if (reservations.size() == 1) {
      91          93 :             taxi->dispatch(*reservations.front());
      92             :         } else {
      93         379 :             taxi->dispatchShared(reservations);
      94             :         }
      95           5 :     } catch (ProcessError& e) {
      96          10 :         throw InvalidArgument(e.what());
      97           5 :     }
      98             :     // in case of ride sharing the same reservation may occur multiple times
      99         280 :     std::set<const Reservation*> unique(reservations.begin(), reservations.end());
     100         881 :     for (const Reservation* res : unique) {
     101         601 :         servedReservation(res);
     102             :     }
     103         280 : }
     104             : 
     105             : 
     106             : std::string
     107           6 : MSDispatch_TraCI::splitReservation(std::string resID, std::vector<std::string> personIDs) {
     108           6 :     if (myReservationLookup.hasString(resID)) {
     109           6 :         Reservation* res = const_cast<Reservation*>(myReservationLookup.get(resID));
     110             :         if (myRunningReservations.count(res) != 0) {
     111           0 :             throw InvalidArgument("Cannot split reservation '" + resID + "' after dispatch");
     112             :         }
     113             :         std::set<std::string> allPersons;
     114          30 :         for (const MSTransportable* t : res->persons) {
     115             :             allPersons.insert(t->getID());
     116             :         }
     117          18 :         for (std::string p : personIDs) {
     118             :             if (allPersons.count(p) == 0) {
     119           0 :                 throw InvalidArgument("Person '" + p + "' is not part of reservation '" + resID + "'");
     120             :             }
     121             :         }
     122           6 :         if (personIDs.size() == allPersons.size()) {
     123           0 :             throw InvalidArgument("Cannot remove all person from reservation '" + resID + "'");
     124             :         }
     125             :         std::vector<const MSTransportable*> split;
     126          18 :         for (const std::string& p : personIDs) {
     127          13 :             for (const MSTransportable* const t : res->persons) {
     128          13 :                 if (t->getID() == p) {
     129             :                     res->persons.erase(t);
     130          12 :                     split.push_back(t);
     131             :                     break;
     132             :                 }
     133             :             }
     134             :         }
     135           6 :         Reservation* newRes = new Reservation(toString(myReservationCount++), split,
     136             :                                               res->reservationTime, res->pickupTime,
     137             :                                               res->earliestPickupTime,
     138             :                                               res->from, res->fromPos, res->fromStop,
     139             :                                               res->to, res->toPos, res->toStop,
     140           6 :                                               res->group, res->line);
     141           6 :         myGroupReservations[res->group].push_back(newRes);
     142          18 :         myReservationLookup.insert(newRes->id, newRes);
     143           6 :         return newRes->id;
     144             :     } else {
     145           0 :         throw InvalidArgument("Reservation id '" + resID + "' is not known");
     146             :     }
     147             : }
     148             : 
     149             : 
     150             : 
     151             : //
     152             : /****************************************************************************/

Generated by: LCOV version 1.14