2017-05-17 10 views
0

反復掘り出し検索を実装しようとしています。私は何が間違っているのか分かりませんが、私はそれを正しくしているようには見えません。私はいつも無限ループに終わる。 誰かが私の間違いを指摘できますか? Depth-Limited Searchを実装し、IDSコードで使用しました。 DLS自体は問題なく動作しているようですが、IDSは理解できず、なぜ無限ループに終わっているのでしょうか。反復深化検索C#

public class IterativeDeepeningSearch<T> where T : IComparable 
{ 
    string closed; 

    public int maximumDepth; 
    public int depth = 0; 
    bool Found = false; 
    Stack<Vertex<T>> open; 

    public IterativeDeepeningSearch() 
    { 
     open = new Stack<Vertex<T>>(); 
    } 

    public bool IDS(Vertex<T> startNode, Vertex<T> goalNode) 
    { 
     // loops through until a goal node is found 
     for (int _depth = 0; _depth < Int32.MaxValue; _depth++) 
     { 
      bool found = DLS(startNode, goalNode, _depth); 
      if (found) 
      { 
       return true; 
      } 
     } 
     // this will never be reached as it 
     // loops forever until goal is found 
     return false; 
    } 

    public bool DLS(Vertex<T> startNode, Vertex<T> goalNode, int _maximumDepth) 
    { 
     maximumDepth = _maximumDepth; 

     open.Push(startNode); 

     while (open.Count > 0 && depth < maximumDepth)  
     { 
      Vertex<T> node = open.Pop();  

      closed = closed + " " + node.Data.ToString(); 
      if (node.Data.ToString() == goalNode.Data.ToString()) 
      { 
       Debug.Write("Success"); 
       Found = true; 
       break; 

      } 

      List<Vertex<T>> neighbours = node.Neighbors; 

      depth++;  

      if (neighbours != null) 
      { 
       foreach (Vertex<T> neighbour in neighbours) 
       { 
        if (!closed.Contains(neighbour.ToString())) 
         open.Push(neighbour); 
       } 
       Debug.Write("Failure"); 
      } 


     } 
     Console.WriteLine(closed); 
     return Found; 
    } 

} 
} 

PS:私の頂点クラスがちょうど_depth上のループを反復処理するための2つのプロパティ、データおよび子供

+1

ある深さを、渡していますSimoneが言ったように、間違った議論(深度の代わりに_depthを使用)でDLSを呼び出しています。さらに、深さの上限をグラフのサイズに制限する必要があります。前処理でグラフノードを数えることで実行できます。 – Mockingbird

+0

'Int32.MaxValue;'は本当に永遠に実行されないことを意味します。ちょうど数年待って、それは停止します。 –

答えて

1

ザ・を持っていますが、DLS機能には、必ず0

for (int _depth = 0; _depth < Int32.MaxValue; _depth++) 
    { 
     bool found = DLS(startNode, goalNode, depth); 
     if (found) 
     { 
      return true; 
     } 
    } 
+0

私はちょうどそれを変更し、それはまだ無限大です – Joe

関連する問題