Skip to content

Commit f8e35d1

Browse files
committed
Updated variable namings and README
1 parent ff4fe26 commit f8e35d1

File tree

6 files changed

+72
-54
lines changed

6 files changed

+72
-54
lines changed

README.md

+20-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
# A-Star-Java-Implementation
2-
A* also called A Star, algorithm java implementation
32

4-
This is a java implementation of the A Star algorithm. I couldn't find any good java implementations of this famous AI algorithm on the web so I decided to make my own.
3+
A*, also called A Star, is a graph traversal and path search algorithm, which is used in many fields of computer science due to its completeness, optimality, and optimal efficiency.
54

6-
The implementation includes 3 files:
7-
- AStar.java : Main algorithm class.
8-
- Node.java : Class for the nodes used by the algorithm.
9-
- AStarTest.java : Class with a main method and a simple test for the algorithm implementation
5+
The implementation includes several files:
6+
- AStar.java: Main algorithm class.
7+
- Node.java: Class for the nodes used by the algorithm.
8+
- searchstrategy package: Strategy pattern with several map search algorithms:
9+
1. Diagonals - when enabled, also considers diagonal moves
10+
2. Manhattan distance - default mode, only considers vertical and horizontal moves
1011

11-
## Search Area
12+
13+
## A* specifications
14+
15+
- For node n, gScore[n] is the cost of the cheapest path currently known from start to n.
16+
- For node n, fScore[n] = gScore[n] + heuristic(n), where gScore(n) is the distance from the starting node to n
17+
and h(n) is a heuristic value of estimation distance from node n to finish node.
18+
- h(n) is a heuristic value of estimation distance from node n to finish node
19+
20+
## Example provided
21+
22+
### Search Area
1223

1324
0 1 2 3 4 5 6
1425
0 - - - - - - -
@@ -18,7 +29,7 @@ The implementation includes 3 files:
1829
4 - - - - - - -
1930
5 - - - - - - -
2031

21-
## Search Path with diagonals
32+
### Search Path with diagonals
2233

2334
0 1 2 3 4 5 6
2435
0 - - - * - - -
@@ -28,7 +39,7 @@ The implementation includes 3 files:
2839
4 - - - - - - -
2940
5 - - - - - - -
3041

31-
## Search Path without diagonals
42+
### Search Path without diagonals
3243

3344
0 1 2 3 4 5 6
3445
0 - - * * * - -

app/src/main/java/com/ai/astar/domain/AStar.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
package com.ai.astar.domain;
22

33
import com.ai.astar.domain.searchstrategy.DiagonalMapChecker;
4-
import com.ai.astar.domain.searchstrategy.HorizontalVerticalChecker;
4+
import com.ai.astar.domain.searchstrategy.ManhattanMapChecker;
55
import com.ai.astar.domain.searchstrategy.MapChecker;
66
import com.ai.astar.domain.searchstrategy.NoOpChecker;
77

88
import java.util.*;
99

