我试图用Eclipse在Java中实现AStar算法。我在图中的位置由对象表示。我使用TreeSet来存储位置,并为特定于对象的排序实现了一个比较器。但是,在代码的一行上,应该从TreeSet中删除当前对象,这是行不通的。我设法改为使用pollFirst(),并且该算法有效。但是我找不到treeSet.remove(Object)不起作用的原因。
我添加了布尔值equals和compareTo。两者都是正确的,因此根据equals和compareTo current等于openSet.first(),但是openSet.remove(current)无法从openSet中删除电流
我添加了整个代码!我在具有大量测试用例的代码战中对其进行了测试,因此如果我使用pollFirst()而不是remove(current)
,则代码可以正常工作编辑:在阅读JavaDoc for Set Interface(https://docs.oracle.com/javase/7/docs/api/java/util/Set.html)之后,我发现了以下警告:
如果将可变对象用作集合元素,则必须格外小心。如果在对象是集合中的一个元素的情况下,如果对象的值以影响相等比较的方式更改,则不会指定集合的行为。
我怀疑这与我的问题有关。但是,当我将openSet.remove(current)替换为openSet.pollFirst()时,为什么程序可以正常工作仍然很奇怪
Edit2:我根据Loris Securo的建议更改了Comparator。不幸的是,它仍然无法正常工作
public class Finder implements Comparable<Finder> {
public int i; // All integers are initialized to zero.
public int j;
public int id;
public int fScore;
public int gScore;
public ArrayList<Finder> neighbours = new ArrayList<Finder>();
public Object character;
@Override
public String toString() {
return "Finder [i=" + i + ", j=" + j + ", gScore=" + gScore + ",fScore =" + fScore + ", id=" + id
+ ", character=" + character + "]";
}
public static void main(String[] args) {
String a = "......\n" + "......\n" + "......\n" + "......\n" + "......\n" + "......";
Object[] mazeParts = a.split("\n");
System.out.println("mazeParts is " + Arrays.toString(mazeParts));
Object[][] maze = new Object[mazeParts.length][];
int r = 0;
for (Object t : mazeParts) {
System.out.println("t is " + t);
maze[r++] = ((String) t).split("");
}
Finder[][] mazeOfnodes = new Finder[mazeParts.length][maze[0].length];
int id = 0;
for (int i = 0; i < maze.length; i++) {
for (int j = 0; j < maze[i].length; j++) {
System.out.println("maze" + i + j + " is " + maze[i][j]);
Finder node = new Finder();
node.character = maze[i][j];
node.i = i;
node.j = j;
node.id = id;
mazeOfnodes[i][j] = node;
id++;
if (i == 0 && j == 0) {
node.fScore = heuristic(i, j, mazeOfnodes);
node.gScore = 0;
} else {
node.fScore = Integer.MAX_VALUE;
node.gScore = Integer.MAX_VALUE;
}
}
}
for (int i = 0; i < mazeOfnodes.length; i++) {
for (int j = 0; j < maze[i].length; j++) {
mazeOfnodes[i][j].neighbours = mazeOfnodes[i][j].findNeighbours(i, j, mazeOfnodes);
System.out.println("mazeOfnodes is " + mazeOfnodes[i][j]);
}
}
}
public static int findWay(Finder[][] myMaze) {
Finder goal = myMaze[myMaze.length - 1][myMaze.length - 1];
TreeSet<Finder> openSet = new TreeSet<Finder>();
openSet.add(myMaze[0][0]);
TreeSet<Finder> closedSet = new TreeSet<Finder>();
while (openSet.size() != 0) {
Finder current = openSet.first();
if (current == goal) {
return current.gScore;
}
boolean equals = current.equals(openSet.first());
boolean compareTo = (current.compareTo(openSet.first()) == 0);
openSet.remove(current); //if I use openSet.pollFirst() the code works fine. I tested it on Codewars with several huge test cases
boolean success = openSet.remove(current);
System.out.println("success is " + success);
closedSet.add(current);
for (Finder s : current.neighbours) {
if (closedSet.contains(s)) {
continue;
}
int tentative_gScore = current.gScore + 1;
if (tentative_gScore >= s.gScore) {
continue;
}
s.gScore = tentative_gScore;
s.fScore = s.gScore + heuristic(s.i, s.j, myMaze);
if (!openSet.contains(s) && !s.character.equals("W")) {
openSet.add(s);
}
}
}
return -1;
}
private ArrayList<Finder> findNeighbours(int i, int j, Finder[][] maze) {
if (i > 0) {
neighbours.add(maze[i - 1][j]);
}
if (i < maze.length - 1) {
neighbours.add(maze[i + 1][j]);
}
if (j < maze.length - 1) {
neighbours.add(maze[i][j + 1]);
}
if (j > 0) {
neighbours.add(maze[i][j - 1]);
}
return neighbours;
}
public static int heuristic(int i, int j, Object[][] mazeFinal) {
int distance = (int) Math.sqrt(Math.pow(mazeFinal.length - 1 - i, 2) + Math.pow(mazeFinal.length - 1 - j, 2));
return distance;
public int compareTo(Finder2 arg0) {
if (this.fScore < arg0.fScore) {
return -1;
} else if (this.id == arg0.id) { // If id is the same, fScore is the same too
return 0;
} else if (this.fScore == arg0.fScore) { //If id is different, fScore could still be the same
return 0;
} else {
return 1;
}
}
答案 0 :(得分:1)
您的 在您的情况下,如果 由于这种不一致,树可能无法找到特定的对象,因为它无法正确选择在左分支或右分支执行搜索。 例如,如果您有3个具有相同 然后添加B, 然后添加C, 现在您要删除存在的A,但是使用比较器,A大于B,因此树检查右分支,在那里找到C并停止,因此该方法无法执行查找和删除A 此博客文章Algosome - Java: The misouse of a TreeSet中也讨论了此问题,该博客文章基于此论坛主题:Java Programming Forum - TreeSet contains(obj) doe not behave as expected。compareTo
方法无法在TreeSet
上正常工作,因为它不满足以下规则:如果A> B则B
fScore
等于(并且id
不同),则A> B,而且B> A。TreeSet
由基于compareTo
方法构建的自平衡二叉树支持。fScore
的对象A,B和C:
compareTo
说它大于A,因此它可以生成此树(现在的根是B): B
/
A
compareTo
说它大于B,因此它可以生成此树: B
/ \
A C
pollFirst
可以只选择最左边的对象,因此它始终能够删除某些内容,但是删除的对象将取决于实现。