-
Notifications
You must be signed in to change notification settings - Fork 0
/
AStarSearch.cs
171 lines (152 loc) · 5.57 KB
/
AStarSearch.cs
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
using System;
using System.Collections.Generic;
namespace AStarSearchImplementation
{
public class AStarSearch
{
#region Fields
private Dictionary<Location, Node> open;
private Dictionary<Location, Node> closed;
private int[,] grid;
private Location startLoc, goalLoc;
#endregion
#region Constructor
/// <summary>
///
/// </summary>
/// <param name="grid"></param>
/// <param name="startLoc"></param>
/// <param name="goalLoc"></param>
public AStarSearch(int[,] grid, Location startLoc, Location goalLoc)
{
this.grid = grid;
this.startLoc = startLoc;
this.goalLoc = goalLoc;
open = new Dictionary<Location, Node>(grid.Length);
closed = new Dictionary<Location, Node>(grid.Length);
}
#endregion
#region Methods
public List<Node> ExecuteSearch()
{
PriorityQueue openQ = new PriorityQueue();
Node startNode = new Node();
startNode.Location = startLoc;
startNode.CostFromStart = 0;
startNode.CostToGoal = FindHeuristic(startLoc, goalLoc);
startNode.Parent = null;
openQ.Enqueue(startNode);
open.Add(startNode.Location, startNode);
while (openQ.Count > 0)
{
Node node = openQ.Dequeue();
open.Remove(node.Location);
if (node.Location.Equals(goalLoc))
return GetSolution(node);
List<Node> neighbors = FindNeighbors(node);
foreach (Node newNode in neighbors)
{
Location nnLoc = newNode.Location;
if (closed.ContainsKey(nnLoc)) continue;
decimal newCostFromStart = node.CostFromStart + FindActualCost(node.Location, newNode.Location);
decimal newCostToGoal = FindHeuristic(newNode.Location, goalLoc);
decimal newTotalCost = newCostFromStart + newCostToGoal;
Node holderO = null;
open.TryGetValue(newNode.Location, out holderO);
if (holderO != null && holderO.TotalCost <= newTotalCost)
continue;
newNode.CostFromStart = newCostFromStart;
newNode.CostToGoal = newCostToGoal;
newNode.Parent = node;
if (holderO != null) //remove old location if it exists
{
openQ.Remove(holderO);
open.Remove(newNode.Location);
}
openQ.Enqueue(newNode);
open.Add(newNode.Location, newNode);
}
closed.Add(node.Location, node);
}
return null;
}
/// <summary>
///
/// </summary>
/// <param name="location1"></param>
/// <param name="location2"></param>
/// <returns></returns>
private static decimal FindActualCost(Location location1, Location location2)
{
return (decimal)Math.Sqrt(Math.Abs(location2.X - location1.X) +
Math.Abs(location2.Y - location1.Y));
}
/// <summary>
///
/// </summary>
/// <param name="node"></param>
/// <returns></returns>
private static List<Node> GetSolution(Node node)
{
var pathList = new List<Node> {node};
while (node.Parent != null)
{
pathList.Insert(0, node.Parent);
node = node.Parent;
}
return pathList;
}
/// <summary>
///
/// </summary>
/// <param name="node"></param>
/// <returns></returns>
private List<Node> FindNeighbors(Node node)
{
Location nodeLoc = node.Location;
var neighbors = new List<Node>();
AddConditional(neighbors, nodeLoc, -1, 0);
AddConditional(neighbors, nodeLoc, 0, -1);
AddConditional(neighbors, nodeLoc, 0, 1);
AddConditional(neighbors, nodeLoc, 1, 0);
return neighbors;
}
/// <summary>
///
/// </summary>
/// <param name="addTo"></param>
/// <param name="loc"></param>
/// <param name="x"></param>
/// <param name="y"></param>
private void AddConditional(IList<Node> addTo, Location loc, int x, int y)
{
int newX = loc.X + x, newY = loc.Y + y;
if (newX < 0 || newX >= grid.GetLength(0))
{
return;
}
if (newY < 0 || newY >= grid.GetLength(1))
{
return;
}
if (grid[newX, newY] == Constants.SOLID)
return;
var location = new Location(newX, newY);
var newNode = new Node() {Location = location};
addTo.Add(newNode);
}
/// <summary>
///
/// </summary>
/// <param name="startLoc"></param>
/// <param name="goalLoc"></param>
/// <returns></returns>
private static decimal FindHeuristic(Location startLoc, Location goalLoc)
{
decimal dx = Math.Abs(goalLoc.X - startLoc.X);
decimal dy = Math.Abs(goalLoc.Y - startLoc.Y);
return ((decimal)0.5 * (dx + dy));
}
#endregion
}
}