Illegal positions

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

Moderator: Andres Valverde

Illegal positions

Postby Peter Fendrich » 04 Oct 2005, 22:26

What do you think is the best way of handling obvious illegal positions?
(I'm talking about UCI here and positions that can't be played with normal chess rules)
In Arena it is possible to feed the engine with all sorts of positions. For instance the "wrong" side is in check or one King is missing etc.
Now, if you just ignore it or just give an error message, there will follow a "go" with unexpected and not meaningful results.
If you exit the engine, Arena doesn't know that the engine is out and has to be reloaded before it can be used again.

I do both in Alaric depending on situation but none of the alternatives are very good from a user perspective.
I would like a command from the engine to the GUI with the meaning "Illegal position, I'm out".
We can of course blame the GUI that let such positions pass but...

What do you do in these cases?

/Peter
User avatar
Peter Fendrich
 
Posts: 193
Joined: 26 Sep 2004, 20:28
Location: Sweden

Re: Illegal positions

Postby Dann Corbit » 04 Oct 2005, 22:38

queencount <= 9 for each side
rookcount bishopcount and knightcount <= 10 for each side
pawncount <= 8 for each side
No pawns on rank 1 or 8
Exactly one king
At most, one side in check
Side not on the move not in check
Exactly 7 row separators in FEN/EPD
Count of all chessmen of a given color <= 16
Active color == 'w' or == 'b'
0 or 1 black king in castling rights
0 or 1 white king in castling rights
0 or 1 black queen in castling rights
0 or 1 white queen in castling rights
no other symbols in castling rights
e.p. file 1-8
e.p. rank 3 or 6
half-move clock between 0 and 99 strictly
full move number between 1 and 6000
Dann Corbit
 

Re: Illegal positions

Postby Dann Corbit » 04 Oct 2005, 22:41

Here is an EPD/FEN checker that I wrote some time ago:
http://cap.connx.com/chess-engines/new- ... fenepd.cpp

Uri Blass has made some important improvements to it.
If you ask him, he may send you a copy.
Dann Corbit
 

Re: Illegal positions

Postby Peter Fendrich » 05 Oct 2005, 00:36

Thanks Dann,
but that wasn't exactly what I asked for...

When you recognise an illegal position how do you handle it?
I found no good solution to it. My two alternatives both sucks.

/Peter
User avatar
Peter Fendrich
 
Posts: 193
Joined: 26 Sep 2004, 20:28
Location: Sweden

Re: Illegal positions

Postby Alessandro Scotti » 05 Oct 2005, 00:38

Dann Corbit wrote:queencount <= 9 for each side
rookcount bishopcount and knightcount <= 10 for each side
pawncount <= 8 for each side


Sum of pieces obtained by promotion should not exceed that of missing pawns!

Dann Corbit wrote:no other symbols in castling rights


I also accept "a-h" files for FRC, but they must match the rook positions.

Dann Corbit wrote:e.p. file 1-8
e.p. rank 3 or 6


Also square "in front of" e.p. contains a pawn of the proper color.

Peter Fendrich wrote:We can of course blame the GUI that let such positions pass but...

What do you do in these cases?


