chessprob.py
This script transforms the formulas given in this answer on Math Stack Overflow to Python code.
The code was tested with Python 3.7.
The chessprob module has six functions, corresponding to the formulas in the above link. They calculate the probability that, in a (idealized) set of games ...
identical_any()
- any two games are identicalidentical_pov_player()
- any of the games played by a specific player is identical to any of the other games played by other playersidentical_pov_game()
- a specific game is identical to any other gameidentical_these_moves()
- any two games are identical and have a specific move orderidentical_these_moves_pov_player()
- any of the games played by a specific player is identical to any of the games played by the other players and they have a specific move orderidentical_these_moves_pov_game()
- a specific game is identical to any other game, and they have a specific move order
There are two formulas for each case, one exact formula and an approximation formula. The exact formula gives a precise result but can not calculate bigger inputs in a reasonable time (See the description for the factorial function in the below glossary for the reason why). The approximation formula (the default) is precise up to around five positions after the first number which is not null, and it can calculate even very big inputs in no time.
See the example_usage.py
for an example.
You can switch between precision and approximation mode by using set_options(exact=True)
or set_options(exact=False)
.
Too long, didn´t read – See the example_usage.py
.
The chessprob module behaves like a class.
If you want other that the default options, first use the set_options()
function to set them. Use the print_options()
function to print them.
These are the options you can set with set_options()
:
choices
int: The amount of choices in a chess position. Defaults to 3 [1].depth
int: Alias ply. The amount of half moves per game. Defaults to 80, alias 40 moves [1].players
int: The amount of chess players. Defaults to 1.500.000.000. A guess of the amount of chess players in the history of chess.games
int: The amount of games every player plays. Defaults to 2500. A guess of the average amount of games played by the average chess player in his life.exact
bool: If the exact algorithms shall be used to calculate the different probabilities. Defaults to False (recommened).precision
int: Sets the amount of numbers after the comma. Defaults to 100.
An example is set_options(choices=3, depth=80, players=1_500_000_000, games=2_500, exact=False, precision=100)
(these are also the default values).
[1] Number of Sensible Chess Games
The get_formulas()
function returns the formulas of the identical_*
functions you pass to it, as a list of lists of strings. For each function two formulas. The first is the exact and the second the approximation formula.
The mathify()
function converts such a formula string to math syntax.
The populate()
function replaces the variables in a formula string with the current values you have set (see the set_options()
function).
The wolfram_open()
function opens the default webbrowser and opens each formula string passed to it at wolframalpha.com, populated with the current values, where it will be evaluated. The wolfram engine can work with bigger numbers when using the exact formulas.
Again, see the example_usage.py
for examples.
Meaning of variables, operators and functions used in Eyeballfrogs Answer and in the chessprob module:
Variables:
- N =
CHOICES
= the amount of move choices in a chess position. Set it withset_options(choices=<number>)
- P =
DEPTH
= how many half moves a game lasts. 40 moves = 80 half moves. Set it withset_options(depth=<number>)
- Np =
POSSGAMES
= N ^ P = all possible different chess games that can be played, depending on N and P. Automatically calculated. - Nc =
PLAYERS
= The amount of players who play games (originally computers). Set it withset_options(players=<number>)
- Ng =
GAMES
= The amount of games each player plays. Set it withset_options(games=<number>)
- Na =
ALLGAMES
= Nc * Ng / 2 = all games played by all players. Automatically calculated.
Operators:
+
plus-
minus*
multiplication (left away in math notation, whereA B
means A * B)/
division (÷
in math notation)**
power (^
in math notation)
Functions:
fac(number)
The factorial function, eg.fac(5)
= 1 * 2 * 3 * 4 * 5 = 120. Written(number)!
in math notation. Some of the exact algorithms use it, which is why they are slow on big numbers. Eg. if we assume that 3 ^ 80 is the amount of all possible different chess games with sensible moves (already a number with 38 digits), thenfac(3 ^ 80)
is a number with 5577626172914152352366030303784576483327 digits (you can calculate the amount of digits but not the number itself).exp(number)
Returns e ^ number, where e is 2.718281... – Euler´s number. Most of the approximation functions use this function.
Many thanks go to to Eyeballfrog for providing the formulas.
Written by Nils-Hero Lindemann in 2019.08.12.
public domain