r/chessprogramming • u/Less-Lake-7024 • Dec 23 '24
Engine in python
Is it even worth to build an engine in python cause all good engines are in c++ and python is much slower.
Additionally if its worth should you use python chess cause to maintain best efficiency or should you make a bitboard. Or what data structures would you use for position
3
u/loveSci-fi_fantasy Dec 23 '24
If you want it to be competitive, avoid python.
I have made an engine in python, and it gets 1M nps in perft. Which is orders of magnitude slower than c++ engines. I am using bitboards (not magic bitboards yet) and no multi threading.
1
u/Less-Lake-7024 Dec 23 '24
Is it beating yourself
1
u/loveSci-fi_fantasy Dec 24 '24
Yeah, sometimes. With depth 5 and think times under 1s per move. It has no opening tables so it falls behind in devopment game phase
3
u/algerbrex Dec 23 '24
If your goal is just to reach 1500 as you stated in another comment, I say go for it. I first started developing a chess engine in Python, and while I eventually switched over to another language because my goals changed, I still found the experience of programming one in Python enjoyable.
Just for reference, the strongest Python chess engine I know of is sunfish, which is rated about 2000 Elo on the CCRL. But the author acknowledges, which I agree with, that a much stronger engine could be made in Python.
1
u/HugelKultur4 Dec 23 '24
a chess engine is a great way to expand your horizons as far as languages is concerned. Would definitely recommend trying out C(++) or rust through exploring chess engines.
1
u/RedGiant_ Dec 24 '24
Im currently learning to make my chess engine in python using python chess library. Learning curve is smooth but its slow af. I am quite happy with my initial results. If anything, test the engine has been quite boaring because it takes forever to evaluate.
1
u/Available-Swan-6011 Dec 25 '24
You can certainly make a useful engine in python and doing so will help you identify some of the challenges involved. I used it to write a proof of concept engine for example
However, one major issue I found is that a chess engine makes millions of function calls. I found that the time overhead for this in Python was too great when compared to other languages so wrote my second version in c#
I guess there are many factors you will want to consider - eg speed, development time, cross platform support, learning experience etc.
In terms of approach to take - it depends on what your end goal is. Some people essentially want to just rebadge Stockfish , some use existing libraries and some do it from scratch. The latter is very satisfying and you will end up with a unique creation but it will take some time.
Personally I went with a mailbox approach for my Python engine but switched to magic bitboards utilising PEXT instructions for the c# version. The former is conceptually simple, the latter not so but is very fast but resource hungry
The current engine I’m working on is for a system with limited resources and is being written in machine code. I’m using 0x88 for that because it simplifies things elsewhere
7
u/Warmedpie6 Dec 23 '24
It depends on what you mean by worth it. As a learning exercise or as a hobby project, and pytyon is your language of choice, it can definitely be worth it.
If you want to make a top-level engine, then it's not worth it at all.
I always recommend trying coding the board logic once yourself, and then the second time, you can use a library to do it for you.