Skip to content

Commit 86ada4d

Browse files
author
rahul
committed
refactor commit
0 parents  commit 86ada4d

File tree

9 files changed

+48397
-0
lines changed

9 files changed

+48397
-0
lines changed

LICENSE

Lines changed: 339 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Chess Misc
2+
==========
3+
position_eval.py
4+
----------------
5+
This is a command line tool for quickly evaluating a position in chess.
6+
Here are the basics you need to know to get started :
7+
* **Input** : Any FEN string(s) or a file containing FENs
8+
9+
```python
10+
>> python position_eval.py <fen1> [<fen2>...<fenN>]
11+
OR
12+
>> python position_eval.py file <filename>
13+
(Note : filename must have the extension .fen)
14+
```
15+
* **Output** : Current evaluation score and the best move. The FEN(s) along with the evaluation score is also written to a file called *game_moves.log*
16+
17+
plotter.py
18+
----------
19+
You can also plot evaluation score vs number of moves using
20+
```python
21+
>> python plotter.py
22+
```
23+
This will take data from *game_moves.log*
24+
25+
make_dataset.py
26+
---------------
27+
This file takes in a puzzle file in PGN format which contains Mate in 4+ puzzles, parses out the FEN strings, finds the optimal move sequence for mate and prints it to `stdout` and a file named `dataset`

chess-position-eval/plotter.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import re
2+
import matplotlib.pyplot as plt
3+
4+
def main() :
5+
6+
with open('./game_moves.log', 'r') as f:
7+
read_data = f.readlines()
8+
f.close()
9+
10+
moves = []
11+
change_eval = []
12+
num_moves = 0
13+
for s in read_data :
14+
split = s.split("[")
15+
change_eval.append(int(re.match(r'-?\d+', split[1]).group()))
16+
num_moves += 1
17+
18+
for x in range(0,num_moves):
19+
moves.append(x)
20+
21+
print moves
22+
print change_eval
23+
24+
plt.plot(moves, change_eval)
25+
plt.ylabel('change in eval score')
26+
plt.xlabel('move number')
27+
# plt.xlim([0,20])
28+
# plt.ylim([-5000,5000])
29+
plt.show()
30+
31+
main()
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import sys
2+
import subprocess, time
3+
4+
engine = subprocess.Popen(
5+
'./stockfish',
6+
universal_newlines=True,
7+
stdin=subprocess.PIPE,
8+
stdout=subprocess.PIPE,
9+
)
10+
11+
def put(command):
12+
engine.stdin.write(command+'\n')
13+
14+
def get():
15+
# using the 'isready' command (engine has to answer 'readyok')
16+
# to indicate current last line of stdout
17+
eval_score = ''
18+
bestmove = ''
19+
engine.stdin.write('isready\n')
20+
while True:
21+
text = engine.stdout.readline().strip()
22+
if text == 'readyok':
23+
break
24+
elif text != '' and text.startswith('info depth'):
25+
eval_score = text.split()[7]
26+
elif text != '' and text.startswith('bestmove'):
27+
bestmove = text.split()[1]
28+
return float(eval_score), bestmove
29+
30+
def get_score(fen):
31+
put('position fen ' + fen)
32+
put('go movetime 1000')
33+
time.sleep(1.2)
34+
35+
if __name__ == "__main__":
36+
if len(sys.argv) < 2:
37+
print 'Atleast one FEN must be provided'
38+
sys.exit()
39+
40+
args = sys.argv
41+
del args[0]
42+
if(args[0] == 'file'):
43+
if(len(args) < 2):
44+
print 'Please enter a filename'
45+
sys.exit()
46+
else:
47+
with open(args[1]) as f:
48+
content = f.readlines()
49+
args = content
50+
51+
f = open('game_moves.log','w')
52+
for fen in args:
53+
# 8/6pk/8/1R5p/3K3P/8/6r1/8 b - - 0 42
54+
# fen = fen.split('[')[0]
55+
fen = fen.strip()
56+
get_score(fen)
57+
data = get()
58+
print data
59+
60+
string = fen + "[" + str(data[0]) + "]\n"
61+
f.write(string)
62+
63+
f.close();
64+
put('quit')

chess-position-eval/stockfish

328 KB
Binary file not shown.

0 commit comments

Comments
 (0)