コード例 #1
0
ファイル: AI_BFS.cs プロジェクト: djquirke/AIPersonal
        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;
        }
コード例 #2
0
ファイル: AI_AStar.cs プロジェクト: djquirke/AIPersonal
        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;
        }