forked from justinhj/astar-algorithm-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAStarExample.cs
243 lines (200 loc) · 6.48 KB
/
AStarExample.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
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
using System.Collections;
using System.Collections.Generic;
using System.Text;
public struct NodePosition
{
public int x;
public int y;
public NodePosition(int x, int y)
{
this.x = x;
this.y = y;
}
}
public class Path : List<NodePosition>
{
public override string ToString()
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < Count; ++i)
{
sb.Append(string.Format("Node {0}: {1}, {2}", i, this[i].x, this[i].y));
if (i < Count - 1)
{
sb.Append(" - ");
}
}
return sb.ToString();
}
}
public class Map
{
const int MAP_WIDTH = 20;
const int MAP_HEIGHT = 20;
static int[] map = new int[MAP_WIDTH * MAP_HEIGHT]
{
// 0001020304050607080910111213141516171819
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 00
1,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,1, // 01
1,9,9,1,1,9,9,9,1,9,1,9,1,9,1,9,9,9,1,1, // 02
1,9,9,1,1,9,9,9,1,9,1,9,1,9,1,9,9,9,1,1, // 03
1,9,1,1,1,1,9,9,1,9,1,9,1,1,1,1,9,9,1,1, // 04
1,9,1,1,9,1,1,1,1,9,1,1,1,1,9,1,1,1,1,1, // 05
1,9,9,9,9,1,1,1,1,1,1,9,9,9,9,1,1,1,1,1, // 06
1,9,9,9,9,9,9,9,9,1,1,1,9,9,9,9,9,9,9,1, // 07
1,9,1,1,1,1,1,1,1,1,1,9,1,1,1,1,1,1,1,1, // 08
1,9,1,9,9,9,9,9,9,9,1,1,9,9,9,9,9,9,9,1, // 09
1,9,1,1,1,1,9,1,1,9,1,1,1,1,1,1,1,1,1,1, // 10
1,9,9,9,9,9,1,9,1,9,1,9,9,9,9,9,1,1,1,1, // 11
1,9,1,9,1,9,9,9,1,9,1,9,1,9,1,9,9,9,1,1, // 12
1,9,1,9,1,9,9,9,1,9,1,9,1,9,1,9,9,9,1,1, // 13
1,9,1,1,1,1,9,9,1,9,1,9,1,1,1,1,9,9,1,1, // 14
1,9,1,1,9,1,1,1,1,9,1,1,1,1,9,1,1,1,1,1, // 15
1,9,9,9,9,1,1,1,1,1,1,9,9,9,9,1,1,1,1,1, // 16
1,1,9,9,9,9,9,9,9,1,1,1,9,9,9,1,9,9,9,9, // 17
1,9,1,1,1,1,1,1,1,1,1,9,1,1,1,1,1,1,1,1, // 18
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 19
};
public static int GetMap(int x, int y)
{
if (x < 0 || x >= MAP_WIDTH || y < 0 || y >= MAP_HEIGHT)
{
return 9;
}
return map[(y * MAP_WIDTH) + x];
}
}
public class MapSearchNode
{
public NodePosition position;
AStarPathfinder pathfinder = null;
public MapSearchNode(AStarPathfinder _pathfinder)
{
position = new NodePosition(0, 0);
pathfinder = _pathfinder;
}
public MapSearchNode(NodePosition pos, AStarPathfinder _pathfinder)
{
position = new NodePosition(pos.x, pos.y);
pathfinder = _pathfinder;
}
// Here's the heuristic function that estimates the distance from a Node
// to the Goal.
public float GoalDistanceEstimate(MapSearchNode nodeGoal)
{
double X = (double)position.x - (double)nodeGoal.position.x;
double Y = (double)position.y - (double)nodeGoal.position.y;
return ((float)System.Math.Sqrt((X * X) + (Y * Y)));
}
public bool IsGoal(MapSearchNode nodeGoal)
{
return (position.x == nodeGoal.position.x && position.y == nodeGoal.position.y);
}
public bool ValidNeigbour(int xOffset, int yOffset)
{
// Return true if the node is navigable and within grid bounds
return (Map.GetMap(position.x + xOffset, position.y + yOffset) < 9);
}
void AddNeighbourNode(int xOffset, int yOffset, NodePosition parentPos, AStarPathfinder aStarSearch)
{
if (ValidNeigbour(xOffset, yOffset) &&
!(parentPos.x == position.x + xOffset && parentPos.y == position.y + yOffset))
{
NodePosition neighbourPos = new NodePosition(position.x + xOffset, position.y + yOffset);
MapSearchNode newNode = pathfinder.AllocateMapSearchNode(neighbourPos);
aStarSearch.AddSuccessor(newNode);
}
}
// This generates the successors to the given Node. It uses a helper function called
// AddSuccessor to give the successors to the AStar class. The A* specific initialisation
// is done for each node internally, so here you just set the state information that
// is specific to the application
public bool GetSuccessors(AStarPathfinder aStarSearch, MapSearchNode parentNode)
{
NodePosition parentPos = new NodePosition(0, 0);
if (parentNode != null)
{
parentPos = parentNode.position;
}
// push each possible move except allowing the search to go backwards
AddNeighbourNode(-1, 0, parentPos, aStarSearch);
AddNeighbourNode( 0, -1, parentPos, aStarSearch);
AddNeighbourNode( 1, 0, parentPos, aStarSearch);
AddNeighbourNode( 0, 1, parentPos, aStarSearch);
return true;
}
// given this node, what does it cost to move to successor. In the case
// of our map the answer is the map terrain value at this node since that is
// conceptually where we're moving
public float GetCost(MapSearchNode successor)
{
// Implementation specific
return Map.GetMap(successor.position.x, successor.position.y);
}
public bool IsSameState(MapSearchNode rhs)
{
return (position.x == rhs.position.x &&
position.y == rhs.position.y);
}
}
public class AStarExample
{
static void Main()
{
AStarPathfinder pathfinder = new AStarPathfinder();
Pathfind(new NodePosition(0, 0), new NodePosition(2, 4), pathfinder);
}
static bool Pathfind(NodePosition startPos, NodePosition goalPos, AStarPathfinder pathfinder)
{
// Reset the allocated MapSearchNode pointer
pathfinder.InitiatePathfind();
// Create a start state
MapSearchNode nodeStart = pathfinder.AllocateMapSearchNode(startPos);
// Define the goal state
MapSearchNode nodeEnd = pathfinder.AllocateMapSearchNode(goalPos);
// Set Start and goal states
pathfinder.SetStartAndGoalStates(nodeStart, nodeEnd);
// Set state to Searching and perform the search
AStarPathfinder.SearchState searchState = AStarPathfinder.SearchState.Searching;
uint searchSteps = 0;
do
{
searchState = pathfinder.SearchStep();
searchSteps++;
}
while (searchState == AStarPathfinder.SearchState.Searching);
// Search complete
bool pathfindSucceeded = (searchState == AStarPathfinder.SearchState.Succeeded);
if (pathfindSucceeded)
{
// Success
Path newPath = new Path();
int numSolutionNodes = 0; // Don't count the starting cell in the path length
// Get the start node
MapSearchNode node = pathfinder.GetSolutionStart();
newPath.Add(node.position);
++numSolutionNodes;
// Get all remaining solution nodes
for( ;; )
{
node = pathfinder.GetSolutionNext();
if( node == null )
{
break;
}
++numSolutionNodes;
newPath.Add(node.position);
};
// Once you're done with the solution we can free the nodes up
pathfinder.FreeSolutionNodes();
System.Console.WriteLine("Solution path length: " + numSolutionNodes);
System.Console.WriteLine("Solution: " + newPath.ToString());
}
else if (searchState == AStarPathfinder.SearchState.Failed)
{
// FAILED, no path to goal
System.Console.WriteLine("Pathfind FAILED!");
}
return pathfindSucceeded;
}
}