Example #1
0
        /// <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
        /// 该函数使用A* 算法 分析 Pathfinding图中最有希望的节点的邻居,并返回该节点。
        /// </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))
            {
                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)
                    {
                        gCost = MaxCost;
                    }

                    considered.AddLast(new Pair <CPos, int>(neighborCPos, gCost));
                }
            }

            return(currentMinNode);
        }
Example #2
0
        /// <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();

            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);
                }

                Graph[neighborCPos] = new CellInfo(gCost, gCost + hCost, currentMinNode, CellStatus.Open);

                if (neighborCell.Status != CellStatus.Open)
                {
                    OpenQueue.Add(neighborCPos);
                }

                if (Debug)
                {
                    if (gCost > MaxCost)
                    {
                        MaxCost = gCost;
                    }

                    considered.AddLast(new Pair <CPos, int>(neighborCPos, gCost));
                }
            }

            return(currentMinNode);
        }
Example #3
0
        /// <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 currentInfo = Graph[currentMinNode];

            Graph[currentMinNode] = new CellInfo(CellStatus.Closed, currentInfo.CostSoFar, currentInfo.EstimatedTotalCost, currentInfo.PreviousNode);

            if (Graph.CustomCost != null && Graph.CustomCost(currentMinNode) == PathGraph.PathCostForInvalidPath)
            {
                return(currentMinNode);
            }

            foreach (var connection in Graph.GetConnections(currentMinNode))
            {
                // Calculate the cost up to that point
                var costSoFarToNeighbor = currentInfo.CostSoFar + connection.Cost;

                var neighbor     = connection.Destination;
                var neighborInfo = Graph[neighbor];

                // Cost is even higher; next direction:
                if (neighborInfo.Status == CellStatus.Closed ||
                    (neighborInfo.Status == CellStatus.Open && costSoFarToNeighbor >= neighborInfo.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 estimatedRemainingCostToTarget;
                if (neighborInfo.Status == CellStatus.Open)
                {
                    estimatedRemainingCostToTarget = neighborInfo.EstimatedTotalCost - neighborInfo.CostSoFar;
                }
                else
                {
                    estimatedRemainingCostToTarget = heuristic(neighbor);
                }

                var estimatedTotalCostToTarget = costSoFarToNeighbor + estimatedRemainingCostToTarget;
                Graph[neighbor] = new CellInfo(CellStatus.Open, costSoFarToNeighbor, estimatedTotalCostToTarget, currentMinNode);

                if (neighborInfo.Status != CellStatus.Open)
                {
                    OpenQueue.Add(new GraphConnection(neighbor, estimatedTotalCostToTarget));
                }
            }

            return(currentMinNode);
        }