File: evtest.c

package info (click to toggle)
crafty 23.4-6%2Bdeb8u1
  • links: PTS
  • area: non-free
  • in suites: jessie
  • size: 3,296 kB
  • ctags: 2,891
  • sloc: ansic: 30,650; cpp: 5,829; makefile: 901; sh: 178; perl: 30
file content (132 lines) | stat: -rw-r--r-- 5,708 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "chess.h"
#include "data.h"
/* last modified 01/18/09 */
/*
 *******************************************************************************
 *                                                                             *
 *   EVTest() is used to test the program against a suite of test positions to *
 *   measure its performance on a particular machine, or to evaluate its skill *
 *   after modifying it in some way.                                           *
 *                                                                             *
 *   The test is initiated by using the "evtest <filename>" command to read in *
 *   the suite of problems from file <filename>.  The format of this file is   *
 *   as follows:                                                               *
 *                                                                             *
 *   <forsythe-string>  This sets the board position using the usual Forsythe  *
 *   notation (see module SetBoard() for a full explanation explanation of the *
 *   syntax).                                                                  *
 *                                                                             *
 *   After reading this position, the program then calls Evaluate() to produce *
 *   a positional evaluation, along with any debug output from Evaluate(), and *
 *   then goes on to the next position.                                        *
 *                                                                             *
 *   A common use (with code included below) is to take a position, and then   *
 *   "flip" it (reverse the board rank by rank changing the color of each      *
 *   piece as this is done, giving us two positions.  We then take each of     *
 *   these and mirror them, which reverses them file by file, which now gives  *
 *   four positions in total.  We run these thru Evaluate() to make sure that  *
 *   all produce exactly the same score, except for the color change for the   *
 *   two that changed the piece colors.  This is used to make certain that the *
 *   evaluation is not asymmetric with respect to the colors, or to the side   *
 *   of the board, which catches errors that are difficult to spot otherwise.  *
 *                                                                             *
 *******************************************************************************
 */
void EVTest(char *filename) {
  FILE *test_input;
  char *eof;
  char buff[4096];
  TREE *const tree = block[0];

/*
 ************************************************************
 *                                                          *
 *   Read in the position and then the solutions.  Then do  *
 *   a call to Evaluate() which will normally display the   *
 *   debugging stuff that is enabled.                       *
 *                                                          *
 ************************************************************
 */
  if (!(test_input = fopen(filename, "r"))) {
    printf("file %s does not exist.\n", filename);
    return;
  }
  while (1) {
    eof = fgets(buffer, 4096, test_input);
    if (eof) {
      char *delim;

      delim = strchr(buffer, '\n');
      if (delim)
        *delim = 0;
      delim = strchr(buffer, '\r');
      if (delim)
        *delim = ' ';
    } else
      break;
    strcpy(buff, buffer);
    nargs = ReadParse(buffer, args, " ;");
    if (!strcmp(args[0], "end"))
      break;
/*
 ************************************************************
 *                                                          *
 *   Now for the asymmetry tests.  We use the built-in      *
 *   commands "flip" and "flop" to create the four          *
 *   positions, and call Evaluate() for each.               *
 *                                                          *
 ************************************************************
 */
    else {
      int s1, s2, s3, s4, id;

      for (id = 2; id < nargs; id++)
        if (!strcmp(args[id], "id"))
          break;
      if (id >= nargs)
        id = 0;
      SetBoard(tree, nargs, args, 0);
      Castle(0, white) = 0;
      Castle(1, white) = 0;
      Castle(0, black) = 0;
      Castle(1, black) = 0;
      root_wtm = wtm;
      tree->pawn_score.key = 0;
      s1 = Evaluate(tree, 0, wtm, -99999, 99999);
      strcpy(buffer, "flop");
      (void) Option(tree);
      tree->pawn_score.key = 0;
      s2 = Evaluate(tree, 0, wtm, -99999, 99999);
      strcpy(buffer, "flip");
      (void) Option(tree);
      tree->pawn_score.key = 0;
      s3 = Evaluate(tree, 0, wtm, -99999, 99999);
      strcpy(buffer, "flop");
      (void) Option(tree);
      tree->pawn_score.key = 0;
      s4 = Evaluate(tree, 0, wtm, -99999, 99999);
/*
 ************************************************************
 *                                                          *
 *   If the evaluations (with sign corrected if piece color *
 *   was changed) are different, we display the four values *
 *   and then display the original board position so that   *
 *   we can debug the cause of the asymmetry.               *
 *                                                          *
 ************************************************************
 */
      if (s1 != s2 || s1 != s3 || s1 != s4 || s2 != s3 || s2 != s4 ||
          s3 != s4) {
        strcpy(buffer, "flip");
        (void) Option(tree);
        printf("FEN = %s\n", buff);
        DisplayChessBoard(stdout, tree->pos);
        if (id)
          Print(4095, "id=%s  ", args[id + 1]);
        Print(4095, "wtm=%d  score=%d  %d (flop)  %d (flip)  %d (flop)\n",
            wtm, s1, s2, s3, s4);
      }
    }
  }
  input_stream = stdin;
}