解決済み:申し訳ありません。パスを不適切に再構築していました。closedSet には最初から最後まですべてのウェイポイントしかないと思っていましたが、他のウェイポイントもいくつかあります。私は概念を理解していません。今では正常に動作しています!
私はまだA *に問題があります。
私のキャラクターは自分のパスを見つけていますが、マップ上のどこをクリックしたかによって、アルゴリズムが最短パスまたはパスを見つけることがありますが、選択すべきではない多くのノードがあります。
WikipediaとA* Pathfinding for Beginner's implementationに従おうとしましたが、同じ結果が得られました。ヒューリスティックなのかアルゴリズム自体なのかはわかりませんが、何かがおかしいです。
これは、2 つの異なるノードをクリックする際の問題の例です: http://i.imgur.com/gtgxi.jpg
Pathfind クラスは次のとおりです。
import java.util.ArrayList;
import java.util.Collections;
import java.util.TreeSet;
public class Pathfind {
public Pathfind(){
}
public ArrayList<Node> findPath(Node start, Node end, ArrayList<Node> nodes){
ArrayList<Node> openSet = new ArrayList<Node>();
ArrayList<Node> closedSet = new ArrayList<Node>();
Node current;
openSet.add(start);
while(openSet.size() > 0){
current = openSet.get(0);
current.setH_cost(ManhattanDistance(current, end));
if(start == end) return null;
else if(closedSet.contains(end)){
System.out.println("Path found!");
return closedSet;
}
openSet.remove(current);
closedSet.add(current);
for(Node n : current.getNeigbours()){
if(!closedSet.contains(n)){
if(!openSet.contains(n) || (n.getG_cost() < (current.getG_cost()+10))){
n.setParent(current);
n.setG_cost(current.getG_cost()+10);
n.setH_cost(ManhattanDistance(n, end));
if(!openSet.contains(n))
openSet.add(n);
Collections.sort(openSet);
}
}
}
}
return null;
}
private int ManhattanDistance(Node start, Node end){
int cost = start.getPenalty();
int fromX = start.x, fromY = start.y;
int toX = end.x, toY = end.y;
return cost * (Math.abs(fromX - toX) + Math.abs(fromY - toY));
}
}