All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
show-eval.cc
Go to the documentation of this file.
1 /* show-eval.cc
2  */
5 #include "osl/record/csaRecord.h"
8 #include "osl/oslConfig.h"
9 #include "osl/pieceStand.h"
10 #include "osl/record/kanjiPrint.h"
11 #include <iostream>
12 #include <iomanip>
13 #include <cstdlib>
14 #include <cstdio>
15 #include <unistd.h>
16 
17 using namespace osl;
18 using namespace osl::eval;
19 
20 void usage(const char *prog)
21 {
22  using namespace std;
23  cerr << "Usage: " << prog << " csa-filename"
24  << endl;
25  exit(1);
26  }
27 
28 void show(const char *filename);
29 int verbose = 0;
31 
32 int main(int argc, char **argv)
33 {
34  const char *program_name = argv[0];
35  bool error_flag = false;
36 
37  extern char *optarg;
38  extern int optind;
39  char c;
40  while ((c = getopt(argc, argv, "e:vh")) != EOF)
41  {
42  switch(c)
43  {
44  case 'e':
45  if (atoi(optarg) > 0)
46  piece_estimate_level = atoi(optarg);
47  break;
48  default: error_flag = true;
49  }
50  }
51  argc -= optind;
52  argv += optind;
53 
54  if (error_flag)
55  usage(program_name);
56 
59 
60  for (int i=0; i<argc; ++i)
61  {
62  show(argv[i]);
63  }
64 }
65 
66 void make1(const NumEffectState& state, const eval::ml::OpenMidEndingEval& eval, PieceValues& values)
67 {
68  for (int i=0; i<Piece::SIZE; ++i) {
69  const Piece piece = state.pieceOf(i);
70  if (! piece.isOnBoard() || unpromote(piece.ptype()) == KING)
71  continue;
72  const NumEffectState removed(state.emulateCapture(piece, piece.owner()));
73  const eval::ml::OpenMidEndingEval eval_removed(removed);
74  values[piece.number()] = eval.value() - eval_removed.value();
75  }
76 }
77 
78 void make2(const NumEffectState& state, const eval::ml::OpenMidEndingEval& eval, PieceValues& values)
79 {
80  CArray<int,40> count = {{ 0 }};
81  for (int i=0; i<Piece::SIZE; ++i) {
82  values[i] = 0;
83  const Piece piece = state.pieceOf(i);
84  if (! piece.isOnBoard() || unpromote(piece.ptype()) == KING)
85  continue;
86  const NumEffectState removed(state.emulateCapture(piece, piece.owner()));
87  const eval::ml::OpenMidEndingEval eval_removed(removed);
88  for (int j=0; j<Piece::SIZE; ++j) {
89  const Piece piece2 = state.pieceOf(j);
90  if (! piece2.isOnBoard() || unpromote(piece2.ptype()) == KING || i == j)
91  continue;
92  const NumEffectState removed2(removed.emulateCapture(piece2, piece2.owner()));
93  const eval::ml::OpenMidEndingEval eval_removed2(removed2);
94  values[j] += eval_removed.value() - eval_removed2.value();
95  count[j]++;
96  }
97  }
98  for (int i=0; i<Piece::SIZE; ++i)
99  if (count[i])
100  values[i] /= count[i];
101 }
102 
103 void show(const NumEffectState& state)
104 {
105  static const boost::shared_ptr<osl::record::KIFCharacters> characters(new osl::record::KIFCharacters());
106  static osl::record::KanjiPrint printer(std::cout, characters);
107  static const double scale = 200.0
109  const eval::ml::OpenMidEndingEval eval(state);
110  PieceValues values;
111  if (piece_estimate_level == 1)
112  make1(state, eval, values);
113  else
114  make2(state, eval, values);
115  printer.print(state);
116  for (int z=0; z<2; ++z) {
117  for (size_t t=0; t<PieceStand::order.size(); ++t) {
118  const Ptype ptype = PieceStand::order[t];
119  bool shown = false;
120  for (int i=Ptype_Table.getIndexMin(ptype); i<Ptype_Table.getIndexLimit(ptype); ++i) {
121  const Piece piece = state.pieceOf(i);
122  if (! piece.isOnBoard() || unpromote(piece.ptype()) != ptype
123  || piece.owner() != indexToPlayer(z))
124  continue;
125  if (! shown)
126  std::cout << Ptype_Table.getCsaName(ptype);
127  shown = true;
128  std::cout << " (" << piece.square().x() << "," << piece.square().y()
129  << ") " << (int)(values[piece.number()]*scale);
130  }
131  if (shown)
132  std::cout << "\n";
133  }
134  }
135  std::cout << "total " << (int)(eval.value()*scale)
136  << " " << eval.value()
137  << " (progress: " << eval.progress16().value()
138  << " " << progress::ml::NewProgress(state).progress()
139  << ", open-mid-mid2-endgame: " << eval.openingValue()
140  << " " << eval.midgameValue()
141  << " " << eval.midgame2Value()
142  << " " << eval.endgameValue()
143  << " )\n";
144 }
145 
146 void show(const char *filename)
147 {
148  CsaFile file(filename);
149  const vector<osl::Move> moves = file.getRecord().getMoves();
150  NumEffectState state(file.getInitialState());
151  for (unsigned int i=0; i<moves.size(); i++)
152  {
153  show(state);
154  const Move m = moves[i];
155  state.makeMove(m);
156  }
157  show(state);
158 }
159 
160 /* ------------------------------------------------------------------------- */
161 // ;;; Local Variables:
162 // ;;; mode:c++
163 // ;;; c-basic-offset:2
164 // ;;; End: