/// <summary> /// This function analyzes the neighbors of the most promising node in the Pathfinding graph /// using the A* algorithm (A-star) and returns that node /// </summary> /// <returns>The most promising node of the iteration</returns> public override CPos Expand() { var currentMinNode = OpenQueue.Pop().Destination; var currentCell = Graph[currentMinNode]; Graph[currentMinNode] = new CellInfo(currentCell.CostSoFar, currentCell.EstimatedTotal, currentCell.PreviousPos, CellStatus.Closed); if (Graph.CustomCost != null && Graph.CustomCost(currentMinNode) == PathGraph.CostForInvalidCell) { return(currentMinNode); } foreach (var connection in Graph.GetConnections(currentMinNode)) { // Calculate the cost up to that point var gCost = currentCell.CostSoFar + connection.Cost; var neighborCPos = connection.Destination; var neighborCell = Graph[neighborCPos]; // Cost is even higher; next direction: if (neighborCell.Status == CellStatus.Closed || gCost >= neighborCell.CostSoFar) { continue; } // Now we may seriously consider this direction using heuristics. If the cell has // already been processed, we can reuse the result (just the difference between the // estimated total and the cost so far int hCost; if (neighborCell.Status == CellStatus.Open) { hCost = neighborCell.EstimatedTotal - neighborCell.CostSoFar; } else { hCost = heuristic(neighborCPos); } var estimatedCost = gCost + hCost; Graph[neighborCPos] = new CellInfo(gCost, estimatedCost, currentMinNode, CellStatus.Open); if (neighborCell.Status != CellStatus.Open) { OpenQueue.Add(new GraphConnection(neighborCPos, estimatedCost)); } if (Debug) { if (gCost > MaxCost) { MaxCost = gCost; } considered.AddLast(new Pair <CPos, int>(neighborCPos, gCost)); } } return(currentMinNode); }
/// <summary> /// This function analyzes the neighbors of the most promising node in the Pathfinding graph /// using the A* algorithm (A-star) and returns that node /// </summary> /// <returns>The most promising node of the iteration</returns> public override CPos Expand() { var currentMinNode = OpenQueue.Pop().Destination; var currentCell = Graph[currentMinNode]; Graph[currentMinNode] = new CellInfo(currentCell.CostSoFar, currentCell.EstimatedTotal, currentCell.PreviousPos, CellStatus.Closed); if (Graph.CustomCost != null && Graph.CustomCost(currentMinNode) == Constants.InvalidNode) return currentMinNode; foreach (var connection in Graph.GetConnections(currentMinNode)) { // Calculate the cost up to that point var gCost = currentCell.CostSoFar + connection.Cost; var neighborCPos = connection.Destination; var neighborCell = Graph[neighborCPos]; // Cost is even higher; next direction: if (neighborCell.Status == CellStatus.Closed || gCost >= neighborCell.CostSoFar) continue; // Now we may seriously consider this direction using heuristics. If the cell has // already been processed, we can reuse the result (just the difference between the // estimated total and the cost so far int hCost; if (neighborCell.Status == CellStatus.Open) hCost = neighborCell.EstimatedTotal - neighborCell.CostSoFar; else hCost = heuristic(neighborCPos); var estimatedCost = gCost + hCost; Graph[neighborCPos] = new CellInfo(gCost, estimatedCost, currentMinNode, CellStatus.Open); if (neighborCell.Status != CellStatus.Open) OpenQueue.Add(new GraphConnection(neighborCPos, estimatedCost)); if (Debug) { if (gCost > MaxCost) MaxCost = gCost; considered.AddLast(new Pair<CPos, int>(neighborCPos, gCost)); } } return currentMinNode; }
protected override void AddInitialCell(CPos 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)); }
protected override void AddInitialCell(CPos location) { Graph[location] = new CellInfo(0, heuristic(location), location, CellStatus.Open); OpenQueue.Add(location); startPoints.Add(location); considered.AddLast(new Pair<CPos, int>(location, 0)); }