home *** CD-ROM | disk | FTP | other *** search
/ Dream 57 / Amiga_Dream_57.iso / Amiga / Jeux / Reflexion / Crafty-15.19.lha / crafty-15.19 / src / test.c < prev    next >
C/C++ Source or Header  |  1998-09-13  |  7KB  |  169 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "chess.h"
  5. #include "data.h"
  6.  
  7. /* last modified 10/18/97 */
  8. /*
  9. ********************************************************************************
  10. *                                                                              *
  11. *   Test() is used to test the program against a suite of test positions to    *
  12. *   measure its performance on a particular machine, or to evaluate its skill  *
  13. *   after modifying it in some way.                                            *
  14. *                                                                              *
  15. *   the test is initiated by using the "test <filename>" command to read in    *
  16. *   the suite of problems from file <filename>.  the format of this file is    *
  17. *   as follows:                                                                *
  18. *                                                                              *
  19. *   setboard <forsythe-string>:  this sets the board position using the usual  *
  20. *   forsythe notation (see module SetBoard() in setc for a full ex-      *
  21. *   planation of the syntax).                                                  *
  22. *                                                                              *
  23. *   solution <move1> <move2> ... <moven>:  this provides a solution move (or   *
  24. *   set of solution moves if more than one is correct).  if the search finds   *
  25. *   one of these moves, then the prblem is counted as correct, otherwise it    *
  26. *   is counted wrong.                                                          *
  27. *                                                                              *
  28. *   after reading these two lines, the program then searches to whatever time  *
  29. *   or depth limit has been set, when it reaches the end-of-file condition or  *
  30. *   when it reads a record containing the string "end" it then displays the    *
  31. *   number correct and the number missed.                                      *
  32. *                                                                              *
  33. ********************************************************************************
  34. */
  35. void Test(char *filename)
  36. {
  37.   FILE *test_input;
  38.   int i, move, right=0, wrong=0, correct;
  39.   int time=0, len;
  40.   double nodes=0.0;
  41.   char *eof;
  42.   float avg_depth=0.0;
  43.   TREE *tree=local[0];
  44. /*
  45.  ----------------------------------------------------------
  46. |                                                          |
  47. |   read in the position and then the solutions.  after    |
  48. |   executing a search to find the best move (according    |
  49. |   to the program, anyway) compare it against the list    |
  50. |   of solutions and count it right or wrong.              |
  51. |                                                          |
  52.  ----------------------------------------------------------
  53. */
  54.   if (!(test_input=fopen(filename,"r"))) {
  55.     printf("file %s does not exist.\n",filename);
  56.     return;
  57.   }
  58.   test_mode=1;
  59.   if (book_file) {
  60.     fclose(book_file);
  61.     fclose(books_file);
  62.     book_file=0;
  63.     books_file=0;
  64.   }
  65.   while (1) {
  66.     eof=fgets(buffer,512,test_input);
  67.     if (eof) {
  68.       char *delim;
  69.       delim=strchr(buffer,'\n');
  70.       if (delim) *delim=0;
  71.       delim=strchr(buffer,'\r');
  72.       if (delim) *delim=' ';
  73.     }
  74.     else break;
  75.     nargs=ReadParse(buffer,args," ;");
  76.     if (!strcmp(args[0],"end")) break;
  77.     else if (!strcmp(args[0],"title")) {
  78.       Print(4095,"======================================================================\n");
  79.       Print(4095,"! ");
  80.       len=0;
  81.       for (i=1;i<nargs;i++) {
  82.         Print(4095,"%s ",args[i]);
  83.         len+=strlen(args[i])+1;
  84.         if (len > 65) break;
  85.       }
  86.       for (i=len;i<67;i++) printf(" ");
  87.       Print(4095,"!\n");
  88.       Print(4095,"======================================================================\n");
  89.     }
  90.     else if (!strcmp(args[0],"setboard")) {
  91.       SetBoard(nargs-1,args+1,0);
  92.     }
  93.     else if (!strcmp(args[0],"solution")) {
  94.       number_of_solutions=0;
  95.       solution_type=0;
  96.       Print(4095,"solution ");
  97.       for (i=1;i<nargs;i++) {
  98.         if (args[i][strlen(args[i])-1] == '?') {
  99.           solution_type=1;
  100.           args[i][strlen(args[i])-1]='\0';
  101.         }
  102.         else if (*(args+i)[strlen(args[i])-1] == '!') {
  103.           solution_type=0;
  104.           args[i][strlen(args[i])-1]='\0';
  105.         }
  106.         move=InputMove(tree,args[i],0,wtm,0,0);
  107.         if (move) {
  108.           solutions[number_of_solutions]=move;
  109.           Print(4095,"%d. %s",(number_of_solutions++)+1,OutputMove(tree,move,0,wtm));
  110.           if (solution_type==1) Print(4095,"? ");
  111.           else Print(4095,"  ");
  112.         }
  113.         else DisplayChessBoard(stdout,tree->pos);
  114.       }
  115.       Print(4095,"\n");
  116.       InitializeHashTables();
  117.       last_pv.path_iteration_depth=0;
  118.       largest_positional_score=100;
  119.       thinking=1;
  120.       tree->position[1]=tree->position[0];
  121.       (void) Iterate(wtm,think,0);
  122.       thinking=0;
  123.       nodes+=tree->nodes_searched;
  124.       avg_depth+=(float)iteration_depth;
  125.       time+=(end_time-start_time);
  126.       correct=solution_type;
  127.       for (i=0;i<number_of_solutions;i++) {
  128.         if (!solution_type) {
  129.           if (solutions[i] == tree->pv[1].path[1]) correct=1;
  130.         }
  131.         else
  132.           if (solutions[i] == tree->pv[1].path[1]) correct=0;
  133.       }
  134.       if (correct) {
  135.         right++;
  136.         Print(4095,"----------------------> solution correct (%d/%d).\n",
  137.               right,right+wrong);
  138.       }
  139.       else {
  140.         wrong++;
  141.         Print(4095,"----------------------> solution incorrect (%d/%d).\n",
  142.               right,right+wrong);
  143.       }
  144.     }
  145.   }
  146. /*
  147.  ----------------------------------------------------------
  148. |                                                          |
  149. |   now print the results.                                 |
  150. |                                                          |
  151.  ----------------------------------------------------------
  152. */
  153.   if (right+wrong) {
  154.     Print(4095,"\n\n\n");
  155.     Print(4095,"test results summary:\n\n");
  156.     Print(4095,"total positions searched..........%10d\n",right+wrong);
  157.     Print(4095,"number right......................%10d\n",right);
  158.     Print(4095,"number wrong......................%10d\n",wrong);
  159.     Print(4095,"percentage right..................%10d\n",right*100/(right+wrong));
  160.     Print(4095,"percentage wrong..................%10d\n",wrong*100/(right+wrong));
  161.     Print(4095,"total nodes searched..............%10.1f\n",nodes);
  162.     Print(4095,"average search depth..............%10.1f\n",avg_depth/(right+wrong));
  163.     Print(4095,"nodes per second..................%10d\n",(int) ((float) nodes/(float) time*100.0));
  164.   }
  165.   input_stream=stdin;
  166.   early_exit=99;
  167.   test_mode=0;
  168. }
  169.