Skip to content

Commit e33a4d3

Browse files
committed
New tables
1 parent 31ac375 commit e33a4d3

File tree

1 file changed

+77
-89
lines changed

1 file changed

+77
-89
lines changed

sunfish.py

+77-89
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,70 @@
66
from itertools import count
77
from collections import OrderedDict, namedtuple
88

9-
# The table size is the maximum number of elements in the transposition table.
10-
TABLE_SIZE = 1e8
9+
###############################################################################
10+
# Piece-Square tables. Tune these to change sunfish's behaviour
11+
###############################################################################
1112

12-
# Mate value must be greater than 8*queen + 2*(rook+knight+bishop)
13-
# King value is set to twice this value such that if the opponent is
14-
# 8 queens up, but we got the king, we still exceed MATE_VALUE.
15-
# When a MATE is detected, we'll set the score to MATE_UPPER - plies to get there
16-
# E.g. Mate in 3 will be MATE_UPPER - 6
17-
MATE_LOWER = 60000 - 8*2700
18-
MATE_UPPER = 60000 + 8*2700
13+
piece = { 'P': 100, 'N': 280, 'B': 320, 'R': 479, 'Q': 929, 'K': 60000 }
14+
pst = {
15+
'P': ( 0, 0, 0, 0, 0, 0, 0, 0,
16+
78, 83, 86, 73, 102, 82, 85, 90,
17+
7, 29, 21, 44, 40, 31, 44, 7,
18+
-17, 16, -2, 15, 14, 0, 15, -13,
19+
-26, 3, 10, 9, 6, 1, 0, -23,
20+
-22, 9, 5, -11, -10, -2, 3, -19,
21+
-31, 8, -7, -37, -36, -14, 3, -31,
22+
0, 0, 0, 0, 0, 0, 0, 0),
23+
'N': ( -66, -53, -75, -75, -10, -55, -58, -70,
24+
-3, -6, 100, -36, 4, 62, -4, -14,
25+
10, 67, 1, 74, 73, 27, 62, -2,
26+
24, 24, 45, 37, 33, 41, 25, 17,
27+
-1, 5, 31, 21, 22, 35, 2, 0,
28+
-18, 10, 13, 22, 18, 15, 11, -14,
29+
-23, -15, 2, 0, 2, 0, -23, -20,
30+
-74, -23, -26, -24, -19, -35, -22, -69),
31+
'B': ( -59, -78, -82, -76, -23,-107, -37, -50,
32+
-11, 20, 35, -42, -39, 31, 2, -22,
33+
-9, 39, -32, 41, 52, -10, 28, -14,
34+
25, 17, 20, 34, 26, 25, 15, 10,
35+
13, 10, 17, 23, 17, 16, 0, 7,
36+
14, 25, 24, 15, 8, 25, 20, 15,
37+
19, 20, 11, 6, 7, 6, 20, 16,
38+
-7, 2, -15, -12, -14, -15, -10, -10),
39+
'R': ( 35, 29, 33, 4, 37, 33, 56, 50,
40+
55, 29, 56, 67, 55, 62, 34, 60,
41+
19, 35, 28, 33, 45, 27, 25, 15,
42+
0, 5, 16, 13, 18, -4, -9, -6,
43+
-28, -35, -16, -21, -13, -29, -46, -30,
44+
-42, -28, -42, -25, -25, -35, -26, -46,
45+
-53, -38, -31, -26, -29, -43, -44, -53,
46+
-30, -24, -18, 5, -2, -18, -31, -32),
47+
'Q': ( 6, 1, -8,-104, 69, 24, 88, 26,
48+
14, 32, 60, -10, 20, 76, 57, 24,
49+
-2, 43, 32, 60, 72, 63, 43, 2,
50+
1, -16, 22, 17, 25, 20, -13, -6,
51+
-14, -15, -2, -5, -1, -10, -20, -22,
52+
-30, -6, -13, -11, -16, -11, -16, -27,
53+
-36, -18, 0, -19, -15, -15, -21, -38,
54+
-39, -30, -31, -13, -31, -36, -34, -42),
55+
'K': ( 4, 54, 47, -99, -99, 60, 83, -62,
56+
-32, 10, 55, 56, 56, 55, 10, 3,
57+
-62, 12, -57, 44, -67, 28, 37, -31,
58+
-55, 50, 11, -4, -19, 13, 0, -49,
59+
-55, -43, -52, -28, -51, -47, -8, -50,
60+
-47, -42, -43, -79, -64, -32, -29, -32,
61+
-4, 3, -14, -50, -57, -18, 13, 4,
62+
17, 30, -3, -14, 6, -1, 40, 18),
63+
}
64+
# Pad tables and join piece and pst dictionaries
65+
for k, table in pst.items():
66+
padrow = lambda row: (0,) + tuple(x+piece[k] for x in row) + (0,)
67+
pst[k] = sum((padrow(table[i*8:i*8+8]) for i in range(8)), ())
68+
pst[k] = (0,)*20 + pst[k] + (0,)*20
1969

20-
QS_LIMIT = 150
21-
EVAL_ROUGHNESS = 20
70+
###############################################################################
71+
# Global constants
72+
###############################################################################
2273

2374
# Our board is represented as a 120 character string. The padding allows for
2475
# fast detection of moves that don't stay within the board.
@@ -38,10 +89,7 @@
3889
' \n' # 110 -119
3990
)
4091

41-
###############################################################################
42-
# Move and evaluation tables
43-
###############################################################################
44-
92+
# Lists of possible moves for each piece type.
4593
N, E, S, W = -10, 1, 10, -1
4694
directions = {
4795
'P': (N, N+N, N+W, N+E),
@@ -52,80 +100,20 @@
52100
'K': (N, E, S, W, N+E, S+E, S+W, N+W)
53101
}
54102

