Fix pathfinding using PriorityQueue incorrectly.
By providing a comparer that could change over time (as estimated costs on the graph were updated), this meant the priority queue could have its heap property invalidated and thus not maintain a correct ordering. Instead we store elements into the queue with their estimations at the time. This preserves the heap property and thus ensures the queue returns properly ordered results, although it may contain out of date estimations. This also improves performance. The fixed comparer need not perform expensive lookups into the graph, but can instead use the readily available value. This speeds up adds and removes on the queue significantly.
This commit is contained in:
@@ -84,9 +84,11 @@ namespace OpenRA.Mods.Common.Pathfinder
|
||||
|
||||
protected override void AddInitialCell(CPos location)
|
||||
{
|
||||
Graph[location] = new CellInfo(0, heuristic(location), location, CellStatus.Open);
|
||||
OpenQueue.Add(location);
|
||||
startPoints.Add(location);
|
||||
var cost = heuristic(location);
|
||||
Graph[location] = new CellInfo(0, cost, location, CellStatus.Open);
|
||||
var connection = new GraphConnection(location, cost);
|
||||
OpenQueue.Add(connection);
|
||||
StartPoints.Add(connection);
|
||||
considered.AddLast(new Pair<CPos, int>(location, 0));
|
||||
}
|
||||
|
||||
@@ -99,7 +101,7 @@ namespace OpenRA.Mods.Common.Pathfinder
|
||||
/// <returns>The most promising node of the iteration</returns>
|
||||
public override CPos Expand()
|
||||
{
|
||||
var currentMinNode = OpenQueue.Pop();
|
||||
var currentMinNode = OpenQueue.Pop().Destination;
|
||||
|
||||
var currentCell = Graph[currentMinNode];
|
||||
Graph[currentMinNode] = new CellInfo(currentCell.CostSoFar, currentCell.EstimatedTotal, currentCell.PreviousPos, CellStatus.Closed);
|
||||
@@ -128,10 +130,11 @@ namespace OpenRA.Mods.Common.Pathfinder
|
||||
else
|
||||
hCost = heuristic(neighborCPos);
|
||||
|
||||
Graph[neighborCPos] = new CellInfo(gCost, gCost + hCost, currentMinNode, CellStatus.Open);
|
||||
var estimatedCost = gCost + hCost;
|
||||
Graph[neighborCPos] = new CellInfo(gCost, estimatedCost, currentMinNode, CellStatus.Open);
|
||||
|
||||
if (neighborCell.Status != CellStatus.Open)
|
||||
OpenQueue.Add(neighborCPos);
|
||||
OpenQueue.Add(new GraphConnection(neighborCPos, estimatedCost));
|
||||
|
||||
if (Debug)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user