On a illegal position, I log an error and ignore it (I don't like terminating the engine for this reason). Oh... and I blame the GUI of course! :-)
User avatar
Alessandro Scotti
 
Posts: 306
Joined: 20 Nov 2004, 00:10
Location: Rome, Italy

Alaric NAQs (off-topic)

Postby Alessandro Scotti » 05 Oct 2005, 00:44

Hi Peter,
BTW I happened to click on your WWW button and found the NAQ page on your Alaric site... that's quite interesting! :-)
If a question is never asked then it belongs to the NAQ list, but then you list and answer it so it must be removed from the list. Then, it goes back to the "real" NAQs, and you should include it in the list again... hm! :shock:
User avatar
Alessandro Scotti
 
Posts: 306
Joined: 20 Nov 2004, 00:10
Location: Rome, Italy

Re: Illegal positions

Postby Pradu » 05 Oct 2005, 07:32

Here's how I handle illegal positions.
Code: Select all
/*isIllegalPosition()
 *Determines whether the position is legal.
 *The position can be illegal, but it must be legal enough for the engine to
 *work without errors
 *
 *@returns true if the position is illegal
 */
bool isIllegalPosition()
{
   //check if the number of Kings on each side is ok
   if(popCount(piecesSide(mainboard,K))!=1 || popCount(piecesXSide(mainboard,K))!=1)
   {
      if(!openedInConsole)
         printf("tellusererror Illegal Position: Wrong number of kings.\n");
      return true;
   }
   //make sure the opposing side is not in check
   if(inCheck(mainboard,mainboard.xside))
   {
      if(!openedInConsole)
         printf("tellusererror Illegal Position: Opposite side in Check.\n");
      return true;
   }
   //make sure the ep square matches with an ep pawn
   if(mainboard.EP)
   {
      //if white just moved
      if(mainboard.side && !((mainboard.EP>>8)&(mainboard.PiecesSide[WHITE]&mainboard.Pieces[P])))
      {
         if(!openedInConsole)
            printf("tellusererror Illegal Position: Bad E.P. square\n");
         return true;
      }
      //if black just moved
      if(!((mainboard.EP<<8)&(mainboard.PiecesSide[BLACK]&mainboard.Pieces[P])))
      {
         if(!openedInConsole)
            printf("tellusererror Illegal Position: Bad E.P. square\n");
         return true;
      }
   }
   //edit castling legality if the king and rook positions are to the contrary
   if(hasCastlingPrivilages(mainboard,WHITE))
   {
      if(!(mainboard.Pieces[K]&mainboard.PiecesSide[WHITE]&toBit[4]))
         mainboard.castling&=notAllCastlingBitsSide[WHITE];
      else
      {
         if((mainboard.castling&WK) && !(mainboard.Pieces[R]&mainboard.PiecesSide[WHITE]&toBit[7]))
            mainboard.castling&=notCastlingBits[WHITE][KINGSIDE];
         if((mainboard.castling&WQ) && !(mainboard.Pieces[R]&mainboard.PiecesSide[WHITE]&toBit[0]))
            mainboard.castling&=notCastlingBits[WHITE][QUEENSIDE];
      }
   }
   if(hasCastlingPrivilages(mainboard,BLACK))
   {
      if(!(mainboard.Pieces[K]&mainboard.PiecesSide[BLACK]&toBit[56+4]))
         mainboard.castling&=notAllCastlingBitsSide[BLACK];
      else
      {
         if((mainboard.castling&BK) && !(mainboard.Pieces[R]&mainboard.PiecesSide[BLACK]&toBit[56+7]))
            mainboard.castling&=notCastlingBits[BLACK][KINGSIDE];
         if((mainboard.castling&BQ) && !(mainboard.Pieces[R]&mainboard.PiecesSide[BLACK]&toBit[56+0]))
            mainboard.castling&=notCastlingBits[BLACK][QUEENSIDE];
      }
   }
   return false;
}
User avatar
Pradu
 
Posts: 343
Joined: 12 Jan 2005, 19:17
Location: Chandler, Arizona, USA

Re: Illegal positions

Postby Fabien Letouzey » 05 Oct 2005, 09:31

Hi Peter,

Peter Fendrich wrote:In Arena it is possible to feed the engine with all sorts of positions. For instance the "wrong" side is in check or one King is missing etc.

Sending a bug report to the Arena team would be a useful step in any case. UCI interfaces are not allowed to send illegal positions to engines.

Fabien.
Fabien Letouzey
 
Posts: 110
Joined: 03 Dec 2004, 10:17
Location: France

Re: Illegal positions

Postby Uri Blass » 05 Oct 2005, 10:22

Fabien Letouzey wrote:Hi Peter,

Peter Fendrich wrote:In Arena it is possible to feed the engine with all sorts of positions. For instance the "wrong" side is in check or one King is missing etc.

Sending a bug report to the Arena team would be a useful step in any case. UCI interfaces are not allowed to send illegal positions to engines.

Fabien.


You can avoid some types of illegal positions(like wrong side in check or one king is missing) but
I doubt if there are programmers that are good enough to avoid all illegal positions.

There are a lot of types of illegal positions:

I will mention only some of them:
1)king is attacked from more than 2 directions
2)king is attacked from 2 bishops directions
3)king is attacked by 2 knights.
4)black bishop at d8 and black pawns at e7 c7
5)king that is attacked by 2 rooks when no rook is on a square of promoted pawn(note that capture that promote to a root may make double check by 2 rooks so I cannot say generally king that is attacked by 2 rooks).

6)cases when you can prove by pawn structure that the position is illegal because it is possible to prove based on the pawn structure that the sum of the number of pieces in the board and the number of captured pieces of one side is more than 16.

Uri
User avatar
Uri Blass
 
Posts: 727
Joined: 09 Oct 2004, 05:59
Location: Tel-Aviv

Wrong question being answered, guys - except Fabien

Postby Anonymous » 05 Oct 2005, 11:28

The OP was asking what can he do in Arena when his program is given an illegal position.

Y'all mostly answered how to test if a position is legal or not. Which is just not the same thing, really. Very interesting, for sure, but not the question at hand.

My question to Peter would be "do you want your engine to deal with the legality of the position, or do you want it to be handled only by the GUI?

Sounds like you want the GUI to handle that. If so, then I'd follow Fabien's advice, and try to get Arena adjusted to handle illegal positions before they ever get to your engine.

Squeaky wheel gets the grease. :D

Dave
Anonymous
 

Re: Illegal positions

Postby Pradu » 05 Oct 2005, 14:47

For the winboard protocol, you must expect illegal positions sent from the GUI. You just send back an error message when that happens. As Uri said, it is quite difficult to test every illegal position. My policy here is to just test if the position is good enough for normal engine operation. Why limit what your engine can do by constraining the isIllegal() more than you need to? For instance, some puzzles in chess are illegal positions but some people use chess engines to solve them.

