-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAstart.java
More file actions
150 lines (126 loc) · 4.42 KB
/
Copy pathAstart.java
File metadata and controls
150 lines (126 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Astart {
public ArrayList<Fiver> pq = new ArrayList<Fiver>();
public Fiver[][] parent; //each cell in the parent matrix contains the parent coordinates of the specific cell we are in
boolean[][] isPopped;
// initializing all the vars
public void init (int x, int y) {
parent = new Fiver[x][y];
isPopped = new boolean[x][y];
for (int i=0; i<x; i++) {
for (int j=0; j<y; j++) {
isPopped[i][j]=false;
}
}
}
//comparator, first will order by the f function, if its equal we will sort by the direction as requested in the instructions
class fComp implements Comparator<Fiver> {
public int compare (Fiver t1, Fiver t2) {
if (t1.f > t2.f) return 1;
if (t1.f < t2.f) return -1;
if (t1.direction > t2.direction) return 1;
if (t1.direction < t2.direction) return -1;
return 0;
}
}
// Function to perform the A* search traversal
public void aStart (char grid[][], int startRow, int startCol, int totalRow, int totalCol, int goalRow, int goalCol) {
init(totalRow*2, totalCol*2);
pq.add(new Fiver(startRow, startCol, 0)); //adding the first cell to the Q
while (!pq.isEmpty()) { // the algorithm runs till the Q is empty or we reach the goal location
Fiver point = pq.get(0);
int x = point.first;
int y = point.second;
int g = point.g;
pq.remove(0);
isPopped[x][y] = true; //after popping the location we will mark it as we visited the cell
if (General.getInstance().goalTest(x, y, goalRow, goalCol)) {
break;
}
// Go to the adjacent cells
ArrayList<Cell> nextStates = General.getInstance().successor(x, y);
for (Cell c : nextStates) {
if (!isPopped[c.getX()][c.getY()]) { // checking each time if the states we have received from the successor were visited, if not we will add them to the Q
if (pq.contains(new Fiver(c.getX(), c.getY()))) { //if it is in the Q we will compare the heuristic and the f length and will the the lower one
int i = pq.indexOf(new Fiver(c.getX(), c.getY()));
if (pq.get(i).f > g+1+c.getHeuristic()) {
pq.get(i).f = g+1+c.getHeuristic();
pq.get(i).direction = c.getDirection();
parent[c.getX()][c.getY()] = new Fiver(x, y, 0, 0, c.getDirection());
}
}
else { // else we will add the new location to the Q
pq.add(new Fiver(c.getX(), c.getY(), g+1, g+1+c.getHeuristic(), c.getDirection()));
parent[c.getX()][c.getY()] = new Fiver(x, y, 0, 0, c.getDirection());
}
}
}
Collections.sort(pq, new fComp()); // sorting the arraylist each time
}
//printing all the needed details
ArrayList<Fiver> route = route(goalRow, goalCol, startRow, startCol);
Collections.reverse(route);
/*System.out.println("Alg Name : A*");
System.out.println("Input : " + Main.fileName);*/
System.out.print("Path : ");
printRoute(route);
System.out.println("Cost : " + (route.size())); //we count the start state and the goal state
System.out.println("Visit Count : " + (calcNumOfVisitedNodes(totalRow*2, totalCol*2))); //we count the start state and the goal state
}
//this method gets: goal location and start location and returns: the route that the algorithm found
public ArrayList<Fiver> route (int goalRow, int goalCol, int startRow, int startCol) {
ArrayList<Fiver> path = new ArrayList<Fiver>();
Fiver point = new Fiver(goalRow, goalCol);
while (point != null) {
Fiver t = new Fiver();
t.first = (point.first)/2;
t.second = (point.second)/2;
t.direction = point.direction;
path.add(t);
point = parent[point.first][point.second];
}
return path;
}
// counting the number of nodes visited
public int calcNumOfVisitedNodes (int totalRow, int totalCol) {
int counter = 0;
for (int i=0; i<totalRow; i++) {
for (int j=0; j<totalCol; j++) {
if (isPopped[i][j] == true) {
counter++;
}
}
}
return counter;
}
public void printRoute (ArrayList<Fiver> route) {
String dir ="";
for (int i=0; i<route.size(); i++) {
switch (route.get(i).direction) {
case 1:
dir = "UP";
break;
case 2:
dir = "RIGHT";
break;
case 3:
dir = "DOWN";
break;
case 4:
dir = "LEFT";
break;
default:
dir="";
break;
}
if (dir != "") {
System.out.print(route.get(i).toString() + " -> " + dir + " -> ");
}
else {
System.out.println(route.get(i).toString());
}
}
}
}