From bd475e72f494345d81c7748221d700e9e59b238c Mon Sep 17 00:00:00 2001
From: Romain BERNARD <romain.bernard@uca.fr>
Date: Wed, 19 Jun 2024 17:53:57 +0200
Subject: [PATCH] update request weights in insert/remove request methods

---
 src/routes/vehicle/SAEVRoute.cpp | 35 +++++++++++++++++++++++++++-----
 src/routes/vehicle/SAEVRoute.h   | 13 +++++++++++-
 2 files changed, 42 insertions(+), 6 deletions(-)

diff --git a/src/routes/vehicle/SAEVRoute.cpp b/src/routes/vehicle/SAEVRoute.cpp
index dccb0a4..20e23ce 100644
--- a/src/routes/vehicle/SAEVRoute.cpp
+++ b/src/routes/vehicle/SAEVRoute.cpp
@@ -67,11 +67,16 @@ void SAEVRoute::insertRequest(size_t requestId, SAEVKeyPoint &originRequestPrede
         originRequestPredecessorKP.setSuccessor(&originKp);
         originSuccKp->setPredecessor(&destinationKp);
     }
+    //Once insertion is done, update weights on the route
+    addRequestWeightToRoute(requestId);
 }
 
-void SAEVRoute::removeRequest(int requestId) {
-    SAEVKeyPoint& originKp = _route.at(requestId);
-    SAEVKeyPoint& destinationKp = _route.at(requestId + 1);
+void SAEVRoute::removeRequest(size_t requestId) {
+    //Before undoing the insertion, update weights on the route
+    addRequestWeightToRoute(requestId);
+
+    SAEVKeyPoint& originKp = getOrigin(requestId);
+    SAEVKeyPoint& destinationKp = getDestination(requestId);
 
     //get predecessor and successor for request
     SAEVKeyPoint* originPredecessor = originKp.getPredecessor();
@@ -92,8 +97,6 @@ void SAEVRoute::removeRequest(int requestId) {
     originKp.setMaxTw(_requestList->at(requestId).getMaxDepartureTw());
     destinationKp.setMinTw(_requestList->at(requestId).getMinArrivalTw());
     destinationKp.setMaxTw(_requestList->at(requestId).getMaxArrivalTw());
-    originKp.setCurrentOccupation(0);
-    destinationKp.setCurrentOccupation(0);
 }
 
 SAEVRouteChangelist
@@ -397,3 +400,25 @@ BestInsertionQueue SAEVRoute::getBestInsertionsQueue(size_t requestId, size_t ve
     return bestInsertionQueue;
 }
 
+void SAEVRoute::addRequestWeightToRoute(size_t requestId) {
+    SAEVKeyPoint& currentKP = getOrigin(requestId);
+    int requestWeight = currentKP.getRequest()->getWeight();
+    currentKP.setCurrentOccupation(currentKP.getPredecessor()->getCurrentOccupation() + requestWeight); //O.Weight = Prec(O).Weight + R.Weight (request enters the vehicle=)
+    do {
+        currentKP = *currentKP.getSuccessor();
+        currentKP.setCurrentOccupation(currentKP.getCurrentOccupation() + requestWeight);
+    } while (currentKP != getDestination(requestId));
+    currentKP.setCurrentOccupation(currentKP.getPredecessor()->getCurrentOccupation() - requestWeight); //D.Weight = Prec(D).Weight - R.Weight (request leaves the vehicle)
+}
+
+void SAEVRoute::removeRequestWeightFromRoute(size_t requestId) {
+    SAEVKeyPoint& currentKP = getOrigin(requestId);
+    int requestWeight = currentKP.getRequest()->getWeight();
+    currentKP.setCurrentOccupation(0); //reset request weight on origin KP
+    do {
+        currentKP = *currentKP.getSuccessor();
+        currentKP.setCurrentOccupation(currentKP.getCurrentOccupation() - requestWeight);
+    } while (currentKP != getDestination(requestId));
+    currentKP.setCurrentOccupation(0); //reset request weight on destination KP
+}
+
diff --git a/src/routes/vehicle/SAEVRoute.h b/src/routes/vehicle/SAEVRoute.h
index d9ebee9..d879a3f 100644
--- a/src/routes/vehicle/SAEVRoute.h
+++ b/src/routes/vehicle/SAEVRoute.h
@@ -44,7 +44,18 @@ public:
      * Raw request removal method. Public for debug purposes, but should effectively never be called by an outside member
      * @param requestId index of the request we want to remove from the route
      */
-    void removeRequest(int requestId);
+    void removeRequest(size_t requestId);
+
+    /**
+     * Updates weight in-between request's Origin/Destination (both included) to account for the given request's weight
+     * @param requestId ID of the request that serves as basis for iteration and weight to add
+     */
+    void addRequestWeightToRoute(size_t requestId);
+    /**
+     * Resets current weight to 0 on the request Origin/Destination key points and decreases weight on the nodes in-between by the request's weight
+     * @param requestId ID of the request that serves as basis for iteration and weight to remove
+     */
+    void removeRequestWeightFromRoute(size_t requestId);
 
     /**
      * Tries to insert the request origin and destination next to the given origin/destination predecessors. \n \n
-- 
GitLab