我想在Java中實現A *,但是我碰到了一堵磚牆,基本上不知道如何從這一點開始。Java中的A星實現
我目前正在關注來自Wikipedia的僞代碼。
我的節點構造,像這樣:
static class Node {
int col;
int row;
int g;
int h;
int f;
public Node(int col, int row, int g, int h) {
this.col = col;
this.row = row;
this.g = g;
this.h = h;
this.f = g+h;
}
}
它重要注意到,在創建節點時f
計算。
我當前的A *代碼,這是uncomplete:
public void makeAstar(MapParser parsedMap) {
// create the open list of nodes, initially containing only our starting node
ArrayList<Node> openList = new ArrayList<Node>();
// create the closed list of nodes, initially empty
Map closedMap = new Map(rowSize, columnSize, "Closed map");
// Fill closedMap with 0's
closedMap.buildMapWithValue(0);
// Create neighbourlist
ArrayList<Node> neighbourList = new ArrayList<Node>();
// Some vars
int rowTemp = 0;
int colTemp = 0;
int currentCost = 0;
int tempCost = 0;
//TODO hardcore cost! rowTemp+colTemp
// Initialize with the first node
openList.add(new Node(0,0,9,this.getMapValue(0, 0)));
while(!openList.isEmpty()) {
// Consider the best node, by sorting list according to F
Node.sortByF(openList);
Node currentNode = openList.get(0);
openList.remove(0);
// Stop if reached goal (hardcoded for now)
if(currentNode.getCol() == 4 && currentNode.getRow() == 4) {
System.out.println("Reached goal");
break;
}
// Move current node to the closed list
closedMap.setMapValue(currentNode.getRow(), currentNode.getCol(), 1);
// Get neighbours
rowTemp = currentNode.getRow()-1;
colTemp = currentNode.getCol();
if(!currentNode.isTopRow()) { // Add value^
neighbourList.add(new Node(rowTemp, colTemp,rowTemp+colTemp,this.getMapValue(rowTemp, colTemp)));
}
rowTemp = currentNode.getRow();
colTemp = currentNode.getCol()-1;
if(!currentNode.isRightColumn()) { // Add value <
neighbourList.add(new Node(rowTemp, colTemp,rowTemp+colTemp,this.getMapValue(rowTemp, colTemp)));
}
rowTemp = currentNode.getRow();
colTemp = currentNode.getCol()+1;
if(currentNode.isLeftColumn()) { // Add value >
neighbourList.add(new Node(rowTemp, colTemp,rowTemp+colTemp,this.getMapValue(rowTemp, colTemp)));
}
rowTemp = currentNode.getRow()+1;
colTemp = currentNode.getCol();
if(currentNode.isBottomColumn()) { // Add value v
neighbourList.add(new Node(rowTemp, colTemp,rowTemp+colTemp,this.getMapValue(rowTemp, colTemp)));
}
// As long as the neighbour list is not empty
while(!neighbourList.isEmpty()) {
Node temp = neighbourList.get(0);
neighbourList.remove(0);
if(!isNotInClosedMap(temp.getRow(), temp.getCol())) {
continue;
}
//Stuck here !
}
}
}
最後一行的評論基本上是在那裏我已經結束,這相當於在僞附近for each neighbor in neighbor_nodes(current)
東西。此外,我明白,G
是從開始節點的總成本,但我如何計算這個值?
由於有些人可能會注意到在初始創建鄰居row+col
時增加了一個硬編碼的G
值,這就開始位置是0,0
而言。
'嘗試在Java中實現A *,但是我碰到了一堵磚牆。笑話是A *通常會避免磚牆。大聲笑。 :) – st0le