Editing tscp

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

Editing tscp

Post by sje »

I've been editing my copy of tscp and after a few more rounds I'll ask its author for permission to distribute the modified source. None of my edits will change its play; they are instead intended to improve readability and to better support testing.

----

I've added a perft command to help ensure that my other edits haven't disrupted the original mechanics.

Code: Select all

static unsigned int count_legal_moves()
{
  unsigned int count = 0;
  unsigned int base = first_move[ply];
  unsigned int limit = first_move[ply + 1] - base;
  unsigned int index;

  for &#40;index = 0; index < limit; index++)
  &#123;
    if &#40;makemove&#40;gen_dat&#91;base + index&#93;.m.b&#41;)
    &#123;
      count++;
      takeback&#40;);
    &#125;;
  &#125;;
  return count;
&#125;

static unsigned long int perft_aux&#40;unsigned int draft&#41;
&#123;
  unsigned long sum;
  
  if &#40;draft == 0&#41;
    sum = 1;
  else
  &#123;
    if &#40;ply > 0&#41;
      gen&#40;);
    if &#40;draft == 1&#41;
      sum = &#40;unsigned long int&#41; count_legal_moves&#40;);
    else
    &#123;
      unsigned int newdraft = draft - 1;
      unsigned int base = first_move&#91;ply&#93;;
      unsigned int limit = first_move&#91;ply + 1&#93; - base;
      unsigned int index;

      sum = 0;
      for &#40;index = 0; index < limit; index++)
      &#123;
        if &#40;makemove&#40;gen_dat&#91;base + index&#93;.m.b&#41;)
        &#123;
          sum += perft_aux&#40;newdraft&#41;;
          takeback&#40;);
        &#125;;
      &#125;;
    &#125;;
  &#125;;
  return sum;
&#125;

void perft&#40;unsigned int draft&#41;
&#123;
  unsigned long int sum;
  int t0 = get_ms&#40;), t1;
  double freq;
  
  sum = perft_aux&#40;draft&#41;;
  t1 = get_ms&#40;);
  freq = &#40;double&#41; sum / &#40;double&#41; &#40;t1 - t0&#41; * 1.0e3;
  printf&#40;"perft&#40;%d&#41;&#58; %lu   %.3lf MHz\n", draft, sum, &#40;freq / 1.0e6&#41;);
&#125;
Sample calculations, all correct:

Code: Select all

tscp> perft 0
perft&#40;0&#41;&#58; 1   0.001 MHz
tscp> perft 1
perft&#40;1&#41;&#58; 20   inf MHz
tscp> perft 2
perft&#40;2&#41;&#58; 400   inf MHz
tscp> perft 3
perft&#40;3&#41;&#58; 8902   0.742 MHz
tscp> perft 4
perft&#40;4&#41;&#58; 197281   1.273 MHz
tscp> perft 5
perft&#40;5&#41;&#58; 4865609   1.132 MHz
tscp> perft 6
perft&#40;6&#41;&#58; 119060324   1.237 MHz
tscp> e2e4
tscp> e7e5
tscp> perft 4
perft&#40;4&#41;&#58; 728887   1.225 MHz
tscp> perft 5
perft&#40;5&#41;&#58; 22273312   1.092 MHz
tscp> perft 6
perft&#40;6&#41;&#58; 673070116   1.195 MHz
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

Re: Editing tscp

Post by sje »

The Readability Improvement Campaign continues with only the eval.c file mostly untouched.

What I have not been doing is to change any of the logic. I am also not changing any of the internal representation, other than to give the empty color its own value instead of sharing one with the empty piece. I feel that non-trivial changes would be against the spirit of tscp which is to remain a simple program.

What I might do is to add a few color-indexed constant value vectors, mostly dealing with directional constants used for pawn motion. This would fold by two nearly all of the color-specific code in generation, search, and evaluation. The motivation here is that less code is likely to be easier to understand.

I am considering two minor logic changes which will significantly speed the search without changing the result of a search. The first of these is to track the locations of the kings (speeding up check determination) and providing incremental hash signature updating (speeding up move execution).

Another idea is to add FEN I/O capability; at present there's no easy way to set up an arbitrary position. A further possibility is to provide SAN I/O to replace coordinate move notation, although this would entail a slight modification of the move representation structure.

----

Running on a Raspberry Pi model B 2:

Code: Select all

tscp> perft 0
perft&#40;0&#41;&#58; 1   inf MHz
tscp> perft 1
perft&#40;1&#41;&#58; 20   inf MHz
tscp> perft 2
perft&#40;2&#41;&#58; 400   0.200 MHz
tscp> perft 3
perft&#40;3&#41;&#58; 8902   0.144 MHz
tscp> perft 4
perft&#40;4&#41;&#58; 197281   0.225 MHz
tscp> perft 5
perft&#40;5&#41;&#58; 4865609   0.210 MHz
tscp> perft 6
perft&#40;6&#41;&#58; 119060324   0.229 MHz
tscp> bench

8  . r b . . r k .
7  p . . . . p p p
6  . p . q p . n .
5  . . . n . . N .
4  . . p P . . . .
3  . . P . . . P .
2  P P Q . . P B P
1  R . B . R . K .

   a b c d e f g h

ply      nodes  score  pv
  1        130     20  c1e3
  2       3441      5  g5e4 d6c7
  3       8911     30  g5e4 d6c7 c1e3
  4     141367     10  g5e4 d6c7 c1e3 c8d7
  5     550778     26  c2a4 d6c7 g2d5 e6d5 c1e3
Time&#58; 5490 ms
ply      nodes  score  pv
  1        130     20  c1e3
  2       3441      5  g5e4 d6c7
  3       8911     30  g5e4 d6c7 c1e3
  4     141367     10  g5e4 d6c7 c1e3 c8d7
  5     550778     26  c2a4 d6c7 g2d5 e6d5 c1e3
Time&#58; 5445 ms
ply      nodes  score  pv
  1        130     20  c1e3
  2       3441      5  g5e4 d6c7
  3       8911     30  g5e4 d6c7 c1e3
  4     141367     10  g5e4 d6c7 c1e3 c8d7
  5     550778     26  c2a4 d6c7 g2d5 e6d5 c1e3
Time&#58; 5445 ms

Nodes&#58; 550778
Best time&#58; 5445 ms
Nodes per second&#58; 101152 &#40;Score&#58; 0.416&#41;
PK
Posts: 893
Joined: Mon Jan 15, 2007 11:23 am
Location: Warsza

Re: Editing tscp

Post by PK »

You can replace manually defined "useful squares" with:

Code: Select all

// square numbers 0..63, then size of the board
enum esqare &#123;
	A8, B8, C8, D8, E8, F8, G8, H8, 
	A7, B7, C7, D7, E7, F7, G7, H7, 
	A6, B6, C6, D6, E6, F6, G6, H6, 
	A5, B5, C5, D5, E5, F5, G5, H5, 
	A4, B4, C4, D4, E4, F4, G4, H4, 
	A3, B3, C3, D3, E3, F3, G3, H3, 
	A2, B2, C2, D2, E2, F2, G2, H2, 
	A1, B1, C1, D1, E1, F1, G1, H1,
	BOARD_SIZE
&#125;;
It's kind of a comment disguised as a piece of code, that facilitates understanding of board representation.
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

Re: Editing tscp

Post by sje »

There are several places where enumeration types would be appropriate, but for some reason the author didn't use them.

For the moment, I have deferred adding enumerations. But it would be easy to do so. Current source:

Code: Select all

/* Chessboard files */

#define FileA 0
#define FileB 1
#define FileC 2
#define FileD 3
#define FileE 4
#define FileF 5
#define FileG 6
#define FileH 7

#define FileLen 8

/* Chessboard ranks */

#define Rank1 7
#define Rank2 6
#define Rank3 5
#define Rank4 4
#define Rank5 3
#define Rank6 2
#define Rank7 1
#define Rank8 0

#define RankLen 8

/* Square generation */

#define MapToSq&#40;file, rank&#41; (&#40;file&#41; + &#40;FileLen * &#40;rank&#41;))

/* Squares */

#define SqNil (-1&#41;

#define SqA1 MapToSq&#40;FileA, Rank1&#41;
#define SqB1 MapToSq&#40;FileB, Rank1&#41;
#define SqC1 MapToSq&#40;FileC, Rank1&#41;
#define SqD1 MapToSq&#40;FileD, Rank1&#41;
#define SqE1 MapToSq&#40;FileE, Rank1&#41;
#define SqF1 MapToSq&#40;FileF, Rank1&#41;
#define SqG1 MapToSq&#40;FileG, Rank1&#41;
#define SqH1 MapToSq&#40;FileH, Rank1&#41;

#define SqA8 MapToSq&#40;FileA, Rank8&#41;
#define SqB8 MapToSq&#40;FileB, Rank8&#41;
#define SqC8 MapToSq&#40;FileC, Rank8&#41;
#define SqD8 MapToSq&#40;FileD, Rank8&#41;
#define SqE8 MapToSq&#40;FileE, Rank8&#41;
#define SqF8 MapToSq&#40;FileF, Rank8&#41;
#define SqG8 MapToSq&#40;FileG, Rank8&#41;
#define SqH8 MapToSq&#40;FileH, Rank8&#41;

#define SqLen &#40;FileLen * RankLen&#41;

/* File and rank extraction */

#define MapToFile&#40;sq&#41; (&#40;sq&#41; & 7&#41;
#define MapToRank&#40;sq&#41; (&#40;sq&#41; >> 3&#41;
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

Re: Editing tscp

Post by sje »

I've emailed a copy of my edited version of the tscp source to Tom and have asked for his permission for distribution. If he agrees, then I might add some code for SAN and FEN capability.

----

Symbolic and tscp are now engaged in another 1,000 game match. The current score is 204 W / 55 L / 37 D (+192 elo). Here's a win by tscp:
[pgn][Event "XBoard Event"]
[Site "gail"]
[Date "2015-09-23"]
[Round "6"]
[White "Symbolic 2015-09-23"]
[Black "tscp"]
[Result "0-1"]
[FinalFEN "4N3/8/7p/2p1k1pP/8/8/3r1K2/1q6 w - - 3 66"]
[Termination "White resigns"]

1. d4 d5 2. c4 e6 3. Nf3 Nf6 4. Bg5 Bb4+ 5. Nc3 Nc6 6. e3 Bxc3+ 7. bxc3 O-O 8. cxd5 exd5 9. Qb3 Re8 10. Bd3 a5 11. O-O a4 12.
Bxf6 Qxf6 13. Qxd5 Ra5 14. Qc4 Be6 15. d5 Bxd5 16. Qh4 Qxh4 17. Nxh4 Rc5 18. Rfc1 Ne5 19. Bf1 Bc6 20. Be2 Be4 21. f4 Nd3 22.
Bxd3 Bxd3 23. Kf2 Be4 24. Nf3 Rh5 25. h3 Rb5 26. Re1 Rb2+ 27. Re2 Rb6 28. Rd1 Rc6 29. Ng5 Bg6 30. Rc1 f6 31. Nf3 Rce6 32. Rd1
Be4 33. Rd7 Rc6 34. Nd4 Rc5 35. Rd2 Bd5 36. g4 a3 37. Nf5 Bf7 38. R2d3 Kf8 39. Rxf7+ Kxf7 40. Rd7+ Kf8 41. Nxg7 Re7 42. Rxe7
Kxe7 43. Nf5+ Kf7 44. Nd4 Rxc3 45. Ke2 c5 46. Nb5 Rc2+ 47. Kd3 Rxa2 48. Ke4 Kg6 49. h4 h6 50. h5+ Kg7 51. Kf5 Ra1 52. Ke4 a2
53. Nc3 b5 54. Nxa2 Rxa2 55. f5 Rd2 56. Kf4 b4 57. e4 b3 58. Ke3 Rd4 59. e5 b2 60. g5 fxg5 61. f6+ Kf7 62. e6+ Kxf6 63. e7 b1=Q
64. e8=N+ Ke5 65. Kf2 Rd2+ 0-1[/pgn]
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

Another match

Post by sje »

I Installed transposition table scores and bound adjustments then restarted the match. So far, the score is 10-1-1 (+338 elo).

A win by tscp:
[pgn][Event "XBoard Event"]
[Site "gail"]
[Date "2015-09-24"]
[Round "5"]
[White "Symbolic 2015-09-24"]
[Black "tscp"]
[Result "0-1"]
[FinalFEN "8/8/8/2K3k1/8/2q5/8/5q2 w - - 2 70"]
[Termination "White resigns"]

1. d4 e6 2. g3 Nc6 3. Nf3 Bb4+ 4. Bd2 Bxd2+ 5. Qxd2 d5 6. Bh3 Nf6 7. Nc3 Ne4 8. Qe3 Nb4 9. Kd1 Nxc3+ 10. Qxc3 Nc6 11. b4 Bd7
12. b5 Ne7 13. a4 a6 14. Rb1 axb5 15. axb5 O-O 16. Kd2 Nc8 17. Ne5 Nd6 18. Qd3 Qe8 19. Nxd7 Qxd7 20. Rb3 Nc4+ 21. Kc3 Ra5 22.
Rhb1 Rfa8 23. Bg2 f5 24. Rb4 Ra2 25. Rxc4 R2a3+ 26. Rb3 dxc4 27. Qxc4 Rxb3+ 28. Qxb3 Ra5 29. Kc4 c6 30. bxc6 bxc6 31. Qb6 Ra3
32. Bxc6 Qc8 33. Qb7 Qxb7 34. Bxb7 Kf7 35. Bf3 Kf6 36. c3 Ra1 37. Kd3 Rd1+ 38. Ke3 e5 39. Bd5 Rc1 40. Kd2 Rf1 41. Ke3 g5 42. h4
gxh4 43. gxh4 f4+ 44. Kf3 Rh1 45. Ke4 exd4 46. cxd4 Rxh4 47. f3 h5 48. Bc4 Kg5 49. d5 Rh1 50. Ke5 Rb1 51. d6 Rb7 52. Ke6 h4 53.
Ba6 Ra7 54. Bb7 Rxb7 55. Kd5 h3 56. Kc6 Rb8 57. d7 h2 58. Kc7 Rh8 59. d8=Q+ Rxd8 60. Kxd8 h1=Q 61. Kd7 Qd1+ 62. Kc6 Qxe2 63.
Kd5 Qxf3+ 64. Kc4 Qd1 65. Kc3 f3 66. Kb4 Qd2+ 67. Kc4 f2 68. Kb5 f1=Q+ 69. Kc5 Qc3+ 0-10[/pgn]
User avatar
Rebel
Posts: 6991
Joined: Thu Aug 18, 2011 12:04 pm

Re: Another match

Post by Rebel »

sje wrote:I Installed transposition table scores and bound adjustments then restarted the match. So far, the score is 10-1-1 (+338 elo).
Great!
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

Re: Another match

Post by sje »

Rebel wrote:
sje wrote:I Installed transposition table scores and bound adjustments then restarted the match. So far, the score is 10-1-1 (+338 elo).
Great!
Well, it's gone down a bit: 168-30-23 (+254 elo). Symbolic is hampered somewhat because of not having an SSD for its tablebase files; my only portable SSD is connected to the machine playing on FICS and ICC.
jdart
Posts: 4366
Joined: Fri Mar 10, 2006 5:23 am
Location: http://www.arasanchess.org

Re: Editing tscp

Post by jdart »

If I remember it barely supports the Winboard protocol (and only v1 I think) so improving that is also something you could do, if you had spare time.

--Jon
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

Re: Editing tscp

Post by sje »

jdart wrote:If I remember it barely supports the Winboard protocol (and only v1 I think) so improving that is also something you could do, if you had spare time.
You're right about the xboard v1 support.

As for further edits, I'll wait for distribution approval because it's not worthwhile to do the work if it benefits only me.

----

Current match score of Symbolic vs tscp: 267-38-36 (+283 elo)