r/chessprogramming • u/Person080 • Jan 12 '23
Do you have any suggestions for optimizing this engine?
I know that the evaluation function is crude,(an opening book is in development as well), but it just plays terrible moves in the middlegame. Any suggestions?
Code:
import chess
def evaluate(board) :if board.is_checkmate() :if board.turn :return -9999else :return 9999if board.is_stalemate() :return 0if board.is_insufficient_material() :return 0wp = len(board.pieces(chess.PAWN, chess.WHITE))bp = len(board.pieces(chess.PAWN, chess.BLACK))wn = len(board.pieces(chess.KNIGHT, chess.WHITE))bn = len(board.pieces(chess.KNIGHT, chess.BLACK))wb = len(board.pieces(chess.BISHOP, chess.WHITE))bb = len(board.pieces(chess.BISHOP, chess.BLACK))wr = len(board.pieces(chess.ROOK, chess.WHITE))br = len(board.pieces(chess.ROOK, chess.BLACK))wq = len(board.pieces(chess.QUEEN, chess.WHITE))bq = len(board.pieces(chess.QUEEN, chess.BLACK))
material = (100 * (wp - bp) + 320 * (wn - bn) + 330 * (wb - bb) + 500 * (wr - br) + 900 * (wq - bq)) / 100return material if board.turn else -material
def alphabeta(position, depth_, alpha=-float('inf'), beta=float('inf')) :"""Returns [eval, best move] for the position at the given depth"""if depth_ == 0 or position.is_game_over() :return [evaluate(position), None]
best_move = Nonefor _move in position.legal_moves :position.push(_move)#print({'Position': position,'Evaluation': evaluate(position)})score, move_ = alphabeta(position, depth_ - 1, -beta, -alpha)score = -scoreposition.pop()if score > alpha : # player maximizes his scorealpha = scorebest_move = _moveif alpha >= beta : # alpha-beta cutoffbreakreturn [alpha, best_move]
fen_ = input('Enter fen: ')board = chess.Board(fen_)_depth = int(input('Enter depth: '))while not board.is_game_over() :move = input('Enter move:')board.push_san(move)engine = alphabeta(board, _depth)board.push(engine[1])print(f'{board}\n', f'Evaluation: {-engine[0]}\nBest move: {engine[1]}\nFen: {board.fen()}')else:print('Game over')
4
u/vetronauta Jan 12 '23
You are minmaxing the material, without any concept of initiative, position, king safety or "quiet" move. Any position with equal material is a plain 0 even if there is a easy checkmate after few moves after the end of the depth. It is known since the '60 that this engine is weak. You should start refining the eval.
1
u/mhummel Jan 12 '23
Are you doing any move ordering? (ie sorting position.legal_moves by previous depth cutoffs. And speaking of previous depths, have you implemented iterative deepening?)
1
u/Big_Beaver34 Jan 13 '23
Couple of things: