From 801c85de5a6157f194f774780c072393f6456618 Mon Sep 17 00:00:00 2001
From: Romain BERNARD <romain.bernard@uca.fr>
Date: Thu, 29 Feb 2024 18:37:44 +0100
Subject: [PATCH] WIP refactor and debug after discussion with HT

---
 .../Transit/TransitAlgorithmState.h           | 37 +++++++++++--------
 .../Transit/TransitShortestPathPrecompute.cpp | 10 ++---
 .../Transit/TransitStateContainer.h           | 33 ++++++++++-------
 src/instance/graph/Line.h                     | 12 +++---
 src/instance/graph/LineStop.h                 |  1 -
 src/instance/graph/Node.cpp                   |  4 +-
 src/instance/graph/Node.h                     |  7 ++--
 7 files changed, 56 insertions(+), 48 deletions(-)

diff --git a/src/ShortestPath/Transit/TransitAlgorithmState.h b/src/ShortestPath/Transit/TransitAlgorithmState.h
index 6a6bf0f..26767ef 100644
--- a/src/ShortestPath/Transit/TransitAlgorithmState.h
+++ b/src/ShortestPath/Transit/TransitAlgorithmState.h
@@ -29,6 +29,7 @@ public:
         _nodeIndex = baseState.getNodeIndex();
         _instant = baseState.getInstant();
         _passageIndex = baseState.getPassageIndex();
