diff --git a/src/ShortestPath/Transit/TransitShortestPathPrecompute.cpp b/src/ShortestPath/Transit/TransitShortestPathPrecompute.cpp index 7354508711ac8c2651d8f5fcbc18b0576260124d..6213dc22ff560fe3ea20927875b1889d67c55345 100644 --- a/src/ShortestPath/Transit/TransitShortestPathPrecompute.cpp +++ b/src/ShortestPath/Transit/TransitShortestPathPrecompute.cpp @@ -38,13 +38,13 @@ TransitStateContainer TransitShortestPathPrecompute::executeAlgorithm(const Grap if(nextNode != -1 && (currentState.isEmpty() || nextNode != currentState.getPrecedingNodeIndex())) { DEBUG_MSG("Extension from line " + lineStop.getLineRef().getLineId() + " towards node " + std::to_string(nextNode)); TransitAlgorithmState newState; //define variable before conditionals + int nextPassageIndex = currentState.getPassageIndex(); //default value if we stay on the same line and no turn back happens if(currentState.isEmpty() || currentState.getLastConnectionLine() != lineStop.getLineRef()) // if new line is different than current line { if(currentState.canAddConnection()) { - int nextPassageIndex = lineStop.getLineRef().findNextScheduledPassage( - lineStop.getStopIndex(), currentState.getInstant()); + nextPassageIndex = lineStop.findNextScheduledPassage(lineStop.getStopIndex(), currentState.getInstant()); if (nextPassageIndex == lineStop.getLineRef().scheduleSize()) { - newState.setInstant(INT16_MAX); + newState.setNodeIndex(-1); } else { newState = TransitAlgorithmState(currentState, lineStop); newState.setNodeIndex(nextNode); @@ -54,11 +54,16 @@ TransitStateContainer TransitShortestPathPrecompute::executeAlgorithm(const Grap } } } else { - newState = TransitAlgorithmState(currentState); - 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()); + //Check for a cycle, and in this case, look for a new passage index + if(lineStop.getInstant(lineStop.getStopIndex() + 1, currentState.getPassageIndex()) < currentState.getInstant()) { + newState.setNodeIndex(-1); + } else { + newState = TransitAlgorithmState(currentState); + newState.setNodeIndex(nextNode); + newState.setPassageIndex(currentState.getPassageIndex()); //get next passage for new line + newState.setInstant(lineStop.getInstant(lineStop.getStopIndex() + 1, nextPassageIndex)); //replace time with + newState.setPrecedingNodeIndex(currentState.getNodeIndex()); + } } DEBUG_MSG("Created new state " + newState.toString()); diff --git a/src/instance/graph/LineStop.h b/src/instance/graph/LineStop.h index e4883c2bb8b262dcf5cc4d92ee5e008c1526a2c0..9d15019cab6313ceab02cec3756cb18d001c4202 100644 --- a/src/instance/graph/LineStop.h +++ b/src/instance/graph/LineStop.h @@ -23,6 +23,7 @@ public: } [[nodiscard]] int getInstant(int stationIdx, int scheduleIdx) const { return _lineRef.getInstant(stationIdx, scheduleIdx); } + [[nodiscard]] int findNextScheduledPassage(int stationIdx, int instant) const { return _lineRef.findNextScheduledPassage(stationIdx, instant); } /** * @return -1 if there are no valid successors to this LineStop's node. Returns the node index in the graph if there is a valid successor