0

我尝试实施Greedy Best First Search。 My图表和启发式是这样的:构造函数的值未能获得值并分配给变量

enter image description here

来源:的目的地:G.正确的方法是:SACE G.

,我看到那里的是,他不采取hNod问题从构造函数,当我宣布节点它是:Node s = new Node("S", 12); 我打印出节点的hNod在我尝试调试,但我没有得到问题的地方。

这里是我的代码:

package com.gbfs.algorithm; 

import java.util.ArrayList; 
import java.util.Collections; 
import java.util.Comparator; 
import java.util.HashSet; 
import java.util.List; 
import java.util.PriorityQueue; 
import java.util.Set; 

class Node{ 
    public String numeNod; 
    public int hNod; 
    public Node parent; 
    public Edge[] adjacencies = new Edge[]{}; 

    public Node(String numeNod, int hNod){ 
      this.numeNod = numeNod; 
      hNod = this.hNod; 
    } 

    public String toString(){ 
      return numeNod; 
    }  
} 

class Edge{ 
    public Node target; 

    public Edge[] adjacencies = new Edge[]{}; 

    public Edge(Node target){ 
      this.target = target; 
    } 
} 

public class GreedyBFS { 

    public static void main(String[] args){ 


     Node s = new Node("S", 12); 
     Node a = new Node("A", 5); 
     Node b = new Node("B", 5); 
     Node c = new Node("C", 5); 
     Node d = new Node("D", 2); 
     Node e = new Node("E", 2); 
     Node f = new Node("F", 1); 
     Node h = new Node("H", 1); 
     Node g = new Node("G", 0); 

     s.adjacencies = new Edge[]{ 
       new Edge(b), 
       new Edge(a) 
     }; 

     b.adjacencies = new Edge[]{ 
       new Edge(d), 
       new Edge(g) 
     }; 

     d.adjacencies = new Edge[]{ 
       new Edge(g), 
       new Edge(h) 
     }; 

     h.adjacencies = new Edge[]{ 
       new Edge(f) 
     }; 

     a.adjacencies = new Edge[]{ 
       new Edge(g), 
       new Edge(c) 
     }; 

     c.adjacencies = new Edge[]{ 
       new Edge(e) 
     }; 

     e.adjacencies = new Edge[]{ 
       new Edge(g) 
     }; 

     g.adjacencies = new Edge[] { 
       new Edge(b), 
       new Edge(e), 
       new Edge(c), 
       new Edge(a) 
     }; 

     f.adjacencies = new Edge[] { 
       new Edge(h) 
     }; 

     GreedyBFS(s, g); 

     List<Node> path = printPath(g); 

     System.out.println("Path: " + path); 
} 

    public static void GreedyBFS(final Node source, final Node goal) { 

     Set<Node> explored = new HashSet<Node>(); 

     PriorityQueue<Node> queue = new PriorityQueue<Node>(8, new Comparator<Node>() { 

      @Override 
      public int compare(Node o1, Node o2) { 

       if(o1.hNod <= o2.hNod) { 
        System.out.println("Primu if: o1.hnod " + o1.hNod); 
        System.out.println("Primu if: o2.hnod " + o2.hNod); 
        return 1; 
       } 

       else if(o1.hNod >= o2.hNod) { 
        System.out.println("2 if: o1.hnod " + o1.hNod); 
        System.out.println("2 if: o2.hnod " + o2.hNod); 

        return -1; 
       } 

       else 
        return 0; 
      } 
     }); 

     queue.add(source); 

     boolean found = false; 

     while(!queue.isEmpty() && !found) { 

      Node current = (Node) queue.poll(); 

      explored.add(current); 

      if(current.numeNod.equals(goal.numeNod)){ 
       found = true; 
      } 

      for(Edge o : current.adjacencies) { 

       Node child = o.target; 
       int temp_hNod = current.hNod; 
       System.out.println("temp_hnod = current.Hnod " + temp_hNod); 

       if(explored.contains(child) && (temp_hNod >= child.hNod)) { 
        continue; 
       } 

       else if(!(queue.contains(child)) || (temp_hNod < child.hNod)) { 

        child.parent = current; 
        child.hNod = temp_hNod; 

        if(queue.contains(child)) { 
         queue.remove(child); 
        } 

        queue.add(child); 

       } 
      } 
     }   
    } 

    public static List<Node> printPath(Node target){ 

     List<Node> path = new ArrayList<Node>(); 

     for(Node node = target; node!=null; node = node.parent){ 
      path.add(node); 
     } 

     Collections.reverse(path); 

     return path; 
    } 

} 
+0

你'Comparator'是没有意义的,如果,否则,如果案件有重叠'='而且还'hNod'始终为0,两个节点 –

