gpt4 book ai didi

python - Othello Alpha-Beta Pruning 玩得很厉害 python

转载 作者:行者123 更新时间:2023-11-28 16:49:45 25 4
gpt4 key购买 nike

我目前正在尝试为奥赛罗制作一个好的 AI,并且已经使用 Minimax 算法完成了。然而,当我尝试使用 alpha-beta 修剪进行更深入的搜索时,该算法似乎运行得很糟糕。我用 Wiki 和 Berkely.edu 等其他来源进行了检查,我认为我已经正确实现了它,但我仍然找不到问题。

def alphabeta(board, player, a, b, lev):
h = heur(board, player)
if lev == 0:
return h, None
poss = get_legal_moves(board, player)
if len(poss) == 0:
return h, None
move = 0
for x in poss:
cpboard = board[:]
cpboard[x] = player
bracket(cpboard, player, x)
a1, q = alphabeta(cpboard, opponent_color(player), a, b, lev-1)
if player is me:
if a1 > a:
a, move = a1, x
else:
if a1 < b:
b, move = a1, x
if b <= a:
break
if player is me:
return a, move
else:
return b, move

最佳答案

您的 alpha-beta 代码可能是错误的。请注意当玩家“通过转弯”(即没有可用的 Action )时会发生什么,因此我的代码中有一个棘手的错误。

您调用递归时是否调换了 alpha 和 beta 值?我的工作方式如下(Java 代码):

private float minimax(OthelloBoard board, OthelloMove best, float alpha, float beta, int depth)
{
float bestResult = -Float.MAX_VALUE;
OthelloMove garbage = new OthelloMove();

int state = board.getState();
int currentPlayer = board.getCurrentPlayer();

if (state == OthelloBoard.STATE_DRAW)
return 0.0f;
if ((state == OthelloBoard.STATE_BLACK_WINS) && (currentPlayer == OthelloBoard.BLACK))
return INFINITY;
if ((state == OthelloBoard.STATE_WHITE_WINS) && (currentPlayer == OthelloBoard.WHITE))
return INFINITY;
if ((state == OthelloBoard.STATE_BLACK_WINS) && (currentPlayer == OthelloBoard.WHITE))
return -INFINITY;
if ((state == OthelloBoard.STATE_WHITE_WINS) && (currentPlayer == OthelloBoard.BLACK))
return -INFINITY;

if (depth == maxDepth)
return OthelloHeuristics.eval(currentPlayer, board);

ArrayList<OthelloMove> moves = board.getAllMoves(currentPlayer);

for (OthelloMove mv : moves)
{
board.makeMove(mv);
alpha = - minimax(board, garbage, -beta, -alpha, depth + 1);
board.undoMove(mv);

if (beta <= alpha)
return alpha;
if (alpha > bestResult)
{
best.setFlipSquares(mv.getFlipSquares());
best.setIdx(mv.getIdx());
best.setPlayer(mv.getPlayer());
bestResult = alpha;
}
}

return bestResult;
}

调用如下:

 OthelloMove bestFound = new OthelloMove();
int maxDepth = 8;
minimax(board, bestFound, -Float.MAX_VALUE, Float.MAX_VALUE, maxDepth);
//Wait for Thread to finish
board.makeMove(bestFound);

编辑:如果玩家没有可用的移动,getAllMoves() 返回一个“虚拟移动”,即根本不改变棋盘,只是通过转弯。

希望对您有所帮助!

关于python - Othello Alpha-Beta Pruning 玩得很厉害 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8826230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com