Hi! I’m trying to implement a A* Path Finding Alghoritm. It’s supposed to iterate over hundreds of Objects to see which one is closest to the target. This implementation works but it takes around 15 seconds for each object which makes it impossible to use. Can somebody help me figure out why my implementation is so slow? Thank you
public ArrayList<ArrayList<Integer>> A_Star(ArrayList<Integer> start, ArrayList<Integer> goal){
HashMap<ArrayList<Integer>, Integer> openSet = new HashMap<ArrayList<Integer>, Integer>();
openSet.put(start,1);
HashMap<ArrayList<Integer>, ArrayList<Integer>> cameFrom = new HashMap<ArrayList<Integer>, ArrayList<Integer>>();
HashMap<ArrayList<Integer>, Float> gScore = new HashMap<ArrayList<Integer>, Float>();
HashMap<ArrayList<Integer>, Float> fScore = new HashMap<ArrayList<Integer>, Float>();
// gScore := map with default value of Infinity
// fScore := map with default value of Infinity
for (int xI=0;xI<=XSIZE;xI++){
for (int yI=0;yI<=YSIZE;yI++){
ArrayList<Integer> tempArray = new ArrayList<Integer>();
tempArray.add(0,xI);
tempArray.add(1,yI);
gScore.put(tempArray, 9998.0);
fScore.put(tempArray, 9998.0);
}
}
gScore.replace(start, 0.0);
fScore.replace(start, dist(start.get(0),start.get(1),goal.get(0),goal.get(1)));
while(openSet.size()>0){
//current := the node in openSet having the lowest fScore[] value
ArrayList<Integer> current = new ArrayList<Integer>();
Float currentFscore=9999.0;
for (HashMap.Entry<ArrayList<Integer>, Integer> set : openSet.entrySet()) {
ArrayList<Integer> tempKey = set.getKey();
if (fScore.get(tempKey)<currentFscore){
currentFscore=fScore.get(tempKey);
current=tempKey;
}
}
int currentX = current.get(0);
int currentY = current.get(1);
if (currentX == goal.get(0) && currentY == goal.get(1)){
return reconstruct_path(cameFrom, current);
}
openSet.remove(current);
for(int xI2=(-1);xI2<=1;xI2++){
for (int yI2=(-1);yI2<=1;yI2++){
if (xI2!=0 || yI2!=0){
if ((((currentX)+xI2)>0) && (((currentX)+xI2)<XSIZE)){
if ((((currentY)+yI2)>0) && (((currentY)+yI2)<YSIZE)){
boolean obstacleCoord=false;
for (Obstacle obstacle : Obstacles) {
if ((((currentX)+xI2) > obstacle.getObPoint().x) && (((currentX)+xI2) < obstacle.getObPoint().x+(obstacle.getObSize().x))) {
if ((((currentY)+yI2) > obstacle.getObPoint().y) && (((currentY)+yI2) < obstacle.getObPoint().y+(obstacle.getObSize().y))) {
obstacleCoord=true;
}
}
}
if(obstacleCoord==false) {
ArrayList<Integer> neighbor = new ArrayList<Integer>();
neighbor.add(0,(currentX)+xI2);
neighbor.add(1,(currentY)+yI2);
float tentative_gScore = (gScore.get(current))+dist(neighbor.get(0),neighbor.get(1),currentX,currentY);
if (tentative_gScore < gScore.get(neighbor)){
cameFrom.put(neighbor, current);
gScore.replace(neighbor, tentative_gScore);
fScore.replace(neighbor, (tentative_gScore+dist(neighbor.get(0),neighbor.get(1),goal.get(0),goal.get(1))));
if (openSet.get(neighbor)==null){
openSet.put(neighbor,1);
}
}
}
}
}
}
}
}
}
return new ArrayList<ArrayList<Integer>>();
}
public ArrayList<ArrayList<Integer>> reconstruct_path(HashMap<ArrayList<Integer>,ArrayList<Integer>> cameFrom, ArrayList<Integer> current){
ArrayList<ArrayList<Integer>> total_path = new ArrayList<ArrayList<Integer>>();
total_path.add(0, current);
while (cameFrom.containsKey(current)){
current = cameFrom.get(current);
total_path.add(0, current);
}
return total_path;
}