Ejemplo n.º 1
0
    public System.Collections.IEnumerator FindPath(Vertex startVertex, Vertex goalVertex, System.Action <List <Edge> > callback = null)
    {
        openList.Add(startVertex, new PathRecord(startVertex));
        PathRecord current = null;

        #region Not a part of algorithm, visualization purposes only
        gridMarkerController.PutOpenNodeMarker(startVertex);
        #endregion

        while (openList.Count > 0)
        {
            current = openList.PopMinValue();
            #region Not a part of algorithm, visualization purposes only
            gridMarkerController.PutCurrentNodeMarker(current.node);
            #endregion

            if (current.node == goalVertex)
            {
                //WOW! we found goal!
                break;
            }
            yield return(ProcessAllEdgesFromCurrent(current, goalVertex));

            closeList.Add(current.node, current);
            #region Not a part of algorithm, visualization purposes only
            gridMarkerController.RemoveMarker(current.node);
            gridMarkerController.PutClosedNodeMarker(current.node);
            #endregion
            yield return(new WaitForSecondsRealtime(sleepTime));
        }
        List <Edge> path = new List <Edge>();
        if (current.node == goalVertex)
        {
            while (current.node != startVertex)
            {
                #region Not a part of algorithm, visualization purposes only
                if (goalVertex != current.node)
                {
                    gridMarkerController.PutPathNodeMarker(current.node);
                    yield return(new WaitForSecondsRealtime(sleepTime));
                }
                #endregion
                path.Add(current.connection.edge);
                current = current.connection.fromRecord;
            }
            path.Reverse();
        }

        if (callback != null)
        {
            callback.Invoke(path);
        }
    }
Ejemplo n.º 2
0
        public Stack <PathNode> FindPath(State state, Piece startPiece, Piece endPiece)
        {
            openList.Clear();
            closedHashSet.Clear();

            openList.Add(new PathNode(startPiece.position));

            while (openList.Count > 0)
            {
                PathNode currentNode = openList.Pop();

                if (currentNode.Position == endPiece.position)
                {
                    return(ReconstructPath(currentNode, endPiece));
                }

                closedHashSet.Add(currentNode);

                foreach (Vector2i neighbor in GetNeighbors(state, endPiece, currentNode.Position))
                {
                    if (closedHashSet.Contains(neighbor))
                    {
                        continue;
                    }

                    float gValue = currentNode.g + neighbor.Distance(currentNode.Position);

                    if (openList.Update(neighbor, gValue))
                    {
                        continue;
                    }

                    float hValue = GetH(neighbor, endPiece.position);

                    PathNode currentNeighbor = new PathNode(neighbor)
                    {
                        g      = gValue,
                        h      = hValue,
                        f      = gValue + hValue,
                        Parent = currentNode
                    };

                    openList.Add(currentNeighbor);
                }
            }

            return(null);
        }
Ejemplo n.º 3
0
        public void OpenSquare(int y, int x, int[] parent, int moveCost, double heuristic, bool newSquare)
        {
            if (!newSquare)
            {
                foreach (OpenSquare op in OpenList)
                {
                    if (op.Y == y && op.X == x)
                    {
                        newSquare = true;
                        break;
                    }
                }
            }

            if (!newSquare)
            {
                OpenList.Add(new OpenSquare(y, x));
                MapStatus.RemoveAll((c) => c.X == x && c.Y == y);
                MapStatus.Add(new CellInfo(heuristic, null, true, false, x, y));
            }

            CellInfo cell = GetCellInfo(y, x);

            cell.Parent       = parent;
            cell.MovementCost = moveCost;
        }
Ejemplo n.º 4
0
        public SearchResult <T> Search()
        {
            T        state = Problem.InitialState;
            Node <T> node  = new Node <T>(state);

            if (Problem.GoalTest(state))
            {
                return(new SearchResult <T>(node));
            }
            Frontier.Put(node);
            OpenList.Add(state);

            while (!Frontier.IsEmpty)
            {
                node  = Frontier.Take();
                state = node.State;
                ClosedList.Add(state);
                foreach (IAction <T> action in Problem.Actions(state))
                {
                    Node <T> childNode  = node.ChildNode(Problem, action);
                    T        childState = childNode.State;
                    if (!ClosedList.Contains(childState) && !OpenList.Contains(childState))
                    {
                        if (Problem.GoalTest(childState))
                        {
                            return(new SearchResult <T>(childNode));
                        }
                        Frontier.Put(childNode);
                        OpenList.Add(childState);
                    }
                }
            }
            return(new SearchResult <T>(null));
        }
