搜索算法:IDA*算法

 

搜索算法:IDA*算法

分类: 算法与数据结构   2922人阅读  评论(2)  收藏  举报

今天学习了IDA*算法,在这里总结一下:

 

IDA*算法是A*算法和迭代加深算法的结合。

 

迭代加深算法是在dfs搜索算法的基础上逐步加深搜索的深度,它避免了广度优先搜索占用搜索空间太大的缺点,也减少了深度优先搜索的盲目性。它主要是在递归搜索函数的开头判断当前搜索的深度是否大于预定义的最大搜索深度,如果大于,就退出这一层的搜索,如果不大于,就继续进行搜索。这样最终获得的解必然是最优解。

 

而在A*算法中,我们通过使用合理的估价函数,然后在获得的子节点中选择fCost最小的节点进行扩展,以此完成搜索最优解的目的。但是A*算法中,需要维护关闭列表和开放列表,需要对扩展出来的节点进行检测,忽略已经进入到关闭列表中的节点(也就是所谓的“已经检测过的节点”),另外也要检测是否与待扩展的节点重复,如果重复进行相应的更新操作。所以A*算法主要的代价花在了状态检测和选择代价最小节点的排序上,这个过程中占用的内存是比较大的,一般为了提高效率都是使用hash进行重复状态检测。

 

