Python chess engine

Discussion of chess software programming and technical issues.

Moderators: hgm, Dann Corbit, Harvey Williamson

Forum rules
This textbox is used to restore diagrams posted with the [d] tag before the upgrade.
eligolf
Posts: 64
Joined: Sat Nov 14, 2020 11:49 am
Full name: Elias Nilsson

Python chess engine

Post by eligolf » Fri Nov 27, 2020 12:31 pm

Hi all,

Thank you for all your help lately. As a beginner programmer (and even more beginner chess programmer) it has been a fun and frustrating journey but now I have finally got some working code. If you are interested you can find the code on https://github.com/eligolf/affinity_chess.

Any feedback or improvement suggestions is highly appreciated. There are still lots of things to develop and this will probably be an ongoing project for a while. Which is very fun!

And again, thank you so much for the helpful comments to my newbie questions the past weeks :)

KLc
Posts: 55
Joined: Wed Jun 03, 2020 4:46 am
Full name: Kurt Lanc

Re: Python chess engine

Post by KLc » Mon Nov 30, 2020 5:44 pm

I like python chess engines. But is there any chance to connect this via UCI to an own GUI? You did 99.9% of the work already so I hope you can add this.

eligolf
Posts: 64
Joined: Sat Nov 14, 2020 11:49 am
Full name: Elias Nilsson

Re: Python chess engine

Post by eligolf » Tue Dec 01, 2020 1:46 pm

Hi KLc, it is not currently possible but I can surely give it a try. This is my first chess engine so I am still a bit unsure of the standard way of doing things. Thanks for checking it out.

User avatar
Kotlov
Posts: 232
Joined: Fri Jul 10, 2015 7:23 pm
Location: Russia

Re: Python chess engine

Post by Kotlov » Tue Dec 01, 2020 2:15 pm

python too slow
Eugene Kotlov
Hedgehog 2.1 64-bit coming soon...

KLc
Posts: 55
Joined: Wed Jun 03, 2020 4:46 am
Full name: Kurt Lanc

Re: Python chess engine

Post by KLc » Tue Dec 01, 2020 3:31 pm

Kotlov wrote:
Tue Dec 01, 2020 2:15 pm
python too slow
You can run it with PyPy to get more speed. And too slow is relative and depends on what you want. Beating Stockfish? No. Having fun? Yes. I like fun.

hafni
Posts: 6
Joined: Fri Nov 22, 2019 3:12 pm
Full name: Hafni Rojo

Re: Python chess engine

Post by hafni » Tue Dec 01, 2020 7:54 pm

+ 1 :wink:


Best regards

User avatar
maksimKorzh
Posts: 692
Joined: Sat Sep 08, 2018 3:37 pm
Location: Ukraine
Full name: Maksim Korzh
Contact:

Re: Python chess engine

Post by maksimKorzh » Tue Dec 01, 2020 11:21 pm

eligolf wrote:
Fri Nov 27, 2020 12:31 pm
Hi all,

Thank you for all your help lately. As a beginner programmer (and even more beginner chess programmer) it has been a fun and frustrating journey but now I have finally got some working code. If you are interested you can find the code on https://github.com/eligolf/affinity_chess.

Any feedback or improvement suggestions is highly appreciated. There are still lots of things to develop and this will probably be an ongoing project for a while. Which is very fun!

And again, thank you so much for the helpful comments to my newbie questions the past weeks :)
Awesome work!
GUI is fantastic.

A few downsides though:
To run it on linux I need to comment out these lines in main driver:

Code: Select all

# Set the correct icon in Windows taskbar
#myappid = u'_'
#ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
I think above must be done conditionally if OS is windows.
In C it's done as simple as:

Code: Select all

#ifdef WIN64
//your code
#endif
Not sure how to make similar in python but that should be easy.

Also after submitting initial window I had this error:

Code: Select all

Traceback (most recent call last):
  File "gui.py", line 581, in <module>
    Gui().main()
  File "gui.py", line 149, in main
    self.draw(self.gamestate.get_valid_moves())
  File "gui.py", line 172, in draw
    self.draw_board(valid_moves)
  File "gui.py", line 206, in draw_board
    self.highlight_squares(valid_moves)
  File "gui.py", line 253, in highlight_squares
    self.draw_highlighting(s.grey[1], s.grey_t[1], start_col, start_row, 2)
  File "gui.py", line 277, in draw_highlighting
    pygame.draw.rect(self.screen, color, (col * s.sq_size + s.board_offset, row * s.sq_size + s.board_offset, s.sq_size, s.sq_size), thickness, border_radius=1)
TypeError: rect() takes no keyword arguments
Which was resolved by commenting out this line:

