Ejemplo n.º 1
0
 public AStarNode(Board board, AStarNode parent, Position blank, Move move)
 {
     parent_ = parent;
     board_ = board;
     blank_pos_ = blank;
     move_ = move;
 }
Ejemplo n.º 2
0
        private void ShiftDown(AStarNode node, int idx)
        {
            if (IsEmpty() || idx >= size_ / 2) return;// || (node.f < nodes_[2 * idx + 1].f && node.f < nodes_[2 * idx + 2].f)) return;

            if(size_ % 2 == 0 && idx == (size_ - 1) / 2) //last parent has 1 child
            {
                if(node.f < nodes_[2 * idx + 1].f)
                {
                    nodes_[idx] = nodes_[2 * idx + 1];
                    nodes_[2 * idx + 1] = node;
                }
                return;
            }

            if((node.f < nodes_[2 * idx + 1].f && node.f < nodes_[2 * idx + 2].f)) return;

            if(nodes_[2 * idx + 1].f <= nodes_[2 * idx + 2].f && node.f > nodes_[2 * idx + 1].f)
            {
                nodes_[idx] = nodes_[2 * idx + 1];
                nodes_[2 * idx + 1] = node;
                ShiftDown(node, 2 * idx + 1);
            }
            else if(nodes_[2 * idx + 2].f < nodes_[2 * idx + 1].f && node.f > nodes_[2 * idx + 2].f)
            {
                nodes_[idx] = nodes_[2 * idx + 2];
                nodes_[2 * idx + 2] = node;
                ShiftDown(node, 2 * idx + 2);
            }
        }
Ejemplo n.º 3
0
 public AStarNode()
 {
     parent_ = null;
     board_ = new Board();
     blank_pos_ = new Position();
     move_ = new Move();
 }
Ejemplo n.º 4
0
        private void ShiftUp(AStarNode node, int idx)
        {
            if (IsEmpty() || nodes_[(idx - 1) / 2].f <= node.f) return;

            nodes_[idx] = nodes_[(idx - 1) / 2];
            nodes_[(idx - 1) / 2] = node;
            ShiftUp(node, (idx - 1) / 2);
        }
Ejemplo n.º 5
0
        public bool Run(Board board, Position blank_pos, Board goal_board, ref List<Move> moves, int grid_size, ref int boards_searched, ref int open_list_size, ref long timer, ref long memory_used)
        {
            hf_ = new HelperFunctions();

            if (hf_.CompareBoards(board, goal_board))
                return true;

            size_ = grid_size;

            AStarNode root = new AStarNode(board, null, blank_pos, null);
            AStarNode goal = new AStarNode(goal_board, null, null, null);

            PriorityQueue open_q = new PriorityQueue();
            HashSet<AStarNode> open_d = new HashSet<AStarNode>();
            HashSet<AStarNode> closed_list = new HashSet<AStarNode>();
            
            open_q.Add(root);
            open_d.Add(root);
            bool goal_found = false;

            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            sw.Start();

            
            while(open_q.Size() > 0 && !goal_found) // && sw.ElapsedMilliseconds < 10000)
            {
                AStarNode next_node = open_q.GetNext();
                open_d.Remove(next_node);
                if (next_node.board_.Equals(goal.board_))
                {
                    timer = sw.ElapsedMilliseconds;
                    sw.Stop();
                    goal_found = true;
                    goal = next_node;
                    continue;
                }

                List<AStarNode> successors = GetChildren(next_node);

                foreach(AStarNode successor in successors)
                {
                    if (hf_.CompareBoards(successor, closed_list))
                        continue;

                    successor.g = next_node.g + 1;

                    if (hf_.CompareBoards(successor, open_d)) 
                        continue;

                    successor.h = ManhattanDistance(successor.board_);
                    successor.f = successor.g + successor.h;
                    open_q.Add(successor);
                    open_d.Add(successor);
                }

                closed_list.Add(next_node);
            }

            memory_used = GC.GetTotalMemory(false);
            open_list_size = open_q.Size();
            boards_searched = closed_list.Count;

            if(goal_found) TraverseTree(ref moves, goal);

            closed_list.Clear();
            open_d.Clear();
            open_q.Clear();
            GC.Collect();

            return goal_found;
        }
Ejemplo n.º 6
0
        private void TraverseTree(ref List<Move> moves, AStarNode node)
        {
            moves.Add(node.move_);

            if (node.parent_ == null) return;

            TraverseTree(ref moves, node.parent_);
        }
Ejemplo n.º 7
0
        private List<AStarNode> GetChildren(AStarNode node_to_expand)
        {
            List<AStarNode> children = new List<AStarNode>();

            foreach (Direction dir in directions)
            {
                Position next_pos = hf_.GetCoordsFromDirection(node_to_expand.blank_pos_, dir, size_);
                Move move = new Move(next_pos, node_to_expand.blank_pos_);

                if (next_pos.x == node_to_expand.blank_pos_.x && next_pos.y == node_to_expand.blank_pos_.y) continue;

                Board nextBoard = hf_.SimulateMove(node_to_expand.board_, move);

                AStarNode node = new AStarNode(nextBoard, node_to_expand, next_pos, move);
                children.Add(node);
            }

            return children;
        }
Ejemplo n.º 8
0
 public void UpdateKey(AStarNode node, float priority)
 {
     
 }
Ejemplo n.º 9
0
 public void Add(AStarNode node)
 {
     nodes_.Add(node);
     size_++;
     ShiftUp(node, size_ - 1);
 }