+0

这是一个之外,还有几件事情。我不明白你的数据模型,它看起来像是边缘是双向的并且有权重,在Java中它们似乎被建模为单向(其中一些被相反),我没有看到权重。我不知道它是否重要 –

回答

0

这种分配去错误方式:hNod = this.hNod;。比较它上面的行。它应该是this.hNod = hNod;

您的IDE应该警告过您这里有点可疑。当你的程序没有像预期的那样行事时,你可以经常从试图了解你得到的警告信息中得知原因。

+0

我没有看到,但现在他取得父母的价值并保持这样的状态。 12到处 –

+0

看来你的for循环把'current'的'hNod'赋给'current'的所有chidren。难道这不是12被分发到所有节点的原因吗? –

0

我改变了你说的,现在我看到问题出在哪里。我大胆地认为我错了。

因此,贪婪的bfs比较每个节点的启发式并进入最低的一个。所以我把一个节点排除在队列之外,我说他是一个孩子,我怎么知道那个父母的所有孩子可以比较他们之间的?这是我失败的地方,我知道在我的更新的代码是什么与粗体我比较完全相同的事情,但我不知道如何写代码.... :(

这是我的代码更新:

package com.gbfs.algorithm; 

import java.util.ArrayList; 
import java.util.Collections; 
import java.util.Comparator; 
import java.util.HashSet; 
import java.util.List; 
import java.util.PriorityQueue; 
import java.util.Set; 

class Node{ 
    public final String numeNod; 
    public final int hNod; 
    public Node parent; 
    public Edge[] adjacencies = new Edge[]{}; 
    public int temp_hNod; 

    public Node(String numeNod, int hNod){ 
      this.numeNod = numeNod; 
      this.hNod = hNod; 
    } 

    public String toString(){ 
      return numeNod; 
    }  
} 

class Edge{ 
    public Node target; 

    public Edge[] adjacencies = new Edge[]{}; 

    public Edge(Node target){ 
      this.target = target; 
    } 
} 

public class GreedyBFS { 

    public static void main(String[] args){ 


     Node s = new Node("S", 12); 
     Node a = new Node("A", 5); 
     Node b = new Node("B", 5); 
     Node c = new Node("C", 5); 
     Node d = new Node("D", 2); 
     Node e = new Node("E", 2); 
     Node f = new Node("F", 1); 
     Node h = new Node("H", 1); 
     Node g = new Node("G", 0); 

     s.adjacencies = new Edge[]{ 
       new Edge(b), 
       new Edge(a) 
     }; 

     b.adjacencies = new Edge[]{ 
       new Edge(g), 
       new Edge(d) 
     }; 

     d.adjacencies = new Edge[]{ 
       new Edge(g), 
       new Edge(h) 
     }; 

     h.adjacencies = new Edge[]{ 
       new Edge(f) 
     }; 

     a.adjacencies = new Edge[]{ 
       new Edge(g), 
       new Edge(c) 
     }; 

     c.adjacencies = new Edge[]{ 
       new Edge(e) 
     }; 

     e.adjacencies = new Edge[]{ 
       new Edge(g) 
     }; 

     GreedySearch(s, g); 

     List<Node> path = printPath(g); 

     System.out.println("Path: " + path); 
} 

    public static void GreedySearch(Node source, Node goal) { 

     Set<Node> explored = new HashSet<Node>(); 

     PriorityQueue<Node> queue = new PriorityQueue<Node>(8, new Comparator<Node>() { 

      @Override 
      public int compare(Node o1, Node o2) { 

       if(o1.hNod > o2.hNod) { 
        return 1; 
       } 

       else if(o1.hNod <o2.hNod){ 

        return -1; 
       } 

       else 
        return 0; 
      } 
     }); 

     queue.add(source); 

     boolean found = false; 

     while(!queue.isEmpty() && !found) { 

      Node current = (Node) queue.poll(); 

      explored.add(current); 

      if(current.numeNod.equals(goal.numeNod)){ 
       found = true; 
      } 

      for(Edge o : current.adjacencies) { 

       **Node child = o.target;** 

       if(explored.contains(child) && (**child.hNod >= child.hNod**)) { 
        continue; 
       } 

       else if(!(queue.contains(child)) || (**child.hNod < child.hNod**)) { 

        child.parent = current; 

        if(queue.contains(child)) { 
         queue.remove(child); 
        } 

        queue.add(child); 

       } 
      } 
     }   
    } 

    public static List<Node> printPath(Node target){ 

     List<Node> path = new ArrayList<Node>(); 

     for(Node node = target; node!=null; node = node.parent){ 
      path.add(node); 
     } 

     Collections.reverse(path); 

     return path; 
    } 

}