Ejemplo n.º 5
0
        public bool FindTheShortesPath(Node Start, Node Target)
        {
            Node CurrentNode = Start;

            OpenList.Add(CurrentNode);

            for (; OpenList.Count > 0;)
            {
                CurrentNode = OpenList[0];

                if (CurrentNode.Equals(Target))
                {
                    VisitedNodes.Add(Target);

                    for (int Index = 0; Index < VisitedNodes.Count - 1; Index++)
                    {
                        ShortesPath.Add(InnerGraph.FindEdge(VisitedNodes[Index], VisitedNodes[Index + 1]));
                    }

                    return(true);
                }

                OpenList.Remove(CurrentNode);

                ClosedList.Add(CurrentNode);

                foreach (Node Inheritor in CurrentNode.Inheritors.Where(Node => Node != null && Node.Index != Node.Inheritors.Length - 1))
                {
                    if (!ClosedList.Contains(Inheritor))
                    {
                        if (!OpenList.Contains(Inheritor))
                        {
                            Inheritor[Inheritor.Index] = CurrentNode;

                            Inheritor.HeuristicPathWeight = CalculateHeuristic(Inheritor, Target);

                            Inheritor.GainedPathWeight = InnerGraph.FindEdge(CurrentNode, Inheritor).Weight;

                            Inheritor.TotalPathWeight = Inheritor.GainedPathWeight + Inheritor.HeuristicPathWeight;

                            OpenList.Add(Inheritor);

                            OpenList = OpenList.OrderBy(Node => Node.TotalPathWeight).ToList <Node>();
                        }
                    }
                }

                VisitedNodes.Add(CurrentNode);
            }

            return(true);
        }
 //Initializes Search
 public Search(Node[,] nodes, Node start, Node end, SearchAlgorithm searchAlgorithm)
 {
     //Set starting variables
     Start           = start;
     End             = end;
     SearchAlgorithm = searchAlgorithm;
     //Starting node should always be open, so add it to the OpenNodes list.
     OpenList.Add(start);
     //Set start node as in OpenNodes.
     start.InOpenNodes = true;
     //Apply Start F cost
     start.F = CalculateAStarH(start, end);
 }
Ejemplo n.º 7
0
    bool PushOpenList(int StandardPos, int OpenPos)
    {
        if (!ClosedList.Contains(OpenPos))
        {
            if (!OpenList.Contains(OpenPos))
            {
                OpenList.Add(OpenPos);

                ((Tile)Tiles[OpenPos]).Goal       = CalcuGoal(StandardPos, OpenPos);
                ((Tile)Tiles[OpenPos]).Heuristic  = CalcuHeuristic(OpenPos, EndPos);
                ((Tile)Tiles[OpenPos]).Fitness    = ((Tile)Tiles[OpenPos]).Goal + ((Tile)Tiles[OpenPos]).Heuristic;
                ((Tile)Tiles[OpenPos]).ParentTile = StandardPos;
                return(true);
            }
        }
        return(false);
    }
Ejemplo n.º 8
0
        private bool ProcessCell(int originX, int originY, int direction, double moveCost)
        {
            var delta = GetDelta(direction);

            int x = originX + delta.X;
            int y = originY + delta.Y;

            double fNew;
            double gNew;
            double hNew;

            if (!IsValid(x, y))
            {
                return(false);
            }

            if (x == DestinationX && y == DestinationY)
            {
                CellDetails[x, y].ParentX = originX;
                CellDetails[x, y].ParentY = originY;
                SetNodes();

                return(true);
            }

            if (ClosedList[x, y] == false && Map.Tiles[x, y].Walkable)
            {
                gNew = CellDetails[originX, originY].GValue + moveCost;
                hNew = CalculateHValue(x, y);
                fNew = gNew + hNew;

                if (CellDetails[x, y].FValue > fNew)
                {
                    OpenList.Add(new NodeScore(fNew, new Point(x, y)));

                    var curCell = CellDetails[x, y];
                    curCell.FValue  = fNew;
                    curCell.GValue  = gNew;
                    curCell.HValue  = hNew;
                    curCell.ParentX = originX;
                    curCell.ParentY = originY;
                }
            }

            return(false);
        }
