/// <summary> /// Searches the specified searcher. /// </summary> /// <param name="searcher">The searcher.</param> /// <returns></returns> public override Solution <T> Search(ISearcheble <T> searcher) { State <T> .StatePool.Clear(); State <T> goal = searcher.GetFinalState(); stack.Push(searcher.GetInitialState()); while (stack.Count > 0) { State <T> temp = stack.Pop(); evaluatedNodes++; greyList.Add(temp); // if the current node is the goal node. if (temp.Equals(goal)) { return(BackTrace(temp)); } List <State <T> > succerssors = searcher.GetListStates(temp); // for each neighbor of the current node: foreach (State <T> s in succerssors) { if (!blackList.Contains(s) && !greyList.Contains(s)) { s.CameFrom = temp; stack.Push(s); } } blackList.Add(temp); } // threre is no solution! return(null); }
/// <summary> /// Searches the specified searchable. /// </summary> /// <param name="searchable">The searchable.</param> /// <returns></returns> public override Solution <T> Search(ISearcheble <T> searchable) { State <T> .StatePool.Clear(); // add the first node to the open list. AddToOpenList(searchable.GetInitialState()); // all the nodes that the algo finished to handle them. HashSet <State <T> > closed = new HashSet <State <T> >(); State <T> goal = searchable.GetFinalState(); // while the open list is not empty. while (OpenListSize > 0) { State <T> n = PopOpenList(); closed.Add(n); // if the node that we handle is the goal stateItem. if (n.Equals(goal)) { return(BackTrace(n)); } List <State <T> > succerssors = searchable.GetListStates(n); // for each nieghbor of current node foreach (State <T> s in succerssors) { // if the node is bot in the open or close list. if (!closed.Contains(s) && !OpenListContains(s)) { s.CameFrom = n; s.Cost = n.Cost + we.GetWeight(n, s); AddToOpenList(s); } else { // if the new Cost is lower then the current Cost. if (n.Cost + we.GetWeight(n, s) < s.Cost) { s.Cost = n.Cost + we.GetWeight(n, s); if (!OpenListContains(s)) { AddToOpenList(s); } else { RemoveFromOpenList(s); AddToOpenList(s); } } } } } /*there is no solution*/ return(null); }