/// <summary> /// DFS search on searchable. /// </summary> /// <param name="searchable">The searchable to search.</param> /// <returns>the search States Tree</returns> private TreeSearchResult <T> DFSSearch(ISearchable <T> searchable) { TreeSearchResult <T> resultTree = new TreeSearchResult <T>(searchable.GetInitialState()); // closed list - HashSet HashSet <State <T> > visited = new HashSet <State <T> >(); // open list - Stack Stack <State <T> > pending = new Stack <State <T> >(); pending.Push(searchable.GetInitialState()); while (pending.Count > 0) { State <T> currState = pending.Pop(); if (!visited.Contains(currState)) { foreach (State <T> state in searchable.GetReachableStatesFrom(currState)) { if (!pending.Contains(state) && !visited.Contains(state)) { // found a new State - add to stack pending.Push(state); // add to the result tree as currState's child resultTree.Add(state, currState); } } } visited.Add(currState); } return(resultTree); }
/// <summary> /// Searches the specified searchable. /// </summary> /// <param name="searchable">The searchable to search.</param> /// <returns> /// the search Path resulted by the search /// </returns> public PathSearchResult <T> Search(ISearchable <T> searchable) { TreeSearchResult <T> tree = this.DFSSearch(searchable); State <T> goal = tree.Find(searchable.GetGoalState()); if (goal != null) { return(new PathSearchResult <T>(goal)); } return(null); }
/// <summary> /// Branches the specified searchable. /// </summary> /// <param name="searchable">The searchable.</param> /// <returns> /// a Search States Tree /// </returns> public TreeSearchResult <T> Branch(ISearchable <T> searchable) { TreeSearchResult <T> result = new TreeSearchResult <T>(searchable.GetInitialState()); // closed list - HashSet HashSet <State <T> > visited = new HashSet <State <T> >(); // opened list - RandomList RandomList <State <T> > pending = new RandomList <State <T> >(); pending.RandomInsert(searchable.GetInitialState()); while (pending.Count > 0) { State <T> currState = pending.RandomRemoval(); if (!visited.Contains(currState)) { foreach (State <T> state in searchable.GetReachableStatesFrom(currState)) { if (!visited.Contains(state)) { if (!pending.Contains(state)) { // found a brand new State pending.RandomInsert(state); result.Add(state, currState); } else if (this.RandomBool()) { // change the father of state in the tree result.RemoveLeaf(state); result.Add(state, currState); } } } visited.Add(currState); } } return(result); }