-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAstar.java
160 lines (143 loc) · 6.13 KB
/
Astar.java
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
151
152
153
154
155
156
157
158
159
160
import java.util.*;
public class Astar {
private Point start;
private Point goal;
private boolean[][] empty;
private int n;
private int m;
private boolean[][] visited;
private boolean finalgoal;
private ArrayList<Point> FirstPath;
private int numOfNodes;
private PriorityQueue<State> states;
public Astar(Point s, Point gl, boolean fingl, int step, boolean[][] em, ArrayList<Point> FstPath) {
this.start = s;
this.goal = gl;
this.finalgoal = fingl;
this.empty = em;
this.FirstPath = FstPath;
this.n = this.empty.length;
this.m = this.empty[0].length;
this.visited = new boolean[this.n][this.m];
this.visited[this.start.x][this.start.y] = true;
this.numOfNodes = 1;
Comparator<State> comp = new StateComparator();
this.states = new PriorityQueue<State>(1000, comp);
State root = new State(this.start, this.goal, step);
this.states.add(root);
}
public ArrayList<Point> solve() {
State cur;
Point temp, prev;
int x, y;
while (true) {
cur = this.states.poll();
if (cur == null) {
System.out.println("Impossible!");
System.exit(0);
}
x = cur.getSq().x;
y = cur.getSq().y;
if (this.FirstPath != null) {
if (cur.getStep() >= FirstPath.size() ||
cur.getStep() == 0) {
temp = FirstPath.get(FirstPath.size() - 1);
prev = null;
}
else {
temp = FirstPath.get(cur.getStep());
prev = FirstPath.get(cur.getStep() - 1);
}
if (x == temp.x && y == temp.y) {
System.out.println("** Conflict **");
System.out.println("Robot 1 considering new position <" + (x + 1) + "," + (y + 1) +
"> at step " + cur.getStep());
System.out.println("Robot 2 considering new position <" + (x + 1) + "," + (y + 1) + "> at step " + cur.getStep());
if ((x == goal.x && y == goal.y)
|| (this.finalgoal && manhDist(cur.getSq(), this.goal) == 1)
|| (this.states.peek() == null)) {
System.out.println("Resolving -- Robot 2 considering stalling at step "
+ (cur.getStep()));
cur.incStep();
this.states.add(cur);
}
else {
System.out.println("Resolving -- Robot 2 considering new alternative position <" + (this.states.peek().getSq().x + 1) + "," + (this.states.peek().getSq().y + 1) + "> at step " + (this.states.peek().getStep()));
this.visited[x][y] = false;
}
System.out.println("** End of Conflict **");
continue;
}
else if (prev != null &&
x == prev.x && y == prev.y && (cur.getParent() == null ||
cur.getParent().getSq().x == temp.x && cur.getParent().getSq().y == temp.y)) {
System.out.println("Conflict -- Danger of crashing");
System.out.println("Robot 1 considering new position <" + (x + 1) + "," + (y + 1) +
"> at step " + (cur.getStep() - 1));
System.out.println("Robot 2 considering new position <" + (x + 1) + "," + (y + 1) + "> at step " + cur.getStep());
if (this.states.peek() == null) {
System.out.println("Impossible to resolve");
System.exit(0);
}
else {
System.out.println("Resolving -- Robot 2 considering new alternative position <" + (this.states.peek().getSq().x + 1) + "," + (this.states.peek().getSq().y + 1) + "> at step " + (this.states.peek().getStep()));
this.visited[x][y] = false;
System.out.println("** End of Conflict **");
continue;
}
}
}
if (x == goal.x && y == goal.y || (this.finalgoal && manhDist(cur.getSq(), this.goal) == 1)) {
ArrayList<Point> path = new ArrayList<Point>();
temp = cur.getSq();
temp.setStep(cur.getStep());
path.add(temp);
while (cur.getParent() != null) {
cur = cur.getParent();
temp = cur.getSq();
temp.setStep(cur.getStep());
path.add(temp);
}
Collections.reverse(path);
return path;
}
else {
findNeighb(cur);
}
}
}
private void findNeighb(State cur) {
int x = cur.getSq().x;
int y = cur.getSq().y;
int numOfNew = 0;
Point[] nextSquares = new Point[4];
if (x + 1 < this.n && empty[x+1][y] && !visited[x+1][y]) {
nextSquares[numOfNew] = new Point(x+1, y);
numOfNew += 1;
}
if (x - 1 >= 0 && empty[x-1][y] && !visited[x-1][y]) {
nextSquares[numOfNew] = new Point(x-1, y);
numOfNew += 1;
}
if (y + 1 < this.m && empty[x][y+1] && !visited[x][y+1]) {
nextSquares[numOfNew] = new Point(x, y+1);
numOfNew += 1;
}
if (y - 1 >= 0 && empty[x][y-1] && !visited[x][y-1]) {
nextSquares[numOfNew] = new Point(x, y-1);
numOfNew += 1;
}
this.numOfNodes += numOfNew;
for (int i = 0; i < numOfNew; i++) {
State temp = new State(cur, nextSquares[i], this.goal);
this.visited[nextSquares[i].x][nextSquares[i].y] = true;
this.states.add(temp);
}
}
public int getNumOfNodes() {
return this.numOfNodes;
}
private static int manhDist(Point a, Point b) {
return Math.abs(a.x - b.x) + Math.abs(a.y - b.y);
}
}