Giter Site home page Giter Site logo

fulldecent / pawn-bfs Goto Github PK

View Code? Open in Web Editor NEW
5.0 3.0 2.0 30 KB

A breadth-first enumeration of all reachable chess diagrams

License: MIT License

Makefile 0.70% C 88.80% C++ 10.49%
pawn chess-position chess chess-game breadth-first-search enumeration chess-engine

pawn-bfs's Introduction

Pawn BFS

When counting possible chess positions, pawns are most important because they can promote to any other piece. This quickly increases the number of potential "armies" on the board and the number of possible diagrams. This project searches for possible pawn diagrams on the board, considering specifically how many diagonal captures are necessary to reach each. Our goal is to demonstrate a tight upper bound on the number of reachable chess positions.

Method

A pawn diagram includes the position and color of each pawn on the board. A diagram can be measured on the maximum remaining moves for white and black pawns. For example, at the start position, white and black both have 40 moves remaining (each pawn could progress 5 squares, with captures). With all pawns gone, zero moves are remaining.

This makes 41×41=1681 categories of pawn diagrams. Due to symmetries, only 861 categories are traversed. The included programs begin from the opening position and branch to all possible positions, creating useful tallies.

All programs require a 64-bit little endian system with a GNU compiler.

  • xxd - Standard command line tool, use to create opening position file, which has one record
  • print - Lists all records in a file for debugging
  • tally - Summarizes the records in a file
  • bfs - Reads each record in a file, finds all diagrams reachable by one move, saves those records to a new file
  • hsort - Sorts records in a file using heapsort (qsort was slower on SSDs)
  • merge - Removes duplicate records in a sorted file

Record format

The opening position is represented with:

mkdir -p data
echo 'ff00 0000 00ff ffff 0000 0000 00ff 0000' | xxd -r -p > data/40-40.records

Records are stored in files named like: data/W-B.records

  • W — Maximum remaining moves for white

  • B — Maximum remaining moves for black

The 128-bit record format is (little endian values):

  • Bits 0…47 — 1 if pawn is on the board (file A…H, rank 7…2)
  • Bits 48…95 — 1 if the pawn is black
  • Bits 96…126 — required piece captures

For each record we are interested in the required number of white piece (♕♖♗♘♙) captures and black piece captures (♛♜♝♞♟). If a certain position is reachable by either 3W/2B or 2W/2B captures then only the latter is notable because you can easily find another way to remove a white piece from the board. You can encode an NxM efficient curve with N+M−1 bits. For example, the below is encoded 000010101101101 (the last bit can always be inferred).

encode-captures

Tally phase

./tally data/40-40.records

The tally program reads each record and finds which number of white captures and black captures is compatible with that record (the efficiency curve). It also counts how many ways the remaining armies could be placed on the board. This also outputs the total number of chess diagrams.

Branching phase

./bfs data/40-39.records

This will open the W40/B39 file where white pawns are in the opening position and one black pawn made progress. The BFS search will find records where:

  • A white pawn is captured (W35/B39)
  • A white pawn makes progress (W39/B39)
  • A black pawn is captured (W40/B35) or (W40/B34)
  • A black pawn makes progress (W40/B38)

In every case, one of the numbers will decrease. Outputs are saved to data/W-B-branched-NEWW-NEWB.records.

Sorting / unique phase

