I designed my own aStar algorithm but i am afraid it is running a bit slow. Can anyone please look at it and let me know how it can be improved.
public class PathFinder {
private int endX, endY;
private int startX, startY;
private int[][] map;
private ArrayList<Node> open = new ArrayList<Node>();
public static ArrayList<Node> closed = new ArrayList<Node>();
public PathFinder(int[][] map){
this.map = map;
}
public ArrayList<Node> findPath(Node start, Node end){
open.clear();
closed.clear();
this.endX = end.x;
this.endY = end.y;
this.startX = start.x;
this.startY = start.y;
open.add(start);
while (open.size() > 0){
if (open.get(0).x == endX && open.get(0).y == endY){
closed.add(open.get(0));
open.remove(0);
break;
}else{
addAdj(open.get(0));
}
}
return reconstructPath();
}
long startTime;
public ArrayList<Node> findPath(int startX, int startY, int endX, int endY){
open.clear();
closed.clear();
this.endX = endX;
this.endY = endY;
this.startX = startX;
this.startY = startY;
open.add(new Node(startX, startY, startX, startY, 0));
while (open.size() > 0){
if (open.get(0).x == endX && open.get(0).y == endY){
closed.add(open.get(0));
open.remove(0);
break;
}else{
addAdj(open.get(0));
}
}
return reconstructPath();
}
private void addAdj(Node parent){
closed.add(open.get(0));
open.remove(0);
if (map[parent.y+1][parent.x] != 1 && notClosed(parent.x,parent.y+1,Math.abs(endX-parent.x)+Math.abs(endY-(parent.y+1))+parent.fCost,parent.x,parent.y)){
if (open.size()==0){
// long startTime = System.nanoTime();
open.add(new Node(parent.x,parent.y+1, parent.x, parent.y, Math.abs(endX-parent.x)+Math.abs(endY-(parent.y+1))+parent.fCost));
// System.out.println("add time" + (System.nanoTime()-startTime));
}else if (notQueued(parent.x,parent.y+1)){
startTime = System.nanoTime();
for (int i=0; i<open.size(); i++){
if (Math.abs(endX-parent.x)+Math.abs(endY-(parent.y+1))+parent.fCost<open.get(i).fCost){
open.add(i, new Node(parent.x,parent.y+1, parent.x, parent.y, Math.abs(endX-parent.x)+Math.abs(endY-(parent.y+1))+parent.fCost));
break;
}else if (i==open.size()-1){
open.add(new Node(parent.x,parent.y+1, parent.x, parent.y, Math.abs(endX-parent.x)+Math.abs(endY-(parent.y+1))+parent.fCost));
break;
}
}
}
}if (map[parent.y-1][parent.x] != 1 && notClosed(parent.x,parent.y-1,Math.abs(endX-parent.x)+Math.abs(endY-(parent.y-1))+parent.fCost,parent.x,parent.y)){
if (open.size()==0){
open.add(new Node(parent.x,parent.y-1, parent.x, parent.y, Math.abs(endX-parent.x)+Math.abs(endY-(parent.y-1))+parent.fCost));
}else if (notQueued(parent.x,parent.y-1)){
for (int i=0; i<open.size(); i++){
if (Math.abs(endX-parent.x)+Math.abs(endY-(parent.y-1))+parent.fCost<open.get(i).fCost){
open.add(i, new Node(parent.x,parent.y-1, parent.x, parent.y, Math.abs(endX-parent.x)+Math.abs(endY-(parent.y-1))+parent.fCost));
break;
}else if (i==open.size()-1){
open.add(new Node(parent.x,parent.y-1, parent.x, parent.y, Math.abs(endX-parent.x)+Math.abs(endY-(parent.y-1))+parent.fCost));
break;
}
}
}
}if (map[parent.y][parent.x+1] != 1 && notClosed(parent.x+1,parent.y,Math.abs(endX-(parent.x+1))+Math.abs(endY-parent.y)+parent.fCost,parent.x,parent.y) ){
if (open.size()==0){
open.add(new Node(parent.x+1,parent.y, parent.x, parent.y, Math.abs(endX-(parent.x+1))+Math.abs(endY-parent.y)+parent.fCost));
}else if (notQueued(parent.x+1,parent.y)){
for (int i=0; i<open.size(); i++){
if (Math.abs(endX-(parent.x+1))+Math.abs(endY-parent.y)+parent.fCost<open.get(i).fCost){
open.add(i, new Node(parent.x+1,parent.y, parent.x, parent.y, Math.abs(endX-(parent.x+1))+Math.abs(endY-parent.y)+parent.fCost));
break;
}else if (i==open.size()-1){
open.add(new Node(parent.x+1,parent.y, parent.x, parent.y, Math.abs(endX-(parent.x+1))+Math.abs(endY-parent.y)+parent.fCost));
break;
}
}
}
}if (map[parent.y][parent.x-1] != 1 && notClosed(parent.x-1,parent.y,Math.abs(endX-(parent.x-1))+Math.abs(endY-parent.y)+parent.fCost,parent.x,parent.y)){
if (open.size()==0){
open.add(new Node(parent.x-1,parent.y, parent.x, parent.y, Math.abs(endX-(parent.x-1))+Math.abs(endY-parent.y)+parent.fCost));
}else if (notQueued(parent.x-1,parent.y)){
for (int i=0; i<open.size(); i++){
if (Math.abs(endX-(parent.x-1))+Math.abs(endY-parent.y)+parent.fCost<open.get(i).fCost){
open.add(i, new Node(parent.x-1,parent.y, parent.x, parent.y, Math.abs(endX-(parent.x-1))+Math.abs(endY-parent.y)+parent.fCost));
break;
}else if (i==open.size()-1){
open.add(new Node(parent.x-1,parent.y, parent.x, parent.y, Math.abs(endX-(parent.x-1))+Math.abs(endY-parent.y)+parent.fCost));
break;
}
}
}
}
}
private boolean notClosed(int x, int y, int fCost, int parentX, int parentY){
for (int i=0; i<closed.size(); i++){
if (x == closed.get(i).x && y == closed.get(i).y){
if (closed.get(i).fCost>fCost){
closed.get(i).parentX=parentX;
closed.get(i).parentY=parentY;
}
return false;
}
}
return true;
}
private boolean notQueued(int x, int y){
for (int i=0; i<open.size(); i++){
if (x == open.get(i).x && y == open.get(i).y){
return false;
}
}
return true;
}
private ArrayList<Node> reconstructPath(){
ArrayList<Node> path = new ArrayList<Node>();
int currentParentX = closed.get(closed.size()-1).parentX;
int currentParentY = closed.get(closed.size()-1).parentY;
path.add(closed.get(closed.size()-1));
boolean finished = false;
here:
while (finished == false){
for (int i=0; i<closed.size(); i++){
if (closed.get(i).x == currentParentX && closed.get(i).y == currentParentY){
path.add(0, closed.get(i));
if (currentParentX == startX && currentParentY == startY) finished = true;
currentParentX = closed.get(i).parentX;
currentParentY = closed.get(i).parentY;
break;
}else if (i==closed.size()-1){
System.out.println("ERROR: could not reconstruct path.");
break here;
}
}
}
return path;
}
and this is the node class
public class Node {
public int x, y;
public int parentX, parentY;
public int fCost;
public Node(int x, int y, int parentX, int parentY, int fCost){
this.x = x;
this.y = y;
this.parentX = parentX;
this.parentY = parentY;
this.fCost = fCost;
}
public String toString(){
return "(" + x + ", " + y + ")";
}
}
thank you so much in advance… any help would be great! ;D