David Mitchell wrote:Y'all mostly answered how to test if a position is legal or not. Which is just not the same thing, really. Very interesting, for sure, but not the question at hand.


IMHO, they are very much related if you assume the testing is not the GUI's problem.

Of course he was talking about UCI here but my above statement might be useful to winboard users. I'm not sure how Polyglot works, but if it just translates Winboard commands directly then the UCI engine must expect illegal positions.

I don't know much about UCI but is there no error command defined in it? I'm not sure if you can use TellGUI("copyprotection error\n"); or something of the sort, hopefully more replys about this here.

By the way is there a definition anywhere of what UCI concider's an illegal position?
User avatar
Pradu
 
Posts: 343
Joined: 12 Jan 2005, 19:17
Location: Chandler, Arizona, USA

Re: Illegal positions

Postby Dann Corbit » 05 Oct 2005, 16:07

Sometimes, illegal positions are wanted.

It is fun to solve positions that can never occur in a real game.
http://membres.lycos.fr/albillo/

I think an engine should know what it can handle and what it can't and this discussion shows a hole in both the Winboard and UCI protocol for setboard type of operations.

The interface should look for an "illegal" response from the engine.

Also, the variants might be expanded. If your program can handle 64 chessmen or expanded boards or other crazy things there should be a way to communicate that also.
Dann Corbit
 

Proposed Extension

Postby Dann Corbit » 05 Oct 2005, 16:12

Proposed extension (engine response):

MAXQUEENS=N0
MAXKINGS=N1
MAXROOKS=N2
MAXBISHOPS=N3
MAXKNIGHTS=N4
MAXPAWNS=N5

Where (N0-N5) are the maximum number of any single color of chessman your engine can handle.

So if you can allow 64 white kings or black kings, then N1=64.

MAXROWS=N6
MAXCOLS=N7

by default N6 and N7 are 8

Also a boolean:
ACCEPTS_ILLEGAL=t/f

So if you want to accept a position where 7 knights attack the king, then fine.
Dann Corbit
 

Re: Illegal positions

Postby Sven Schüle » 05 Oct 2005, 17:52

Dann Corbit wrote:Sometimes, illegal positions are wanted.

It is fun to solve positions that can never occur in a real game.
http://membres.lycos.fr/albillo/

I think an engine should know what it can handle and what it can't and this discussion shows a hole in both the Winboard and UCI protocol for setboard type of operations.

The interface should look for an "illegal" response from the engine.

Also, the variants might be expanded. If your program can handle 64 chessmen or expanded boards or other crazy things there should be a way to communicate that also.

Hi,

the WB protocol proposes to send a "tellusererror ..." message back to the GUI and to respond to any further moves with "Illegal move". As far as I understand the UCI spec the UCI protocol does not consider such error responses from the engine. Obviously this was the background of Peter's original post.

Extending the (UCI) protocol may be a solution. But first we should describe what we are really talking about. What is an "illegal position" (in standard chess)?

Some possible answers, not necessarily excluding each other:

a) (always "obvious") a position that does not allow playing a game according to the chess rules; examples: wrong king in check, wrong number of kings;

b) (depending on amount of "knowledge") a position for which it is not possible to determine any legal move leading directly to that position provided the predecessor position was not illegal in terms of case a) above; examples: triple check, some double checks (see Uri's posting);

c) a position that can't be reached by a sequence of legal moves from the starting position due to one of the following "static" reasons:
- wrong number of pieces of some type and colour (except kings), also taking into account possible number of promotions
- wrong total number of pieces of some colour
- pieces on squares where pieces of that type can never get to (i.e., pawns on rank 1 or 8)

d) a position that can't be reached by a sequence of legal moves from the starting position for _any_ reason, including cases which can only be detected by retro analysis or by using comparable knowledge. This is the most general answer because its formal specification is simple, but it can't be realized perfectly by an engine with current technology.

We might specify a common set of criteria for illegal position detection in standard chess. IMHO the cases a) and c) would definitely be part of it, while b) is a bit difficult because there may be many cases and you don't know when you are done with it.

Dann's proposal of some options helps to allow deviations from the standard, useful for chess variants, testing purposes, or just fun. But at least there should be some commonly accepted standard for the default scenario, in order to avoid having to set options for some engines to accept a position while others accept it immediately.

Just my 0,02?!

Sven
User avatar
Sven Schüle
 
Posts: 240
Joined: 26 Sep 2004, 20:19
Location: Berlin, Germany

Re: Illegal positions

Postby Anonymous » 05 Oct 2005, 19:05

Peter Fendrich wrote:What do you think is the best way of handling obvious illegal positions?
(I'm talking about UCI here and positions that can't be played with normal chess rules)


IMHO, the best way of handling this, is to exit. Perhaps after sending some "info string Illegal Position" or similar first. When the position is illegal, but the engine can handle it (say wPa2,a3,b2), one might want to just ignore.

Regards,
Dieter
Anonymous
 


Return to Programming and Technical Discussions

Who is online

Users browsing this forum: No registered users and 42 guests