-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprimitive_evaluate.py
More file actions
162 lines (145 loc) · 4.75 KB
/
primitive_evaluate.py
File metadata and controls
162 lines (145 loc) · 4.75 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
global center_squares
center_squares = {(3, 3), (3, 4), (4, 3), (4, 4)}
outer_center_squares = {(2, 2), (2, 3), (2, 4), (2, 5), (5, 5), (5, 4), (5, 3), (5, 2), (3, 2), (3, 5), (4, 2), (4, 2)}
material_dict = {"P": 0, "N": 781, "B": 825, "R": 1276, "Q": 2538, "K": 0, "p": 0, "n": 781, "b": 825, "r": 1276, "q": 2538, "k": 0}
midgame_values = [126, 781, 825, 1276, 2538]
endgame_values = [208, 854, 915, 1380, 2682]
def evaluate(position):
score = 0
material_sum = 0
for (key, val) in position.items(): # Ignore pawns for calculating game stage
material_sum += material_dict[key[0]]
if material_sum > 15258:
pawn_value, knight_value, bishop_value, rook_value, queen_value = midgame_values
opening = True
endgame_index = 0
elif material_sum < 3915:
pawn_value, knight_value, bishop_value, rook_value, queen_value = endgame_values
opening = False
endgame_index = 1
else:
endgame_index = (material_sum - 3915) / 11343
values = []
pawn_value = midgame_values[0] + (endgame_values[0]-midgame_values[0]) * endgame_index
knight_value = midgame_values[1] + (endgame_values[1]-midgame_values[1]) * endgame_index
bishop_value = midgame_values[2] + (endgame_values[2]-midgame_values[2]) * endgame_index
rook_value = midgame_values[3] + (endgame_values[3]-midgame_values[3]) * endgame_index
queen_value = midgame_values[4] + (endgame_values[4]-midgame_values[4]) * endgame_index
opening = False
promotion_value = bishop_value / 1.5
for (key, val) in position.items():
piece = key[0]
if piece == "P":
score += pawn_value
if opening:
if val in center_squares:
score += 300
elif val in outer_center_squares:
score += 150
else:
score += promotion_value / (val[0] + 1) * (1-endgame_index)
elif piece == "p":
score -= pawn_value
if opening:
if val in center_squares:
score -= 300
elif val in outer_center_squares:
score -= 150
else:
score -= promotion_value / (8-val[0]) * (1-endgame_index)
elif piece == "N":
score += knight_value
if opening:
if val in center_squares:
score += 300
elif val in outer_center_squares:
score += 150
elif val == (7, 1) or val == (7, 6):
score -= 75
elif piece == "B":
score += bishop_value
if opening:
if val in center_squares:
score += 250
elif val in outer_center_squares:
score += 225
elif val == (7, 2) or val == (7, 5):
score -= 75
elif piece == "R":
score += rook_value
if opening:
if val in center_squares:
score += 40
elif val in outer_center_squares:
score += 40
elif piece == "n":
score -= knight_value
if opening:
if val in center_squares:
score -= 300
elif val in outer_center_squares:
score -= 150
elif val == (0, 1) or val == (0, 6):
score += 75
elif piece == "b":
score -= bishop_value
if opening:
if val in center_squares:
score -= 250
elif val in outer_center_squares:
score -= 225
elif val == (0, 2) or val == (0, 5):
score += 75
elif piece == "r":
score -= rook_value
if opening:
if val in center_squares:
score -= 40
elif val in outer_center_squares:
score -= 40
elif piece == "k":
if opening:
if val == (0, 6) or val == (0, 2):
score -= 100
elif piece == "K":
if opening:
if val == (7, 6) or val == (7, 2):
score += 100
elif piece == "q":
score -= queen_value
if opening:
if val in center_squares:
score -= 40
elif val in outer_center_squares:
score -= 40
elif piece == "Q":
score += queen_value
if opening:
if val in center_squares:
score += 40
elif val in outer_center_squares:
score += 40
return score
def time_test(starting_position):
import timeit
from legal_moves import legal_move_search
times = []
for i in range(10):
starttime = timeit.default_timer()
previous_move = ("P", (6, 4), (4, 4))
position_swapped = dict([(value, key) for key, value in starting_position.items()])
for j in range(10000):
legal_move_search(starting_position, position_swapped, True, previous_move, True, True)
#evaluate(starting_position)
times.append(timeit.default_timer() - starttime)
print(sum(times) / len(times))
#testing only
if __name__ == '__main__':
starting_position = {"r0": (0, 0), "n0": (0, 1), "b0": (0, 2), "q0": (0, 3), "k0": (0, 4), "b1": (0, 5), "n2": (0, 6), "r2": (0, 7),
"p0": (1, 0), "p1": (1, 1), "p2": (1, 2), "p3": (1, 3), "p4": (1, 4), "p5": (1, 5), "p6": (1, 6), "p7": (1, 7),
"P0": (6, 0), "P1": (6, 1), "P2": (6, 2), "P3": (6, 3), "P4": (6, 4), "P5": (6, 5), "P6": (6, 6), "P7": (6, 7),
"R0": (7, 0), "N0": (7, 1), "B0": (7, 2), "Q0": (7, 3), "K0": (7, 4), "B1": (7, 5), "N1": (7, 6),
"R1": (7, 7)}
#score = evaluate(starting_position)
#print(score)
time_test(starting_position)