+        _connections.reserve(2);
 
         //Copy old connections
         for(auto& lineStop : baseState.getConnections()) {
@@ -42,6 +43,7 @@ public:
         _nodeIndex = baseState.getNodeIndex();
         _instant = baseState.getInstant();
         _passageIndex = baseState.getPassageIndex();
+        _connections.reserve(2);
 
         //Copy old connections
         for(auto& lineStop : baseState.getConnections()) {
@@ -55,12 +57,14 @@ public:
         _nodeIndex = nodeIndex;
         _instant = INT16_MAX;
         _passageIndex = -1;
+        _connections.reserve(2);
     }
 
     explicit TransitAlgorithmState() {
         _nodeIndex = -1;
         _instant = INT16_MAX;
         _passageIndex = -1;
+        _connections.reserve(2);
     }
 
     [[nodiscard]] int getNodeIndex() const {
@@ -79,6 +83,10 @@ public:
         return _connections;
     }
 
+    [[nodiscard]] size_t getNbConnections() const {
+        return _connections.size();
+    }
+
     void setNodeIndex(int nodeIndex) {
         _nodeIndex = nodeIndex;
     }
@@ -115,9 +123,7 @@ public:
      * @return
      */
     [[nodiscard]] bool strictlyDominates(const TransitAlgorithmState& rhs) const {
-        return this->_nodeIndex == rhs.getNodeIndex() //same current node
-               && ((!this->getConnections().empty() && rhs.getConnections().empty())
-               || (this->getInstant() <= rhs.getInstant() && this->getConnections().size() <= rhs.getConnections().size()));
+        return this->getInstant() <= rhs.getInstant() && this->getConnections().size() <= rhs.getConnections().size();
     }
 
     /**
@@ -151,18 +157,19 @@ public:
                 || this->getConnections().size() != rhs.getConnections().size();
     }
 
-    TransitAlgorithmState& operator=(const TransitAlgorithmState& baseState) {
-        _nodeIndex = baseState.getNodeIndex();
-        _instant = baseState.getInstant();
-        _passageIndex = baseState.getPassageIndex();
-        //Copy old connections
-        _connections.clear(); //TODO: see with HT if there's a better way to do this for an assignment operator
-        for(auto& lineStop : baseState.getConnections()) {
-            _connections.emplace_back(lineStop);
-        }
-
-        return *this;
-    }
+    TransitAlgorithmState& operator=(const TransitAlgorithmState& baseState) = default;
+//    TransitAlgorithmState& operator=(const TransitAlgorithmState& baseState) {
+//        _nodeIndex = baseState.getNodeIndex();
+//        _instant = baseState.getInstant();
+//        _passageIndex = baseState.getPassageIndex();
+//        //Copy old connections
+//        _connections.clear();
+//        for(auto& lineStop : baseState.getConnections()) {
+//            _connections.emplace_back(lineStop);
+//        }
+//
+//        return *this;
+//    }
 
     [[nodiscard]] std::string toString() const {
         std::string res = "Node: " + std::to_string(_nodeIndex) + ", Instant: " + std::to_string(_instant);
diff --git a/src/ShortestPath/Transit/TransitShortestPathPrecompute.cpp b/src/ShortestPath/Transit/TransitShortestPathPrecompute.cpp
index 250a1a9..e5d5ed0 100644
--- a/src/ShortestPath/Transit/TransitShortestPathPrecompute.cpp
+++ b/src/ShortestPath/Transit/TransitShortestPathPrecompute.cpp
@@ -18,12 +18,8 @@
 //      - priority queue order
 //      - Reference value still valid after popping (otherwise, pop at the end, before adding new state)
 TransitStateContainer TransitShortestPathPrecompute::executeAlgorithm(const Graph& graph, int nodeIndex, int instant) {
-    TransitStateContainer solutionsContainer{};
-    solutionsContainer.resizeSolutionsVector(graph.getNodesVector().size());
-    for(int i = 0; i < graph.getNodesVector().size(); ++i) {
-        solutionsContainer.pushEmptyState(i);
-    }
-
+    //Init container, priority queue and state variables
+    TransitStateContainer solutionsContainer{graph.getNodesVector().size()};
     std::priority_queue<TransitAlgorithmState> statePriorityQueue;
     statePriorityQueue.emplace(nodeIndex, instant,0);
 
@@ -67,7 +63,7 @@ TransitStateContainer TransitShortestPathPrecompute::executeAlgorithm(const Grap
                     //Add new state to the solution container and the priority queue if it's not strictly dominated by an existing solution
                     if(!solutionsContainer.getBestSolution(currentState.getNodeIndex()).strictlyDominates(currentState)) {
                         DEBUG_MSG("Candidate state " + newState.toString() + " is being added to solution container and priority queue");
-                        solutionsContainer.tryAddNewState(nextNode, newState);
+                        solutionsContainer.addNewState(nextNode, newState);
                         statePriorityQueue.emplace(newState);
                     }
                 }
diff --git a/src/ShortestPath/Transit/TransitStateContainer.h b/src/ShortestPath/Transit/TransitStateContainer.h
index 2b8a88d..194f155 100644
--- a/src/ShortestPath/Transit/TransitStateContainer.h
+++ b/src/ShortestPath/Transit/TransitStateContainer.h
@@ -21,6 +21,22 @@ private:
     std::vector<std::vector<TransitAlgorithmState>>  solutionVector;
 
 public:
+    explicit TransitStateContainer(int size)
+    {
+        solutionVector.reserve(size);
+//        TransitAlgorithmState emptyState{}; FIXME
+//        for(size_t i = 0; i < size; ++i) {
+//            solutionVector.at(i).assign(2, TransitAlgorithmState(emptyState));
+//        }
+    }
+    explicit TransitStateContainer(size_t size)
+    {
+        solutionVector.reserve(size);
+//        TransitAlgorithmState emptyState{}; FIXME
+//        for(size_t i = 0; i < size; ++i) {
+//            solutionVector.at(i).assign(2, TransitAlgorithmState(emptyState));
+//        }
+    }
     /**
      * Returns the current best solution for the given node index
      * @param nodeIndex
@@ -37,22 +53,11 @@ public:
     void resizeSolutionsVector(int nbNodes){ solutionVector.resize(nbNodes);}
 
 
-    bool tryAddNewState(int nodeIndex, const TransitAlgorithmState& newState)
+    void addNewState(int nodeIndex, const TransitAlgorithmState& newState)
     {
         DEBUG_MSG("Trying to add state " + newState.toString());
-        if(!solutionVector.at(nodeIndex).empty() && newState.strictlyDominates(solutionVector.at(nodeIndex)[0])) {
-            DEBUG_MSG("Added state to position 0, replacing " + solutionVector.at(nodeIndex)[0].toString());
-            solutionVector.at(nodeIndex)[0] = newState;
-            return true;
-        } else if(solutionVector.at(nodeIndex).size() > 1 && newState.strictlyDominates(solutionVector.at(nodeIndex)[1])) {
-            DEBUG_MSG("Added state to position 1, replacing " + solutionVector.at(nodeIndex)[1].toString());
-            solutionVector.at(nodeIndex)[1] = newState;
-            return true;
-        } else {
-            DEBUG_MSG("State wasn't added to the container because it doesn't dominate \n" +
-                        solutionVector.at(nodeIndex)[0].toString() +
-                        (solutionVector.at(nodeIndex).size() > 1 ? "\nor " + solutionVector.at(nodeIndex)[1].toString() : ""));
-            return false;
+        if(newState.getNbConnections() > 0) {
+            solutionVector.at(nodeIndex)[newState.getNbConnections()] = newState;
         }
     }
 
diff --git a/src/instance/graph/Line.h b/src/instance/graph/Line.h
index c17a56f..54433fb 100644
--- a/src/instance/graph/Line.h
+++ b/src/instance/graph/Line.h
@@ -62,12 +62,12 @@ public:
         return _lineID != rhs.getLineId();
     }
 
-    Line& operator=(const Line &rhs) {
-        _lineID = rhs.getLineId();
-        _nodes = rhs.getNodes();
-        _timetables = rhs.getTimetables();
-        return *this;
-    }
+//    Line& operator=(const Line &rhs) {
+//        _lineID = rhs.getLineId();
+//        _nodes = rhs.getNodes();
+//        _timetables = rhs.getTimetables();
+//        return *this;
+//    }
 
 };
 
diff --git a/src/instance/graph/LineStop.h b/src/instance/graph/LineStop.h
index afcd980..5a1e195 100644
--- a/src/instance/graph/LineStop.h
+++ b/src/instance/graph/LineStop.h
@@ -16,7 +16,6 @@ private:
     int _stopIndex{}; //index for the stop relative to this node in the Line object
 
 public:
-    LineStop() = default;
     LineStop(const Line& lineRef, int stopIndex) : _lineRef(lineRef), _stopIndex(stopIndex) {}
 
     [[nodiscard]] Line getLineRef() const {
diff --git a/src/instance/graph/Node.cpp b/src/instance/graph/Node.cpp
index e8e42c5..6b6f7d9 100644
--- a/src/instance/graph/Node.cpp
+++ b/src/instance/graph/Node.cpp
@@ -17,14 +17,14 @@ bool Node::isPTNode() {
     return &_ptLines == nullptr || _ptLines.empty();
 }
 
-Node::Node(Status status, double x, double y) : _status(status), _x(x), _y(y), _ptLines(std::set<LineStop>()) {
+Node::Node(Status status, double x, double y) : _status(status), _x(x), _y(y), _ptLines(std::vector<LineStop>()) {
     this->_status = status;
     this->_x = x;
     this->_y = y;
 }
 
 void Node::addBusLine(const Line& line, int indexInLine) {
-    this->_ptLines.emplace(line, indexInLine);
+    this->_ptLines.emplace_back(line, indexInLine);
 }
 
 bool Node::operator==(const Node &rhs) const {
diff --git a/src/instance/graph/Node.h b/src/instance/graph/Node.h
index 757319b..9e6ee2c 100644
--- a/src/instance/graph/Node.h
+++ b/src/instance/graph/Node.h
@@ -21,7 +21,8 @@ enum Status {
     residential
 };
 
-static std::unordered_map<std::string,Status> const stringToStatusMap = {{"work",        Status::work}, {"leisure", Status::leisure},
+static std::unordered_map<std::string,Status> const stringToStatusMap = {{"work",        Status::work},
+                                                                         {"leisure",     Status::leisure},
                                                                          {"residential", Status::residential} };
 
 class Line;
@@ -31,7 +32,7 @@ private:
     Status _status;
     double _x;
     double _y;
-    std::set<LineStop> _ptLines;
+    std::vector<LineStop> _ptLines;
     std::vector<int> _incomingEdgesIndex; //List of edge index in the graph structure for all edges leading to this node
     std::vector<int> _outgoingEdgesIndex; //List of edge index in the graph structure for all edges leading to this node
     //TODO : Should these vectors be considered complete over the whole set of nodes ? Probably ? Considering we will probably pre-process shortest paths between all SAEV stations
@@ -73,7 +74,7 @@ public:
     [[nodiscard]] Status getStatus() const {return _status;}
     [[nodiscard]] std::vector<int> getIncomingEdges() const {return _incomingEdgesIndex;}
     [[nodiscard]] std::vector<int> getOutgoingEdges() const {return _outgoingEdgesIndex;}
-    [[nodiscard]] std::set<LineStop> getPTLinesSet() const {return _ptLines;}
+    [[nodiscard]] std::vector<LineStop> getPTLinesSet() const {return _ptLines;}
 
     /**
      * Verify if _x, _y and _status are equal to check for node equality
-- 
GitLab