## Where communities thrive

• Join over 1.5M+ people
• Join over 100K+ communities
• Free without limits
• Create your own community
##### Activity
• 16:49
• Jan 17 15:57
niklasf closed #731
• Jan 17 15:57
niklasf commented #731
• Jan 17 14:41

niklasf on master

Test king captures unmoved rook (compare)

• Jan 17 11:57
josephernest edited #731
• Jan 17 11:56
josephernest opened #731
• Jan 14 19:23
• Jan 14 13:44
niklasf commented #724
• Jan 14 13:29
niklasf commented #724
• Jan 14 13:28
niklasf commented #724
• Jan 14 12:50
Maxximiliann commented #724
• Jan 13 14:20
atinm forked
• Jan 10 01:10
BM123499 commented on 4ae64a7
• Jan 09 17:21
niklasf commented #724
• Jan 09 17:19
niklasf commented #727
• Jan 09 17:18
niklasf closed #727
• Jan 09 17:18

niklasf on master

Read and write millisecond %clk… (compare)

• Jan 09 00:08
• Jan 08 18:26
PedanticHacker commented #724
• Jan 08 12:21
niklasf commented #725
yeranosyanvahan
@yeranosyanvahan
hello
I have some questions regarding python-chess
I am using jupyter-notebook on my windows machine.
I don't want to run engines, but I want something to evaluate my board, how do I do that?
Also I want to access the movement history and save it somewhere, how do I do that too?
and... check if en-passent has happened or not
during a game
Niklas Fiekas
@niklasf
Please stop spamming your questions everywhere. You now posted in 3 different places, without waiting for a reply.
Boštjan Mejak
@PedanticHacker
So my wish is to have python-chess v1.1.0 before Christmas. Is it doable?
Vel Wu
@velwu

Hello there,
I'm designing a variant chess game with modifications to existing pieces' moves and play it on 5x5 Minichess board. Are such custom pieces and board layouts supported by python-chess, and how do I implement them without changing the library?

The custom pieces are:
(1) ♝ Crusaders (modded Bishops) ♗:

• Can move in any direction (vertical, diagonal, horizontal)
• Can jump over obstacles and capture all enemy pieces in its path but must move exactly 2 squares. No more, no less.

(2) ♞ Horse Archers (modded Knights)♘:

• Work like Rooks when moving. Can travel for any distance orthogonally, but cannot jump over other pieces.
• Behave "kind of" like grasshoppers when capturing. Jump over exactly one piece (can be ally or enemy) orthogonally to land on the enemy piece behind it.

The custom boards are Gardner and Mallett under the 5×5 chess section here:
https://en.wikipedia.org/wiki/Minichess

