alpha-zero-general icon indicating copy to clipboard operation
alpha-zero-general copied to clipboard

Chess, possible moves, how would you experts go about it?

Open JernejHenigman opened this issue 3 years ago • 3 comments

Decided to implement game of AntiChess (https://lichess.org/variant/antichess) into alpha-zero-general framework. Essentially I am not sure about one thing:

def getActionSize(self): # return number of actions return ?

What should method getActionSize(self) return? You see in connect4, it is trivial to understand that there are always n possible moves at max (if all columns still have a room for stone), so in that case the return value of getActionSize methoud would be n, where n is size of n*n grid.

But what is the upper limit of possible moves in chess? (or AntiChess for that matter). In chess we have following pieces: pawn, knight, bishop, rook, queen and a king. Do we need to count every possible move for every piece from every possible square on the board that piece has a right to move to, to arrive to desired number for upper limit of getActionSize() method?

My thinking is the following: number_of_actions = PawnMoves + KnightMoves + BishopMoves + RookMoves + QueenMoves + KingMoves.

PawnMoves:

  • from starting positions 2 possible moves --> either move 1 square forward or 2 squares forward (16 pawns * 2 = 32 possible moves)
  • from none starting position, 1 possible move --> move 1 square forward (16 pawns * 1 * 5 = 80, 5 is the number of ranks in chess that pawn can occupy without starting position, so move pawn moves from e5 to e6 is different move than pawn moves from e6 to e7)
  • pawn captures, pawn can capture in 2 possible ways (actually in 3, because there is en passant move) --> pawn can capture pieces diagonally forward so 2 possible capture moves, except for the edge pawns, where pawn can capture only in 1 possible way. so possible pawn captures without en passant moves are --> 14 * 6 * 2 + 2 * 6 * 1 = 180,
  • so we have now total of 32 + 80 +180 = 292 possible pawn moves in the game of chess

Next, do the same possible count for other pieces, sum all together and whatever sum we get this is our desired limit for possible moves in game of chess?

I saw that in the game of tafl for instance, this method return self.n**4, what is explanation behind that?

JernejHenigman avatar May 25 '21 13:05 JernejHenigman

The action size depends largely on how you define an action for your game programmatically. Typically, for games where you place pieces, people have defined actions by the location the piece is placed in. For games where you move pieces, people have defined an action by (a pair) where the piece was, and where it is moved to.

That's why for Connect4 there are n=boardwidth many actions,

for TicTactToe there are n**2 actions,

and for TAFL there are n**4 = (n**2 many board spaces for the piece to be in) x (n**2 many board spaces for the piece to be moved to).

If you look at TaflLogic: https://github.com/suragnair/alpha-zero-general/blob/f0f1106b93cb74364233a65dbb8d2b85c9c88608/tafl/TaflLogic.py#L88-L113

Or at SantoriniLogic: https://github.com/suragnair/alpha-zero-general/blob/f0f1106b93cb74364233a65dbb8d2b85c9c88608/santorini/SantoriniLogic.py#L157-L168

You'll see there's typically a lot of code that specifies which moves are actually legal given the board-state, and that information is used to mask illegal moves in the actions vector.

rlronan avatar Jun 09 '21 17:06 rlronan

https://github.com/goshawk22/alpha-zero-chess seems use pip python-chess

gms2009 avatar Sep 11 '21 22:09 gms2009

@rlronan Hi, did your training in Santorini yield any results? I want to train the Santorini. But I don't know is my hardware strong. I just have one rtx2080 GPU and i7 4 cores CPU.

coder-free avatar Jan 05 '22 15:01 coder-free