-
Notifications
You must be signed in to change notification settings - Fork 4
/
David_AI_v6_tests.py
122 lines (109 loc) · 3.29 KB
/
David_AI_v6_tests.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
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
import David_AI_v6 as ai
from time import perf_counter as now
initialPosition = '''
r n b q k b n r
p p p p p p p p
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
P P P P P P P P
R N B Q K B N R'''
initialPosition = initialPosition.replace(' ', '').split()
initialPosition.reverse()
difficultPosition = '''
r . b q . . . r
p p p p n k p p
. . n b . p . .
. . . . p . . .
. . P . N . . .
P . . P B N . .
. P . . P P P P
R . . Q K B . R'''
difficultPosition = difficultPosition.replace(' ', '').split()
difficultPosition.reverse()
promotionPosition = '''
r . . . . . . .
. P . P . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . p . p .
. . . . . . . R'''
promotionPosition = promotionPosition.replace(' ', '').split()
promotionPosition.reverse()
pawnTakePosition1 = '''
. . . . . . . .
. . . p . . . .
. . . . P . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .'''
pawnTakePosition1 = pawnTakePosition1.replace(' ', '').split()
pawnTakePosition1.reverse()
pawnTakePosition2 = '''
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . p . . . .
. . . . P . . .
. . . . . . . .'''
pawnTakePosition2 = pawnTakePosition2.replace(' ', '').split()
pawnTakePosition2.reverse()
kingSavePosition = '''
r . . . . . . .
p . . . . . . .
P . . k p . . .
. . . . r . . .
. . . . Q . . .
. . . p . . P .
. . . . . . . P
. . . . K . N R'''
kingSavePosition = kingSavePosition.replace(' ', '').split()
kingSavePosition.reverse()
ps = ai.position_score
'''
assert ps('R', 0, 0) == ps('R', 7, 7)
assert ps('R', 0, 0) == -ps('r', 7, 7)
assert ps('R', 1, 0) > ps('R', 0, 0)
assert ps('R', 0, 1) > ps('R', 0, 0)
assert ps('R', 4, 4) > ps('R', 0, 0)
assert ps('R', 3, 4) == ps('R', 4, 3)
assert ps('R', 3, 4) == -ps('r', 4, 3)
assert ps('P', 0, 0) < ps('P', 0, 7)
assert ps('P', 0, 0) < ps('P', 1, 0)
assert ps('p', 0, 0) == -ps('P', 0, 7)
assert ps('p', 0, 1) == -ps('P', 0, 6)
assert ps('p', 1, 1) == -ps('P', 1, 6)
assert ps('p', 0, 0) < ps('p', 0, 1)
assert ps('P', 3, 4) == -ps('p', 3, 3)
assert ps('P', 3, 4) == -ps('p', 3, 3)
assert ps('K', 3, 0) == -ps('k', 3, 7)
assert ps('K', 4, 0) == -ps('k', 4, 7)
'''
print(ai.evaluate(initialPosition))
assert abs(ai.evaluate(initialPosition)) < 0.000001
assert len(list(ai.moves(initialPosition, True))) == 20
assert len(list(ai.moves(difficultPosition, True))) == 42
assert len(list(ai.moves(pawnTakePosition1, True))) == 2
assert len(list(ai.moves(pawnTakePosition1, False))) == 3
assert len(list(ai.moves(pawnTakePosition2, True))) == 3
assert len(list(ai.moves(pawnTakePosition2, False))) == 2
def performance_test():
ai.total_leaves = 0
test_start_time = now()
for depth in range(2, 5):
start_time = now()
_possible_moves = list(ai.moves(difficultPosition, True))
_possible_moves.sort(key=lambda x: x[1], reverse=True)
best_move, _ = ai.search(_possible_moves, depth, ai.get_cscore(difficultPosition), True, -99999, 99999)
print('{}\t\t\t{}\t\t{:.3f}'.format(ai.total_leaves, depth, now() - start_time))
# print('\n'.join(' '.join(piece for piece in row) for row in best_move.__reversed__()) + '\n')
print('{} leaves searched per second'.format(int(ai.total_leaves/(now()-test_start_time))))
ai.transpositionTable = dict()
performance_test()