My GitHub fork (mostly built on someone else's) currently has a weakly-working AI playing standard pieces and board layouts:
https://github.com/velwu/PythonChessAi

Thank you in advance and looking forward to your response.
Warmest regards from my country Taiwan,
Vel (Tien-Yun) Wu

Niklas Fiekas
@niklasf
hi @velwu. the library uses 64 bit integers as bitboards internally and assumes 8x8 in many places
so it's probably going to be tricky to do, without making a fork
kdnune
@kdnune
First of all, Niklas, thanks for creating this library. I see how to get the best move according to an engine with str(board.san(info['pv'][0])). However, I don't see how to get an evaluation score if this move had been played. Thanks.
Niklas Fiekas
@niklasf
hi @kdnune. aside: str(...) is not needed in that code
kdnune
@kdnune
Yeah, thanks Niklas. So I've been able to get scores (well expectations actually) for all the moves that were actually played in the game, and I put this in a pandas df . However, I was wondering if there was an easy way to do this with the best moves. Will I need to create a different pgn with the best moves? Thanks again.
Niklas Fiekas
@niklasf
The best move is expected to maintain the score. It should be the same before and after.
Boštjan Mejak
@PedanticHacker
It would be better (at least visually) if info["score"] could be used as info.score.
kdnune
@kdnune
Hey Niklas, Thank you for your responses. I'm able to produce a dataframe that like the following:
    j    expectation    gamenbr    movecolor    movenbr
0 1 0.4385 1 white 1
1 2 0.4565 1 black 1
2 3 0.4345 1 white 2
3 4 0.4295 1 black 2
4 5 0.4330 1 white 3
... ... ... ... ... ...
53 54 0.5000 3 black 27
54 55 0.5385 3 white 28
55 56 0.5305 3 black 28
56 57 0.5565 3 white 29
57 58 0.5370 3 black 29
kdnune
@kdnune
However, it takes about 3-4 minutes to run for just a 3 game pgn, and I have about 150 of my correspondence games that I would like to analyze. There is also other information that I would like to include. I'm running this on a jupyter notebook on my laptop (acer nitro 5, cpu: i5-9300h, gpu: Nvidia GTX 1650, 256 SSD, 8gb ram). Do you have ideas for how I can speed this up? If you need to see the code let me know. Thanks.
kdnune
@kdnune
I forgot to mention that my computer has windows 10 home OS.
Niklas Fiekas
@niklasf
it's normal that engine analysis takes time. if you're not doing so already, you can engine.configure({"Threads": 4}) # or even higher to speed it up a little by using more cpu cores
TheNarwhalArmy
@TheNarwhalArmy

I'm sorry because I'm not sure if this is the right place for this, but I get the error "ValueError: 'pl' is not in list" in a file from python-chess. i am not sure exactly what this is doing but I think it could be causing an issue with changing the orientation of the board, as after I try to do that, after a few times this error occurs. Here is the full error if this helps.

Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\ethan\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\client.py", line 333, in _run_event
await coro(args, *kwargs)
File "D:\Documents\chess\main.py", line 82, in on_message
if chess.Move.from_uci(message.content) in board.legal_moves:
File "C:\Users\ethan\AppData\Local\Programs\Python\Python38\lib\site-packages\chess__init__.py", line 522, in from_uci
from_square = SQUARE_NAMES.index(uci[0:2])
ValueError: 'pl' is not in list

Niklas Fiekas
@niklasf
looks to me like message.content from your code is not a valid uci move
so chess.Move.from_uci(message.content) raises ValueError
kdnune
@kdnune
Thanks for responding Niklas. I tried engine.configure({"Threads": 8}) , and the speed up was still barely noticeable. Again between about 3-4 minutes. I noticed from the task manager that the GPU was not being used at all for some reason. In my notebook, the line print("Num GPUs Available", len(tf.config.experimental.list_physical_devices('GPU'))) returns "Num GPUs Available 1", but it doesn't look like my GPU is actually being used. Is there any way I can actually utilize my GPU? Thanks again.
Niklas Fiekas
@niklasf
which chess.engine.Limit are you using?
what resources the engine uses is up to the engine itself. for example, stockfish is designed as a cpu engine and never uses gpu
kdnune
@kdnune
I used info = engine.analyse(board, chess.engine.Limit(time=1)). I'm using stockfish. I'm open to using other engines that would use the gpu. Do you know which engines would. Thanks.
Niklas Fiekas
@niklasf
lc0 would, but sf is the strongest engine there is, currently
time=1 means to spend 1 second to analyse the position, no matter what
so increasing threads was improving analysis quality, but still taking 1 second
you could chose a lower time (e.g. time=0.5), to trade quality for speed. there's also depth=20 or sth like that, or nodes=2100000 (just example values)
kdnune
@kdnune
OK, I'll have to play around a little bit more. I just tried setting the time = .01 and it finish just a couple seconds later. But this is probably giving too little time. Also thanks for the info that Lc0 would use the gpu. So far the only other engines I've tried are stockfish and komodo.
nmwitzig
@nmwitzig
Hey everyone, I have a question: I know that we can get the best move of the next player quickly by info['pv'][0], but I was wondering whether the second, third, fourth and so on best moves are stored already anywhere else. Currently, to get this info I loop through all legal moves, get the evaluation score and sort accordingly, but this is computationally very expensive. Glad for any other tips! Thanks!
Niklas Fiekas
@niklasf
sounds like you're looking for analyse(..., multipv=n). it has considerable extra cost, but is still better than looping through all legal moves
nmwitzig
@nmwitzig
Thanks! Ill give this a try!
Boštjan Mejak
@PedanticHacker

I have a question. What's the simplest way to check whether a chess engine has been stopped? I noticed this "stopped" attribute throughout the engine.py module. Is that just for internal use or can it be used as a way to check whether a chess engine has been stopped? And if yes, can it be used as engine.stopped (where engine is an instance of chess.engine.SimpleEngine)?

So, I am using the chess.engine.SimpleEngine instance and have implemented a way to stop the engine by using its close() method. To restart it, I have found a way, but I need to check whether the engine has been stopped before restarting it. What's your best solution?

Niklas Fiekas
@niklasf
currently the only reliable way is to track the state yourself
set a variable when you start it, and unset it when the command finishes
Boštjan Mejak
@PedanticHacker
Ah, interesting idea! I will do that. I thought there was something already implemented to use, just like that "stopped" attribute I saw. Thank you.
Boštjan Mejak
@PedanticHacker
I have completed my previous task. Now I have a new task to complete. I want my chess GUI to display the name of the opening and I just want to ask if you could point me in the direction to achieve this. Is there any file in python-chess with such opening names or must I download it from somewhere? And what is the preffered way to use such a file?
Boštjan Mejak
@PedanticHacker

I initially thought chess.polyglot is what I need, but I don't think I can get an opening name for a certain board position with it. Please correct me if I'm wrong.

So what I want is that if moves "1.e4 e5 2.Nf3 Nc6 3.Bb5" have been played, the opening name "Ruy Lopez / Spanish Opening" should appear. So what can I do or use to classify such move orders and give them names?

Boštjan Mejak
@PedanticHacker
So I am essentially asking whether python-chess is capable of displaying ECO codes.
nmwitzig
@nmwitzig

sounds like you're looking for analyse(..., multipv=n). it has considerable extra cost, but is still better than looping through all legal moves

Thanks again for this hint, works like a charm! But another question: Often, we are interested in the evaluation of the engine for a given move.

I know how to analyze a position and some scientific papers simply use formulas such as evaluation_after_a_move - evaluation_before_move (with correct presigns etc.) to obtain the score of a given move a player made.

However, is there an easier way to obtain scores for given moves? Thanks to multipv=3 i can get the three best engine moves, but I'd like to have a more nuanced picture how the engine actually rated each possible move.

Thanks!
Niklas Fiekas
@niklasf
@nmwitzig don't think so. most uci engines should support root_moves (https://python-chess.readthedocs.io/en/latest/engine.html#chess.engine.Protocol.analysis), but essentially it's not better than making the move and analysing that position
@PedanticHacker i maintain such a dataset at https://github.com/niklasf/eco, that you could write probing code for
nmwitzig
@nmwitzig
@niklasf Thanks for the prompt response! I think info[n]["score"] (with n \in 0,...,N-1) and multipv = N ) should suffice for my purpose! Best regards!