1010
public class AStar {
11-
private static final int DEFAULT_HV_COST = 10; // Horizontal - Vertical Cost
11+
private static final int DEFAULT_MANHATTAN_COST = 10;
1212
private static final int DEFAULT_DIAGONAL_COST = 14;
1313
private final Node[][] searchArea;
1414
private final PriorityQueue<Node> openList;
1515
private final Set<Node> closedSet;
1616
private final Node initialNode;
1717
private final Node finalNode;
1818
private final MapChecker diagonalsChecker;
19-
private final MapChecker hvChecker;
19+
private final MapChecker manhattanChecker;
2020

2121
public AStar(
2222
int rows,
@@ -30,7 +30,7 @@ public AStar(
3030
this.initialNode = initialNode;
3131
this.finalNode = finalNode;
3232
this.searchArea = new Node[rows][cols];
33-
this.openList = new PriorityQueue<>(Comparator.comparingInt(Node::f));
33+
this.openList = new PriorityQueue<>(Comparator.comparingInt(Node::fScore));
3434
initNodes();
3535
initBlocks(blocksArray);
3636
this.closedSet = new HashSet<>();
@@ -39,11 +39,11 @@ public AStar(
3939
} else {
4040
this.diagonalsChecker = new NoOpChecker(null, null, null);
4141
}
42-
this.hvChecker = new HorizontalVerticalChecker(searchArea, openList, closedSet, hvCost);
42+
this.manhattanChecker = new ManhattanMapChecker(searchArea, openList, closedSet, hvCost);
4343
}
4444

4545
public AStar(int rows, int cols, Node initialNode, Node finalNode, int[][] blocksArray, boolean searchDiagonals) {
46-
this(rows, cols, initialNode, finalNode, blocksArray, searchDiagonals, DEFAULT_DIAGONAL_COST, DEFAULT_HV_COST);
46+
this(rows, cols, initialNode, finalNode, blocksArray, searchDiagonals, DEFAULT_DIAGONAL_COST, DEFAULT_MANHATTAN_COST);
4747
}
4848

4949
private void initNodes() {
@@ -105,17 +105,17 @@ private void addAdjacentNodes(Node currentNode) {
105105

106106
private void addAdjacentLowerRow(Node currentNode, int row, int col) {
107107
diagonalsChecker.checkNode(currentNode, col, row + 1);
108-
hvChecker.checkNode(currentNode, col, row + 1);
108+
manhattanChecker.checkNode(currentNode, col, row + 1);
109109
}
110110

111111
private void addAdjacentMiddleRow(Node currentNode, int row, int col) {
112-
hvChecker.checkNode(currentNode, col - 1, row);
113-
hvChecker.checkNode(currentNode, col + 1, row);
112+
manhattanChecker.checkNode(currentNode, col - 1, row);
113+
manhattanChecker.checkNode(currentNode, col + 1, row);
114114
}
115115

116116
private void addAdjacentUpperRow(Node currentNode, int row, int col) {
117117
diagonalsChecker.checkNode(currentNode, col, row - 1);
118-
hvChecker.checkNode(currentNode, col, row - 1);
118+
manhattanChecker.checkNode(currentNode, col, row - 1);
119119
}
120120

121121
private boolean isFinalNode(Node currentNode) {

app/src/main/java/com/ai/astar/domain/Node.java

+20-13
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
public class Node {
44

5-
private int g;
6-
private int f;
7-
private int heuristic;
5+
private int gScore;
6+
private int fScore;
7+
private int h;
88
private final int row;
99
private final int col;
1010
private boolean isBlocked;
@@ -21,31 +21,38 @@ public static Node of(int row, int col) {
2121
}
2222

2323
public void calculateHeuristic(Node finalNode) {
24-
this.heuristic = Math.abs(finalNode.row() - row) + Math.abs(finalNode.col() - col);
24+
this.h = Math.abs(finalNode.row() - row) + Math.abs(finalNode.col() - col);
2525
}
2626

2727
public void updateNode(Node currentNode, int cost) {
28-
int gCost = currentNode.g() + cost;
2928
this.parent = currentNode;
30-
this.g = gCost;
31-
this.f = g + heuristic;
29+
updateGScore(currentNode.gScore(), cost);
30+
updateFScore();
3231
}
3332

3433
public boolean checkBetterPath(Node currentNode, int cost) {
35-
int gCost = currentNode.g() + cost;
36-
if (gCost < g()) {
34+
int updatedScore = currentNode.gScore() + cost;
35+
if (updatedScore < gScore()) {
3736
updateNode(currentNode, cost);
3837
return true;
3938
}
4039
return false;
4140
}
4241

43-
public int g() {
44-
return g;
42+
public int gScore() {
43+
return gScore;
4544
}
4645

47-
public int f() {
48-
return f;
46+
private void updateGScore(int currentGScore, int cost) {
47+
this.gScore = currentGScore + cost;
48+
}
49+
50+
public int fScore() {
51+
return fScore;
52+
}
53+
54+
private void updateFScore() {
55+
this.fScore = gScore + h;
4956
}
5057

5158
public Node parent() {

app/src/main/java/com/ai/astar/domain/searchstrategy/HorizontalVerticalChecker.java

-21
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.ai.astar.domain.searchstrategy;
2+
3+
import com.ai.astar.domain.Node;
4+
5+
import java.util.PriorityQueue;
6+
import java.util.Set;
7+
8+
public class ManhattanMapChecker extends MapChecker {
9+
10+
private final int manhattanCost;
11+
12+
public ManhattanMapChecker(Node[][] searchArea, PriorityQueue<Node> openList, Set<Node> closedSet, int manhattanCost) {
13+
super(searchArea, openList, closedSet);
14+
this.manhattanCost = manhattanCost;
15+
}
16+
17+
@Override
18+
public void checkNode(Node currentNode, int col, int row) {
19+
check(currentNode, col, row, manhattanCost);
20+
}
21+
}

app/src/main/java/com/ai/astar/domain/searchstrategy/MapChecker.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ protected MapChecker(Node[][] searchArea, PriorityQueue<Node> openList, Set<Node
1818
this.columns = searchArea == null ? 0 : searchArea[0].length;
1919
}
2020

21-
void check(Node currentNode, int col, int row, int cost) {
21+
protected void check(Node currentNode, int col, int row, int cost) {
2222
if (row < 0 || row >= searchArea.length) {
2323
return;
2424
}

0 commit comments

Comments
 (0)