$search
00001 /* 00002 * Copyright (c) 2008, Maxim Likhachev 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * * Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * * Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * * Neither the name of the University of Pennsylvania nor the names of its 00014 * contributors may be used to endorse or promote products derived from 00015 * this software without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00018 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00021 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00022 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00023 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00024 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00025 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00026 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00027 * POSSIBILITY OF SUCH DAMAGE. 00028 */ 00029 #ifndef __2DGRIDSEARCH_H_ 00030 #define __2DGRIDSEARCH_H_ 00031 00032 #define SBPL_2DGRIDSEARCH_NUMOF2DDIRS 16 00033 00034 #define SBPL_2DSEARCH_OPEN_LIST_ID 0 00035 00036 enum SBPL_2DGRIDSEARCH_TERM_CONDITION {SBPL_2DGRIDSEARCH_TERM_CONDITION_OPTPATHFOUND, SBPL_2DGRIDSEARCH_TERM_CONDITION_20PERCENTOVEROPTPATH, SBPL_2DGRIDSEARCH_TERM_CONDITION_TWOTIMESOPTPATH, 00037 SBPL_2DGRIDSEARCH_TERM_CONDITION_THREETIMESOPTPATH, SBPL_2DGRIDSEARCH_TERM_CONDITION_ALLCELLS}; 00038 00039 enum SBPL_2DGRIDSEARCH_OPENTYPE {SBPL_2DGRIDSEARCH_OPENTYPE_HEAP, SBPL_2DGRIDSEARCH_OPENTYPE_SLIDINGBUCKETS}; 00040 00041 //#define SBPL_2DGRIDSEARCH_HEUR2D(x,y) ((int)(1000*cellSize_m_*sqrt((double)((x-goalX_)*(x-goalX_)+(y-goalY_)*(y-goalY_))))) 00042 #define SBPL_2DGRIDSEARCH_HEUR2D(x,y) ((int)(1000*cellSize_m_*__max(abs(x-goalX_),abs(y-goalY_)))) 00043 00046 class SBPL_2DGridSearchState : public AbstractSearchState 00047 { 00048 public: 00049 00052 int x,y; 00053 00056 int g; 00059 int iterationaccessed; 00060 00061 public: 00062 SBPL_2DGridSearchState() {iterationaccessed = 0;}; 00063 ~SBPL_2DGridSearchState() {}; 00064 00065 }; 00066 00067 00068 // Forward declaration needed to allow instance of CIntHeap* 00069 class CIntHeap; 00070 class CSlidingBucket; 00071 00074 class SBPL2DGridSearch 00075 { 00076 public: 00077 00078 SBPL2DGridSearch(int width_x, int height_y, float cellsize_m); 00079 ~SBPL2DGridSearch(){destroy();} 00080 00083 bool setOPENdatastructure(SBPL_2DGRIDSEARCH_OPENTYPE OPENtype); 00084 00087 void destroy(); 00088 00095 bool search(unsigned char** Grid2D, unsigned char obsthresh, int startx_c, int starty_c, int goalx_c, int goaly_c, SBPL_2DGRIDSEARCH_TERM_CONDITION termination_condition); 00096 00099 void printvalues(); 00100 00103 inline int getlowerboundoncostfromstart_inmm(int x, int y) 00104 { 00105 if(term_condition_usedlast == SBPL_2DGRIDSEARCH_TERM_CONDITION_OPTPATHFOUND) 00106 { 00107 //heuristic search 00108 int h = SBPL_2DGRIDSEARCH_HEUR2D(x,y); 00109 //the logic is that if s wasn't expanded, then g(s) + h(s) >= maxcomputed_fval => g(s) >= maxcomputed_fval - h(s) 00110 return ( (searchStates2D_[x][y].iterationaccessed == iteration_ && searchStates2D_[x][y].g+h <= largestcomputedoptf_) ? 00111 searchStates2D_[x][y].g:(largestcomputedoptf_ < INFINITECOST?largestcomputedoptf_-h:INFINITECOST)); 00112 } 00113 else 00114 { 00115 //Dijkstra's search 00116 //the logic is that if s wasn't expanded, then g(s) >= maxcomputed_fval => g(s) >= maxcomputed_fval - h(s) 00117 return ( (searchStates2D_[x][y].iterationaccessed == iteration_ && searchStates2D_[x][y].g <= largestcomputedoptf_) ? 00118 searchStates2D_[x][y].g:largestcomputedoptf_); 00119 00120 } 00121 }; 00122 00125 int getlargestcomputedoptimalf_inmm() {return largestcomputedoptf_;}; 00126 00127 00128 private: 00129 00130 inline bool withinMap(int x, int y) {return (x >= 0 && y >= 0 && x < width_ && y < height_);}; 00131 void computedxy(); 00132 inline void initializeSearchState2D(SBPL_2DGridSearchState* state2D); 00133 bool createSearchStates2D(void); 00134 00135 bool search_withheap(unsigned char** Grid2D, unsigned char obsthresh, int startx_c, int starty_c, int goalx_c, int goaly_c, 00136 SBPL_2DGRIDSEARCH_TERM_CONDITION termination_condition); 00137 bool search_exp(unsigned char** Grid2D, unsigned char obsthresh, int startx_c, int starty_c, int goalx_c, int goaly_c); 00138 bool search_withbuckets(unsigned char** Grid2D, unsigned char obsthresh, int startx_c, int starty_c, int goalx_c, int goaly_c); 00139 bool search_withslidingbuckets(unsigned char** Grid2D, unsigned char obsthresh, int startx_c, int starty_c, int goalx_c, int goaly_c, 00140 SBPL_2DGRIDSEARCH_TERM_CONDITION termination_condition); 00141 00142 00143 //2D search data 00144 CSlidingBucket* OPEN2DBLIST_; 00145 CIntHeap* OPEN2D_; 00146 SBPL_2DGridSearchState** searchStates2D_; 00147 int dx_[SBPL_2DGRIDSEARCH_NUMOF2DDIRS]; 00148 int dy_[SBPL_2DGRIDSEARCH_NUMOF2DDIRS]; 00149 //the intermediate cells through which the actions go 00150 //(for all the ones with the first index <=7, there are none(they go to direct neighbors) -> initialized to -1) 00151 int dx0intersects_[SBPL_2DGRIDSEARCH_NUMOF2DDIRS]; 00152 int dx1intersects_[SBPL_2DGRIDSEARCH_NUMOF2DDIRS]; 00153 int dy0intersects_[SBPL_2DGRIDSEARCH_NUMOF2DDIRS]; 00154 int dy1intersects_[SBPL_2DGRIDSEARCH_NUMOF2DDIRS]; 00155 //distances of transitions 00156 int dxy_distance_mm_[SBPL_2DGRIDSEARCH_NUMOF2DDIRS]; 00157 00158 //OPEN data structure type 00159 SBPL_2DGRIDSEARCH_OPENTYPE OPENtype_; 00160 00161 //start and goal configurations 00162 int startX_, startY_; 00163 int goalX_, goalY_; 00164 00165 //map parameters 00166 int width_, height_; 00167 float cellSize_m_; 00168 00169 //search iteration 00170 int iteration_; 00171 00172 //largest optimal g-value computed by search 00173 int largestcomputedoptf_; 00174 00175 //termination criterion used in the search 00176 SBPL_2DGRIDSEARCH_TERM_CONDITION term_condition_usedlast; 00177 }; 00178 #endif 00179