00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef A_STAR_SEARCH_H_
00011 #define A_STAR_SEARCH_H_
00012
00013
00014 #include "PfieldDatatypes.h"
00015 #include "Pfield.h"
00016 #include "FieldObject.h"
00017 #ifdef POTENTIALFIELDS_FOR_GT2004_
00018 #include "Tools/Debugging/Debugging.h"
00019 #include "Tools/Debugging/DebugDrawings.h"
00020 #endif //POTENTIALFIELDS_FOR_GT2004_
00021
00022
00023
00024
00025
00026
00027
00028 class PotentialfieldAStarParameterSet
00029 {
00030 public:
00031
00032 PotentialfieldAStarParameterSet()
00033 {stabilizationObject = 0; useStabilization = false;}
00034
00035
00036 unsigned int minBranchingFactor;
00037
00038 unsigned int maxBranchingFactor;
00039
00040 double minExpansionRadius;
00041
00042 double maxExpansionRadius;
00043
00044 double distanceToGoal;
00045
00046 Potentialfield* field;
00047
00048 double endOfNear;
00049
00050 double endOfFar;
00051
00052 double standardGradientLength;
00053
00054 PfVec startPosition;
00055
00056 unsigned long numberOfCalls;
00057
00058 bool useStabilization;
00059
00060 Object* stabilizationObject;
00061
00062 double stabilizationDistance;
00063 };
00064
00065
00066
00067
00068
00069
00070
00071 class PotentialfieldAStarNode
00072 {
00073 public:
00074
00075 PotentialfieldAStarNode()
00076 { expanded = false; parentNode = 0;}
00077
00078
00079
00080
00081 PotentialfieldAStarNode(const PotentialfieldAStarNode& node)
00082 {
00083 pose = node.pose;
00084 fValue = node.fValue;
00085 gValue = node.gValue;
00086 parentNode = node.parentNode;
00087 expanded = node.expanded;
00088 valueAtPos = node.valueAtPos;
00089 expansionRadius = node.expansionRadius;
00090 }
00091
00092
00093
00094
00095 void setParentNode(unsigned int parentNode)
00096 { this->parentNode = parentNode;}
00097
00098
00099
00100
00101 unsigned int getParentNode() const
00102 { return parentNode;}
00103
00104
00105
00106
00107
00108 void setFunctionValues(double g, double h)
00109 { gValue = g; fValue = g+h;}
00110
00111
00112
00113
00114 double f() const
00115 { return fValue;}
00116
00117
00118
00119
00120 double g() const
00121 { return gValue;}
00122
00123
00124
00125
00126 void setPose(const PfPose& pose)
00127 { this->pose = pose;}
00128
00129
00130
00131
00132 PfPose getPose() const
00133 { return pose;}
00134
00135
00136
00137
00138 void setValueAtPos(const PotentialfieldAStarParameterSet& parameterSet);
00139
00140
00141
00142
00143
00144
00145
00146
00147 void expand(std::vector<PotentialfieldAStarNode>& searchTree,
00148 const std::vector<unsigned int>& expandedNodes,
00149 const PotentialfieldAStarNode& goal,
00150 const PotentialfieldAStarParameterSet& parameterSet,
00151 unsigned int ownNodeNum);
00152
00153
00154
00155
00156 bool hasBeenExpanded() const
00157 { return expanded;}
00158
00159
00160
00161
00162
00163
00164 bool hasReached(const PotentialfieldAStarNode& goal,
00165 const PotentialfieldAStarParameterSet& parameterSet) const;
00166
00167
00168
00169
00170
00171 double distToOtherNode(const PotentialfieldAStarNode& other)
00172 { return (pose.pos - other.getPose().pos).length();}
00173
00174 protected:
00175
00176 PfPose pose;
00177
00178 double fValue;
00179
00180 double gValue;
00181
00182 unsigned int parentNode;
00183
00184 bool expanded;
00185
00186 double valueAtPos;
00187
00188 double expansionRadius;
00189
00190
00191
00192
00193 double getExpansionRadius() const
00194 { return expansionRadius;}
00195
00196
00197
00198
00199
00200
00201 double computeCostsTo(const PotentialfieldAStarNode& node,
00202 const PotentialfieldAStarParameterSet& parameterSet) const;
00203
00204
00205
00206
00207
00208
00209
00210 double computeHeuristicBetween(const PfPose& p1, const PfPose& p2,
00211 unsigned int currentBranchingFactor) const;
00212
00213
00214
00215
00216
00217
00218 void computeCurrentParameters(double& currentExpansionRadius,
00219 unsigned int& currentBranchingFactor,
00220 const PotentialfieldAStarParameterSet& parameterSet) const;
00221
00222
00223
00224
00225
00226
00227
00228 bool tooCloseToAnotherNode(std::vector<PotentialfieldAStarNode>& searchTree,
00229 const std::vector<unsigned int>& expandedNodes,
00230 const PfPose& pose) const;
00231 };
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241 template<class N, class P, class U> class AStarSearch
00242 {
00243 public:
00244
00245
00246
00247
00248
00249 AStarSearch(long minCacheSize, long maxTreeSize, P parameterSet)
00250 {
00251 unsigned int elementsToReserve;
00252 if(maxTreeSize != -1)
00253 {
00254 elementsToReserve = (maxTreeSize+parameterSet.maxBranchingFactor);
00255 this->maxTreeSize = maxTreeSize;
00256 }
00257 else
00258 {
00259 elementsToReserve = minCacheSize;
00260 this->maxTreeSize = -1;
00261 }
00262 searchTree.reserve(elementsToReserve);
00263 expandedNodes.reserve(elementsToReserve);
00264 }
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274 N search(const N& start, const N& goal, const P& parameterSet, U& pathLength)
00275 {
00276 this->parameterSet = parameterSet;
00277 if(start.hasReached(goal, parameterSet))
00278 {
00279 return start;
00280 }
00281 searchTree.clear();
00282 expandedNodes.clear();
00283 searchTree.push_back(start);
00284 searchTree[0].setParentNode(0);
00285 unsigned int indexOfBestNode;
00286 unsigned int nextNodeToExpand;
00287 while(true)
00288 {
00289 nextNodeToExpand = findIndexOfNextNode();
00290 if(searchTree.size() >= (unsigned int)maxTreeSize)
00291 {
00292 indexOfBestNode = nextNodeToExpand;
00293 break;
00294 }
00295 else
00296 {
00297 unsigned int sizeBeforeExpand(searchTree.size());
00298 searchTree[nextNodeToExpand].expand(searchTree, expandedNodes,
00299 goal, parameterSet, nextNodeToExpand);
00300 expandedNodes.push_back(nextNodeToExpand);
00301 unsigned int sizeAfterExpand(searchTree.size());
00302 int result(testNewNodesAgainstGoal(sizeBeforeExpand, sizeAfterExpand-1,goal));
00303 if(result != -1)
00304 {
00305 indexOfBestNode = result;
00306 break;
00307 }
00308 }
00309 }
00310 N result = backtraceNode(indexOfBestNode, pathLength);
00311 #ifdef POTENTIALFIELDS_FOR_GT2004_
00312
00313 COMPLEX_DRAWING(behavior_aStarSearch,draw(indexOfBestNode));
00314 DEBUG_DRAWING_FINISHED(behavior_aStarSearch);
00315 #endif //POTENTIALFIELDS_FOR_GT2004_
00316 return result;
00317 }
00318
00319 private:
00320
00321 std::vector<N> searchTree;
00322
00323 std::vector<unsigned int> expandedNodes;
00324
00325 long maxTreeSize;
00326
00327 P parameterSet;
00328
00329
00330
00331
00332
00333 unsigned int findIndexOfNextNode()
00334 {
00335 unsigned int expandableNode(0);
00336 double f;
00337 while(searchTree[expandableNode].hasBeenExpanded())
00338 {
00339 ++expandableNode;
00340 }
00341 f = searchTree[expandableNode].f();
00342 for(unsigned int i=(expandableNode+1); i<searchTree.size(); i++)
00343 {
00344 if((!searchTree[i].hasBeenExpanded()) && (searchTree[i].f() < f))
00345 {
00346 f = searchTree[i].f();
00347 expandableNode = i;
00348 }
00349 }
00350 return expandableNode;
00351 }
00352
00353
00354
00355
00356
00357
00358
00359
00360 int testNewNodesAgainstGoal(unsigned int firstNode, unsigned int lastNode,
00361 const N& goal)
00362 {
00363 for(unsigned int i = firstNode; i<= lastNode; i++)
00364 {
00365 if(searchTree[i].hasReached(goal, parameterSet))
00366 {
00367 return (int)i;
00368 }
00369 }
00370 return -1;
00371 }
00372
00373
00374
00375
00376
00377
00378
00379 N backtraceNode(unsigned int indexOfBestNode, U& pathLength)
00380 {
00381 unsigned int currentNode(indexOfBestNode);
00382 pathLength = searchTree[currentNode].distToOtherNode(searchTree[searchTree[currentNode].getParentNode()]);
00383 while(searchTree[currentNode].getParentNode() != 0)
00384 {
00385 currentNode = searchTree[currentNode].getParentNode();
00386 pathLength += searchTree[currentNode].distToOtherNode(searchTree[searchTree[currentNode].getParentNode()]);
00387 }
00388 return searchTree[currentNode];
00389 }
00390
00391 #ifdef POTENTIALFIELDS_FOR_GT2004_
00392
00393
00394
00395 void draw(unsigned int indexOfBestNode)
00396 {
00397 PfPose pose, parentPose;
00398 for(unsigned int i=0; i<searchTree.size(); i++)
00399 {
00400 pose = searchTree[i].getPose();
00401 parentPose = searchTree[searchTree[i].getParentNode()].getPose();
00402 LINE(behavior_aStarSearch, pose.pos.x, pose.pos.y, parentPose.pos.x, parentPose.pos.y,
00403 1, Drawings::ps_solid, Drawings::red);
00404 }
00405 unsigned int currentNode(indexOfBestNode);
00406 do
00407 {
00408 pose = searchTree[currentNode].getPose();
00409 currentNode = searchTree[currentNode].getParentNode();
00410 parentPose = searchTree[currentNode].getPose();
00411 LINE(behavior_aStarSearch, pose.pos.x, pose.pos.y, parentPose.pos.x, parentPose.pos.y,
00412 1, Drawings::ps_solid, Drawings::yellow);
00413 } while(currentNode != 0);
00414 if(parameterSet.useStabilization)
00415 {
00416 PfPose stabPose(parameterSet.stabilizationObject->getPose());
00417 LARGE_DOT(behavior_aStarSearch, stabPose.pos.x, stabPose.pos.y,
00418 Drawings::red, Drawings::red);
00419 }
00420 }
00421 #endif //POTENTIALFIELDS_FOR_GT2004_
00422 };
00423
00424
00425 #endif //A_STAR_SEARCH_H_
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443