而IDA*算法具有如下的特点:(

综合了A*算法的人工智能性和回溯法对空间的消耗较少的优点,在一些规模很大的搜索问题中会起意想不到的效果。它的具体名称是 Iterative Deepening A*, 1985年由Korf提出。该算法的最初目的是为了利用深度搜索的优势解决广度A*的空间问题,其代价是会产生重复搜索。归纳一下,IDA*的基本思路是:首先将初始状态结点的H值设为阈值maxH,然后进行深度优先搜索,搜索过程中忽略所有H值大于maxH的结点;如果没有找到解,则加大阈值maxH,再重复上述搜索,直到找到一个解。在保证H值的计算满足A*算法的要求下,可以证明找到的这个解一定是最优解。在程序实现上,IDA* 要比 A* 方便,因为不需要保存结点,不需要判重复,也不需要根据 H值对结点排序,占用空间小。 
而这里在IDA*算法中也使用合适的估价函数,来评估与目标状态的距离。

在一般的问题中是这样使用IDA*算法的,当前局面的估价函数值+当前的搜索深度 > 预定义的最大搜索深度时,就进行剪枝。

这个估计函数的选取没有统一的标准,找到合适的该函数并不容易,但是可以大致按照这个原则:在一定范围内加大各个状态启发函数值的差别

 

下面是poj2286 the rotation game的一道题目,使用了IDA*算法:

 

 

 

 

[cpp]  view plain copy
  1. #include <iostream>  
  2. #include <string>  
  3. using namespace std;  
  4. //a:记录数字的数组   
  5. int a[25], cnt[25];  
  6. char out[1000];  
  7. int deep;  
  8.   
  9. //最后的局面中心的结果数字   
  10. int res;  
  11.   
  12. //判断是否到达目标状态   
  13. bool ok(int *a) {  
  14.     int tmp=a[7];  
  15.   
  16.     if( tmp!=a[8] || tmp!=a[9] || tmp!=a[12] || tmp!=a[13]  
  17.     || tmp!=a[16] || tmp!=a[17] || tmp!=a[18] )  
  18.         return false;  
  19.     return true;  
  20. }  
  21. //旋转   
  22. inline void change(int* cur,int a,int b,int c,int d,int e,int f,int g)  
  23. {  
  24.     int tmp=cur[a];  
  25.     cur[a]=cur[b],cur[b]=cur[c],cur[c]=cur[d],cur[d]=cur[e],cur[e]=cur[f],cur[f]=cur[g],cur[g]=tmp;  
  26. }  
  27.   
  28. //计算8个标准位置中数量最多的数字有多少个   
  29. inline int count(int* a)  
  30. {  
  31.     memset(cnt,0,sizeof(cnt));  
  32.     cnt[a[7]]++;cnt[a[8]]++;cnt[a[9]]++;  
  33.     cnt[a[12]]++;cnt[a[13]]++;cnt[a[16]]++;  
  34.     cnt[a[17]]++;cnt[a[18]]++;  
  35.     cnt[2]=cnt[1]>cnt[2]?cnt[1]:cnt[2];  
  36.     return max(cnt[2],cnt[3]);  
  37. }  
  38.   
  39. //pre:上一次递归移动的方向   
  40. //now:当前递归的步数   
  41. bool dfs(int *a, int now, int pre) {  
  42.     //剪枝:如果当前可以递归的深度已经小于局面中心数字所需量   
  43.     if (deep - now  < 8 - count(a))  
  44.        return false;  
  45.     //超过当前的搜索深度   
  46.     if (now >= deep)   
  47.         return false;  
  48.     int temp[25];           //备份当前的局面   
  49.     for (int i = 0; i < 8; ++i) {  
  50.         //如果前后两次拉动的方向恰好相反,那么就剪枝   
  51.         if( (pre==0 && i==5) || (pre==5 && i==0) ) continue;  
  52.         if( (pre==1 && i==4) || (pre==4 && i==1) ) continue;  
  53.         if( (pre==2 && i==7) || (pre==7 && i==2) ) continue;  
  54.         if( (pre==3 && i==6) || (pre==6 && i==3) ) continue;  
  55.         for (int j = 1; j <= 24; ++j) {  
  56.             temp[j] = a[j];  
  57.         }  
  58.         switch(i) {  
  59.         case 0:out[now]='A';change(temp,1,3,7,12,16,21,23);break;  
  60.         case 1:out[now]='B';change(temp,2,4,9,13,18,22,24);break;  
  61.         case 2:out[now]='C';change(temp,11,10,9,8,7,6,5);break;  
  62.         case 3:out[now]='D';change(temp,20,19,18,17,16,15,14);break;  
  63.         case 4:out[now]='E';change(temp,24,22,18,13,9,4,2);break;  
  64.         case 5:out[now]='F';change(temp,23,21,16,12,7,3,1);break;  
  65.         case 6:out[now]='G';change(temp,14,15,16,17,18,19,20);break;  
  66.         case 7:out[now]='H';change(temp,5,6,7,8,9,10,11);break;  
  67.         }  
  68.         if (ok(temp)) {  
  69.             res = temp[7];  
  70.             out[now+1] = '/0';  
  71.             return true;  
  72.         }  
  73.         if (dfs(temp, now + 1, i))  
  74.             return true;  
  75.   
  76.     }  
  77.     return false;  
  78. }  
  79.   
  80. int main() {  
  81.     while (scanf("%d", &a[1]) && a[1]) {  
  82.         for (int i = 2; i <= 24; ++i)   
  83.             scanf("%d", a+i);  
  84.         if (ok(a)) {                //如果已经符合要求了   
  85.             printf("No moves needed/n");  
  86.             printf("%d/n", a[7]);  
  87.         }  
  88.         else {  
  89.             deep = 1;  
  90.             //clock_t time= clock();  
  91.             while (1) {  
  92.                 if (dfs(a, 0, -1))  
  93.                     break;  
  94.                 ++deep;  
  95.               
  96.             }  
  97.             printf("%s/n", out);  
  98.             printf("%d/n", res);  
  99.   
  100.         }  
  101.     }  
  102.     return 0;  
  103. }  

 

有空找一下错误,发现错误的哥们可以给我留个言:(ps:错误解决了,在IDA*算法的递归调用if语句为true时,应该返回的是true,而不是false)。

 

[cpp]  view plain copy
  1. /* 
  2.  *  POJ2286 The Rotation Game 
  3.  *  解题思路:使用迭代加深的深度搜索算法,这里非常要注意还是剪枝的问题 
  4.  *  只有较好的针对题目环境的剪枝,才能提高搜索效率  
  5. */   
  6. #include<iostream>  
  7. #include<fstream>  
  8. #include<string>  
  9. using namespace std;  
  10.   
  11. //使用数组表示游戏局面   
  12. int map[25],countArray[25];  
  13. //搜索深度 , 最终的局面中心数字   
  14. int DEPTH,res;  
  15.   
  16. char outPath[100];  
  17.   
  18. //判断是否到达了目标局面   
  19. bool isOk(const int* state){  
  20.       
  21.     int tmp = state[7];  
  22.     if( tmp!= state[8] || tmp!=state[9] || tmp!= state[12]   
  23.         || tmp!= state[13] || tmp!=state[16] || tmp!= state[17]   
  24.         || tmp!= state[18] ){  
  25.               
  26.             return false  ;   
  27.         }  
  28.     return true ;  
  29. }  
  30.   
  31. //统计局面的中心区域含有相同数字的最大数量   
  32. int countMaxSameNumber(const int* state){  
  33.       
  34.     memset(countArray , 0 ,sizeof(countArray) ) ;         
  35.     countArray[ state[7] ]++; countArray[state[8]] ++; countArray[state[9]]++;   
  36.     countArray[ state[12] ]++; countArray[ state[13] ]++ ; countArray[state[16]]++;  
  37.     countArray[state[17]] ++ ; countArray[state[18]]++;  
  38.       
  39.     countArray[2] = (countArray[2]>countArray[1]) ? countArray[2]: countArray[1];  
  40.     return max(countArray[2],countArray[3]);  
  41. }  
  42.   
  43. void changeState(int *state,int a1,int a2,int a3,int a4,int a5,int a6,int a7){  
  44.       
  45.     int tmp = state[a1];  
  46.     state[a1]=state[a2],state[a2]=state[a3],state[a3]=state[a4],  
  47.     state[a4]=state[a5],state[a5]=state[a6],state[a6]=state[a7],state[a7]=tmp;  
  48.       
  49. }  
  50.   
  51. //迭代加深搜索  
  52. //state:当前局面 currDepth :当前所处的搜索深度 preDir:当前搜索选择的旋转的方向   
  53. bool dfsSearch( int* state ,int currDepth , int preDir) {  
  54.       
  55.     //剪枝 1  : 本质上使用的就是IDA*估价函数进行剪枝   
  56.     if( DEPTH - currDepth < 8- countMaxSameNumber(state))  
  57.         return false ;  
  58.       
  59.     //超过了当前的搜索深度   
  60.     if( DEPTH <= currDepth )  
  61.         return false ;  
  62.           
  63.     int tmp[25];  
  64.     for(int i=1 ; i<=8 ; i++){  
  65.           
  66.         //剪枝2 :前后连续的相反方向的两次旋转是没有意义的   
  67.         if( (1 == i && 6 == preDir) || (6==i && 1== preDir) )   continue ;  
  68.         if( (2 == i && 5 == preDir) || (5==i && 2== preDir) )   continue ;  
  69.         if( (3 == i && 8 == preDir) || (8==i && 3== preDir) )   continue ;  
  70.         if( (4 == i && 7 == preDir) || (7==i && 4== preDir) )   continue ;  
  71.           
  72.     //  memcpy( tmp , state , sizeof(state)) ;  
  73.         for(int k=1 ; k<=24 ; k++)  
  74.             tmp[k]=state[k];  
  75.           
  76.         switch(i){  
  77.             //记录搜索路径   
  78.             case 1 : outPath[currDepth] = 'A' ; changeState(tmp,1,3,7,12,16,21,23); break;  
  79.             case 2 : outPath[currDepth] = 'B' ; changeState(tmp,2,4,9,13,18,22,24); break;  
  80.             case 3 : outPath[currDepth] = 'C' ; changeState(tmp,11,10,9,8,7,6,5); break;  
  81.             case 4 : outPath[currDepth] = 'D' ; changeState(tmp,20,19,18,17,16,15,14); break;  
  82.             case 5 : outPath[currDepth] = 'E' ; changeState(tmp,24,22,18,13,9,4,2); break;  
  83.             case 6 : outPath[currDepth] = 'F' ; changeState(tmp,23,21,16,12,7,3,1); break;  
  84.             case 7 : outPath[currDepth] = 'G' ; changeState(tmp,14,15,16,17,18,19,20); break;  
  85.             case 8 : outPath[currDepth] = 'H' ; changeState(tmp,5,6,7,8,9,10,11); break;  
  86.             default : cout<<"ERROR!"<<endl;  
  87.         }  
  88.           
  89.         if( isOk(tmp) ){  
  90.               
  91.             res = tmp[7];  
  92.             outPath[currDepth +1 ]='/0';  
  93.             return true ;  
  94.         }  
  95.               
  96.         if( dfsSearch(tmp , currDepth+1 , i))  
  97.             return true ;  
  98.     }  
  99.       
  100.     return false ;  
  101. }  
  102.   
  103.   
  104. int main(){  
  105.       
  106.     ifstream in("test.txt");  
  107.       
  108.     while(1){  
  109.           
  110.         in>>map[1];  
  111.         if( 0 == map[1])  
  112.             break;  
  113.           
  114.         for(int i=2 ; i<=24 ; i++)  
  115.             in>>map[i];  
  116.               
  117.         if( isOk(map)){  
  118.             cout<<"No moves needed"<<endl;  
  119.             cout<<map[7]<<endl;  
  120.         }  
  121.           
  122.         else{  
  123.             DEPTH =1 ;  
  124.             while(1){  
  125.                 if(dfsSearch(map , 0 , -1 ))  
  126.                     break;  
  127.                 DEPTH ++ ;  
  128.             }  
  129.               
  130.             cout<<outPath<<endl;  
  131.             cout<<res<<endl;  
  132.         }  
  133.           
  134.     }  
  135. }  

 

下面是一道自己做的poj2870的搜索题,在这道题中,我也使用了IDA*算法来剪枝,比起纯粹的dfs,效果还是很明显的。

[cpp]  view plain copy
  1. /* 
  2.  *  POJ2870 Light Up 
  3. */  
  4. //#include<cstdio>  
  5. #include<iostream>  
  6. #include<fstream>  
  7. #include<ctime>  
  8. #define MAXSIZE 8  
  9. #define MAX_BARRIAR 50  
  10. #define NOT !  
  11. #define __DEBUG 0  
  12. using namespace std;  
  13.   
  14. char board[MAXSIZE][MAXSIZE];  
  15. int wid,height,barriarNum;  
  16. //表示搜索的深度   
  17. int depth,minLamp;  
  18.   
  19. //记录有编号的障碍的位置的结构体   
  20. typedef struct numedbarriar{  
  21.       
  22.     int x;  
  23.     int y;  
  24.     int no;  
  25. }NumedBarriar;  
  26. NumedBarriar barriarArray[MAX_BARRIAR] ;  
  27.   
  28. static int dir[4][2]={ {0,-1},{-1,0},{0,1},{1,0} };  
  29.   
  30. //打印当前局面   
  31. void print(char c[][MAXSIZE]){  
  32.       
  33.     for(int i=1 ; i<=height ; i++){  
  34.           
  35.         for(int j=1 ; j<=wid ; j++){  
  36.               
  37.             cout<<c[i][j]<<" ";  
  38.         }  
  39.         cout<<endl;  
  40.     }  
  41.     cout<<endl<<endl;  
  42. }  
  43.   
  44. inline bool isInBoard(int x,int y){  
  45.       
  46.     return ( x<1 || x>height || y<1 || y>wid ) ? false : true ;  
  47. }  
  48.    
  49. //标记被照亮的地方   
  50. void change(char map[MAXSIZE][MAXSIZE],int x,int y){  
  51.       
  52.     map[x][y]='L';  
  53.     int m,n ;  
  54.       
  55.     //往左   
  56.     for(int j=0 ; j<4 ; j++){  
  57.           
  58.         for( m=x+dir[j][0] , n=y+dir[j][1]; isInBoard(m,n) ; m+=dir[j][0], n+=dir[j][1] ){  
  59.               
  60.             if'.'== map[m][n] || 'm' == map[m][n]){  
  61.               
  62.                 map[m][n]='m';  
  63.             }   
  64.             else{  
  65.               
  66.                 break;  
  67.             }  
  68.         }  
  69.     }  
  70.   
  71. }  
  72.   
  73. //计算有编号的障碍周围还缺少的Lamp数目   
  74. int gCost(char map[][MAXSIZE]){  
  75.       
  76.     int num=0 ,x,y , dist =0 ;  
  77.        
  78.     for(int i=0 ; i<barriarNum ;i++) {  
  79.           
  80.         if( -1 == barriarArray[i].no )  
  81.             continue ;  
  82.               
  83.         num = 0;   
  84.         for(int j=0; j<4 ;j++){  
  85.               
  86.             x = barriarArray[i].x + dir[j][0];  
  87.             y = barriarArray[i].y + dir[j][1];  
  88.               
  89.             if( isInBoard(x,y) && 'L' == map[x][y] ){  
  90.                   
  91.                 num ++ ;  
  92.             }  
  93.         }  
  94.           
  95.         dist += barriarArray[i].no - num ;  
  96.     }  
  97.       
  98.     return dist ;  
  99. }  
  100.   
  101. inline bool isCompleted(char map[][MAXSIZE]){  
  102.       
  103.     for(int i=1 ; i<=height ;i++){  
  104.           
  105.         for(int j=1; j<=wid ; j++){  
  106.               
  107.             if'.' == map[i][j])  
  108.                 return false ;  
  109.         }  
  110.     }  
  111.     return (true && 0 == gCost(map));  
  112. }  
  113.   
  114.   
  115. //检查是否超出带编号barriar规定的周围最大的lamp数量    
  116. bool isAllowed(char map[][MAXSIZE]){  
  117.       
  118.     int x,y,num;  
  119.       
  120.     for(int i=0 ; i<barriarNum ;i++) {  
  121.           
  122.         if( -1 == barriarArray[i].no )  
  123.             continue ;  
  124.   
  125.         //每次检查前初始化   
  126.         num = 0;  
  127.         for(int j=0; j<4 ;j++){  
  128.               
  129.             x = barriarArray[i].x + dir[j][0];  
  130.             y = barriarArray[i].y + dir[j][1];  
  131.               
  132.             if( isInBoard(x,y) && 'L' == map[x][y] ){  
  133.                   
  134.                 num ++ ;  
  135.             }  
  136.         }  
  137.           
  138.         if( num > barriarArray[i].no)  
  139.             return false ;  
  140.     }  
  141.       
  142.     return true  ;  
  143.       
  144. }  
  145.   
  146. bool dfsSearch(char map[MAXSIZE][MAXSIZE],int currDepth) {  
  147.       
  148.       
  149.     //IDA*剪枝   
  150.     if( currDepth + gCost(map) > depth )  
  151.         return false ;  
  152.           
  153.     if( currDepth >=minLamp )  
  154.         return false ;  
  155.       
  156.     //剪枝1    
  157.     if(NOT isAllowed(map)){  
  158.           
  159.         return false ;  
  160.     }  
  161.       
  162.       
  163.     int i,j;  
  164.     char tmpMap[MAXSIZE][MAXSIZE];  
  165.       
  166.     for(i=1 ; i<= height ; i++){  
  167.         for(j=1 ; j<=wid ; j++){  
  168.               
  169.             //找到可放置的位置   
  170.             if'.' == map[i][j] ){  
  171.                   
  172.                   
  173.                 //memcpy(tmpMap , map , sizeof(map) );  
  174.                 for(int m=1; m<= height ; m++){  
  175.                       
  176.                     for(int n=1 ; n<=wid ; n++){  
  177.                           
  178.                         tmpMap[m][n]= map[m][n];  
  179.                     }  
  180.                 }  
  181.                   
  182.                 //改变当前的局面,标记当前lamp照亮的位置   
  183.                 change(tmpMap,i,j);  
  184.                   
  185.                 if( isCompleted(tmpMap) ) {  
  186.           
  187.                     if( currDepth < minLamp )  
  188.                     minLamp = currDepth+1;  
  189. //                  cout<<"find"<<minLamp<<endl;  
  190.                     print(tmpMap);  
  191.                     return true;  
  192.                 }  
  193.                           
  194.                 if(dfsSearch( tmpMap , currDepth+1)){  
  195. //                  print(tmpMap);  
  196.                     return true ;  
  197.                 }  
  198.                       
  199.                   
  200.                 //memcpy(map,tmpMap,sizeof(tmpMap));  
  201.                 //for(int m=1; m<= height ; m++){  
  202. //                    
  203. //                  for(int n=1 ; n<=wid ; n++){  
  204. //                        
  205. //                      map[m][n]= tmpMap[m][n];  
  206. //                  }  
  207. //              }  
  208.   
  209.             }  
  210.         }  
  211.     }  
  212.     return false ;  
  213. }  
  214.   
  215. int main(){  
  216.       
  217.       
  218.     ifstream in("test.txt");  
  219.     bool findSolution;  
  220.       
  221.     while(1){  
  222.           
  223.         in>>height>>wid ;  
  224.         if( 0==wid && 0== height)  
  225.             break;  
  226.   
  227.         //初始化局面   
  228.         for(int i=0 ; i< MAXSIZE ; i++){  
  229.               
  230.             for(int j=0 ; j<MAXSIZE ; j++){  
  231.                   
  232.                 board[i][j] = '.';  
  233.             }  
  234.         }  
  235.         findSolution = true ;         
  236.         minLamp=100;  
  237.           
  238.         in>>barriarNum;  
  239.         for(int i=0 ; i<barriarNum ; i++){  
  240.               
  241.             in>>barriarArray[i].x>>barriarArray[i].y>>barriarArray[i].no;  
  242.             //普通的barriar   
  243.             if( -1 == barriarArray[i].no)  
  244.                 board[ barriarArray[i].x ][barriarArray[i].y]='b';  
  245.             //有编号的barriar   
  246.             else{  
  247.                   
  248.                 board[ barriarArray[i].x ][barriarArray[i].y]=barriarArray[i].no+'0';  
  249.             }   
  250.         }  
  251.           
  252.         depth = 0;  
  253.           
  254.           
  255.         clock_t time=clock();  
  256.         while(1){  
  257.               
  258.               
  259.             if( NOT dfsSearch(board , 0))  
  260.                 depth ++;  
  261.             else  
  262.                 break;  
  263.                   
  264.             if(depth>100){  
  265.                   
  266.                 cout<<"No solution"<<endl;  
  267.                 findSolution = false ;  
  268.                 break;  
  269.             }  
  270.                   
  271.         }  
  272.         cout <<"time: "<< clock()-time<<" MS" <<endl;  
  273.         cout<<"Depth: "<<depth<<endl;  
  274.         if(findSolution )  
  275.             cout<<minLamp<<endl;  
  276.           
  277.     }  
  278. }  

 

