コード例 #1
0
        public SearchResult AStarSearch(IHeuristic heuristic)
        {
            NodeComparator comparator = (node1, node2) => node1.FHat.CompareTo(node2.FHat);

            return(GenericSearch(_initialState, _goalState, "AStar",
                                 comparator, (state1, state2) => 1, heuristic));
        }
コード例 #2
0
        public SearchResult GreedyBestFirstSearch(IHeuristic heuristic)
        {
            NodeComparator comparator = (node1, node2) => node1.HHat.CompareTo(node2.HHat);

            return(GenericSearch(_initialState, _goalState, "Greedy Best First Search",
                                 comparator, (state1, state2) => 1, heuristic));
        }
コード例 #3
0
        public SearchResult UCS()
        {
            NodeComparator comparator = (node1, node2) => node1.GHat.CompareTo(node2.GHat);
            CostFunc       cost       = (state1, state2) => 1;

            return(GenericSearch(_initialState, _goalState, "UCS", comparator, cost, null));
        }
コード例 #4
0
        public SearchResult DFS(int depthLimit)
        {
            string         name       = "DFS (DepthLimit: " + depthLimit + ")";
            NodeComparator comparator = (node1, node2) => (-node1.Depth).CompareTo(-node2.Depth);

            return(GenericSearch(_initialState, _goalState, name, comparator, (state1, state2) => 1, null, depthLimit));
        }
コード例 #5
0
 public Frontier(NodeComparator compareNodes)
 {
     _compareNodes = compareNodes;
     _hashByState  = new Dictionary <StateBase, List <Node> >();
     _pQueue       = new SortedList <Key, List <Node> >();
 }
コード例 #6
0
 public Key(Node node, NodeComparator comparator)
 {
     _node         = node;
     _compareNodes = comparator;
 }
コード例 #7
0
        public SearchResult GenericSearch(StateBase initialState,
                                          StateBase goalState,
                                          string algName,
                                          NodeComparator comparator,
                                          CostFunc cost,
                                          IHeuristic heuristic,
                                          int?depthLimit = null)
        {
            Frontier       frontier           = new Frontier(comparator);
            Explored       explored           = new Explored();
            HeuristicCache cache              = new HeuristicCache(goalState, heuristic);
            int            nodesGenerated     = 0;
            int            nodesPrevGenerated = 0;

            Node initNode = new Node(initialState, cost, null, null, cache.Evaluate);

            frontier.Push(initNode);

            while (true)
            {
                if (frontier.Count == 0)
                {
                    return(new SearchResult(null, nodesGenerated, nodesPrevGenerated, 0, explored.Count, algName, heuristic));
                }

                Node node = frontier.Pop();
                explored.Push(node);

                // goal test
                if (node.State.Equals(goalState))
                {
                    return(new SearchResult(node, nodesGenerated, nodesPrevGenerated, frontier.Count, explored.Count, algName, heuristic));
                }

                if (depthLimit != null && node.Depth == depthLimit)
                {
                    continue;
                }

                IEnumerable <Node> children = node.Successors(cost, cache.Evaluate);
                foreach (Node child in children)
                {
                    nodesGenerated++;

                    // If this state is found in the Frontier, replace the old state with the new state if its GHat is smaller
                    Node foundInFrontier = frontier.Find(child.State);
                    if (foundInFrontier != null)
                    {
                        nodesPrevGenerated++;
                        if (foundInFrontier.GHat > child.GHat)
                        {
                            frontier.Replace(foundInFrontier, child);
                        }
                    }
                    else
                    {
                        Node foundInExplored = explored.Find(child.State);

                        // If this state is found in the Explored, replace the old state with the new state if its GHat is smaller
                        if (foundInExplored != null)
                        {
                            nodesPrevGenerated++;
                            if (foundInExplored.GHat > child.GHat)
                            {
                                explored.Remove(foundInExplored);
                                frontier.Push(child);
                            }
                        }
                        else
                        {
                            // doesn't exist in frontier or explored, adding to frontier.
                            frontier.Push(child);
                        }
                    }
                }
            }
        }
コード例 #8
0
        public SearchResult BFS()
        {
            NodeComparator comparator = (node1, node2) => node1.Depth.CompareTo(node2.Depth);

            return(GenericSearch(_initialState, _goalState, "BFS", comparator, (state1, state2) => 1, null));
        }