Implement an opening book to my Python engine

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

eligolf
Posts: 114
Joined: Sat Nov 14, 2020 12:49 pm
Full name: Elias Nilsson

Implement an opening book to my Python engine

Post by eligolf »

Hi,

The next step in the process of creating my hobby chess engine is to incorporate a good opening book. Right now I have a list of around 2000 known openings in a text file like this:

1.e4 d5 2.f4......
1.e4 c4 2. Nf6.......

I then compare my current game move log to this text file and see if a next move exists in any of the txt opening sequences. If a next move exist in one or more sequences I then pick the next move from a random sequence.

This concept is hard since it takes lots manual work to fill the txt file with more openings, so I want to change the concept to something else. I have read a lot about .bin opening files but I have not gotten my head around how they work or how to incorporate them into my Python engine. Here is what I want my opening concept to look like:

1. Input the FEN of the current position
2. Get a list of all opening sequences that have this FEN.
3. Preferably get the score for all sequences to be able to pick from the top ones for the engine color.

Is this possible in some way in Python?
Is FEN a bad board reprentation when it comes to openings or .bin files?
How do I access the content of a .bin file and what does it expect as input?
What does a .bin file contain?

Sorry for all the questions, the information online about this is very limited and I hope someone here has any experience with this. Thank you in advance for any input.
User avatar
hgm
Posts: 27817
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Implement an opening book to my Python engine

Post by hgm »

There are two different issues:
1) Do you want to create a .bin book
2) Do you want to use an existing .bin book

For (1) there are tools. E.g. XBoard can convert a set of games to a bin book, and has an Edit Book dialog where you can change the content of an existing book (e.g. adding or deleting moves, tuning the relative probability with which they are played).

A Polyglot .bin book contains 16 bytes per move:
8 bytes for a 'hash key', representing the position
2 bytes for the move
2 bytes for the 'weight'
2 x 2 bytes for the 'learn' fields (usually unused)

All moves are sorted by ascending hash key, so that a given position can be efficiently found through a binary search.

Tools that build books from PGN typically calculate the weight as the number of half-points scored with the move in the set of games from which the book was made.

For details, see http://hgm.nubati.net/book_format.html .
eligolf
Posts: 114
Joined: Sat Nov 14, 2020 12:49 pm
Full name: Elias Nilsson

Re: Implement an opening book to my Python engine

Post by eligolf »

Thank you for all your input H.G., it is highly apprecited!

I was mainly thinking of using one, but building one could be fun too. I will have a look at those tools.

Okay so there is a predefined Zobrist initiation that one has to use? That is also a way of doing this.
jdart
Posts: 4367
Joined: Fri Mar 10, 2006 5:23 am
Location: http://www.arasanchess.org

Re: Implement an opening book to my Python engine

Post by jdart »

python-chess has already packaged up the code you need for Polyglot book support:

https://python-chess.readthedocs.io/en/ ... yglot.html
eligolf
Posts: 114
Joined: Sat Nov 14, 2020 12:49 pm
Full name: Elias Nilsson

Re: Implement an opening book to my Python engine

Post by eligolf »

jdart wrote: Sun Nov 22, 2020 11:15 pm python-chess has already packaged up the code you need for Polyglot book support:
Yes but I have my own board representation. What format on the board does this function expects? I assume I have to convert my board to the python-chess board representation, but I can't find what format it uses and what variables it needs.
jdart
Posts: 4367
Joined: Fri Mar 10, 2006 5:23 am
Location: http://www.arasanchess.org

Re: Implement an opening book to my Python engine

Post by jdart »

Yes, it has its own Board class, but that class can convert FEN to a Board, so you can output yours as FEN and then read the FEN into the python-chess Board representation.

see https://python-chess.readthedocs.io/en/ ... html#board