Ejemplo n.º 9
0
        public SearchResult <T> Search()
        {
            T state = Problem.InitialState;
            HeuristicNode <T> node = new HeuristicNode <T>(state, 1);

            if (Problem.GoalTest(state))
            {
                return(new SearchResult <T>(node));
            }
            PriorityQueue.Push(node);
            OpenList.Add(state);

            while (!PriorityQueue.IsEmpty)
            {
                node  = PriorityQueue.Pop();
                state = node.State;
                ClosedList.Add(state);
                foreach (IAction <T> action in Problem.Actions(state))
                {
                    HeuristicNode <T> childNode = node.ChildNode(Problem, action, Heuristic);
                    T childState = childNode.State;
                    if (ClosedList.Contains(childState) || OpenList.Contains(childState))
                    {
                        if (PriorityQueue.Contains(childNode) && childNode.HeuristicCost > node.HeuristicCost)
                        {
                            PriorityQueue.Push(childNode);
                            OpenList.Add(childState);
                        }
                    }

                    if (!ClosedList.Contains(childState) && !OpenList.Contains(childState))
                    {
                        if (Problem.GoalTest(childState))
                        {
                            return(new SearchResult <T>(childNode));
                        }
                        PriorityQueue.Push(childNode);
                        OpenList.Add(childState);
                    }
                }
            }
            return(new SearchResult <T>(null));
        }
Ejemplo n.º 10
0
        public void Initialize(Node start, Node goal)
        {
            Start  = start;
            Sprite = start;
            Goal   = goal;

            // Initialize All Nodes on board with coordinates and passible
            for (int i = 0; i < Size; i++)
            {
                State.Add(new List <Node>());
                for (int j = 0; j < Size; j++)
                {
                    State[i].Add(new Node(i, j, true));
                }
            }

            // Set 10% of Nodes as Impassible
            SetImpassibleNodes();

            // Add Start Node to Open List
            GetF(Start);
            OpenList.Add(Start);
        }
Ejemplo n.º 11
0
 private void AddToOpen(AStarNode s)
 {
     OpenList.Add(s);
 }
Ejemplo n.º 12
0
        public bool Compute(int originX, int originY, int destX, int destY)
        {
            OriginX      = originX;
            OriginY      = originY;
            DestinationX = destX;
            DestinationY = destY;
            Nodes.Clear();
            OpenList.Clear();

            if (originX == destX && originY == destY)
            {
                return(true);
            }

            var originOutsideMap = !IsValid(OriginX, OriginY);
            var destOutsideMap   = !IsValid(DestinationX, DestinationY);

            if (originOutsideMap || destOutsideMap)
            {
                return(false);
            }

            if (!Map.Tiles[OriginX, OriginY].Walkable || !Map.Tiles[DestinationX, DestinationY].Walkable)
            {
                return(false);
            }

            InitializeGrid(ClosedList, false, Width, Height);
            InitializeCellDetails();

            OpenList.Add(new NodeScore(0, new Point(OriginX, OriginY)));

            while (OpenList.Count != 0)
            {
                var nodeScore = OpenList[0];
                OpenList.RemoveAt(0);

                var x = nodeScore.Location.X;
                var y = nodeScore.Location.Y;
                ClosedList[x, y] = true;

                if (ProcessCell(x, y, NorthEast, DiagonalCost))
                {
                    break;
                }

                if (ProcessCell(x, y, North, 1))
                {
                    break;
                }

                if (ProcessCell(x, y, NorthWest, DiagonalCost))
                {
                    break;
                }

                if (ProcessCell(x, y, East, 1))
                {
                    break;
                }

                if (ProcessCell(x, y, West, 1))
                {
                    break;
                }

                if (ProcessCell(x, y, SouthEast, DiagonalCost))
                {
                    break;
                }

                if (ProcessCell(x, y, South, 1))
                {
                    break;
                }

                if (ProcessCell(x, y, SouthWest, DiagonalCost))
                {
                    break;
                }
            }

            return(true);
        }