Bitboard Engine

Programming Topics (Computer Chess) and technical aspects as test techniques, book building, program tuning etc

Moderator: Andres Valverde

Bitboard Engine

Postby John Digsby » 09 Apr 2008, 16:30

Hi,

I'm trying something a little new for chess programming in terms of how to make the computer evaluate positions. I previously wrote a move generator based on representing the board as two arrays of squares occupied by black and squares occupied by white, accompanied by a bog-standard 64-length array. I precomputed all valid moves for every piece on every square of an empty board, and loaded them into memeory at initialisation.

All this, just to try to make my code somewhat less verbose and easier for me to maintain!

As I reached the evaluation routines, and wanted to start looking at structures like outposted knights, etc. I found I was writing very complex code, which would produce a bit of a bottleneck during the tree search.

I have since poked around the source of Crafty and the same evaluations that I'm having to write into 30-40 line functions for are represented by simple boolean ifs because of the bitboard representation.

I understand how this works, but didn't implement it because it was so complicated. Since I'm mostly interested in the evaluation end, I wondered if there was some C++ source for a chessboard class (or somesuch) that implemented bitboards. I would convert Crafty happily, except that I don't understand exactly how ordinary C file structures work and can't seem to find the bit I'm after.

If anyone can help either finding an existing C++ solution, or in suggesting how to extract the one in Crafty I would be very appreciative, as I would rather get on with the "interesting" part than be bogged down in generation again!
John Digsby
 
Posts: 10
Joined: 26 Jun 2006, 16:27

Re: Bitboard Engine

Postby Dann Corbit » 09 Apr 2008, 23:25

John Digsby wrote:Hi,

I'm trying something a little new for chess programming in terms of how to make the computer evaluate positions. I previously wrote a move generator based on representing the board as two arrays of squares occupied by black and squares occupied by white, accompanied by a bog-standard 64-length array. I precomputed all valid moves for every piece on every square of an empty board, and loaded them into memeory at initialisation.

All this, just to try to make my code somewhat less verbose and easier for me to maintain!

As I reached the evaluation routines, and wanted to start looking at structures like outposted knights, etc. I found I was writing very complex code, which would produce a bit of a bottleneck during the tree search.

I have since poked around the source of Crafty and the same evaluations that I'm having to write into 30-40 line functions for are represented by simple boolean ifs because of the bitboard representation.

I understand how this works, but didn't implement it because it was so complicated. Since I'm mostly interested in the evaluation end, I wondered if there was some C++ source for a chessboard class (or somesuch) that implemented bitboards. I would convert Crafty happily, except that I don't understand exactly how ordinary C file structures work and can't seem to find the bit I'm after.

If anyone can help either finding an existing C++ solution, or in suggesting how to extract the one in Crafty I would be very appreciative, as I would rather get on with the "interesting" part than be bogged down in generation again!


This is C and not C++, but it is well written and easy to understand:
http://www.prism.gatech.edu/~gtg365v/Buzz/

The author of Elephant has the most interesting C++ bitboard based move generator set I have ever seen. I guess he might let you have a copy if you asked him. He has implemented lots of fundamentally different bitboard techniques in his generators.

I think that the move generator for Adriano is very nice, and Olithink is a very good study also.

Any GPL C++ engine could be used as a base for your studies if your project is intended to be GPL as well.
Dann Corbit
 

Re: Bitboard Engine

Postby Tord Romstad » 10 Apr 2008, 15:59

John Digsby wrote:I understand how this works, but didn't implement it because it was so complicated. Since I'm mostly interested in the evaluation end, I wondered if there was some C++ source for a chessboard class (or somesuch) that implemented bitboards.


Glaurung uses bitboards, is written in C++, and has a chessboard class (called 'Position'). But I should warn you that it is quite a big and messy program (more compact than Crafty, though), and written in quite strange and unidiomatic C++.

Tord
User avatar
Tord Romstad
 
Posts: 639
Joined: 09 Oct 2004, 12:49
Location: Oslo, Norway

Re: Bitboard Engine

Postby Harald Lüßen » 10 Apr 2008, 19:24

Dann Corbit wrote:The author of Elephant has the most interesting C++ bitboard based move generator set I have ever seen. I guess he might let you have a copy if you asked him. He has implemented lots of fundamentally different bitboard techniques in his generators.

