Probing tablebases in python-chess

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

Robert Pope
Posts: 558
Joined: Sat Mar 25, 2006 8:27 pm

Probing tablebases in python-chess

Post by Robert Pope »

I managed to write some code to read an epd file and record the results of a probe of the Gaviota tablebases. It works, but it takes a significant fraction of a second to look up each result (processing maybe 15 positions/second on a non-SSD drive).

Is there anyone more familiar with python who can comment on my code below? I think I may be re-initializing the tablebase code for each new line?

Code: Select all

import sys
sys.path.append('C:\games\chess\python-chess\dist\python-chess-0.17.0')
import chess
import chess.gaviota

f2 = open(r"c:\games\chess\gaviotaTB\kqkfen.epd", 'w')
num=0

with open(r"c:\games\chess\gaviotaTB\kqkfen.txt", 'r') as f:
	for line_terminated in f:
		line = line_terminated.rstrip('\n')
		with chess.gaviota.open_tablebases(r"c:\games\chess\gaviotaTB\Gaviota") as tablebases: 
			wdl_score=tablebases.probe_wdl(chess.Board(line)) 
		with chess.gaviota.open_tablebases(r"c:\games\chess\gaviotaTB\Gaviota") as tablebases:
			dtm_score=tablebases.probe_dtm(chess.Board(line)) 
		num=num+1
		line=line+"; wdl "+str(wdl_score)+"; dtm "+str(dtm_score)+"\n"
		print(line)
		f2.write(line)

f2.close()
Ferdy
Posts: 4833
Joined: Sun Aug 10, 2008 3:15 pm
Location: Philippines

Re: Probing tablebases in python-chess

Post by Ferdy »

Robert Pope wrote:I managed to write some code to read an epd file and record the results of a probe of the Gaviota tablebases. It works, but it takes a significant fraction of a second to look up each result (processing maybe 15 positions/second on a non-SSD drive).

Is there anyone more familiar with python who can comment on my code below? I think I may be re-initializing the tablebase code for each new line?

Code: Select all

import sys
sys.path.append('C:\games\chess\python-chess\dist\python-chess-0.17.0')
import chess
import chess.gaviota

f2 = open(r"c:\games\chess\gaviotaTB\kqkfen.epd", 'w')
num=0

with open(r"c:\games\chess\gaviotaTB\kqkfen.txt", 'r') as f:
	for line_terminated in f:
		line = line_terminated.rstrip('\n')
		with chess.gaviota.open_tablebases(r"c:\games\chess\gaviotaTB\Gaviota") as tablebases: 
			wdl_score=tablebases.probe_wdl(chess.Board(line)) 
		with chess.gaviota.open_tablebases(r"c:\games\chess\gaviotaTB\Gaviota") as tablebases:
			dtm_score=tablebases.probe_dtm(chess.Board(line)) 
		num=num+1
		line=line+"; wdl "+str(wdl_score)+"; dtm "+str(dtm_score)+"\n"
		print(line)
		f2.write(line)

f2.close()
Perhaps,

Code: Select all

with open (TB):
    with open(input fen file):
        wdl_score = ...
        dtm_score = ...
        
        with open (save line, append mode):
That should be self closing and TB is still open as long as there are fen input and TB open attempt is done only once.
Otherwise just try profiling it.
Robert Pope
Posts: 558
Joined: Sat Mar 25, 2006 8:27 pm

Re: Probing tablebases in python-chess

Post by Robert Pope »

Thanks - here's the code I ended up with, and it is much, much faster!

Code: Select all

# import chess modules
import sys
sys.path.append('C:\games\chess\python-chess\dist\python-chess-0.17.0')
import chess
import chess.gaviota

# This is the output filename
f2 = open(r"c:\games\chess\gaviotaTB\kpkfenout.epd", 'w')
num=0

# Update the input filename here
with chess.gaviota.open_tablebases(r"c:\games\chess\gaviotaTB\Gaviota") as tablebases: 
	with open(r"c:\games\chess\gaviotaTB\kpkfen.epd", 'r') as f:
		for line_terminated in f:
			line = line_terminated.rstrip('\n')
			wdl_score=tablebases.probe_wdl(chess.Board(line)) 
			dtm_score=tablebases.probe_dtm(chess.Board(line)) 
			num=num+1
			line=line+"; c0 wdl "+str(wdl_score)+"; c1 dtm "+str(dtm_score)+"\n"
			print(num, line)
			f2.write(line)

f2.close()