Ejemplo n.º 1
0
 public BFSNode(Board nextBoard, Position blank_pos, BFSNode parent = null, Move move = null)
 {
     board = nextBoard;
     this.blank_pos = blank_pos;
     this.parent = parent;
     this.move = move;
 }
Ejemplo n.º 2
0
 public BFSNode()
 {
     board = new Board();
     blank_pos = new Position();
     parent = null;
     move = new Move();
 }
Ejemplo n.º 3
0
 public BFSNode(BFSNode node)
 {
     board = node.board;
     parent = node.parent;
     blank_pos = node.blank_pos;
     move = node.move;
 }
Ejemplo n.º 4
0
 public AStarNode(Board board, AStarNode parent, Position blank, Move move)
 {
     parent_ = parent;
     board_ = board;
     blank_pos_ = blank;
     move_ = move;
 }
Ejemplo n.º 5
0
 public AStarNode()
 {
     parent_ = null;
     board_ = new Board();
     blank_pos_ = new Position();
     move_ = new Move();
 }
Ejemplo n.º 6
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 mem_used)
        {
            hf_ = new HelperFunctions();

            if (hf_.CompareBoards(board, goal_board))
                return true; //start board is goal

            size_ = grid_size;

            BFSNode root = new BFSNode(board, blank_pos);
            BFSNode goal = new BFSNode();
            
            HashSet<Board> searched_boards = new HashSet<Board>(new BoardEqualityComparer());
            Queue<BFSNode> search = new Queue<BFSNode>();
            search.Enqueue(root);

            bool goal_reached = false;
            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            sw.Start();

            while(search.Count > 0 && !goal_reached)// && sw.ElapsedMilliseconds < 10000)
            {
                BFSNode node_to_expand = new BFSNode(search.Dequeue());

                List<BFSNode> children = GetChildren(node_to_expand);

                foreach(BFSNode child in children)
                {
                    if (hf_.CompareBoards(child.board, searched_boards)) continue;
                    else if (child.board.Equals(goal_board)) { timer = sw.ElapsedMilliseconds; sw.Stop(); goal_reached = true; goal = child; }
                    else { search.Enqueue(child); searched_boards.Add(child.board); }
                }
            }

            mem_used = GC.GetTotalMemory(false);
            open_list_size = search.Count;
            boards_searched = searched_boards.Count;

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

            searched_boards.Clear();
            search.Clear();
            GC.Collect();

            return goal_reached;
        }
Ejemplo n.º 7
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.º 8
0
        private float ManhattanDistance(Board other)
        {
            int manhattanDistanceSum = 0;

            for(int i = 0; i < size_; i++)
            {
                for(int j = 0; j < size_; j++)
                {
                    int value = other.board[i][j];
                    if(value != 0) //don't run MD for 0
                    {
                        int target_x = (value - 1) / size_;
                        int target_y = (value - 1) % size_;

                        int dx = i - target_x;
                        int dy = j - target_y;

                        manhattanDistanceSum += Math.Abs(dx) + Math.Abs(dy);
                    }
                }
            }

            return manhattanDistanceSum;
        }
Ejemplo n.º 9
0
        public void Build()
        {
            Initialise();

            //generate goal state
            Board board_state = new Board();
            Position blank_pos;
            int counter = 0;
            for(int i = 0; i < 4; i++)
            {
                List<int> temp = new List<int>();
                for(int j = 0; j < 4; j++)
                {
                    counter++;
                    if (counter == 16)
                    {
                        counter = 0;
                        blank_pos = new Position(3, 3);
                    }
                    temp.Add(counter);
                }
                board_state.board.Add(temp);
            }

            //run BFS in reverse from goal state to generate pattern database

            //Create Hashset for searched boards
            //Create FIFO Queue for successor nodes

            //While successor nodes available
                //Get next node to expand, NEXT
                //Generate its children

                //For each child
                    //if it has been searched already, discard
                    //else
                        //add child board to queue
                        //Find out which pattern was affected by change from NEXT to child
                        //create a new PatternNode for that pattern only
                        //set new PatternNode parent to previous board that affected this pattern

        }