Code: Select all

    def draw_highlighting(self, color, color_t, col, row, thickness):
        surface = pygame.Surface((s.sq_size, s.sq_size), pygame.SRCALPHA)
        surface.fill(color_t)
        self.screen.blit(surface, (col * s.sq_size + s.board_offset, row * s.sq_size + s.board_offset))
        #pygame.draw.rect(self.screen, color, (col * s.sq_size + s.board_offset, row * s.sq_size + s.board_offset, s.sq_size, s.sq_size), thickness, border_radius=1)
Apart from the above it's very impressive.
Do you have UCI/winboard version of your engine to play in custom GUI?
Playing vs other engines allows to reveal lots of bug.

I'm just curious what is the project goal?
Bare minimum chess variants in vanilla JavaScript
https://maksimkorzh.github.io/bmcp-variants/

Chess programming YouTube channel:
https://www.youtube.com/channel/UCB9-pr ... KKqDgXhsMQ

OfekShochat
Posts: 30
Joined: Thu Oct 15, 2020 8:19 am
Full name: Ofek Shochat

Re: Python chess engine

Post by OfekShochat » Wed Dec 02, 2020 6:02 am

I made a python chess engine. with AB, mcts (the native one), and uct.
I made also the uci and I don't think you need more than that. a gui for that will just be a nightmare if you use something like pygame.

eligolf
Posts: 64
Joined: Sat Nov 14, 2020 11:49 am
Full name: Elias Nilsson

Re: Python chess engine

Post by eligolf » Wed Dec 02, 2020 6:35 am

@maksimKorzh, thank you for your nice words! It is always nice to have someone else test out the code to find bugs I haven't thought about. About your findings:

1. You are right about setting the icon in task bar, I need to add an if statement to make it only for Windows machines.
2. The error you get with the pygame.rect is due to the 'border_radius' key word. Make sure you have the latest version of pygame installed and it should work. I tried with pygame 1.9.something and got the same error but with 2.0.0 it worked as it should. I might need to write down a list of what modules are needed for this program.
3. Unfortunately I have no UCI version but that is on the todo list.

The project is just a hobby project to learn more about programming in Python, and it also evolved to learn more about chess programming too which is fun and sort of addictive :P

I have a few main goals which are probably going to change along the way:

1. Reach a depth of 8 plies in a reasonble time (max around 5-10 seconds per move) in a midgame position, preferably with quiescence search installed.
- Now it reaches around depth 5-6 in that time depending on position, without extensions in negamax.
2. Be able to beat me constantly.
- Now I beat it 98% of the time if I concentrate enough.
3. Maybe maybe change to bitboard representation to be able to reasonably do more cool evaluation tricks, and to hopefully speed things up.
- I understand the concept but have no idea how to do move generation logic with them.

Any ideas on how to improve efficiency is also very appreciated. Next I will try to add history heuristics and some check extension.

I tried to implement nullmove logic according to the Mediocre engine (https://mediocrechess.blogspot.com/2007 ... moves.html) which seems super easy to do. But somehow it fails. Maybe I am missing to update some logic in the make_null_move function since there are lots of things to consider: Zobrist hasing update, enpassant possible square, latest move made (which is what in this case?) etc.

Madeleine Birchfield
Posts: 312
Joined: Tue Sep 29, 2020 2:29 pm
Location: Dublin, Ireland
Full name: Madeleine Birchfield

Re: Python chess engine

Post by Madeleine Birchfield » Wed Dec 02, 2020 12:04 pm

eligolf wrote:
Wed Dec 02, 2020 6:35 am
I have a few main goals which are probably going to change along the way:

1. Reach a depth of 8 plies in a reasonble time (max around 5-10 seconds per move) in a midgame position, preferably with quiescence search installed.
- Now it reaches around depth 5-6 in that time depending on position, without extensions in negamax.
2. Be able to beat me constantly.
- Now I beat it 98% of the time if I concentrate enough.
3. Maybe maybe change to bitboard representation to be able to reasonably do more cool evaluation tricks, and to hopefully speed things up.
- I understand the concept but have no idea how to do move generation logic with them.

Any ideas on how to improve efficiency is also very appreciated. Next I will try to add history heuristics and some check extension.
Magic bitboards are very fast for board representation, and are the most widely used for board representation in modern engines, so I would highly recommend replacing your current representation with magic bitboards to improve your engine's efficiency. If you are able to use more than one CPU on your computer, then implementing a parallel search for multiple processes such as lazy SMP would also improve your engine's efficiency.

I would also suggest that at some point learn about automated tuning and machine learning techniques, using those to make the parameters in the search and the evaluation more optimal. Specifically for your evaluation, you could also learn about neural networks and use those for your evaluation in place of your current evaluation function, as neural networks tend to be better evaluation functions than the current handcrafted heuristics you are using for evaluation. The trade off with neural networks is that they also tend to be slower to calculate, so may slow down your search speed.

Post Reply