|
5 | 5 | #include <assert.h> |
6 | 6 | #include "stlastar.h" |
7 | 7 |
|
8 | | -// TODO this test suite is a placeholder. Should include some tests of stlastar.h functionality |
| 8 | +const int MAP_WIDTH = 20; |
| 9 | +const int MAP_HEIGHT = 20; |
| 10 | + |
| 11 | +int world_map[ MAP_WIDTH * MAP_HEIGHT ] = |
| 12 | +{ |
| 13 | + |
| 14 | +// 0001020304050607080910111213141516171819 |
| 15 | + 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 00 |
| 16 | + 1,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,1, // 01 |
| 17 | + 1,9,9,1,1,9,9,9,1,9,1,9,1,9,1,9,9,9,1,1, // 02 |
| 18 | + 1,9,9,1,1,9,9,9,1,9,1,9,1,9,1,9,9,9,1,1, // 03 |
| 19 | + 1,9,1,9,1,9,9,9,1,9,1,9,1,1,1,1,9,9,1,1, // 04 |
| 20 | + 1,9,1,1,9,1,1,1,1,9,1,1,1,1,9,1,1,1,1,1, // 05 |
| 21 | + 1,9,9,9,9,1,1,1,1,1,1,9,9,9,9,1,1,1,1,1, // 06 |
| 22 | + 1,9,9,9,9,9,9,9,9,1,1,1,9,9,9,9,9,9,9,1, // 07 |
| 23 | + 1,9,1,1,1,1,1,1,1,1,1,9,1,1,1,1,1,1,1,1, // 08 |
| 24 | + 1,9,1,9,9,9,9,9,9,9,1,1,9,9,9,9,9,9,9,1, // 09 |
| 25 | + 1,9,1,1,1,1,9,1,1,9,1,1,1,1,1,1,1,1,1,1, // 10 |
| 26 | + 1,9,9,9,9,9,1,9,1,9,1,9,9,9,9,9,1,1,1,1, // 11 |
| 27 | + 1,9,1,9,1,9,9,9,1,9,1,9,1,9,1,9,9,9,1,1, // 12 |
| 28 | + 1,9,1,9,1,9,9,9,1,9,1,9,1,9,1,9,9,9,1,1, // 13 |
| 29 | + 1,9,1,1,1,1,9,9,1,9,1,9,1,1,1,1,9,9,1,1, // 14 |
| 30 | + 1,9,1,1,9,1,1,1,1,9,1,1,1,1,9,1,1,1,1,1, // 15 |
| 31 | + 1,9,9,9,9,1,1,1,1,1,1,9,9,9,9,1,1,1,1,1, // 16 |
| 32 | + 1,1,9,9,9,9,9,9,9,1,1,1,9,9,9,1,9,9,9,9, // 17 |
| 33 | + 1,9,1,1,1,1,1,1,1,1,1,9,1,1,1,1,1,1,1,1, // 18 |
| 34 | + 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 19 |
| 35 | + |
| 36 | +}; |
| 37 | + |
| 38 | + |
| 39 | +// map helper functions |
| 40 | + |
| 41 | +int GetMap( int x, int y ) |
| 42 | +{ |
| 43 | + if( x < 0 || |
| 44 | + x >= MAP_WIDTH || |
| 45 | + y < 0 || |
| 46 | + y >= MAP_HEIGHT |
| 47 | + ) |
| 48 | + { |
| 49 | + return 9; |
| 50 | + } |
| 51 | + |
| 52 | + return world_map[(y*MAP_WIDTH)+x]; |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +// Definitions |
| 58 | + |
| 59 | +class MapSearchNode |
| 60 | +{ |
| 61 | +public: |
| 62 | + int x; // the (x,y) positions of the node |
| 63 | + int y; |
| 64 | + |
| 65 | + MapSearchNode() { x = y = 0; } |
| 66 | + MapSearchNode( int px, int py ) { x=px; y=py; } |
| 67 | + |
| 68 | + float GoalDistanceEstimate( MapSearchNode &nodeGoal ); |
| 69 | + bool IsGoal( MapSearchNode &nodeGoal ); |
| 70 | + bool GetSuccessors( AStarSearch<MapSearchNode> *astarsearch, MapSearchNode *parent_node ); |
| 71 | + float GetCost( MapSearchNode &successor ); |
| 72 | + bool IsSameState( MapSearchNode &rhs ); |
| 73 | + |
| 74 | + void PrintNodeInfo(); |
| 75 | + |
| 76 | + |
| 77 | +}; |
| 78 | + |
| 79 | +bool MapSearchNode::IsSameState( MapSearchNode &rhs ) |
| 80 | +{ |
| 81 | + |
| 82 | + // same state in a maze search is simply when (x,y) are the same |
| 83 | + if( (x == rhs.x) && |
| 84 | + (y == rhs.y) ) |
| 85 | + { |
| 86 | + return true; |
| 87 | + } |
| 88 | + else |
| 89 | + { |
| 90 | + return false; |
| 91 | + } |
| 92 | + |
| 93 | +} |
| 94 | + |
| 95 | +void MapSearchNode::PrintNodeInfo() |
| 96 | +{ |
| 97 | + char str[100]; |
| 98 | + sprintf( str, "Node position : (%d,%d)\n", x,y ); |
| 99 | + |
| 100 | + cout << str; |
| 101 | +} |
| 102 | + |
| 103 | +// Here's the heuristic function that estimates the distance from a Node |
| 104 | +// to the Goal. |
| 105 | + |
| 106 | +float MapSearchNode::GoalDistanceEstimate( MapSearchNode &nodeGoal ) |
| 107 | +{ |
| 108 | + return abs(x - nodeGoal.x) + abs(y - nodeGoal.y); |
| 109 | +} |
| 110 | + |
| 111 | +bool MapSearchNode::IsGoal( MapSearchNode &nodeGoal ) |
| 112 | +{ |
| 113 | + |
| 114 | + if( (x == nodeGoal.x) && |
| 115 | + (y == nodeGoal.y) ) |
| 116 | + { |
| 117 | + return true; |
| 118 | + } |
| 119 | + |
| 120 | + return false; |
| 121 | +} |
| 122 | + |
| 123 | +// This generates the successors to the given Node. It uses a helper function called |
| 124 | +// AddSuccessor to give the successors to the AStar class. The A* specific initialisation |
| 125 | +// is done for each node internally, so here you just set the state information that |
| 126 | +// is specific to the application |
| 127 | +bool MapSearchNode::GetSuccessors( AStarSearch<MapSearchNode> *astarsearch, MapSearchNode *parent_node ) |
| 128 | +{ |
| 129 | + |
| 130 | + int parent_x = -1; |
| 131 | + int parent_y = -1; |
| 132 | + |
| 133 | + if( parent_node ) |
| 134 | + { |
| 135 | + parent_x = parent_node->x; |
| 136 | + parent_y = parent_node->y; |
| 137 | + } |
| 138 | + |
| 139 | + |
| 140 | + MapSearchNode NewNode; |
| 141 | + |
| 142 | + // push each possible move except allowing the search to go backwards |
| 143 | + |
| 144 | + if( (GetMap( x-1, y ) < 9) |
| 145 | + && !((parent_x == x-1) && (parent_y == y)) |
| 146 | + ) |
| 147 | + { |
| 148 | + NewNode = MapSearchNode( x-1, y ); |
| 149 | + astarsearch->AddSuccessor( NewNode ); |
| 150 | + } |
| 151 | + |
| 152 | + if( (GetMap( x, y-1 ) < 9) |
| 153 | + && !((parent_x == x) && (parent_y == y-1)) |
| 154 | + ) |
| 155 | + { |
| 156 | + NewNode = MapSearchNode( x, y-1 ); |
| 157 | + astarsearch->AddSuccessor( NewNode ); |
| 158 | + } |
| 159 | + |
| 160 | + if( (GetMap( x+1, y ) < 9) |
| 161 | + && !((parent_x == x+1) && (parent_y == y)) |
| 162 | + ) |
| 163 | + { |
| 164 | + NewNode = MapSearchNode( x+1, y ); |
| 165 | + astarsearch->AddSuccessor( NewNode ); |
| 166 | + } |
| 167 | + |
| 168 | + |
| 169 | + if( (GetMap( x, y+1 ) < 9) |
| 170 | + && !((parent_x == x) && (parent_y == y+1)) |
| 171 | + ) |
| 172 | + { |
| 173 | + NewNode = MapSearchNode( x, y+1 ); |
| 174 | + astarsearch->AddSuccessor( NewNode ); |
| 175 | + } |
| 176 | + |
| 177 | + return true; |
| 178 | +} |
| 179 | + |
| 180 | +// given this node, what does it cost to move to successor. In the case |
| 181 | +// of our map the answer is the map terrain value at this node since that is |
| 182 | +// conceptually where we're moving |
| 183 | + |
| 184 | +float MapSearchNode::GetCost( MapSearchNode &successor ) |
| 185 | +{ |
| 186 | + return (float) GetMap( x, y ); |
| 187 | + |
| 188 | +} |
9 | 189 |
|
10 | 190 | int main(int argc, char *argv[]) { |
11 | 191 |
|
| 192 | + AStarSearch<MapSearchNode> astarsearch; |
| 193 | + |
| 194 | + unsigned int SearchCount = 0; |
| 195 | + |
| 196 | + const unsigned int NumSearches = 1; |
| 197 | + |
| 198 | + while(SearchCount < NumSearches) |
| 199 | + { |
| 200 | + |
| 201 | + // Create a start state |
| 202 | + MapSearchNode nodeStart; |
| 203 | + nodeStart.x = 0; |
| 204 | + nodeStart.y = 0; |
| 205 | + |
| 206 | + // Define the goal state |
| 207 | + MapSearchNode nodeEnd; |
| 208 | + nodeEnd.x = 3; |
| 209 | + nodeEnd.y = 3; |
| 210 | + |
| 211 | + // Set Start and goal states |
| 212 | + |
| 213 | + astarsearch.SetStartAndGoalStates( nodeStart, nodeEnd ); |
| 214 | + |
| 215 | + unsigned int SearchState; |
| 216 | + unsigned int SearchSteps = 0; |
| 217 | + |
| 218 | + do |
| 219 | + { |
| 220 | + SearchState = astarsearch.SearchStep(); |
| 221 | + |
| 222 | + SearchSteps++; |
| 223 | + |
| 224 | + #if DEBUG_LISTS |
| 225 | + |
| 226 | + cout << "Steps:" << SearchSteps << "\n"; |
| 227 | + |
| 228 | + int len = 0; |
| 229 | + |
| 230 | + cout << "Open:\n"; |
| 231 | + MapSearchNode *p = astarsearch.GetOpenListStart(); |
| 232 | + while( p ) |
| 233 | + { |
| 234 | + len++; |
| 235 | + #if !DEBUG_LIST_LENGTHS_ONLY |
| 236 | + ((MapSearchNode *)p)->PrintNodeInfo(); |
| 237 | + #endif |
| 238 | + p = astarsearch.GetOpenListNext(); |
| 239 | + |
| 240 | + } |
| 241 | + |
| 242 | + cout << "Open list has " << len << " nodes\n"; |
| 243 | + |
| 244 | + len = 0; |
| 245 | + |
| 246 | + cout << "Closed:\n"; |
| 247 | + p = astarsearch.GetClosedListStart(); |
| 248 | + while( p ) |
| 249 | + { |
| 250 | + len++; |
| 251 | + #if !DEBUG_LIST_LENGTHS_ONLY |
| 252 | + p->PrintNodeInfo(); |
| 253 | + #endif |
| 254 | + p = astarsearch.GetClosedListNext(); |
| 255 | + } |
| 256 | + |
| 257 | + cout << "Closed list has " << len << " nodes\n"; |
| 258 | + #endif |
| 259 | + |
| 260 | + } |
| 261 | + while( SearchState == AStarSearch<MapSearchNode>::SEARCH_STATE_SEARCHING ); |
| 262 | + |
| 263 | + if( SearchState == AStarSearch<MapSearchNode>::SEARCH_STATE_SUCCEEDED ) |
| 264 | + { |
| 265 | + cout << "Search found goal state\n"; |
| 266 | + |
| 267 | + MapSearchNode *node = astarsearch.GetSolutionStart(); |
| 268 | + |
| 269 | + #if DISPLAY_SOLUTION |
| 270 | + cout << "Displaying solution\n"; |
| 271 | + #endif |
| 272 | + int steps = 0; |
| 273 | + |
| 274 | + node->PrintNodeInfo(); |
| 275 | + for( ;; ) |
| 276 | + { |
| 277 | + node = astarsearch.GetSolutionNext(); |
| 278 | + |
| 279 | + if( !node ) |
| 280 | + { |
| 281 | + break; |
| 282 | + } |
| 283 | + |
| 284 | + node->PrintNodeInfo(); |
| 285 | + steps ++; |
| 286 | + |
| 287 | + }; |
| 288 | + |
| 289 | + cout << "Solution steps " << steps << endl; |
| 290 | + |
| 291 | + // Once you're done with the solution you can free the nodes up |
| 292 | + astarsearch.FreeSolutionNodes(); |
| 293 | + |
| 294 | + |
| 295 | + } |
| 296 | + else if( SearchState == AStarSearch<MapSearchNode>::SEARCH_STATE_FAILED ) |
| 297 | + { |
| 298 | + cout << "Search terminated. Did not find goal state\n"; |
| 299 | + |
| 300 | + } |
| 301 | + |
| 302 | + // Display the number of loops the search went through |
| 303 | + cout << "SearchSteps : " << SearchSteps << "\n"; |
| 304 | + |
| 305 | + SearchCount ++; |
| 306 | + |
| 307 | + astarsearch.EnsureMemoryFreed(); |
| 308 | + } |
| 309 | + |
12 | 310 | assert(true && "failed to be true"); |
13 | 311 |
|
14 | 312 | printf("Tests succeeded\n"); |
|
0 commit comments