From 7c76a64a82ca4f48bc7453c1084718a2a4da818e Mon Sep 17 00:00:00 2001 From: Romain BERNARD <romain.bernard@uca.fr> Date: Tue, 12 Mar 2024 18:08:30 +0100 Subject: [PATCH] add preceding node index to actually prevent turning back --- src/ShortestPath/Transit/TransitAlgorithmState.h | 11 +++++++++-- .../Transit/TransitShortestPathPrecompute.cpp | 7 ++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/ShortestPath/Transit/TransitAlgorithmState.h b/src/ShortestPath/Transit/TransitAlgorithmState.h index 0bfbd83..6a95a31 100644 --- a/src/ShortestPath/Transit/TransitAlgorithmState.h +++ b/src/ShortestPath/Transit/TransitAlgorithmState.h @@ -19,10 +19,11 @@ private: int _precedingNodeIndex; public: - TransitAlgorithmState(int currentNode, int currentInstant, int currentPassageIndex) { + TransitAlgorithmState(int currentNode, int currentInstant, int currentPassageIndex, int precedingNodeIndex) { _nodeIndex = currentNode; _instant = currentInstant; _passageIndex = currentPassageIndex; + _precedingNodeIndex = precedingNodeIndex; _connections.reserve(2); //TODO : replace constant max amount of connexions with a global parameter } @@ -62,6 +63,7 @@ public: _nodeIndex = nodeIndex; _instant = INT16_MAX; _passageIndex = -1; + _precedingNodeIndex = -1; _connections.reserve(2); } @@ -69,6 +71,7 @@ public: _nodeIndex = -1; _instant = INT16_MAX; _passageIndex = -1; + _precedingNodeIndex = -1; _connections.reserve(2); } @@ -109,7 +112,11 @@ public: } [[nodiscard]] int getPrecedingNodeIndex() const { - return _connections.back().getPrecedingNodeIndex(); + return _precedingNodeIndex; + } + + void setPrecedingNodeIndex(int nodeIndex) { + _precedingNodeIndex = nodeIndex; } [[nodiscard]] int getNextNodeIndex() const { diff --git a/src/ShortestPath/Transit/TransitShortestPathPrecompute.cpp b/src/ShortestPath/Transit/TransitShortestPathPrecompute.cpp index 543e70f..8ae226a 100644 --- a/src/ShortestPath/Transit/TransitShortestPathPrecompute.cpp +++ b/src/ShortestPath/Transit/TransitShortestPathPrecompute.cpp @@ -21,7 +21,7 @@ TransitStateContainer TransitShortestPathPrecompute::executeAlgorithm(const Grap //Init container, priority queue and state variables TransitStateContainer solutionsContainer{graph.getNodesVector().size()}; std::priority_queue<TransitAlgorithmState> statePriorityQueue; - statePriorityQueue.emplace(nodeIndex, instant,0); + statePriorityQueue.emplace(nodeIndex, instant,0, nodeIndex); TransitAlgorithmState currentState; while(!statePriorityQueue.empty()) @@ -49,8 +49,8 @@ TransitStateContainer TransitShortestPathPrecompute::executeAlgorithm(const Grap newState = TransitAlgorithmState(currentState, lineStop); newState.setNodeIndex(nextNode); newState.setPassageIndex(nextPassageIndex); //get next passage for new line - newState.setInstant(lineStop.getInstant(lineStop.getStopIndex() + 1, - nextPassageIndex)); //replace time with arrival time on next node + newState.setInstant(lineStop.getInstant(lineStop.getStopIndex() + 1,nextPassageIndex)); //replace time with arrival time on next node + newState.setPrecedingNodeIndex(currentState.getNodeIndex()); } } } else { @@ -58,6 +58,7 @@ TransitStateContainer TransitShortestPathPrecompute::executeAlgorithm(const Grap newState.setNodeIndex(nextNode); newState.setPassageIndex(currentState.getPassageIndex()); //get next passage for new line newState.setInstant(lineStop.getLineRef().getInstant(lineStop.getStopIndex() + 1, currentState.getPassageIndex())); //replace time with + newState.setPrecedingNodeIndex(currentState.getNodeIndex()); } DEBUG_MSG("Created new state " + newState.toString()); -- GitLab