From 815eef9f956387a26f8276d7ffa425d41a9cf377 Mon Sep 17 00:00:00 2001
From: Romain BERNARD <romain.bernard@uca.fr>
Date: Thu, 20 Jun 2024 17:40:09 +0200
Subject: [PATCH] update active vehicle id after insertion TODO: find a more
 elegant way to do this, because it makes the Heuristic(s) responsible for
 updating a member of SAEVRoute I could make it a member of
 BestInsertionHeuristic but then that means we have to instanciate an object
 to use the algorithms which seems inappropriate. Unless maybe if Heuristics
 classes ends up being delegate members of another class to allow
 heuristics/algorithms change via this delegate

---
 src/algorithm/DARP/Heuristics/BestInsertionHeuristic.cpp | 8 ++++++++
 src/routes/vehicle/SAEVRoute.h                           | 4 ++++
 2 files changed, 12 insertions(+)

diff --git a/src/algorithm/DARP/Heuristics/BestInsertionHeuristic.cpp b/src/algorithm/DARP/Heuristics/BestInsertionHeuristic.cpp
index 9293f0d..094abcb 100644
--- a/src/algorithm/DARP/Heuristics/BestInsertionHeuristic.cpp
+++ b/src/algorithm/DARP/Heuristics/BestInsertionHeuristic.cpp
@@ -35,8 +35,16 @@ bool BestInsertionHeuristic::tryVehicleBestInsertion(size_t requestId, size_t ve
 size_t BestInsertionHeuristic::doBestRequestInsertionForRoute(size_t requestId, SAEVRoute route) {
     size_t vehicleId = 0;
     bool insertionSuccess{false};
+    //Iteratively try inserting in every active vehicle and the first inactive vehicle
     while(vehicleId <= route.getLastActiveVehicleId() + 1 && !insertionSuccess) {
         insertionSuccess = tryVehicleBestInsertion(requestId, vehicleId, route);
     }
+
+    //Update last active vehicle ID
+    if(vehicleId > route.getLastActiveVehicleId()) {
+        route.setLastActiveVehicleId(vehicleId);
+        DEBUG_MSG("NEW VEHICLE CREATED, ID :" + std::to_string(vehicleId));
+    }
+
     return vehicleId;
 }
diff --git a/src/routes/vehicle/SAEVRoute.h b/src/routes/vehicle/SAEVRoute.h
index 59232e5..394ed0f 100644
--- a/src/routes/vehicle/SAEVRoute.h
+++ b/src/routes/vehicle/SAEVRoute.h
@@ -18,6 +18,7 @@ private:
     size_t _nbRequest;
     const Graph* _graph;
     const std::vector<Request>* _requestList;
+    size_t _lastActiveVehicleId{0};
 public:
     SAEVRoute() = default;
     /**
@@ -123,6 +124,9 @@ public:
     [[nodiscard]] size_t getOriginDepotIdx(const size_t vehicleId) const { return _nbRequest*2 + vehicleId*2;}
     [[nodiscard]] size_t getDestinationDepotIdx(const size_t vehicleId) const { return _nbRequest*2 + vehicleId*2 + 1;}
 
+    [[nodiscard]] size_t getLastActiveVehicleId() const { return _lastActiveVehicleId; }
+    void setLastActiveVehicleId(size_t lastActiveVehicleId) { _lastActiveVehicleId = lastActiveVehicleId; }
+
     /**
      * Verifies that time windows have been properly propagated for a given vehicle's route
      * @param vehicleId
-- 
GitLab