Repository navigation
Expand file tree
/
Copy pathAlgorithms.cs
More file actions
430 lines (405 loc) · 15.5 KB
/
Copy pathAlgorithms.cs
File metadata and controls
430 lines (405 loc) · 15.5 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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using SplashKitSDK;
namespace PathFinder
{
public class Algorithms
{
private List<Cell> closedSet; // explored cells
private List<Cell> goals; // list of goals
private Cell root; // intitial state/cell
private Map Map;
public Algorithms(Map m)
{
Map = m;
closedSet = new List<Cell>();
goals = Map.Goals;
root = Map.Root;
}
// search algorithms
public string BFS()
{
Queue<Cell> openSet = new Queue<Cell>();
openSet.Enqueue(root);
while (openSet.Count() > 0)
{
SplashKit.ClearScreen();
Map.Draw();
// Dequeue node and add to closed set
Cell current = openSet.Dequeue();
closedSet.Add(current);
// visual purposes
if (current.Type != Type.GOAL && current.Type != Type.ROOT)
{
current.Type = Type.EXPANDED;
}
// check goal
foreach (Cell goal in goals)
{
if (current == goal)
{
Console.WriteLine("Expanded: " + closedSet.Count());
closedSet.Clear();
return Create_Path(current);
}
}
// expand node
foreach (Cell neighbour in current.Neighbours)
{
if (neighbour != null)
{
if (!openSet.Contains(neighbour) && !closedSet.Contains(neighbour) && neighbour.Type != Type.WALL)
{
if (neighbour.Type != Type.GOAL && neighbour.Type != Type.ROOT)
{
neighbour.Type = Type.VISITED;
}
neighbour.Parent = current;
openSet.Enqueue(neighbour);
}
}
}
SplashKit.RefreshScreen();
SplashKit.Delay(200);
}
return null;
}
public string DFS()
{
Stack<Cell> openSet = new Stack<Cell>();
openSet.Push(root);
while (openSet.Count() > 0)
{
SplashKit.ClearScreen();
Map.Draw();
Cell current = openSet.Pop();
closedSet.Add(current);
// visual purposes
if (current.Type != Type.ROOT && current.Type != Type.GOAL)
{
current.Type = Type.EXPANDED;
}
// check goal
foreach (Cell goal in goals)
{
if (current == goal)
{
Console.WriteLine("Expanded: " + closedSet.Count());
closedSet.Clear();
return Create_Path(current);
}
}
// expand node
for (int i = current.Neighbours.Count() - 1; i >= 0; i--)
{
Cell neighbour = current.Neighbours[i];
if (!closedSet.Contains(neighbour) && neighbour.Type != Type.WALL)
{
if (neighbour.Type != Type.ROOT && neighbour.Type != Type.GOAL)
{
neighbour.Type = Type.VISITED;
}
openSet.Push(neighbour);
neighbour.Parent = current;
}
}
SplashKit.RefreshScreen();
SplashKit.Delay(200);
}
return null;
}
public string ASTAR()
{
List<Cell> openSet = new List<Cell>();
Cell goal = Get_goal("MIN");
openSet.Add(root);
while (openSet.Count() > 0)
{
SplashKit.ClearScreen();
Cell current = min_cell(openSet, goal);
openSet.Remove(current);
closedSet.Add(current);
// visual purposes
if (current.Type != Type.GOAL && current.Type != Type.ROOT)
{
current.Type = Type.EXPANDED;
}
// check goal
if (current == goal)
{
Console.WriteLine("Expanded: " + closedSet.Count());
closedSet.Clear();
return Create_Path(current);
}
// expand node
foreach (Cell neighbour in current.Neighbours)
{
if (!openSet.Contains(neighbour) && !closedSet.Contains(neighbour) && neighbour.Type != Type.WALL)
{
if (neighbour.Type != Type.GOAL && neighbour.Type != Type.ROOT)
{
neighbour.Type = Type.VISITED;
}
openSet.Add(neighbour);
neighbour.Parent = current;
}
}
Map.Draw();
SplashKit.RefreshScreen();
SplashKit.Delay(200);
}
return null;
}
public string GBFS()
{
Cell goal = Get_goal("MIN");
List<Cell> openSet = new List<Cell>();
openSet.Add(root);
while (openSet.Count() > 0)
{
SplashKit.ClearScreen();
Cell current = min_cell(openSet,goal);
openSet.Remove(current);
closedSet.Add(current);
// visual purposes
if (current.Type != Type.GOAL && current.Type != Type.ROOT)
{
current.Type = Type.EXPANDED;
}
// check goal
if (current == goal)
{
Console.WriteLine("Expanded: " + closedSet.Count());
closedSet.Clear();
return Create_Path(current);
}
// expand node
foreach (Cell neighbour in current.Neighbours)
{
if (!openSet.Contains(neighbour) && !closedSet.Contains(neighbour) && neighbour.Type != Type.WALL)
{
if (neighbour.Type != Type.GOAL && neighbour.Type != Type.ROOT)
{
neighbour.Type = Type.VISITED;
}
openSet.Add(neighbour);
neighbour.Parent = current;
}
}
Map.Draw();
SplashKit.RefreshScreen();
SplashKit.Delay(200);
}
return null;
}
public string DLS(int limit)
{
Cell goal = Get_goal("MIN"); // asume closest goal is only goal
Stack<Cell> openSet = new Stack<Cell>();
openSet.Push(root);
int depth = 1;
while (openSet.Count() > 0)
{
if (depth <= limit)
{
SplashKit.ClearScreen();
Cell current = openSet.Pop();
closedSet.Add(current);
// visual purposes
if (current.Type != Type.ROOT && current.Type != Type.GOAL)
{
current.Type = Type.EXPANDED;
}
// check goal
if (current == goal)
{
Console.WriteLine("Expanded: " + closedSet.Count());
closedSet.Clear();
return Create_Path(current);
}
// expand node
for (int i = current.Neighbours.Count() - 1; i >= 0; i--)
{
Cell neighbour = current.Neighbours[i];
if (!closedSet.Contains(neighbour) && neighbour.Type != Type.WALL)
{
if (neighbour.Type != Type.ROOT && neighbour.Type != Type.GOAL)
{
neighbour.Type = Type.VISITED;
}
neighbour.Parent = current;
openSet.Push(neighbour);
}
}
depth++;
Map.Draw();
SplashKit.RefreshScreen();
SplashKit.Delay(200);
}
else
{
closedSet.Clear();
return "Goal not found within depth limit of ";
}
}
return null;
}
public string Dijkstra()
{
// order of expansion not fixed
Dictionary<Cell, int> dist = new Dictionary<Cell, int>();
List<Cell> cells = Map.List_cells();
List<Cell> openSet = new List<Cell>();
Cell goal = Get_goal("MIN");
foreach (Cell c in cells)
{
dist.Add(c, int.MaxValue);
openSet.Add(c);
}
dist[root] = 0;
while (openSet.Count() > 0)
{
SplashKit.ClearScreen();
// find node with lowest node (start root is 0) and assign as current node
Cell min = openSet.First();
foreach (Cell c in openSet)
{
if (dist[c] < dist[min])
{
min = c;
}
}
Cell current = min;
// Remove from unexplored Set
openSet.Remove(current);
closedSet.Add(current);
// visual purposes
if (current.Type != Type.ROOT && current.Type != Type.GOAL)
{
current.Type = Type.EXPANDED;
}
// check goal
if (current == goal)
{
Console.WriteLine("Expanded: " + closedSet.Count());
closedSet.Clear();
return Create_Path(current);
}
// expand node
foreach (Cell neighbour in current.Neighbours)
{
if (!closedSet.Contains(neighbour) && neighbour.Type != Type.WALL)
{
int dist_neighbour = dist[current] + Dist_cells(current,neighbour); // dist of current + dist of neighbour = 2 for all cells in grid
if (dist_neighbour < dist[neighbour])
{
// visual purposes
if (neighbour.Type != Type.ROOT && neighbour.Type != Type.GOAL)
{
neighbour.Type = Type.VISITED;
}
dist[neighbour] = dist_neighbour;
neighbour.Parent = current;
}
}
}
Map.Draw();
SplashKit.RefreshScreen();
SplashKit.Delay(100);
}
return null;
}
// methods
private Cell Get_goal(string type)
{
// get goal from sequence of 2 goals in the state space
Cell result = goals.First();
foreach (Cell g in goals)
{
if (type == "MIN")
{
if (Map.Distance(root, g) < Map.Distance(root, result))
{
result = g;
}
}
if (type == "MAX")
{
if (Map.Distance(root, g) > Map.Distance(root, result))
{
result = g;
}
}
}
return result;
}
private Cell min_cell(List<Cell> cells, Cell goal)
{
// get cell with minimum distance from cell to goal
Cell min = cells.First();
foreach (Cell c in cells)
{
int f1 = Dist_cells(c, goal);
int f2 = Dist_cells(min, goal);
if (Math.Min(f1, f2) == f1 && !Math.Equals(f1,f2))
{
min = c;
}
}
return min;
}
private int Dist_cells(Cell c1, Cell c2)
{
// using manhattan distance from node to goal
int x = Math.Abs(c2.X - c1.X);
int y = Math.Abs(c2.Y - c1.Y);
return x + y;
}
private string Create_Path(Cell current)
{
// using cell.Parent to backtrack and return string path
string str_path = "";
Cell next = current.Parent;
List<string> action_list = new List<string>();
action_list.Add(movement(current, next) + " ");
while (next != root)
{
next.Type = Type.PATH;
action_list.Add(movement(next, next.Parent) + " ");
next = next.Parent;
}
int index = action_list.Count() - 1;
while (index >= 0)
{
str_path += action_list[index];
index--;
}
return str_path;
}
private string movement(Cell current, Cell previous)
{
// return action from previous to current
if (current.X - 1 == previous.X && current.Y == previous.Y) // right
{
return "right";
}
if (current.X + 1 == previous.X && current.Y == previous.Y) // right
{
return "left";
}
if (current.X == previous.X && current.Y + 1 == previous.Y) // right
{
return "up";
}
if (current.X == previous.X && current.Y - 1 == previous.Y) // right
{
return "down";
}
return null;
}
}
}