Page 1 of 4

Position flipping

Posted: Mon Aug 05, 2013 8:08 am
by sje
A position flip is the action by which a position is replaced with a copy of itself modified such that each man is replaced with the corresponding man of the opposite color and then moved to the opposite side (front/back) of the board (file is same, but rank is flipped).

Other items must also be flipped: these include the color on the move, the castling status, and the en passant target square.

I notice that in Crafty, which has a flip command, there doesn't seem to be a flip of the en passant target. Am I missing something here?

Symbolic goes a bit further with its position flip. Not only are the board and the FEN scalars flipped, but also the entire move history and the saved state history. This means that it's possible to retract played moves in the flipped position all the way back to its flipped start. Also: flip(flip(P)) ≡ P

A position flip is a handy user command. It can also be used during opening book generation when (position, move) pair data are all stored using a White-to-move convention; this allows retrieval of "reversed" opening data.

Re: Position flipping

Posted: Mon Aug 05, 2013 9:09 am
by mcostalba
sje wrote:A position flip
This is another thing I am about to drop from Stockfish. The only reason while is still there is because it is from original Tord's code....but I found no use for it even once in the last years...

Re: Position flipping

Posted: Mon Aug 05, 2013 9:24 am
by michiguel
mcostalba wrote:
sje wrote:A position flip
This is another thing I am about to drop from Stockfish. The only reason while is still there is because it is from original Tord's code....but I found no use for it even once in the last years...
It is nice for debugging purposes. In debug code, it is good to flip the position, evaluate it, and make sure the score is the same but negative. I mirror it left-right if no castle is available. This process is done every time evaluation is called (in DEBUG mode only, of course) Over the years, I caught bugs very early in this way.

Miguel

Re: Position flipping

Posted: Mon Aug 05, 2013 9:47 am
by sje
michiguel wrote:It is nice for debugging purposes. In debug code, it is good to flip the position, evaluate it, and make sure the score is the same but negative. I mirror it left-right if no castle is available. This process is done every time evaluation is called (in DEBUG mode only, of course) Over the years, I caught bugs very early in this way.
It can also be used during debugging for verifying that perft(P, N) = perft(flip(P), N).

Re: Position flipping

Posted: Mon Aug 05, 2013 9:55 am
by mcostalba
sje wrote:
michiguel wrote:It is nice for debugging purposes. In debug code, it is good to flip the position, evaluate it, and make sure the score is the same but negative. I mirror it left-right if no castle is available. This process is done every time evaluation is called (in DEBUG mode only, of course) Over the years, I caught bugs very early in this way.
It can also be used during debugging for verifying that perft(P, N) = perft(flip(P), N).
This IMHO should be left to an external tool that reverses FEN strings (BTW do you know one?) more than the engine itself.

Re: Position flipping

Posted: Mon Aug 05, 2013 10:08 am
by michiguel
mcostalba wrote:
sje wrote:
michiguel wrote:It is nice for debugging purposes. In debug code, it is good to flip the position, evaluate it, and make sure the score is the same but negative. I mirror it left-right if no castle is available. This process is done every time evaluation is called (in DEBUG mode only, of course) Over the years, I caught bugs very early in this way.
It can also be used during debugging for verifying that perft(P, N) = perft(flip(P), N).
This IMHO should be left to an external tool that reverses FEN strings (BTW do you know one?) more than the engine itself.
In the case of the evaluation is not nearly as effective.

Miguel

Re: Position flipping

Posted: Mon Aug 05, 2013 12:34 pm
by mcostalba
Anyhow it is easier to flip with string manipulation (at least in C++) starting from the FEN representation of a position:

Code: Select all

static char toggle_case(char c) {
  return isupper(c) ? tolower(c) : toupper(c);
}

void Position::flip() {

  string f, token;
  std::stringstream ss(fen());

  for &#40;int i = 0; i < 8; i++)
  &#123;
      std&#58;&#58;getline&#40;ss, token, i < 7 ? '/' &#58; ' ');
      std&#58;&#58;transform&#40;token.begin&#40;), token.end&#40;), token.begin&#40;), toggle_case&#41;;
      f.insert&#40;0, token + &#40;i ? "/" &#58; " "));
  &#125;

  ss >> token; // Side to move
  f += &#40;token == "w" ? "b " &#58; "w ");

  ss >> token; // Castling flags
  std&#58;&#58;transform&#40;token.begin&#40;), token.end&#40;), token.begin&#40;), toggle_case&#41;;
  f += token + " ";

  ss >> token; // En-passant square
  f += &#40;token == "-" ? token &#58; token.replace&#40;1, 1, token&#91;1&#93; == '3' ? "6" &#58; "3"));

  std&#58;&#58;getline&#40;ss, token&#41;; // Full and half moves
  f += token;

  set&#40;f, is_chess960&#40;), this_thread&#40;));

  assert&#40;pos_is_ok&#40;));
&#125;

Re: Position flipping

Posted: Mon Aug 05, 2013 1:04 pm
by sje
michiguel wrote:In debug code, it is good to flip the position, evaluate it, and make sure the score is the same but negative. I mirror it left-right if no castle is available.
If there are no pawns on the board, you can also reflect the position about either main diagonal.

Re: Position flipping

Posted: Mon Aug 05, 2013 1:09 pm
by sje
mcostalba wrote:Anyhow it is easier to flip with string manipulation (at least in C++) starting from the FEN representation of a position:
That code doesn't flip any position history. Move retraction will not work, and neither will repetition detection.

Re: Position flipping

Posted: Mon Aug 05, 2013 1:16 pm
by mcostalba
sje wrote:
mcostalba wrote:Anyhow it is easier to flip with string manipulation (at least in C++) starting from the FEN representation of a position:
That code doesn't flip any position history. Move retraction will not work, and neither will repetition detection.
This was your previous (few hours ago) definition of flip:

Code: Select all

A position flip is the action by which a position is replaced with a copy of itself modified such that each man is replaced with the corresponding man of the opposite color and then moved to the opposite side &#40;front/back&#41; of the board &#40;file is same, but rank is flipped&#41;.

Other items must also be flipped&#58; these include the color on the move, the castling status, and the en passant target square. 
And this is also the definition most engines follow (if this makes any sense for you).

What I have written flips according to this definition.