Opening Books


KKFChess uses it's own opening book format which can be created from PGN files.  Opening books can be created for all supported chess variants.  KKFChess can be use to:

Some configuration items affects the way the usage and creation of opening books, refer to the Configuration section for details.  KKFChess' opening book file holds statistical data on how many times a position has appeared in won, lost, drawn game or results unknown games.  For positions with few games, the win/lose ratio may be large.

An opening book below a certain size will be read into memory and accessed directly unless an index file is also present.  The index file is optional but it can speed up the access of the book file, especially when they are large.  An index file will be created automatically when a large book is generated.

KKFChess supports compressed book formats.  The currently active book can be compressed with the "bookcomp" command.

You can interrupt opening book generation by pressing the space bar.


Opening Book File Access

If failing to load the entire book into memory, KKFChess will access the book file off the disk.  Depending on the presence of an index file, the access can be either direct or indirect:

Compressed book Uncompressed book Index file Access Method
Present Don't care Absent or cannot be loaded Access book file directly
Present Don't care Present and loaded into memory Access book file via indices in memory
Absent Present Absent or cannot be loaded Access book file directly
Absent Present Present and loaded into memory Access book file via indices in memory


Storage of Opening Books

To accommodate opening books for various chess variants, the following file/directory structure are used (with the default opening book filename "KKFChessBook.kkf" and KKFChessBook.kbz):

Variant Uncompressed filename Compressed filename
Regular/Wild $BookBase\normal\KKFChessBook.kkf $BookBase\normal\KKFChessBook.kbz
Suicide $BookBase\suicide\KKFChessBook.kkf $BookBase\suicide\KKFChessBook.kbz
Crazyhouse $BookBase\crazyhouse\KKFChessBook.kkf $BookBase\crazyhouse\KKFChessBook.kbz
Atomic $BookBase\atomic\KKFChessBook.kkf $BookBase\atomic\KKFChessBook.kbz

$BookBase is the base directory set by the "BookBase" configuration item.  The index file "Opening.idx" (if required) must be in the same directory as well.  Uncompressed book files have ".kkf" as file extension while compressed ones have ".kbz".  If both the compressed and uncompressed book is available, the compressed one will be used.


Downloading an Opening Book

A small opening book (333kB) for regular chess is available for download.  It is created from a 3.3GB book file by trimming it to only include positions with more than 1000 games.


Creating an Opening Book

In order to create an opening book, you must have at least one PGN game file with at least one game.  Also these games must be of the same variant.  You can only create opening books in the Console Mode.

When creating an opening book a work buffer is used to hold the positions read so far; this work buffer uses the same memory locations as the pawn structure hash table and has the same capacity.  You can increase this buffer size with the "pawnhash" command to a very large value or estimate the memory needed to hold all positions in a PGN file with the "estimate" command.  When the buffer is full, no further positions are added.

The created opening book file will always be "KKFChessBook.kkf" and resides in the same directory as the engine.  An index file "Opening.idx" will be created as necessary.  There can only be one index file so if you change the opening book name by modifying the configuration file, then a new index must be created (for large books only).

The following steps shows how to create an opening book, assuming you have a PGN file called PGN.pgn:

  1. Start KKFChess in a DOS prompt to enter console mode.

  2. [Optional]  Type "estimate PGN.pgn" to estimate the amount of memory required to build the opening book.

  3. [Optional]  Increase the amount of memory available for creating the book by using the "pawnhash" command

  4. Type "makebook PGN.pgn" to parse the PGN file and attempt to build the book.

  5. If successful, a new book file "KKFChessBook.kkf" will be created in the same directory as the engine.  Also a default "Opening.idx" file will be created for large books.

  6. Quit the engine with the "quit" command.

  7. Move/copy/rename the new book file and the index file to the correct book directory.

  8. [Optional]  If needed, modify the configuration file to use the new book.

  9. [Optional]  Run KKFChess again and replace the default index file using "reindex <N>" which will create an new index for every <N> position.

If an error has occurred when reading the PGN file, the chess position will be shown but the book generation continues.  You can use the command "chkpgn <PGNFile>" to scan over the PGN file to ensure there are no errors before starting book generation.

Moves in the PGN file can be marked by a characters to indicate the goodness of the move.  KKFChess only understands three types of marked moves:

The last marked move will override previous ones from the same position.

