genyrosk / gym-chess

A simple chess environment for openai/gym
MIT License
148 stars 38 forks source link

There is Problem with Promotion move #3

Closed Himanshu032000 closed 3 years ago

Himanshu032000 commented 4 years ago

Recently I am trying to implement AlphaZero for chess using your chess environment but during the training i found one flaw which is for ex. whenever white pawn is in 7th row it directly gets promoted to Queen and also one more move is taken at the same time.

I have tried to find the bug manually but i can't and also i have added 2 new features to your environment (1) it can take standard notation (san) as input and take a move when user is playing and (2) board can be set to any state at any time if you want it i can send it to you

genyrosk commented 3 years ago

Apologies for the absence 🙏

New version released called v1, check it out and let me know if you still have this problem

AbhijeetKrishnan commented 3 years ago

This issue persists with ChessEnvV2. Here's a MWE -

import gym_chess
import numpy as np

basic_board = np.array([[0] * 8] * 8, dtype=np.int8)
basic_board[1, 3] = 6 # white pawn on d7
env =  gym_chess.ChessEnvV2(opponent="none", initial_board=basic_board)
env.possible_moves

This returns [((1, 3), (0, 3))] as the possible moves, which is a single move (d8=Q) assumed to be a queen promotion (see here). However, we would want the underpromotions to knight, bishop and rook also represented, which is not possible in the current move representation.

genyrosk commented 3 years ago

@AbhijeetKrishnan is there a reason why you would want to have it present other than because it's possible ? The number of situations where underpromoting the pawn makes for a better move exists in extremely niche situations, typically to avoid stalemate.

c.f. discussion on chess.com: Promoting pawns - but not to a queen

In terms of development cost, it would require coming up with a new type of move in the code, a Promotion type move. I'm open to suggestions / ideas as to how to implement it in a nifty manner.

AbhijeetKrishnan commented 3 years ago

@genyrosk A chess environment should attempt to implement all the rules of chess. At the very least, any deviations should be documented (en-passant not being implemented is documented, but the inability to do underpromotions is not).

The issue is that the current move representation is something like (from, to, is_castle, castle_info). This does not allow for underpromotions since all promotions will have the same from and to squares.

Your solution of having a Promotion type move seems to be the simplest. Create a Promotion move with the promoting pawn from, to squares, and the promo_piece it is being promoted to. A number of other things would have to be updated, like the MoveEnum, pawn move calculations etc.