我尝试实施Greedy Best First Search。 My图表和启发式是这样的:构造函数的值未能获得值并分配给变量
来源:的目的地: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;
}
}
你'Comparator'是没有意义的,如果,否则,如果案件有重叠'='而且还'hNod'始终为0,两个节点 –
这是一个之外,还有几件事情。我不明白你的数据模型,它看起来像是边缘是双向的并且有权重,在Java中它们似乎被建模为单向(其中一些被相反),我没有看到权重。我不知道它是否重要 –