Thank you. Yes I sometimes give the code away to people who ask for it,
tell me their email address and accept 3 easy things:
- Do not distribute it to others.
- Do not steal too much.
- Make comments on bugs or highlights.

Yes there are a few interesting bitboard techniques in it. It is not so much
about the move generator but about the methods to get the attack-bitboards.
I tried and compared a dozen different methods, each in an exchangeable
source module. Examples are rotated bitboards or magic bitboards.
The rest of the program is typical C++ style. I am not so happy with
some of my classes and I wish I had the time to change them a little.
The code is easy to read and hopefully to understand. Too much #if macros
make some parts of the code look like a little battlefield.

On the other side I wrote the program to improve my C++ skills.
I tried to implement the classes and functions in a very clean and
well dokumented way. And the whole program is written in the same
good :-) style.

And Elephant has only about 2000 ELO points. Perhaps there are some bugs in it.

Harald
User avatar
Harald Lüßen
 
Posts: 29
Joined: 09 Oct 2004, 22:39

Re: Bitboard Engine

Postby Aleks Peshkov » 11 Apr 2008, 00:28

IMHO copy-paste design of any bitboard code implementation will not work, because data representation is a core of a chess engine, but not a common library.
Tord Romstad wrote:
John Digsby wrote:I understand how this works, but didn't implement it because it was so complicated. Since I'm mostly interested in the evaluation end, I wondered if there was some C++ source for a chessboard class (or somesuch) that implemented bitboards.

Glaurung uses bitboards, is written in C++, and has a chessboard class (called 'Position'). But I should warn you that it is quite a big and messy program (more compact than Crafty, though), and written in quite strange and unidiomatic C++.
Glaurung 1 and Glaurung 2 are both ones of the very best (and my first personal favorites) examples of chess code architecture, chess-domain decisions and naming conventions.

But I will not recommend particular Glaurung's bitboard implementation as a start point of a new program. I think classical Crafty old-fashioned C-code can be understood and refactored into simpler C++ with more sucess.
Aleks Peshkov
 
Posts: 27
Joined: 13 Jul 2007, 13:14

Re: Bitboard Engine

Postby John Digsby » 11 Apr 2008, 10:37

But I will not recommend particular Glaurung's bitboard implementation as a start point of a new program. I think classical Crafty old-fashioned C-code can be understood and refactored into simpler C++ with more sucess.


I'm happy to refactor code, but in Crafty I've just had trouble tracking down all the code that only deals with the board representation so that I can quickly refactor it and have a working solution after a brief round of debugging. Can you offer any pointers as to which of the source files actually contain this code, because I'm really in a tangle trying to navigate it!
John Digsby
 
Posts: 10
Joined: 26 Jun 2006, 16:27

Re: Bitboard Engine

Postby bob » 11 Apr 2008, 17:21

Here's a rough run-down:

init.c initializes the stuff, including the magic numbers and such, as well as initializing the chess board, hash signature, etc.

make.c/unmake.c update the board by making/retracting moves.

If you are going to use magic move generation, then movgen.c has the move generators for generating captures, non-captures, and legal moves only when escaping check.

That covers the chess board data structure itself...
User avatar
bob
 
Posts: 156
Joined: 10 May 2006, 17:59

Administration

Postby Volker Pittlik » 11 Apr 2008, 19:27

Hi John,

there is a problem with the e-mail address in your profile. I get an error message for every reply. Please update the mail address or disable to be notified for every reply.

Volker
User avatar
Volker Pittlik
 
Posts: 1031
Joined: 24 Sep 2004, 10:14
Location: Murten / Morat, Switzerland

Crafty very difficult

Postby John Digsby » 13 Apr 2008, 17:16

^^ Sorry about that - should be fixed now

I have spent a few hours today converting the code from Crafty into C++. Because I only wanted to make a chessboard class for representation and move generation, I have tried to be selective. I have, however, encountered a puzzler. The Bitboard is, as expected, an unsigned __int64. Yet, the following code appears:

Code: Select all
Bitboard temp;
...
Bitboard abit = temp & -temp;


This should always be 1, since the minus sign will do nothing to the unsigned variable. Or have I missed something :)

