py-stockfish / stockfish

Integrates the Stockfish chess engine with Python (Official fork)
https://py-stockfish.github.io/stockfish/
MIT License
28 stars 8 forks source link

Compatibility with chess library #6

Open jamesbraza opened 1 year ago

jamesbraza commented 1 year ago

There is another chess library https://github.com/niklasf/python-chess (aka chess) in existence, and it's popular and actively supported. It also can integrate with Stockfish:

import chess
import chess.engine

STOCKFISH_PATH = "/opt/homebrew/bin/stockfish"

board = chess.Board()

engine = chess.engine.SimpleEngine.popen_uci(STOCKFISH_PATH)
engine.configure({"UCI_LimitStrength": True, "UCI_Elo": 1600})
result = engine.play(board, limit=chess.engine.Limit(time=0.1))
board.push(result.move)

There is no acknowledgement of chess in the README.md or issue tracker, from what I can see.

This is an open-ended ticket sort of about:

import chess
from stockfish import Stockfish

STOCKFISH_PATH = "/opt/homebrew/bin/stockfish"

stockfish = Stockfish(path=STOCKFISH_PATH)
board = chess.Board()
stockfish.set_fen_position(fen_position=board.fen())

Edit: I found someone had actually used this pattern here.


This was copied with some modifications from https://github.com/zhelyabuzhsky/stockfish/issues/129.

kieferro commented 1 year ago

Thanks for moving the issue.

I think it is certainly useful to mention and link to the other library (which of course offers much more functionality in terms of chess itself). I've only had a quick look at the engine functions of the other library, but I do have the impression that some methods there are not as easily or directly supported as here. But it would be necessary to investigate this in more detail.

It also seems to make sense to me to establish compatibility, because I assume that these two packages are often used in combination.

Disservin commented 9 months ago

Hi,

I want to talk a bit about the state of the library. I recently just learned about the fact Ilya had passed away and that the project under https://github.com/zhelyabuzhsky/stockfish is lost. This is always rather sad news personally and for any open source project.

As far as I understand any commits made to this repo wont also appear on the pypi project, or why was no new version ever published?

In advance I dont want to sound rude, but simply get to know more about this project.

Generally speaking, I admire the efforts you have put into the project and to provide a nice way to use Stockfish from python.

Though personally I think the library has a major flaw, virtually every time you want to do something with Stockfish (or any engine) you need a corresponding chess library, to provide you with the right tools to create pgns, play a game, go through the moves of a game and so on.
The entire library is based on Stockfish and with that there come a few problems, for example the is_move_correct function, which starts a depth 1 search in Stockfish. This is a really hacky way, if the user specified Stockfish to use 20 threads before this command will essentially spawn 20 threads (internally in Stockfish), or am I wrong?

https://github.com/py-stockfish/stockfish/blob/master/stockfish/models.py#L690

I think every engine related project pretty much just uses the python uci engine implementation nowdays. So I wondering what your plans are for future.

Also we get frequentely asked how someone can do XYZ with this library on the official Stockfish discord, and then we have to a) answer that we are not maintaining this project and b) it's probably better to simply use python-chess because they need some functionality from there.

In all honestly, your efforts are probably better spent on porting some of the nice functionality to python-chess, in which case other engines profit also from them.

I'm interested to hear what you have to say ; )

kieferro commented 9 months ago

Hi, I appreciate your input @Disservin and I am always happy to discuss the best future for this project and hearing different views on this 😃

As far as I understand any commits made to this repo wont also appear on the pypi project, or why was no new version ever published?

We already made a PEP 541 request to transfer the project when we forked it. However, this has not yet received a response. I have now followed up again and very much hope that we can expect a decision soon. Whatever happens with the library, it would definitely be good if someone responsible had access to it and would be able to publish releases.

The entire library is based on Stockfish and with that there come a few problems, for example the is_move_correct function, which starts a depth 1 search in Stockfish. This is a really hacky way, if the user specified Stockfish to use 20 threads before this command will essentially spawn 20 threads (internally in Stockfish), or am I wrong?

No, you're right about this. Overall, there are a few places where functions have been introduced that don't really have anything to do with Stockfish, but rather with chess in general. But we hadn't really decided how to deal with that yet for backwards compatibility reasons.

I think every engine related project pretty much just uses the python uci engine implementation nowdays.

This is an interesting point, because I actually have the impression that there is still a significant user base for this library. And even if I look, for example, at the dependencies listed on Github or at the PyPi download data (which of course should be taken with a grain of salt and are not 100% accurate): stockfish had 2700 downloads last month. Compared to chess, which had 30 times as many, that's not very much, of course, but it's still something. I suspect that this is mainly because stockfish is tailored exactly to Stockfish (which I suspect was also Ilya's vision when he started the project) and thus offers a few more functions and, conversely, can do without complexity because it is really only designed for Stockfish and does not offer as many functions as chess.

For this reason, I personally never made a plan or checked what other libraries with similar functionality already existed - as I would have done if I had wanted to start a whole new project. The old project was no longer maintained and still PRs and issues were opened, so it needed someone to take care of it.

In all honestly, your efforts are probably better spent on porting some of the nice functionality to python-chess, in which case other engines profit also from them.

I would be interested to know if there are any UCI-related functions that stockfish has and chess doesn't (but maybe you're not the right person to ask). I don't really have an overview. It seems to me that all the UCI functions are pretty well and completely implemented in chess. So I'm not quite sure which functions could be transferred from this library here. The only additional functions it offers are those that only make sense with Stockfish and will therefore probably never be supported by chess.


Overall, I understand your points and agree with you for the most part, the situation is unfortunately not quite optimal. I think regardless of the factual questions, it comes down to two fundamental questions:

I'm not clearly decided on either point, so I'd be interested to hear your thoughts on this as well.

(This is just my view, @johndoknjas may have additional points or different opinions)

Disservin commented 9 months ago

Hi, thanks for you response ; )

This is an interesting point, because I actually have the impression that there is still a significant user base for this library.

Maybe my view is limited, but I think not one python project for Stockfish, has ever been written with the Stockfish python library. Which is ironic I guess ?
Our/vondele's matetrack was written with python-chess and the other projects all simply use/have used python-chess (but those dont really need any stockfish (uci) functionality).

I suspect that this is mainly because stockfish is tailored exactly to Stockfish

I rather think that this package shows up as the first thing when you search `python stockfish'.

I suspect that this is mainly because stockfish is tailored exactly to Stockfish (which I suspect was also Ilya's vision when he started the project)

When looking at the git history it seems that the first commit was made in 2016, I think the uci implemention in python-chess might not have existed at that point, and there were simply no alternatives.

Is it worth it to continue a library whose functionality is almost completely fulfilled by another (also designed for more usecases) library, but which still has dependencies and is still used?

In the end that's up to you ;) but I think a majority (some numbers would be interesting?) of users who use this library also use python-chess at which point this is simply a rather redudant library (few dependencies too). I think the second point would make more sense if there was no uci, and every engine would behave differently.

johndoknjas commented 9 months ago

While the python-chess library has a lot more functionality, I think the stockfish library has something to offer when using stockfish specifically. For example, stockfish has a few commands which are non-standard uci ones. In addition, we provide some functions that take raw stockfish output, and then package it together nicely for the user. For example, the get_top_moves function. Stockfish has no single uci command that lets you get a list of the top moves. What our function does is call a number of uci commands, and parse the process output to obtain the top moves. There are a number of other examples too.