Skip to content

Instantly share code, notes, and snippets.

@aiscool
Created April 11, 2016 14:43
Show Gist options
  • Save aiscool/b96f32fbfb5e2313c01dc68cd3c74ae7 to your computer and use it in GitHub Desktop.
Save aiscool/b96f32fbfb5e2313c01dc68cd3c74ae7 to your computer and use it in GitHub Desktop.
This A* Algorithm is not follow the standard(maybe?) since I was not calculate the diagonal part. This code has been written in java and credit to http://www.codebytes.in/2015/02/a-shortest-path-finding-algorithm.html
//source : http://www.codebytes.in/2015/02/a-shortest-path-finding-algorithm.html
import java.util.*;
public class AStar {
public static final int V_H_COST = 1;
static class Cell{
int heuristicCost = 0; //Heuristic cost
int finalCost = 0; //G+H
int i, j;
Cell parent;
Cell(int i, int j){
this.i = i;
this.j = j;
}
@Override
public String toString(){
return "["+this.i+", "+this.j+"]";
}
}
//Blocked cells are just null Cell values in grid
static Cell [][] grid = new Cell[5][5];
static PriorityQueue<Cell> open;
static boolean closed[][];
static int startI, startJ;
static int endI, endJ;
public static void setBlocked(int i, int j){
grid[i][j] = null;
}
public static void setStartCell(int i, int j){
startI = i;
startJ = j;
}
public static void setEndCell(int i, int j){
endI = i;
endJ = j;
}
static void checkAndUpdateCost(Cell current, Cell t, int cost){
if(t == null || closed[t.i][t.j])
return;
int t_final_cost = t.heuristicCost+cost;
boolean inOpen = open.contains(t);
if(!inOpen || t_final_cost<t.finalCost){
t.finalCost = t_final_cost;
t.parent = current;
if(!inOpen)open.add(t);
}
}
public static void AStar(){
//add the start location to open list.
open.add(grid[startI][startJ]);
Cell current;
while(true){
current = open.poll();
if(current==null)
break;
closed[current.i][current.j]=true;
if(current.equals(grid[endI][endJ])){
return;
}
Cell t;
if(current.i-1>=0){
t = grid[current.i-1][current.j];
checkAndUpdateCost(current, t, current.finalCost+V_H_COST);
}
if(current.j-1>=0){
t = grid[current.i][current.j-1];
checkAndUpdateCost(current, t, current.finalCost+V_H_COST);
}
if(current.j+1<grid[0].length){
t = grid[current.i][current.j+1];
checkAndUpdateCost(current, t, current.finalCost+V_H_COST);
}
if(current.i+1<grid.length){
t = grid[current.i+1][current.j];
checkAndUpdateCost(current, t, current.finalCost+V_H_COST);
}
}
}
/*
Params :
tCase = test case No.
x, y = Board's dimensions
si, sj = start location's x and y coordinates
ei, ej = end location's x and y coordinates
int[][] antroute = array containing inaccessible cell coordinates
*/
public static void test(int tCase, int x, int y, int si, int sj, int ei, int ej, int[][] antroute){
System.out.println("\n\nTest Case #"+tCase);
//Reset
grid = new Cell[x][y];
closed = new boolean[x][y];
open = new PriorityQueue<>((Object o1, Object o2) -> {
Cell c1 = (Cell)o1;
Cell c2 = (Cell)o2;
return c1.finalCost<c2.finalCost?-1:
c1.finalCost>c2.finalCost?1:0;
});
//Set start position
setStartCell(si, sj); //Setting to 0,0 by default. Will be useful for the UI part
//Set End Location
setEndCell(ei, ej);
//Initialize all the node with g cost
for(int i=0;i<x;++i){
for(int j=0;j<y;++j){
grid[i][j] = new Cell(i, j);
grid[i][j].heuristicCost = antroute[i][j];
// if value equals to -1 it means blocked
if(antroute[i][j] == -1)
setBlocked(i,j);
// System.out.print(grid[i][j].heuristicCost+" ");
}
// System.out.println();
}
//set starting node's final cost to zero
grid[si][sj].finalCost = 0;
//Display initial map
System.out.println("Grid: ");
for(int i=0;i<x;++i){
for(int j=0;j<y;++j){
if(i==si&&j==sj)System.out.print("SO "); //Source
else if(i==ei && j==ej)System.out.print("DE "); //Destination
else if(grid[i][j]!=null)System.out.printf("%-3d ", 0);
else System.out.print("BL ");
}
System.out.println();
}
System.out.println();
AStar();
System.out.println("\nScores for cells: ");
for(int i=0;i<x;++i){
for(int j=0;j<x;++j){
if(grid[i][j]!=null)System.out.printf("%-3d ", grid[i][j].finalCost);
else System.out.print("BL ");
}
System.out.println();
}
System.out.println();
if(closed[endI][endJ]){
//Trace back the path
System.out.println("Path: ");
Cell current = grid[endI][endJ];
System.out.print(current);
while(current.parent!=null){
System.out.print(" <- "+current.parent);
current = current.parent;
}
System.out.println("\n\nFinal cost : "+grid[endI][endJ].finalCost);
}else System.out.println("No possible path");
}
public static void main(String[] args) throws Exception{
test(1, 5, 5, 2, 0, 2, 4, new int[][]{{6,5,4,3,2},
{6,-1,4,-1,1},
{5,-1,3,-1,0},
{5,4,3,2,1},
{5,4,3,-1,2}});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment