public int Evaluate(StateBase state, StateBase goalState)
 {
     return(Evaluate(state as T, goalState as T));
 }
Beispiel #2
0
 public Node Find(StateBase state)
 {
     return(_hashByState.ContainsKey(state) && _hashByState[state].Count > 0 ? _hashByState[state].First() : null);
 }
Beispiel #3
0
 public HeuristicCache(StateBase goal, IHeuristic heuristic)
 {
     _goal      = goal;
     _heuristic = heuristic;
     _cache     = new Dictionary <StateBase, int>();
 }
Beispiel #4
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);
                        }
                    }
                }
            }
        }
Beispiel #5
0
 public Searcher(StateBase initialState, StateBase goalState)
 {
     _initialState = initialState;
     _goalState    = goalState;
 }
Beispiel #6
0
 public Node Find(StateBase state)
 {
     return(state != null && _hashByState.ContainsKey(state) ? _hashByState[state] : null);
 }