cat data/*-*-branched-39-39.records > data/39-39.records
rm data/*-*-branched-39-39.records
./hsort data/39-39.records
./uniq data/39-39.records

The sorting program moves all records with the same (folded) pawn diagram to be consecutive. The uniquing program calculates the combined efficiency curve and truncates the file to remove duplicates.

Get started

echo 'ff00 0000 00ff ffff 0000 0000 00ff 0000' | xxd -r -p > data/8-8-0-0.records
bfs data/8-8-0-0.records

Then start BFSing through the $(wc -l DRY) number of record files. You need to manage the dependency that data/a-b-c-d --> data/(<=a)-(<=b)-(>=c)-(>=d), and the effect of folding on dependency (e.g. 4-3-10-2 => 3-3-2-8). Then you run cat data/*.records.a-b-c-d > data/a-b-c-d.records and all the sorting and merging.

Project status

  • MERGE FUNCTION IS NOT MERGING THE POSSIBLE ARMIES
  • need to fix this before counting positions look for symmetric records, their possible armies should be symmetric, if not something is wrong
  • Tally and or bfs have a problem noting the number of symmetries
  • Passing SIGINT does not corrupt data in short (it’s a slow process)
  • This is 2^55.4, which would require 785 petabytes of storage (before folding) if all positions were reachable.

Random notes

Get a random pawn diagram:

shuf -e w w w w w w w w b b b b b b b b . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | paste -d' ' - - - - - - - - 

You can intuitively conclude that a vast majority of these are not reachable, so this project may be worthwhile.

Quickly view record files:

xxd data/8-7-0-0.records.8-8-0-0

Discussion on Chess upper bounds:

pawn-bfs's People

Contributors

fulldecent avatar tromp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

tromp stjordanis

pawn-bfs's Issues

Count unopposed pawns

Will be way easier but a weaker lower bound.

https://mail.google.com/mail/u/0/#starred/FMfcgxRrJfVvqvWTGGjDhTppJmsQLhxC

Instead of recording the whole board, record each file as one of the following. Where empty ranks are ignored.

  1. (empty)
  2. ♙♙
  3. ♙♟
  4. ♟♙
  5. ♟♟
  6. ♙♙♙
  7. ♙♙♟
  8. ♙♟♙
  9. ♙♟♟
  10. ♟♙♙
  11. ♟♙♟
  12. ♟♟♙
  13. ♟♟♟
  14. ♙♙♙♙
  15. ♙♙♙♟
  16. ♙♙♟♙
  17. ♙♙♟♟
  18. ♙♟♙♙
  19. ♙♟♙♟
  20. ♙♟♟♙
  21. ♙♟♟♟
  22. ♟♙♙♙
  23. ♟♙♙♟
  24. ♟♙♟♙
  25. ♟♙♟♟
  26. ♟♟♙♙
  27. ♟♟♙♟
  28. ♟♟♟♙
  29. ♟♟♟♟
  30. ♙♙♙♙♙
  31. ♙♙♙♙♟
  32. ♙♙♙♟♙
  33. ♙♙♙♟♟
  34. ♙♙♟♙♙
  35. ♙♙♟♙♟
  36. ♙♙♟♟♙
  37. ♙♙♟♟♟
  38. ♙♟♙♙♙
  39. ♙♟♙♙♟
  40. ♙♟♙♟♙
  41. ♙♟♙♟♟
  42. ♙♟♟♙♙
  43. ♙♟♟♙♟
  44. ♙♟♟♟♙
  45. ♙♟♟♟♟
  46. ♟♙♙♙♙
  47. ♟♙♙♙♟
  48. ♟♙♙♟♙
  49. ♟♙♙♟♟
  50. ♟♙♟♙♙
  51. ♟♙♟♙♟
  52. ♟♙♟♟♙
  53. ♟♙♟♟♟
  54. ♟♟♙♙♙
  55. ♟♟♙♙♟
  56. ♟♟♙♟♙
  57. ♟♟♙♟♟
  58. ♟♟♟♙♙
  59. ♟♟♟♙♟
  60. ♟♟♟♟♙
  61. ♟♟♟♟♟
  62. ♙♙♙♙♙♙
  63. ♙♙♙♙♙♟
  64. ♙♙♙♙♟♙
  65. ♙♙♙♙♟♟
  66. ♙♙♙♟♙♙
  67. ♙♙♙♟♙♟
  68. ♙♙♙♟♟♙
  69. ♙♙♙♟♟♟
  70. ♙♙♟♙♙♙
  71. ♙♙♟♙♙♟
  72. ♙♙♟♙♟♙
  73. ♙♙♟♙♟♟
  74. ♙♙♟♟♙♙
  75. ♙♙♟♟♙♟
  76. ♙♙♟♟♟♙
  77. ♙♙♟♟♟♟
  78. ♙♟♙♙♙♙
  79. ♙♟♙♙♙♟
  80. ♙♟♙♙♟♙
  81. ♙♟♙♙♟♟
  82. ♙♟♙♟♙♙
  83. ♙♟♙♟♙♟
  84. ♙♟♙♟♟♙
  85. ♙♟♙♟♟♟
  86. ♙♟♟♙♙♙
  87. ♙♟♟♙♙♟
  88. ♙♟♟♙♟♙
  89. ♙♟♟♙♟♟
  90. ♙♟♟♟♙♙
  91. ♙♟♟♟♙♟
  92. ♙♟♟♟♟♙
  93. ♙♟♟♟♟♟
  94. ♟♙♙♙♙♙
  95. ♟♙♙♙♙♟
  96. ♟♙♙♙♟♙
  97. ♟♙♙♙♟♟
  98. ♟♙♙♟♙♙
  99. ♟♙♙♟♙♟
  100. ♟♙♙♟♟♙
  101. ♟♙♙♟♟♟
  102. ♟♙♟♙♙♙
  103. ♟♙♟♙♙♟
  104. ♟♙♟♙♟♙
  105. ♟♙♟♙♟♟
  106. ♟♙♟♟♙♙
  107. ♟♙♟♟♙♟
  108. ♟♙♟♟♟♙
  109. ♟♙♟♟♟♟
  110. ♟♟♙♙♙♙
  111. ♟♟♙♙♙♟
  112. ♟♟♙♙♟♙
  113. ♟♟♙♙♟♟
  114. ♟♟♙♟♙♙
  115. ♟♟♙♟♙♟
  116. ♟♟♙♟♟♙
  117. ♟♟♙♟♟♟
  118. ♟♟♟♙♙♙
  119. ♟♟♟♙♙♟
  120. ♟♟♟♙♟♙
  121. ♟♟♟♙♟♟
  122. ♟♟♟♟♙♙
  123. ♟♟♟♟♙♟
  124. ♟♟♟♟♟♙
  125. ♟♟♟♟♟♟

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.