基本搜索算法

12 篇文章 0 订阅

博弈树搜索技术简介:

博弈树的搜索算法,负值极大搜索,alpha-beta搜索,渴望搜索,PVS极窄窗口搜索等。通常来说,搜索算法常常和以下技术联合在一起。

如下:

1.置换表,记录已经搜索过的棋局,避免再次搜索。

2.吃子启发,优先试下能够吃对方棋子的走法。

3.杀手启发,历史启发简化版。

4.历史启发,优先试下历史统计数据得出的比较好的走法。

5.静止期搜索,继续对某些叶子结点搜索,避免水平线效应。

6.迭代加深搜索,根据搜索时间,状态。决定是否继续搜索。

 有兴趣的朋友可以深入研究一下上述技术和算法。

 

吃子棋搜索算法:

我的程序最初的实现使用的负值极大搜索算法,之后改用alpha-beta搜索算法,后来又使用PVS极窄窗口搜索算法。

在我自己的实现里没有使用置换表,历史启发等技术。是因为吃子棋每层的走法数相对较少,所以并没有使用。

但我们知道,这些技术可以很大的提高搜索效率。

 

吃子棋搜索算法源码:

接下来,看看吃子棋搜索算法的源代码:

负值极大算法

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. int CNegaMaxEngine::negaMax(int depth)  
  2. {  
  3.     int currentMaxScore = -20000;//init value mini  
  4.     int score;  
  5.     int nextMoveCount;  
  6.     int overNum = IsGameOver(CurPosition, depth);  
  7.     if (overNum != 0)return overNum;  
  8.     if (depth <= 0)  
  9.         return m_pEval->Eveluate(CurPosition, (m_nMaxDepth - depth ) % 2 );  
  10.     nextMoveCount = m_pMG->CreatePossibleMove(CurPosition, depth, (m_nMaxDepth - depth) % 2);  
  11.     for (int i = 0; i < nextMoveCount; i++)  
  12.     {  
  13.         MakeMove(&m_pMG->m_MoveList[depth][i], (m_nMaxDepth - depth) % 2);  
  14.         score = -negaMax(depth-1);  
  15.         UnMakeMove(&m_pMG->m_MoveList[depth][i]);  
  16.         if (score>currentMaxScore)  
  17.         {  
  18.             currentMaxScore = score;  
  19.             if (depth == m_nMaxDepth){  
  20.               
  21.                 m_cmBestMove = m_pMG->m_MoveList[depth][i];  
  22.             }  
  23.         }  
  24.     }  
  25.     return currentMaxScore;  
  26. }  
alpha-beta算法
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. int CAlphtBetaEngine::alphabeta(int depth,int alpha ,int beta)  
  2. {  
  3.       
  4.     int score;  
  5.     int nextMoveCount;  
  6.     int overNum = IsGameOver(CurPosition, depth);  
  7.       
  8.     if (overNum != 0)return overNum;  
  9.   
  10.     int whoTurn = (m_nMaxDepth - depth) % 2;  
  11.     if (depth <= 0)  
  12.         return m_pEval->Eveluate(CurPosition, whoTurn);  
  13.     nextMoveCount = m_pMG->CreatePossibleMove(CurPosition, depth, whoTurn);  
  14.     for (int i = 0; i < nextMoveCount; i++)  
  15.     {  
  16.         MakeMove(&m_pMG->m_MoveList[depth][i], whoTurn);  
  17.         score = -alphabeta(depth - 1, -beta, -alpha);  
  18.         UnMakeMove(&m_pMG->m_MoveList[depth][i]);  
  19.         if (score>alpha)  
  20.         {  
  21.             alpha = score;  
  22.             if (depth == m_nMaxDepth){  
  23.   
  24.                 m_cmBestMove = m_pMG->m_MoveList[depth][i];  
  25.             }  
  26.         }  
  27.         if (alpha >= beta)break;  
  28.     }  
  29.     return alpha;  
  30. }  

PVS算法:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. int CPVS_Engine::PrincipalVariation(int depth, int alpha, int beta)  
  2. {  
  3.     int score;  
  4.     int Count, i;  
  5.     BYTE type;  
  6.     int best;  
  7.   
  8.     i = IsGameOver(CurPosition, depth);  
  9.     if (i != 0)  
  10.         return i;  
  11.   
  12.     if (depth <= 0)    //叶子节点取估值  
  13.         return m_pEval->Eveluate(CurPosition, false);  
  14.   
  15.     Count = m_pMG->CreatePossibleMove(CurPosition, depth, (m_nMaxDepth - depth ) % 2);  
  16.   
  17.   
  18.      MakeMove(&m_pMG->m_MoveList[depth][0], (m_nMaxDepth - depth ) % 2);  
  19.     best = -PrincipalVariation(depth - 1, -beta, -alpha);  
  20.     UnMakeMove(&m_pMG->m_MoveList[depth][0]);  
  21.     if (depth == m_nMaxDepth)  
  22.         m_cmBestMove = m_pMG->m_MoveList[depth][0];  
  23.   
  24.     for (i = 1; i<Count; i++)  
  25.     {  
  26.   
  27.         if (best < beta)  
  28.         {  
  29.             if (best > alpha)  
  30.                 alpha = best;  
  31.             MakeMove(&m_pMG->m_MoveList[depth][i], (m_nMaxDepth - depth ) % 2);  
  32.             score = -PrincipalVariation(depth - 1, -alpha - 1, -alpha);  
  33.             if (score > alpha && score < beta)  
  34.             {  
  35.                 best = -PrincipalVariation(depth - 1, -beta, -score);  
  36.                 if (depth == m_nMaxDepth)  
  37.                     m_cmBestMove = m_pMG->m_MoveList[depth][i];  
  38.             }  
  39.             else if (score > best)  
  40.             {  
  41.                 best = score;  
  42.                 if (depth == m_nMaxDepth)  
  43.                     m_cmBestMove = m_pMG->m_MoveList[depth][i];  
  44.             }  
  45.             UnMakeMove(&m_pMG->m_MoveList[depth][i]);  
  46.         }  
  47.     }  
  48.   
  49.     return best;  
  50. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值