All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
quiescencestat.cc
Go to the documentation of this file.
1 /* quiescencestat.cc
2  */
9 #include "osl/record/csaString.h"
10 #include "osl/record/csaRecord.h"
11 #include "osl/eval/pieceEval.h"
12 #include "osl/eval/progressEval.h"
13 #include "osl/misc/perfmon.h"
14 
15 #include <iostream>
16 #include <fstream>
17 
18 using namespace osl;
19 using namespace osl::search;
20 using namespace osl::misc;
21 
22 void qsearch(const char *filename);
23 
24 void usage(const char *program_name)
25 {
26  std::cerr << program_name << " [-C] [-P] [-d depth] [-s skip] [-v] csafiles\n";
27  std::cerr << "-C comparison w,w/o table\n";
28  exit(1);
29 }
30 
31 int depth = -6;
32 bool verbose = false;
33 size_t skip_first = 0;
34 bool comparison = false;
35 bool use_progress_eval = false;
36 
37 template <class Eval>
38 void qsearch(const char *filename);
39 
40 int main(int argc, char **argv)
41 {
42  const char *program_name = argv[0];
43  bool error_flag = false;
44 
45  extern char *optarg;
46  extern int optind;
47  char c;
48  while ((c = getopt(argc, argv, "C:Pd:s:vh")) != EOF)
49  {
50  switch(c)
51  {
52  case 'C': comparison = true;
53  break;
54  case 'd': depth = atoi(optarg);
55  break;
56  case 'P': use_progress_eval = true;
57  break;
58  case 's': skip_first = atoi(optarg);
59  break;
60  case 'v': verbose = true;
61  break;
62  default: error_flag = true;
63  }
64  }
65  argc -= optind;
66  argv += optind;
67 
68  if (error_flag || (argc < 1))
69  usage(program_name);
70 
71  std::cerr << "using table record depth " << depth << "\n";
72  try
73  {
74  for (int i=0; i<argc; ++i)
75  {
77  qsearch<eval::ProgressEval>(argv[i]);
78  else
79  qsearch<PieceEval>(argv[i]);
80  }
81  }
82  catch (std::exception& e)
83  {
84  std::cerr << e.what() << "\n";
85  return 1;
86  }
87  catch (...)
88  {
89  throw;
90  }
91 }
92 
93 template <class Eval>
94 void qsearch(const char *filename)
95 {
96  unsigned long long total_cycles=0;
97  unsigned long long positions = 0;
98  Record rec=CsaFile(filename).getRecord();
99  NumEffectState state(rec.getInitialState());
100  const vector<osl::Move> moves=rec.getMoves();
101 
103 
104  SimpleHashTable table(1000000,depth,verbose);
105  SimpleHashTable nulltable(0,0,false);
106  SearchState2Core::checkmate_t checkmate_searcher;
107  Eval ev(state);
108  size_t i=0;
109  while (true)
110  {
111  if (i >= skip_first)
112  {
113  SearchState2Core core(state, checkmate_searcher);
114  qsearch_t qs(core, table);
115  qsearch_t qsnull(core, nulltable);
116  const Move last_move = (i > 0) ? moves[i-1] : Move::PASS(alt(moves[0].player()));
117  if (verbose)
118  std::cerr << i << " " << last_move << "\n";
119  if (verbose)
120  {
121  const char *logfile = "/tmp/q-w-table.log";
122  unlink(logfile);
123  QuiescenceLog::init(logfile);
124  }
125  PerfMon clock;
126  const int val = qs.search(state.turn(), ev, last_move);
127  total_cycles += clock.stop();
128  if (comparison)
129  {
130  if (verbose)
131  {
132  const char *logfile = "/tmp/q-wo-table.log";
133  unlink(logfile);
134  QuiescenceLog::init(logfile);
135  }
136  const int valnull = qsnull.search(state.turn(), ev, last_move);
137  if (verbose || (valnull != val))
138  {
139  std::cerr << state << "\n";
140  std::cerr << ev.value() << " " ;
141  if (! state.inCheck())
142  std::cerr << ((state.turn() == BLACK)
143  ? qs.template staticValueWithThreat<BLACK>(ev)
144  : qs.template staticValueWithThreat<WHITE>(ev)) << " ";
145  std::cerr << val << " " << valnull << "\n";
146  // break;
147  }
148  }
149  positions += qs.nodeCount();
150  }
151  if (i >= moves.size())
152  break;
153  const Move move = moves[i++];
154  state.makeMove(move);
155  ev.update(state, move);
156  }
157  const size_t checkmate_count = checkmate_searcher.totalNodeCount();
158  std::cerr << total_cycles << " / ( " << positions
159  << " + " << checkmate_count << " ) = "
160  << total_cycles/(double)(positions + checkmate_count) << "\n";
161 }
162 
163 /* ------------------------------------------------------------------------- */
164 // ;;; Local Variables:
165 // ;;; mode:c++
166 // ;;; c-basic-offset:2
167 // ;;; End: