调用RoadModel.getDistanceOfPath计算最短路径距离时抛出异常求助
Looks like you've hit a classic issue where your shortest path algorithm is returning a sequence of points that don't actually have valid edges between them in your road graph. Let's break down why this is happening and how to fix it:
First, Diagnose the Exact Problem
The error stack trace points directly to getDistanceOfPath failing because one of the adjacent point pairs in your path doesn't exist as a connection in the graph. From your path output: [(12.0,28.22222222222222), (12.0,32.0), (12.0,36.0), (8.0,36.0)], we need to check which pair is invalid.
Add a quick validation step in your getTimeToReachGivenPoint method to pinpoint the bad connection:
long getTimeToReachGivenPoint(Point point) { System.out.println("Point: " + point); List<Point> path = getRoadModel().getShortestPathTo(this, point); System.out.println("Path: " + path); System.out.println("Path length: " + path.size()); // Validate each consecutive pair in the path for (int i = 0; i < path.size() - 1; i++) { Point current = path.get(i); Point next = path.get(i + 1); boolean hasValidConnection = getRoadModel().getGraph().hasConnection(current, next); if (!hasValidConnection) { System.err.println("ERROR: No connection exists between " + current + " and " + next); // Either throw a more descriptive error or handle it gracefully throw new IllegalStateException("Invalid path segment: " + current + " -> " + next); } } Measure<Double,Length> distance = getRoadModel().getDistanceOfPath(path); long time = (long) (distance.getValue()/AgvAgent.SPEED); return time; }
This will immediately tell you which part of the path is broken—this is critical for fixing the root cause.
Common Root Causes & Fixes
1. Your Shortest Path Algorithm is "Cheating"
It looks like your getShortestPathTo might be calculating a geometrically shortest path (straight line between points) instead of using the actual edges in your graph. For example, the first point (12.0,28.222...) might be a custom position (like your AGV's current location) that isn't a node in the graph, or the algorithm is skipping intermediate nodes required for valid connections.
Fix: Use RinSim's built-in RoadModel.getPathTo() method instead of a custom shortest path implementation. This method guarantees the returned path uses only valid edges in the graph.
2. Stale Graph Snapshot
The error mentions GraphRoadModelSnapshot, which suggests you might be calculating paths against an outdated version of the road graph (e.g., if edges were added/removed after the snapshot was taken).
Fix: Ensure you're using the live DynamicGraphRoadModelImpl instance (not a snapshot) when calculating paths and distances, unless you explicitly need a frozen state.
3. Invalid Node in Path
If the starting point (your AGV's current position) isn't a registered node in the graph, the path algorithm might be creating an invalid first segment. RinSim's road model expects paths to start/end at valid nodes or use RoadModel.moveTo to handle intermediate positions.
Fix: Before calculating the path, move your AGV to the nearest valid node in the graph, or use RoadModel.getPathTo which handles positions that are on edges (not just nodes).
Quick Workaround (If You Need to Test Immediately)
If you need a temporary fix while debugging the path algorithm, you can calculate the distance manually using Euclidean distance between consecutive points. Note that this isn't accurate for real AGV movement (since it ignores actual edge lengths), but it can help you test other parts of your code:
// Replace the getDistanceOfPath call with this double totalDistance = 0; for (int i = 0; i < path.size() - 1; i++) { Point current = path.get(i); Point next = path.get(i + 1); totalDistance += Math.hypot(next.x - current.x, next.y - current.y); } long time = (long) (totalDistance / AgvAgent.SPEED);
Remember, this is just a temporary measure—you'll want to fix the path validation issue properly for production use.
内容的提问来源于stack exchange,提问作者Damcios