Crafty is very, very difficult to tear apart, but I have trouble understanding some of the other engines...oh dear - maybe I will have to write one from scratch after all :(
John Digsby
 
Posts: 10
Joined: 26 Jun 2006, 16:27

Re: Bitboard Engine

Postby delcacho » 13 Apr 2008, 18:23

It will be 1 only if the original variable is not 0 to start with.
User avatar
delcacho
 
Posts: 1
Joined: 25 Feb 2008, 15:59
Location: Bern, Switzerland

Re: Crafty very difficult

Postby Gerd Isenberg » 13 Apr 2008, 19:06

John Digsby wrote:
Code: Select all
Bitboard temp;
...
Bitboard abit = temp & -temp;


This should always be 1, since the minus sign will do nothing to the unsigned variable. Or have I missed something :)

Yes, unary minus applies the two's complement:
Code: Select all
-x == ~x + 1
which is guaranteed with unsigned types in C/C++.
-temp is equivalent to 0-temp, which also is the way to suppress compiler warnings. Thus, temp & -temp isolates the least significant one bit.
For bitboard basics have a look to the chess programming wiki:
http://chessprogramming.wikispaces.com/Bitboards
http://chessprogramming.wikispaces.com/ ... ions#toc79

Gerd
Gerd Isenberg
 
Posts: 285
Joined: 31 Jan 2005, 20:31
Location: Hattingen, Germany

Re: Crafty very difficult

Postby H.G.Muller » 14 Apr 2008, 09:07

John Digsby wrote:This should always be 1, since the minus sign will do nothing to the unsigned variable. Or have I missed something :)

Yes, you missed how arithmetic with unsigned variables is done. With the exception of right shifts, this is exactly the same as how it is done on signed variables. It is only that the result is interpreted differently when you use compares on them.

So for an unsigned byte x=1, -x would be equal to 255.

Plus you missed that the & operator is not a logical, but a bitwise AND. So the result of (tmp & tmp) is not always 1, but always tmp.
User avatar
H.G.Muller
 
Posts: 3453
Joined: 16 Nov 2005, 12:02
Location: Diemen, NL

Re: Crafty very difficult

Postby John Digsby » 14 Apr 2008, 13:06

Yes - I actually ran the code independently and now see how it works. Very intriguing - I think I was put off by the compiler warnings that resulted from this code.

I have decided that if I want a bitboard engine, I shall probably write one from scratch. It might take some time, but I think I generally understand how they work. What would people recommend, rotated or magic bitboards?
John Digsby
 
Posts: 10
Joined: 26 Jun 2006, 16:27

Re: Crafty very difficult

Postby Ilari Pihlajisto » 14 Apr 2008, 19:18

John Digsby wrote:What would people recommend, rotated or magic bitboards?


Magic is a lot easier to use, and gives pretty much the same performance as rotated bitboards. I just grabbed Pradu's magic bitboard source code (http://www.prism.gatech.edu/~gtg365v/Buzz/) and used it in Sloppy without even trying to understand how exactly the magic works.
User avatar
Ilari Pihlajisto
 
Posts: 78
Joined: 18 Jul 2005, 06:58

Re: Crafty very difficult

Postby Tord Romstad » 15 Apr 2008, 17:11

John Digsby wrote:What would people recommend, rotated or magic bitboards?


It doesn't really matter much, and whatever you choose, it is easy to change from one to the other after you have a working program. The main disadvantages of magic bitboards are that they consume rather much memory, that it takes slightly more work to implement them, and that unless you find a very efficient way to generate the "magic numbers", you'll probably have to clutter your code with some big and very ugly constant arrays. The main disadvantage of rotated bitboards is that they make it a little more awkward to generate X-ray attacks.

None of these disadvantages are likely to be very important in practice. Both techniques are fine.

Tord
User avatar
Tord Romstad
 
Posts: 639
Joined: 09 Oct 2004, 12:49
Location: Oslo, Norway

Re: Crafty very difficult

Postby John Digsby » 15 Apr 2008, 23:01

Thank you all very much - I have begun coding a rotated bitboard engine from scratch. I suspect there will be more questions, however... :)
John Digsby
 
Posts: 10
Joined: 26 Jun 2006, 16:27


Return to Programming and Technical Discussions

Who is online

Users browsing this forum: No registered users and 39 guests