在这里我总结一下IDA*搜索算法的一些注意点:

 

1、IDA*算法中,main函数一般使用逐步加大搜索深度来获得最优解,而如果使用dfs搜索策略的话,就需要对整个搜索空间进行搜索,在搜索函数中判断出所有合法解中的最优解(通过比较合法解的深度,选择深度最小的,其实在bfs搜索中,深度对应的就是题目中所要求的最优解量)

2、在IDA*的搜索核心算法中,前半部分就是剪枝,返回false的情况,这点和dfs是一样的。而在常规的搜索中,当找到最优解时,就返回true,而当后面的if语句中的dfs递归为true时,返回true,如此退出整个循环,否则会死循环。为了提高效率,可以把当前状态拷贝到临时状态,然后使用临时状态进行递归搜索,这样就不需要在递归函数返回时,还原原来的整个状态。

 

另外可以看到通过下面的代码中添加适当的语句就可以还原出整个最优解的路径:

[c-sharp]  view plain copy
  1.                 if( isCompleted(tmpMap) ) {  
  2.           
  3.                     if( currDepth < minLamp )  
  4.                     minLamp = currDepth+1;  
  5. //                  cout<<"find"<<minLamp<<endl;  
  6.                     print(tmpMap);  
  7.                     return true;  
  8.                 }  
  9.                           
  10.                 if(dfsSearch( tmpMap , currDepth+1)){  
  11. //                  print(tmpMap);  
  12.                     return true ;  
  13.                 }  

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值