доски, и все казалось хорошо, пока я не посмотрел на оценки для каждой позиции. При перемещении белой пешки с e6 на e7, а затем черного ферзя с h1 на h6, позиция черных получает оценку 69,5, что явно неправильно, поскольку белые могут сделать продвижение на следующем ходу.
Я переписал свою альфа-бета-функцию в ее самую простую форму, как она написана в вики по шахматному программированию,
Моя:
Код: Выделить всё
def search(self, depth: int, whiteTurn: bool, alpha: int, beta: int, baseDepth: int) -> float:
if depth == 0:
return self.evaluate(whiteTurn)
moves = []
squares = list(self.board.keys())
# finding legal moves this turn
if whiteTurn:
for square in squares:
# check for a piece
if self.board[square] != '0' and self.board[square].isupper():
squareMoves = self.orderMoves(findLegalMoves(self.pythonBoard.legal_moves, square), square)
for move, score in squareMoves:
moves.append([ch.Move.from_uci(square + move), score])
moves = [move[0] for move in sorted(moves, key=lambda x: x[1], reverse=True)]
else:
for square in squares:
if self.board[square] != '0' and self.board[square].islower():
squareMoves = self.orderMoves(findLegalMoves(self.pythonBoard.legal_moves, square), square)
for move, score in squareMoves:
moves.append([ch.Move.from_uci(square + move), score])
moves = [move[0] for move in sorted(moves, key=lambda x: x[1], reverse=True)]
for move in moves:
self.pythonBoard.push(move)
fenboard = self.pythonBoard.board_fen()
self.board = fenConverter(fenboard)
evaluation = -self.search(depth - 1, not whiteTurn, -beta, -alpha, baseDepth)
if depth == DEPTH:
print(evaluation, '\n', self.pythonBoard)
self.pythonBoard.pop()
self.board = fenConverter(self.pythonBoard.board_fen())
if evaluation >= beta:
return beta
if evaluation > alpha:
if depth == DEPTH:
self.move = move
self.materialValue = evaluation
alpha = evaluation
return alpha
Код: Выделить всё
int alphaBeta( int alpha, int beta, int depthleft ) {
if( depthleft == 0 ) return quiesce( alpha, beta );
bestValue = -infinity;
for ( all moves) {
score = -alphaBeta( -beta, -alpha, depthleft - 1 );
if( score > bestValue )
{
bestValue = score;
if( score > alpha )
alpha = score; // alpha acts like max in MiniMax
}
if( score >= beta )
return bestValue; // fail soft beta-cutoff, existing the loop here is also fine
}
return bestValue;
}
Код: Выделить всё
def evaluate(self, isWhite: bool) -> float:
"""
evaluate evaluates the position
"""
materialValue = 0
squares = list(self.board.keys())
if self.pythonBoard.is_stalemate():
return 0
if self.pythonBoard.outcome() != None:
if self.pythonBoard.is_checkmate():
return float('-inf')
for square in squares:
# if there's a piece on the square
if self.board[square] != '0':
name = self.board[square]
color = findColor(name)
moves = set()
piece = Piece(name, color, 0, moves)
if color == 'black':
if name == 'p':
vMap = pawnMap(square)
elif name == 'n':
vMap = knightMap(square)
elif name == 'b':
vMap = bishopMap(square)
elif name == 'q':
vMap = queenMap(square)
elif name == 'k':
if ((self.whitePieceCount['Q'] == 0 and self.whitePieceCount['R']
Подробнее здесь: [url]https://stackoverflow.com/questions/78826528/basic-alpha-beta-algorithm-giving-wrong-evaluation[/url]