-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulation.cs
More file actions
424 lines (359 loc) · 14.6 KB
/
Simulation.cs
File metadata and controls
424 lines (359 loc) · 14.6 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
namespace PathfindingAlgorithmVisualiser
{
using System.Timers;
using System.Xml.Linq;
public static class Simulation
{
public static bool Running { get; private set; }
public static bool Paused { get; private set; }
public static bool Visualised { get; private set; }
public static int Speed { get; private set; }
private static bool _pathFound;
private static readonly List<(Node Node, string Action)> _steps = new();
private static readonly List<Node> _path = new();
private static readonly Timer _timer = new();
static Simulation()
{
_timer.Elapsed += Timer_Elapsed;
}
public static void Start()
{
Running = true;
Paused = false;
Grid.Dragging = false;
Grid.Moving = null;
// Runs an algorithm then visualises its processes.
RunAlgorithms();
if (Speed == 6)
InstantVisualise();
else
_timer.Start();
}
public static void Stop()
{
_timer.Stop();
// Resetting variables.
_path.Clear();
_steps.Clear();
Visualised = false;
Grid.ClearVisualisation();
Running = false;
}
public static void Pause()
{
_timer.Stop();
Paused = true;
}
public static void Resume()
{
_timer.Start();
Paused = false;
}
public static void InstantRecalculation()
{
// Instantly shows new visuals and shortest path.
Grid.ClearVisualisation();
_path.Clear();
_steps.Clear();
RunAlgorithms();
InstantVisualise();
}
public static void ChangeSpeed(int speed)
{
// Timer interval is changed depending on the selected speed.
switch (speed)
{
case 1:
Speed = 1;
_timer.Interval = 1000;
break;
case 2:
Speed = 2;
_timer.Interval = 100;
break;
case 3:
Speed = 3;
_timer.Interval = 50;
break;
case 4:
Speed = 4;
_timer.Interval = 10;
break;
case 5:
Speed = 5;
_timer.Interval = 1;
break;
case 6:
Speed = 6;
if (Running)
{
_timer.Stop();
InstantVisualise();
}
break;
}
}
private static void AddPath(Node start, Node end)
{
Node node = end.Parent;
// Backtracks and stores the shortest path.
while (!ReferenceEquals(node, start))
{
if (node.Type != NodeType.Start && node.Type != NodeType.Target && node.Type != NodeType.Diversion)
_path.Add(node);
node = node.Parent;
}
}
private static void RunAlgorithms()
{
bool firstPathFound = false;
bool? secondPathFound = null;
// Calls the necessary pathfinding algorithms and stores their results.
if (Grid.Start is not null && Grid.Target is not null)
{
if (Toolbar.SelectedAlgorithm == Algorithm.Dijkstra)
{
if (Grid.Diversion is null)
{
firstPathFound = Dijkstra(Grid.Start, Grid.Target);
if (firstPathFound)
AddPath(Grid.Start, Grid.Target);
}
else
{
firstPathFound = Dijkstra(Grid.Start, Grid.Diversion);
if (firstPathFound)
AddPath(Grid.Start, Grid.Diversion);
secondPathFound = Dijkstra(Grid.Diversion, Grid.Target);
if (secondPathFound == true)
AddPath(Grid.Diversion, Grid.Target);
}
}
else if (Toolbar.SelectedAlgorithm == Algorithm.AStar)
{
if (Grid.Diversion is null)
{
firstPathFound = AStar(Grid.Start, Grid.Target);
if (firstPathFound)
AddPath(Grid.Start, Grid.Target);
}
else
{
firstPathFound = AStar(Grid.Start, Grid.Diversion);
if (firstPathFound)
AddPath(Grid.Start, Grid.Diversion);
secondPathFound = AStar(Grid.Diversion, Grid.Target);
if (secondPathFound == true)
AddPath(Grid.Diversion, Grid.Target);
}
}
else if (Toolbar.SelectedAlgorithm == Algorithm.BFS)
{
Grid.DisableDenseNodes();
if (Grid.Diversion is null)
{
firstPathFound = BFS(Grid.Start, Grid.Target);
if (firstPathFound)
AddPath(Grid.Start, Grid.Target);
}
else
{
firstPathFound = BFS(Grid.Start, Grid.Diversion);
if (firstPathFound)
AddPath(Grid.Start, Grid.Diversion);
secondPathFound = BFS(Grid.Diversion, Grid.Target);
if (secondPathFound == true)
AddPath(Grid.Diversion, Grid.Target);
}
}
}
// The path should only be drawn if the whole path is found.
if (firstPathFound && (secondPathFound is null || secondPathFound == true))
_pathFound = true;
else
_pathFound = false;
}
private static int Heuristic(Node current, Node end)
{
return Math.Abs(current.Column - end.Column) + Math.Abs(current.Row - end.Row);
}
private static bool Dijkstra(Node start, Node end)
{
// Nodes to be evaluated.
var open = new List<Node>();
// Nodes evaluated.
var closed = new List<Node>();
start.GCost = 0;
open.Add(start);
// Node in 'open' with the lowest g-cost is evaluated next.
while (open.Count > 0)
{
Node current = open[0];
foreach (var node in open)
{
if (node.GCost < current.GCost)
current = node;
}
// Removes it from 'open' and adds it to the 'closed' set.
open.Remove(current);
closed.Add(current);
_steps.Add((current, "close"));
// Path found if this node is the 'end node'.
if (ReferenceEquals(current, end))
return true;
// Loops through the neighbours of the node.
foreach (var neighbour in current.Neighbours)
{
// Skips the neighbour if it's not 'traversable' or has already been evaluated.
if (neighbour.Type == NodeType.Block || closed.Contains(neighbour))
continue;
// If a neighbour is not in the open set, or there is a shorter path to it, it's properties are updated.
if (!open.Contains(neighbour) || current.GCost + neighbour.AdjacentCost < neighbour.GCost)
{
neighbour.GCost = current.GCost + neighbour.AdjacentCost;
neighbour.Parent = current;
// If a neighbour is not in the open set, it's added.
if (!open.Contains(neighbour))
{
open.Add(neighbour);
_steps.Add((neighbour, "open"));
}
}
}
}
return false;
}
private static bool AStar(Node start, Node end)
{
// Nodes to be evaluated.
var open = new List<Node>();
// Nodes evaluated.
var closed = new List<Node>();
start.GCost = 0;
open.Add(start);
// Node in 'open' with the lowest f-cost (g-cost + h-cost) is evaluated next.
// If the f-cost is the same, the node with the lowest h-cost is prioritised.
while (open.Count > 0)
{
Node current = open[0];
foreach (var node in open)
{
int currentFCost = current.GCost + Heuristic(current, end);
int nodeFCost = node.GCost + Heuristic(node, end);
if (nodeFCost < currentFCost)
current = node;
else if (nodeFCost == currentFCost && Heuristic(node, end) < Heuristic(current, end))
current = node;
}
// Removes it from 'open' and adds it to the 'closed' set.
open.Remove(current);
closed.Add(current);
_steps.Add((current, "close"));
// Path found if this node is the 'end node'.
if (ReferenceEquals(current, end))
return true;
// Loops through the neighbours of the node.
foreach (var neighbour in current.Neighbours)
{
// Skips the neighbour if it's not 'traversable' or has already been evaluated.
if (neighbour.Type == NodeType.Block || closed.Contains(neighbour))
continue;
// If a neighbour is not in the open set, or there is a shorter path to it, it's properties are updated.
if (!open.Contains(neighbour) || current.GCost + neighbour.AdjacentCost < neighbour.GCost)
{
neighbour.GCost = current.GCost + neighbour.AdjacentCost;
neighbour.Parent = current;
// If a neighbour is not in the open set, it's added.
if (!open.Contains(neighbour))
{
open.Add(neighbour);
_steps.Add((neighbour, "open"));
}
}
}
}
return false;
}
private static bool BFS(Node start, Node end)
{
// Queue of nodes to evaluate next.
var toCheck = new Queue<Node>();
// List of all visited nodes.
var visited = new List<Node>();
toCheck.Enqueue(start);
visited.Add(start);
while (toCheck.Count > 0)
{
// Next node in the queue to evaluate.
Node current = toCheck.Dequeue();
// If it is the end node, the path is found.
if (ReferenceEquals(current, end))
return true;
// Loops through the neighbours of the node.
foreach (var neighbour in current.Neighbours)
{
// If the neighbour is 'traversable' and hasn't been 'visited', it's added to the queue.
if (neighbour.Type != NodeType.Block && !visited.Contains(neighbour))
{
toCheck.Enqueue(neighbour);
visited.Add(neighbour);
neighbour.Parent = current;
_steps.Add((neighbour, "close"));
}
}
}
return false;
}
private static void DrawPath()
{
// Draws the path.
foreach (var node in _path)
node.Path();
}
private static void InstantVisualise()
{
// Instantly displays visualisation results.
foreach (var step in _steps)
{
if (step.Node.Type != NodeType.Start && step.Node.Type != NodeType.Target && step.Node.Type != NodeType.Diversion)
{
if (step.Action == "close")
step.Node.Close();
else
step.Node.Open();
if (Toolbar.SelectedAlgorithm != Algorithm.BFS && step.Node.Type == NodeType.Dense)
step.Node.Dim();
}
}
if (_pathFound)
DrawPath();
Visualised = true;
}
private static void Timer_Elapsed(object? sender, ElapsedEventArgs e)
{
if (_steps.Count == 0)
{
// Draws the path and stops the timer if all the steps have been shown.
if (_pathFound)
DrawPath();
_timer.Stop();
Visualised = true;
}
else
{
// 'Visualises' the next algorithmic step.
Node node = _steps[0].Node;
if (node.Type != NodeType.Start && node.Type != NodeType.Target && node.Type != NodeType.Diversion)
{
if (_steps[0].Action == "close")
node.Close();
else
node.Open();
if (Toolbar.SelectedAlgorithm != Algorithm.BFS && node.Type == NodeType.Dense)
node.Dim();
}
_steps.RemoveAt(0);
}
}
}
}