forked from justinhj/astar-algorithm-cpp
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAstar.h.old
More file actions
executable file
·187 lines (138 loc) · 4.39 KB
/
Copy pathAstar.h.old
File metadata and controls
executable file
·187 lines (138 loc) · 4.39 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
#ifndef ASTAR_H_
#define ASTAR_H_
#include "stlastar.h" // See header for copyright and usage information
#include <iostream>
#include <stdio.h>
#include <MapSearchNode.h>
#define DEBUG_LISTS 0
#define DEBUG_LIST_LENGTHS_ONLY 0
#define DISPLAY_SOLUTION 0
/******************* NOTES ON THE INPUT MAP **************************
Each element contains an integer from 0 to 5 which indicates the cost
of travel across the terrain. Zero means the least possible difficulty
in travelling (think ice rink if you can skate) whilst 5 represents the
most difficult. 9 indicates that we cannot pass.
**************************************************************************/
std::vector<int> MapSearchNode::map;
int MapSearchNode::MAP_WIDTH;
int MapSearchNode::MAP_HEIGHT;
class Astar
{
int gridSizeX;
int gridSizeY;
AStarSearch<MapSearchNode> astarsearch;
std::vector<int> access_map;
int start, target;
public:
Astar() {}
Astar(std::vector<int> a_map, int sizeX, int sizeY):
gridSizeX(sizeX),
gridSizeY(sizeY)
{
MapSearchNode::map = a_map;
MapSearchNode::MAP_WIDTH = sizeX;
MapSearchNode::MAP_HEIGHT = sizeY;
}
std::vector<int> path;
void run(int v_start, int v_target);
void printMap();
void printSolution();
};
void Astar::run(int v_start, int v_target){
start = v_start;
target = v_target;
path.clear();
access_map = MapSearchNode::map;
MapSearchNode nodeStart;
nodeStart.x = start/gridSizeY;
nodeStart.y = start%gridSizeY;
MapSearchNode nodeEnd;
nodeEnd.x = target/gridSizeY;
nodeEnd.y = target%gridSizeY;
#if DISPLAY_SOLUTION
cout << "Start: " << start << "\tTarget: " << target << endl;
#endif
// Set Start and goal states
astarsearch.SetStartAndGoalStates( nodeStart, nodeEnd );
unsigned int SearchState;
do
{
SearchState = astarsearch.SearchStep();
#if DEBUG_LISTS
int len = 0;
cout << "Open:\n";
MapSearchNode *p = astarsearch.GetOpenListStart();
while( p )
{
len++;
#if !DEBUG_LIST_LENGTHS_ONLY
((MapSearchNode *)p)->PrintNodeInfo();
#endif
p = astarsearch.GetOpenListNext();
}
cout << "Open list has " << len << " nodes\n";
len = 0;
cout << "Closed:\n";
p = astarsearch.GetClosedListStart();
while( p )
{
len++;
#if !DEBUG_LIST_LENGTHS_ONLY
p->PrintNodeInfo();
#endif
p = astarsearch.GetClosedListNext();
}
cout << "Closed list has " << len << " nodes\n";
#endif
}
while( SearchState == AStarSearch<MapSearchNode>::SEARCH_STATE_SEARCHING );
if( SearchState == AStarSearch<MapSearchNode>::SEARCH_STATE_SUCCEEDED )
{
#if DISPLAY_SOLUTION
cout << "A path was found!\n";
#endif
MapSearchNode *node = astarsearch.GetSolutionStart();
/// Saving solution in path vector
do{
path.push_back(node->x * gridSizeY+node->y);
access_map.at(node->x * gridSizeY+node->y) = -1;
node = astarsearch.GetSolutionNext();
}while( node != NULL );
#if DISPLAY_SOLUTION
printSolution();
#endif
// Once you're done with the solution you can free the nodes up
astarsearch.FreeSolutionNodes();
}
else if( SearchState == AStarSearch<MapSearchNode>::SEARCH_STATE_FAILED )
{
cout << "Search terminated. No traversable path found.\n";
}
astarsearch.EnsureMemoryFreed();
}
void Astar::printSolution(){
cout << "Path from " << start << " to " << target << " (path length=" << path.size() << "):" << endl;
for(int i=0; i<path.size(); i++){
cout << path.at(i) << " ";
}
cout << endl;
for(int i=0; i<access_map.size(); i++){
if(i%gridSizeY == 0) printf("\n");
if(i == start) printf("S ");
else if(i == target) printf("T ");
else if(access_map.at(i) >=0 && access_map.at(i) <= 5) printf(". ");
else if(access_map.at(i) == 9) printf("■ ");
else if(access_map.at(i) == -1) printf("X ");
}
cout << endl;
}
void Astar::printMap(){
cout << "Map:" << endl;
for(int i=0; i<access_map.size(); i++){
if(i%gridSizeY == 0) printf("\n");
if(access_map.at(i) >=0 && access_map.at(i) <= 5) printf(". ");
else if(access_map.at(i) == 9) printf("■ ");
}
cout << endl;
}
#endif