-
Notifications
You must be signed in to change notification settings - Fork 0
/
board_evaluation.py
52 lines (43 loc) · 1.74 KB
/
board_evaluation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import chess
from chess.engine import SimpleEngine
from constants import piece_value, outcome_value, chess_pieces
def material_evaluation(board):
"""
Evaluates the white's advantage based on material and the outcome alone,
doesn't take into account the position of the pieces.
:param board: current chess board.
:type board: chess.Board.
:return: Evaluation of the state considering material and outcome alone.
:rtype: float.
"""
if board.is_game_over():
if board.outcome().winner != chess.BLACK:
return outcome_value[board.outcome().termination]
else:
return -outcome_value[board.outcome().termination]
evaluation = 0
for piece_type in chess_pieces:
evaluation += (piece_value[piece_type]
*(len(board.pieces(piece_type, chess.WHITE)) - len(board.pieces(piece_type, chess.BLACK))))
return evaluation
def stockfish_evaluation(board, depth=4):
"""
Evaluates advantage based on stockfish.
:param board: Current chess board.
:type board: chess.Board.
:param side: Whoose turn it is.
:type side: chess.Color.
:param depth: Depth in which Stockfish analises the position.
:type depth: int.
:return: Evaluation of the state by stockfish.
:rtype: float.
"""
with chess.engine.SimpleEngine.popen_uci('stockfish/stockfish_14_x64') as sf:
result = sf.analyse(board, chess.engine.Limit(depth=depth))
evaluation = result['score'].white().score(mate_score=outcome_value[chess.Termination.CHECKMATE])
return evaluation
# Evaluation Function Options
MATERIAL_EVAL = 0
STOCKFISH_EVAL = 1
evaluation_mode = {MATERIAL_EVAL: material_evaluation,
STOCKFISH_EVAL: stockfish_evaluation}