-
Notifications
You must be signed in to change notification settings - Fork 0
/
Path_Algorithms.pde
486 lines (447 loc) · 14.6 KB
/
Path_Algorithms.pde
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
final int EMPTY = 0, WALL = 1, START = 2, GOAL = 3, VISITED = 4, PATH = 5, TESTED = 6; //Cells values. //<>//
final int WAIT_TIME = 150; //Time to wait while drawing the path.
final int BREADTH_FIRST = 0, DEPTH_FIRST = 1, MANHATAN = 2, DIAGONAL = 3, EUCLIDEAN = 4; //Algorithm values.
int i,j, w, h;
int visitedI, pathI, testedI, nbreSteps, algorithm;
ArrayList<Mix> queue;
ArrayList<Mix> visitedNodes;
ArrayList<Mix> testedNodes;
ArrayList<Tree> path;
IVector start, goal;
Tree tree;
boolean drawPath, goalFound, pathDrawn;
float r,g,b;
int [][]obst = {
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,START,0,0,0,0,0,0,1,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1},
{1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,1},
{1,1,0,0,1,0,0,1,0,0,0,0,0,0,0,1},
{1,0,0,0,1,0,0,1,1,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,GOAL,0,0,0,0,0,0,1,0,0,1},
{1,0,0,0,1,0,0,0,0,0,0,0,1,0,0,1},
{1,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,1,0,0,0,0,0,0,0,1,1,1},
{1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
};
void setup() {
size(512, 512);
w = 512;
h = 512;
start = new IVector(6, 1); //Default start position
goal = new IVector(5, 7); //Default goal position
obst[start.y][start.x] = START;
obst[goal.y][goal.x] = GOAL;
algorithm = MANHATAN;
init();
loadPixels();
}
/**
* Initializes the variables.
*/
void init() {
visitedI = 0;
pathI = 0;
testedI = 0;
drawPath = false;
goalFound = false;
pathDrawn = false;
tree = new Tree(start, estimate(start, goal));
queue = new ArrayList<Mix>();
path = new ArrayList<Tree>();
visitedNodes = new ArrayList<Mix>();
testedNodes = new ArrayList<Mix>();
//Empty the cells.
for (int y = 0; y < 16; y++) {
for (int x = 0; x < 16; x++) {
if (obst[y][x] >= VISITED) obst[y][x] = 0;
}
}
obst[start.y][start.x] = START;
obst[goal.y][goal.x] = GOAL;
}
void draw() {
handleMouseClick();
handleKeyPressed();
int loc, stepX, stepY, val;
for (int x = 0; x < w; x++ ) {
for (int y = 0; y < h; y++ ) {
loc = x + y*w;
stepX = w/16;
stepY = h/16;
i = y/stepY;
j = x/stepX;
if (x % stepX == 0 || y % stepY == 0) {//Border
val = 7;
} else {
val = obst[i][j];
}
switch (val) {
case EMPTY: r = 100; g = 100; b = 100; break; //Unused Cell
case WALL: r = 0; g = 0; b = 200; break; //Wall
case START: r = 255; g = 0; b = 0; break; //Start
case GOAL: r = 0; g = 255; b = 0; break; //Goal
case VISITED: r = 255; g = 255; b = 255; break; //Visited
case PATH: r = 255; g = 100; b = 0; break; //Path
case TESTED: r = 255; g = 100; b = 157; break; //Tested
case 7: r = 0; g = 0; b = 0; break; //Borders
}
color c = color(r, g, b);
pixels[loc] = c;
}
}
updatePixels();
if (!drawPath) {
drawPath = true;
nbreSteps = 0;
if (!start.iEquals(goal)) {
switch (this.algorithm) {
case BREADTH_FIRST: drawPathBreadthFirst(); break;
case DEPTH_FIRST: drawPathDepthFirst(); break;
default: drawPathA();
}
if (goalFound) println("Visited cells : " + nbreSteps+".");
}
}
//Handles the change in the cells' color.
if (visitedI < visitedNodes.size()) {
for (int i = 0; i < testedNodes.size(); i++)
if (obst[testedNodes.get(i).node.value.y][testedNodes.get(i).node.value.x] != VISITED && isNeighbour(testedNodes.get(i).node.value, visitedNodes.get(visitedI).node.value)) {
obst[testedNodes.get(i).node.value.y][testedNodes.get(i).node.value.x] = TESTED;
}
obst[visitedNodes.get(visitedI).node.value.y][visitedNodes.get(visitedI).node.value.x] = VISITED;
visitedI++;
delay(WAIT_TIME);
} else {
if (!pathDrawn) {
for (Tree node : path) {
obst[node.value.y][node.value.x] = PATH;
}
obst[start.y][start.x] = START;
obst[goal.y][goal.x] = GOAL;
pathDrawn = true;
}
}
}
/**
* Handles the mouse click events, if the selected cell is empty changes the goal's position if
* a right click occurs, and the start position when it is a left click.
*/
void handleMouseClick() {
if (mousePressed) {
int newX = mouseX/(w/16);
int newY = mouseY/(h/16);
if (newX>0 && newX<16 && newY>0 && newY<16 && obst[newY][newX] != WALL) {
if (mouseButton == LEFT) {
obst[start.y][start.x] = EMPTY;
start.y = newY;
start.x = newX;
init();
}
if (mouseButton == RIGHT) {
obst[goal.y][goal.x] = EMPTY;
goal.y = newY;
goal.x = newX;
init();
}
}
}
}
/**
* Handles the key events to change the used algorithm and resets the display.
*/
void handleKeyPressed() {
if (keyPressed) {
if (key == 'b' || key == 'B') {
this.algorithm = BREADTH_FIRST;
} else if (key == 'd' || key == 'D') {
this.algorithm = DEPTH_FIRST;
} else if (key == 'm' || key == 'M') {
this.algorithm = MANHATAN;
} else if (key == 'l' || key == 'L') {
this.algorithm = DIAGONAL;
} else if (key == 'e' || key == 'E') {
this.algorithm = EUCLIDEAN;
}
init();
}
}
/*********************************Breadth-first***********************************/
/**
* Looks for a path between the start point and the goal using the Breadth-first algorithm.
*/
void drawPathBreadthFirst() {
final Tree tGoal = findGoalBreadthFirst();
if (tGoal == null) {
println("Goal unreachable, visited cells : " + nbreSteps +".\n");
return;
}
this.path.add(tGoal);
Mix currentNode = findFirstOccurenceMix(this.visitedNodes, tGoal.value);
while (!currentNode.node.value.iEquals(this.start)) {
currentNode = findFirstOccurenceMix(this.visitedNodes, currentNode.father.value);
if (currentNode == null) {
return;
}
this.path.add(currentNode.node);
}
}
/**
* Searched for the goal using the Breadth-first algorithm.
*/
Tree findGoalBreadthFirst() {
enqueue(this.queue, tree, null);
Mix temps = dequeue(this.queue);
do {
temps.node.nodes.addAll(findNeighbours(temps.node.value));
for (Tree node : temps.node.nodes) {
if (!existsInList(this.visitedNodes, node.value)) {
visitedNodes.add(new Mix(node, temps.node));
nbreSteps++;
if (node.value.iEquals(this.goal)) {
return node;
}
enqueue(this.queue, node, temps.node);
}
}
temps = dequeue(this.queue);
} while (temps != null);
return null;
}
/***********************************Depth-first*********************************/
/**
* Uses the Depth-first algorithm.
*/
void drawPathDepthFirst() {
//Code Profondeur
if (!findGoalDepthFirst(tree)) {
println("Goal unreachable, visited cells : " + nbreSteps +".\n");
return;
}
}
/**
* Searches for the goal and builds the path to it using the Depth-first algorithm (recurcive mode).
*/
boolean findGoalDepthFirst(final Tree node) {
nbreSteps++;
if (node.value.iEquals(this.goal)) return true;
node.nodes.addAll(findNeighbours(node.value));
for (Tree subTree : node.nodes) {
if (existsInList(this.visitedNodes, subTree.value)) continue;
visitedNodes.add(new Mix(subTree, node));
if (subTree.value.iEquals(this.goal)) return true;
if (findGoalDepthFirst(subTree)) {
this.path.add(subTree);
return true;
}
}
return false;
}
/*************************************A*****************************************/
/**
* Looks for a path between the start point and the goal using the A algorithm.
*/
void drawPathA() {
Mix latestMix = findGoalA();
if (latestMix == null) {
println("Goal unreachable, visited cells : " + nbreSteps +".\n");
return;
}
boolean pathFound = false;
while (!pathFound) {
latestMix = findFatherInList(this.visitedNodes, latestMix);
if (latestMix == null) {
println("Goal unreachable, visited cells : " + nbreSteps +".\n");
return;
}
path.add(latestMix.node);
if (latestMix.father.value.iEquals(this.start)) pathFound = true;
}
}
/**
* Searches for the goal using the A algorithm.
*/
Mix findGoalA() {
tree.nodes.addAll(findNeighbours(tree.value));
estimateAndTestNodes(tree);
Mix mix;
while (!goalFound && this.testedNodes.size() != 0) {
mix = findMinEstimation(this.testedNodes);
nbreSteps++;
this.visitedNodes.add(mix);
if (mix.node.value.iEquals(this.goal)) {
goalFound = true;
return mix;
}
mix.node.nodes.addAll(findNeighbours(mix.node.value));
estimateAndTestNodes(mix.node);
}
return null;
}
/**
* Browse through the direct sons of the given tree, if a node has never been tested or visited,
* calculates an estimation and adds it to the list of testedNodes.
*/
void estimateAndTestNodes(final Tree tree) {
for (Tree subTree : tree.nodes) {
if (!existsInList(testedNodes, subTree.value) && !existsInList(visitedNodes, subTree.value)) {
subTree.estimation = estimate(subTree.value, this.goal);
this.testedNodes.add(new Mix(subTree, tree));
}
}
}
/**
* Returns the element with the lowest estimation in the given ArrayList, may be null if the list is
* empty or null.
*/
Mix findMinEstimation(final ArrayList<Mix> list) {
Mix returnValue = null;
if (list != null) {
for (Mix mix : list) {
if (returnValue == null) returnValue = mix;
if (mix.node.estimation < returnValue.node.estimation) returnValue = mix;
}
list.remove(returnValue);
}
return returnValue;
}
/**
* Calculates an estimation about the proximity to the goal.
*/
int estimate(final IVector pos, final IVector relativeTo) {
switch (this.algorithm) {
case MANHATAN: return abs(pos.x - relativeTo.x) + abs(pos.y - relativeTo.y);
case DIAGONAL: return max(abs(pos.x - relativeTo.x), abs(pos.y - relativeTo.y));
case EUCLIDEAN: return floor(sqrt(pow(pos.x - relativeTo.x,2) + pow(pos.y - relativeTo.y,2)));
default: return 0;
}
}
/*******************************Common functions*********************************/
/**
* Returns an ArrayList of Tree containing the neighbours of the given position.
*/
ArrayList<Tree> findNeighbours(final IVector position) {
final ArrayList<Tree> neighbours = new ArrayList<Tree>();
Tree newNode;
IVector newVector;
if (position.x-1 > 0) {
if (obst[position.y][position.x-1] != WALL) {
//Case Vide
newVector = new IVector(position.x-1, position.y);
newNode = findFirstOccurence(this.visitedNodes, newVector);
if (newNode == null) {
newNode = new Tree(newVector);
}
neighbours.add(newNode);
}
}
if (position.x+1 < 16) {
if (obst[position.y][position.x+1] != WALL) {
//Case Vide
newVector = new IVector(position.x+1, position.y);
newNode = findFirstOccurence(this.visitedNodes, newVector);
if (newNode == null) {
newNode = new Tree(newVector);
}
neighbours.add(newNode);
}
}
if (position.y-1 > 0) {
if (obst[position.y-1][position.x] != WALL) {
//Case Vide
newVector = new IVector(position.x, position.y-1);
newNode = findFirstOccurence(this.visitedNodes, newVector);
if (newNode == null) {
newNode = new Tree(newVector);
}
neighbours.add(newNode);
}
}
if (position.y+1 < 16) {
if (obst[position.y+1][position.x] != WALL) {
//Case Vide
newVector = new IVector(position.x, position.y+1);
newNode = findFirstOccurence(this.visitedNodes, newVector);
if (newNode == null) {
newNode = new Tree(newVector);
}
neighbours.add(newNode);
}
}
return neighbours;
}
/**
* Checks if the given IVector is present in the given ArrayList, returns true if found, false
* otherwise or if one of the parameters is equal to null.
*/
boolean existsInList(final ArrayList<Mix> list, final IVector iVector) {
if (list == null || iVector == null) return false;
for (Mix mix : list) {
if (mix.node.value.iEquals(iVector)) return true;
}
return false;
}
/**
* Browse through the given ArrayList and returns the first occurence found of a Tree element
* whose value corresponds to the given IVector, if it does not exist or one of the parameters
* is equal to null, returns null.
*/
Tree findFirstOccurence(final ArrayList<Mix> source, final IVector iVector) {
if (iVector == null || source == null) return null;
for (Mix mix : source) {
if (mix.node.value.iEquals(iVector)) return mix.node;
}
return null;
}
/**
* Browse through the given ArrayList and returns the first occurence found of a Tree element
* whose value corresponds to the given IVector, if it does not exist or one of the parameters
* is equal to null, returns null.
*/
Mix findFirstOccurenceMix(final ArrayList<Mix> source, final IVector iVector) {
if (iVector == null || source == null) return null;
for (Mix mix : source) {
if (mix.node.value.iEquals(iVector)) return mix;
}
return null;
}
/**
* Checks if pos1 and pos2 are neighbours.
*/
boolean isNeighbour(final IVector pos1, final IVector pos2) {
if (pos1.x == pos2.x && (abs(pos1.y - pos2.y) <= 1)) return true;
if (pos1.y == pos2.y && (abs(pos1.x - pos2.x) <= 1)) return true;
return false;
}
/**
* Searches for the direct father of the given son in the source ArrayList, returns the a mix element
* whose node is the father if found, null otherwise.
*/
Mix findFatherInList(final ArrayList<Mix> source, final Mix son) {
if (source == null || son == null || son.father == null) return null;
for (Mix mix : source) {
if (mix.node.iEquals(son.father)) return mix;
}
return null;
}
/*******************************Queues functions*********************************/
/**
* Adds an element at the end of the given queue.
*/
void enqueue(final ArrayList<Mix> queue, final Tree node, final Tree father) {
if (queue != null && tree != null) queue.add(new Mix(node, father));
}
/**
* Removes and returns the element at the top of the given queue.
*/
Mix dequeue(final ArrayList<Mix> queue) {
if (queue == null || queue.size() == 0) return null;
final Mix mix = queue.get(0);
queue.remove(0);
return mix;
}