55-
pst = {
56-
'P': (0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
57-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
58-
0, 198, 198, 198, 198, 198, 198, 198, 198, 0,
59-
0, 178, 198, 198, 198, 198, 198, 198, 178, 0,
60-
0, 178, 198, 198, 198, 198, 198, 198, 178, 0,
61-
0, 178, 198, 208, 218, 218, 208, 198, 178, 0,
62-
0, 178, 198, 218, 238, 238, 218, 198, 178, 0,
63-
0, 178, 198, 208, 218, 218, 208, 198, 178, 0,
64-
0, 178, 198, 198, 198, 198, 198, 198, 178, 0,
65-
0, 198, 198, 198, 198, 198, 198, 198, 198, 0,
66-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
67-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
68-
'B': (0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
69-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
70-
0, 797, 824, 817, 808, 808, 817, 824, 797, 0,
71-
0, 814, 841, 834, 825, 825, 834, 841, 814, 0,
72-
0, 818, 845, 838, 829, 829, 838, 845, 818, 0,
73-
0, 824, 851, 844, 835, 835, 844, 851, 824, 0,
74-
0, 827, 854, 847, 838, 838, 847, 854, 827, 0,
75-
0, 826, 853, 846, 837, 837, 846, 853, 826, 0,
76-
0, 817, 844, 837, 828, 828, 837, 844, 817, 0,
77-
0, 792, 819, 812, 803, 803, 812, 819, 792, 0,
78-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
79-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
80-
'N': (0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
81-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
82-
0, 627, 762, 786, 798, 798, 786, 762, 627, 0,
83-
0, 763, 798, 822, 834, 834, 822, 798, 763, 0,
84-
0, 817, 852, 876, 888, 888, 876, 852, 817, 0,
85-
0, 797, 832, 856, 868, 868, 856, 832, 797, 0,
86-
0, 799, 834, 858, 870, 870, 858, 834, 799, 0,
87-
0, 758, 793, 817, 829, 829, 817, 793, 758, 0,
88-
0, 739, 774, 798, 810, 810, 798, 774, 739, 0,
89-
0, 683, 718, 742, 754, 754, 742, 718, 683, 0,
90-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
91-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
92-
'R': (0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
93-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
94-
0, 1258, 1263, 1268, 1272, 1272, 1268, 1263, 1258, 0,
95-
0, 1258, 1263, 1268, 1272, 1272, 1268, 1263, 1258, 0,
96-
0, 1258, 1263, 1268, 1272, 1272, 1268, 1263, 1258, 0,
97-
0, 1258, 1263, 1268, 1272, 1272, 1268, 1263, 1258, 0,
98-
0, 1258, 1263, 1268, 1272, 1272, 1268, 1263, 1258, 0,
99-
0, 1258, 1263, 1268, 1272, 1272, 1268, 1263, 1258, 0,
100-
0, 1258, 1263, 1268, 1272, 1272, 1268, 1263, 1258, 0,
101-
0, 1258, 1263, 1268, 1272, 1272, 1268, 1263, 1258, 0,
102-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
103-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
104-
'Q': (0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
105-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
106-
0, 2529, 2529, 2529, 2529, 2529, 2529, 2529, 2529, 0,
107-
0, 2529, 2529, 2529, 2529, 2529, 2529, 2529, 2529, 0,
108-
0, 2529, 2529, 2529, 2529, 2529, 2529, 2529, 2529, 0,
109-
0, 2529, 2529, 2529, 2529, 2529, 2529, 2529, 2529, 0,
110-
0, 2529, 2529, 2529, 2529, 2529, 2529, 2529, 2529, 0,
111-
0, 2529, 2529, 2529, 2529, 2529, 2529, 2529, 2529, 0,
112-
0, 2529, 2529, 2529, 2529, 2529, 2529, 2529, 2529, 0,
113-
0, 2529, 2529, 2529, 2529, 2529, 2529, 2529, 2529, 0,
114-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
115-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
116-
'K': (0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
117-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
118-
0, 60098, 60132, 60073, 60025, 60025, 60073, 60132, 60098, 0,
119-
0, 60119, 60153, 60094, 60046, 60046, 60094, 60153, 60119, 0,
120-
0, 60146, 60180, 60121, 60073, 60073, 60121, 60180, 60146, 0,
121-
0, 60173, 60207, 60148, 60100, 60100, 60148, 60207, 60173, 0,
122-
0, 60196, 60230, 60171, 60123, 60123, 60171, 60230, 60196, 0,
123-
0, 60224, 60258, 60199, 60151, 60151, 60199, 60258, 60224, 0,
124-
0, 60287, 60321, 60262, 60214, 60214, 60262, 60321, 60287, 0,
125-
0, 60298, 60332, 60273, 60225, 60225, 60273, 60332, 60298, 0,
126-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
127-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
128-
}
103+
# Mate value must be greater than 8*queen + 2*(rook+knight+bishop)
104+
# King value is set to twice this value such that if the opponent is
105+
# 8 queens up, but we got the king, we still exceed MATE_VALUE.
106+
# When a MATE is detected, we'll set the score to MATE_UPPER - plies to get there
107+
# E.g. Mate in 3 will be MATE_UPPER - 6
108+
MATE_LOWER = piece['K'] - 10*piece['Q']
109+
MATE_UPPER = piece['K'] + 10*piece['Q']
110+
111+
# The table size is the maximum number of elements in the transposition table.
112+
TABLE_SIZE = 1e8
113+
114+
# Constants for tuning search
115+
QS_LIMIT = 150
116+
EVAL_ROUGHNESS = 20
129117

130118

131119
###############################################################################

0 commit comments

Comments
 (0)