Book creation is done by holding all read records in memory (using the pawn hash table' memory) hence sometimes it is not possible to create the book file in just one go.


Multiple Source Files

The file given to the "makebook" command can be a file containing a list of PGN files (max 128 files), each line containing one filename (can have directory structures or drive letters).  Each listed file is considered relative to the engine's installed directory unless it specially qualified (e.g. has a drive path "c:\PGN\First.pgn").

A special prefix character is used to for controlling the book creation.  An "*" will mark that line as a comment and ignored by the engine.  An '!' will execute the shell / DOS command that follows the '!'.  An '=' indicates an internal command.  Currently supported internal commands are:

Command / Prefix Argument(s) Description
* Treat line as a comment and ignore.
! <cmd> Execute the shell / DOS command <cmd>.
=mem [<N> ["KB" | "MB"]] Sets the buffer space for creating the remaining book files.  This affects the remaining PGN files.
=minmoves <N> Sets the minimum number of moves that a game must have before it is included for book file creation.  This affects the remaining PGN files.
=mingames <N> Sets the minimum number of games a position appears in before it is included for the book file creation.  This affects the remaining PGN files.


Making Very Large Books (> Available Memory)

For machines with limited memory, creating a large book can be a problem.  Fortunately, there are ways to create large books for machine with small amount of memory.

On my machine, I have an opening book of 3.3GB for regular chess.  How did I create this large book?  Simple, I just created lots of small one then combined them with the "mergebook" command.

If your source PGN file(s) are large, you can first separate them into two or more files that your machine can handle with "pgnsplit" and/or "pgnsort".  "pgnsplit" will split one PGN file into two and "pgnsort" will create at most 4 PGN files according to the "Result" tag: "WhiteWon.pgn", "BlackWon.pgn", "Drawn.pgn", "Unknown.pgn".

The "pgnsplit" command allows you to decide what the split files should be named while the "pgnsort" will create the files in the same directory as the engine's executable file (the installed directory).  If these extracted files are still too large, rename them and repeat the splitting / sorting until they are small enough.

As each book creation makes a new "KKFChessBook.kkf" file, you should rename this file after every "makebook" command to avoid the file being overwritten.  Once all the small book files are created, use the "mergebook" command to merge two books into a single one.  Keep merging the book files until all are done.  Remember to move the book file to the correct directory (rename to match the "BookFile" configuration item as necessary) and create an index file if needed.

Here's an example of creating two opening books then merging them:

  1. Create the first opening book "KKFChessBook.kkf".

  2. Rename "KKFChessBook.kkf" (1st book) to "Book_1.kkf".

  3. Create the second opening book "KKFChessBook.kkf"

  4. Rename "KKFChessBook.kkf" (2nd book) to "Book_2.kkf"

  5. Merge the two books with "mergebook  Book_1.kkf   Book_2.kkf   KKFChessBook.kkf".

  6. Delete the two temporary book files "Book_1.kkf" and "Book_2.kkf".

  7. Move the book file to the correct directory.

  8. Create the index file if needed.


Creating an Index File

By default, an index file is created automatically if the book file is considered large (depending on the engine sub-version).  You can change this index file's size by re-indexing the opening book with the "bkidx" or "reindex" command, giving it a skip count as an argument.  The larger the skip count, the smaller the new index file will be but the access time for locating an entry in the opening increases.

The presence of the index file ("Opening.idx") will disable the loading of the opening book into memory, no matter how small the book file is.  The index file can be created from a compressed or uncompressed opening book.  Opening books can still be accessed without the index file but the access will be slower.


Dummy Index File

A special "dummy" index file can be created with the "bkidx dummy" or "reindex dummy" command.  It is in fact an index file created with a very large skipping value.  The dummy index file help to reduce the memory needed for using opening books at the cost of access speed.  Opening books with a dummy index file can be slow to access.  In short, avoid using the dummy index file unless it is absolutely necessary (e.g. no memory left to even load a small index file).  A dummy index file is actually the same as having no index file at all.


Opening Books Maintenance

KKFChess has commands that allows you to:

Note that the merging and trimming can only operate on uncompressed book files.  Re-indexing can work on both compressed and uncompressed book file.

See the section Console Mode Command Summary for their syntax.


Opening Books for Other Variants

Opening books can be created for other chess variants.  Before creating such a book, make sure KKFChess is in the correct variant (use commands "variant", "suicide", "crazyhouse" or "atomic") and the PGN games either has no "Variant" tags or all have the same "Variant" tag values of the desired variant.