-
Notifications
You must be signed in to change notification settings - Fork 1
/
evaluators.py
89 lines (67 loc) · 2.47 KB
/
evaluators.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import chess
import chess_state
class MaterialEvaluator(object):
"""
Simple evaluator based on the material on the board.
Uses basic piece values by default, but can be set up with other values.
"""
def __init__(self, q=9, r=5, b=3, n=3, p=1):
self.q, self.r, self.b, self.n, self.p = q, r, b, n, p
self.values = {
chess.PAWN: p,
chess.KNIGHT: n,
chess.BISHOP: b,
chess.ROOK: r,
chess.QUEEN: q,
}
def __call__(self, board):
acc = 0
for i in range(64):
piece = board.piece_at(i)
if not piece:
continue
if piece.color == chess.WHITE:
acc += self.values.get(piece.piece_type, 0)
else:
acc -= self.values.get(piece.piece_type, 0)
return acc
center = set([chess.D4, chess.D5, chess.E4, chess.E5])
class CenterControlEvaluator(object):
def __call__(self, board):
acc = 0
for move in board.legal_moves:
if move_to_square in center:
acc += (1 if board.turn == chess.WHITE else -1)
board.push(Move(None, None))
for move in board.legal_moves:
if move.to_square in center:
acc += (1 if board.turn == chess.WHITE else -1)
board.pop()
return acc
class MobilityEvaluator(object):
def __call__(self, board):
"""For now, just return the number of legal moves"""
return (len(list(board.legal_moves)) *
(1 if board.turn == chess.WHITE else -1))
class KingSafetyEvaluator(object):
def __call__(self, board):
acc = 0
if board.is_check():
acc += -2 if board.turn == chess.WHITE else 2
# This is probably stupid; we're already searching to some depth; this
# probably only serves to preferentially increase depth by one for this
# particular heuristic.
for move in board.legal_moves:
board.push(move)
if board.is_check():
acc += -1 if board.turn == chess.White else 1
board.pop()
return acc
class CompoundEvaluate(object):
def __init__(self, evaluator_pairs=None):
self.evaluators = evaluator_pairs or [(1, MaterialEvaluator())]
def __call__(self, board):
acc = 0
for weight, evaluator in self.evaluators:
acc += weight * evaluator(board)
return acc