Code Monkey home page Code Monkey logo

chess-variant-puzzler's Introduction

Chess variant puzzle generator

This is a simple puzzle generator for chess variants based on Fairy-Stockfish and its python binding pyffish.

Process

Generating puzzles in PGN format currently consists of running the following scripts

  1. generator.py to generate a set of positions in FEN/EPD format from playing engine games. This step can be skipped if positions are extracted from other sources such as databases of human games.
    • If you download games from lichess in .pgn format, you can use pgn2epd.py to generate FENs.
    • If you download games from pychess in .json format, you can use json2epd.py to generate FENs.
  2. puzzler.py to identify puzzles within those positions and store them as EPD with annotations. This step can be re-run on the resulting EPD to re-evaluate the puzzles, e.g., at higher depth.
  3. filter.py to optionally narrow down the set of puzzles according to difficulty, type, etc.
  4. pgn.py to convert the EPD to a PGN.

Setup

The scripts require at least python3.2 as well as the dependencies from the requirements.txt. Install them using

pip3 install -r requirements.txt

Usage

A simple example of running the scripts with default settings is:

# generate positions
python3 generator.py --engine fairy-stockfish --variant crazyhouse > positions.epd
# extract puzzles
python3 puzzler.py --engine fairy-stockfish positions.epd > puzzles.epd
# convert EPD to PGN
python3 pgn.py puzzles.epd > puzzles.pgn

Run the scripts with --help to get help on the supported parameters.

Usually it makes sense to first run the puzzler with a lower depth but loose filter criteria to pre-filter the positions, followed by a more strict validation at higher depth.

Evaluate

The puzzle generator can be evaluated against an existing database of curated puzzles. E.g., for the example of lichess:

# Download puzzles from https://database.lichess.org/#puzzles
curl -O https://database.lichess.org/lichess_db_puzzle.csv.bz2
# Unpack
bzip2 -dk lichess_db_puzzle.csv.bz2
# Extract subset
head -100 lichess_db_puzzle.csv > test.csv
# Prepare input FENs
cut -d " " -f 1-6 test.csv | cut -d "," -f 2,3 --output-delimiter=";sm " > test.fen
# Run puzzler
python puzzler.py --engine fairy-stockfish -d 10 --variant chess test.fen > test.epd
# Run evaluation
python evaluate.py test.csv test.epd

chess-variant-puzzler's People

Contributors

belzedar94 avatar gbtami avatar ianfab avatar nelloho avatar rainrat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

chess-variant-puzzler's Issues

Suggested enhancements

As the project is starting to drag some interest, I'm opening this thread to compile a few suggestions to inspire future development:

  • Add variants.ini support.
  • Add automatic deletion of repeated puzzles.
  • Add an in-built option to filter puzzles in-out based on the tagged difficulty.
  • Add an in-built option to filter puzzles in-out based on the puzzle type (mate [optionally, adding an option to filter in-out mates in X], winning, defensive).

I'll add more suggestions on the go.

Thanks for your amazing work, I love the idea to have a generic puzzle generator for FSF!

Add progress bar and speed to the generator

It would be nice to have a progress bar and a speed (fens/second) to have a visual tracking of what's happening (And to estimate more or less the time needed for a certain amount of fens (That will depend a lot on the variant).

Support KIF export

Besides EPD and PGN also support KIF export for lishogi compatibility.

Suggested enhancements part 2

As the project is starting to drag some interest, I'm opening this thread to compile a few suggestions to inspire future development:

  • Remove puzzles that share the same solution (to avoid slight variations that lead to the same exact solution as chapters 2-3 of https://lichess.org/study/vxrJlFCV).
  • Add a concurrency option on the generator so games can be generated in paralel when more than 1 thread is available.
  • Add the posibility to filter mates in X (using for example the Eval printed in the epd, e.g.: Eval=# 2)

Add mate length as a complexity term for difficulty calculation

Below two puzzles share the same difficulty while the second one is significantly harder than the first one. Does it make sense to reflect mate length as a factor in the difficulty calculation if puzzle type = mate?

1nbqkbn1/1ppppRp1/5r2/p3R2p/P6P/8/1PPPPrP1/1NBQKBN1 w - - 1 9;variant kamikazerooks;bm e5e7;eval #2;difficulty 6;quality 1.00;type mate;pv e5e7,f8e7,f7f8

3k2B1/1p1bR1p1/2p5/p5P1/P2p2rP/1P1P4/3N4/1N2K3 w - - 2 26;variant kamikazerooks;bm e7e8;eval #7;difficulty 6;quality 0.73;type mate;pv e7e8,d8c7,e8c8,c7d6,d2c4,d6c5,c8c6,c5b4,c6b6

Force all-the-way-through solutions where there is an announced mate

When working only with mates, a lot of times the proposed solution doesn't cover the whole mate sequence. It would be nice to force the puzzler to annotate the whole solution when there is an announced mate.

An example:

[Crazyhouse] - In this case, only B@f8 is printed, while the desired solution would be (shortest forced mate): B@f8+, Ke6, exf5+, Kxf5, Ng3+, Ke6, B@d5+, Ke5, d4#
3r4/p1p1kpQp/3p1N1p/5bbN/4P3/3PP3/PPq2nPP/r3BK1R[Brnppp] w - - 4 39;variant crazyhouse;bm B@f8;eval #5;difficulty 6;quality 1.00;type mate;pv B@f8

Some searching bug

look at this xiangqi fen:

4k4/4a4/3a2N1C/9/9/9/9/3A4B/9/2BAK4 w

through puzzler.py it's #5:

pv i8i4,e10d10,i4d4,d10e10,i3g5

but in pychess analysis, it's #2:
image

this kind of case has happend several times when I load puzzler.py on fens

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.