[英]Issue with MiniMax to Alpha-Beta Search Conversion
OK, my issue should sound pretty familiar to anyone who has played with board game programming, so here it is : 好吧,对于玩过棋盘游戏编程的人来说,我的问题听起来很熟悉,所以这里是:
So, here's my MiniMax code : 那么,这是我的MiniMax代码:
Move* Board::miniMax(int depth)
{
return this->maxMove(1, depth);
}
Move* Board::maxMove(int ply, int depth)
{
vector<Move*> moves = this->possibleMoves();
int movesSize = moves.size();
Move* maxMove = new Move(MINUS_INF);
for (int i=0; i<movesSize; i++)
{
Move* move = moves[i];
HASHMAKE(move,this);
move->value = (ply<depth) ? (this->minMove(ply+1, depth))->value
: this->eval();
maxMove = MAXMOVE(maxMove,move);
UNHASHMAKE(move,this);
}
return maxMove;
}
Move* Board::minMove(int ply, int depth)
{
vector<Move*> moves = this->possibleMoves();
int movesSize = moves.size();
Move* minMove = new Move(PLUS_INF);
for (int i=0; i<movesSize; i++)
{
Move* move = moves[i];
HASHMAKE(move,this);
move->value = (ply<depth) ? (this->maxMove(ply+1, depth))->value
: this->eval();
minMove = MINMOVE(minMove,move);
UNHASHMAKE(move,this);
}
return minMove;
}
Any ideas one how the above thing could be adjusted so that it's an Alpha-Beta search? 任何想法如何调整上述事情,以便它是一个Alpha-Beta搜索?
And here's my attempt at Alpha-Beta conversion (which fails miserably) : 这是我尝试Alpha-Beta转换(失败的失败):
Move* Board::alphaBeta(int depth)
{
return this->alphaMax(1,depth,MINUS_INF,PLUS_INF);
}
Move* Board::alphaMax(int ply, int depth, int a, int b)
{
vector<Move*> moves = this->possibleMoves();
int movesSize = moves.size();
Move* maxMove = new Move(MINUS_INF);
for (int i=0; i<movesSize; i++)
{
Move* move = moves[i];
HASHMAKE(move,this);
move->value = (ply<depth) ? (this->alphaMin(ply+1, depth,a,b))->value
: this->eval();
maxMove = MAXMOVE(maxMove,move);
if (maxMove->value>=b) return maxMove;
a = MAXVAL(a,maxMove->value);
UNHASHMAKE(move,this);
}
return maxMove;
}
Move* Board::alphaMin(int ply, int depth, int a, int b)
{
vector<Move*> moves = this->possibleMoves();
int movesSize = moves.size();
Move* minMove = new Move(PLUS_INF);
for (int i=0; i<movesSize; i++)
{
Move* move = moves[i];
HASHMAKE(move,this);
move->value = (ply<depth) ? (this->alphaMax(ply+1, depth,a,b))->value
: this->eval();
minMove = MINMOVE(minMove,move);
if (minMove->value<=a) return minMove;
b = MINVAL(b,minMove->value);
UNHASHMAKE(move,this);
}
return minMove;
}
HINTS (to avoid any misunderstanding) : 提示(以避免任何误解) :
the this->eval()
function returns a score from player A's perspective. this->eval()
函数从玩家A的角度返回一个分数。 Eg a +100 score means the position is in favour of player A, while a -100 score means the position is in favour of player B. 例如,+ 100得分意味着该位置有利于玩家A,而-100得分意味着该位置有利于玩家B.
MINUS_INF
and PLUS_INF
have been defined as some arbitrarily small and big values, respectively. MINUS_INF
和PLUS_INF
分别被定义为一些任意小的和大的值。
This not anything like a homework or anything (if it was I'd most likely never have any interest to play with such stuff... lol) 这不是什么像家庭作业或任何东西(如果是我很可能从来没有兴趣玩这样的东西......大声笑)
Move
is a simple class containing details about the move, as well as it's respective value (as assigned by the eval
function. Move
是一个简单的类,包含有关移动的详细信息,以及它的相应值(由eval
函数指定)。
HASHMAKE
and UNHASHMAKE
are just 2 move-(un)making and move-(un)hashing macros, that shouldn't make much difference. HASHMAKE
和UNHASHMAKE
只是2个移动(非)制作和移动(非)散列宏,这应该没什么区别。
MAXMOVE
is defined like this : #define MAXMOVE(A,B) (((A)->value>=(B)->value)?(A):(B))
MAXMOVE
的定义如下: #define MAXMOVE(A,B) (((A)->value>=(B)->value)?(A):(B))
MINMOVE
is defined like this : #define MINMOVE(A,B) (((A)->value<=(B)->value)?(A):(B))
MINMOVE
定义如下: #define MINMOVE(A,B) (((A)->value<=(B)->value)?(A):(B))
Not sure if this is it, but I think in alphaMin
不确定是不是这样,但我认为在
alphaMin
if (minMove->value<=a) return minMove;
b = MINVAL(b,minMove->value);
UNHASHMAKE(move,this);
should be 应该
UNHASHMAKE(move,this);
if (minMove->value<=a) return minMove;
b = MINVAL(b,minMove->value);
and also a similar change in alphaMax
. 以及
alphaMax
的类似变化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.