openingBook.h
Go to the documentation of this file.
1 #ifndef _OPENING_BOOK_H
2 #define _OPENING_BOOK_H
4 #include "osl/basic_type.h"
5 #include "osl/numEffectState.h"
6 #include <fstream>
7 #include <functional>
8 
9 namespace osl
10 {
11  namespace book
12  {
13  class OMove
14  {
15  public:
16  OMove(int i) { value = i; }
17  OMove(Move m) {
18  const Square from = m.from();
19  const Square to = m.to();
20  const int bitFrom = (from.isPieceStand() ? 0 :
21  (from.x() << 4 | from.y()));
22  const int bitTo = (to.isPieceStand() ? 0 :
23  (to.x() << 12 | to.y() << 8));
24  value = (bitFrom | bitTo |
25  static_cast<unsigned int>(m.isPromotion()) << 19 |
26  static_cast<unsigned int>(m.capturePtype()) << 20 |
27  static_cast<unsigned int>(m.ptype()) << 24 |
28  static_cast<int>(m.player()) << 28);
29  }
31  if ((value & 0xff) == 0)
32  return Square::STAND();
33  else
34  return Square((value >> 4) & 0xf, value & 0xf);
35  }
36  Square to() {
37  if (((value >> 8) & 0xff) == 0)
38  return Square::STAND();
39  else
40  return Square((value >> 12) & 0xf, (value >> 8) & 0xf);
41  }
42  bool isPromotion() { return (value >> 19) & 1; }
44  return static_cast<Ptype>((value >> 20) & 0xf);
45  }
47  return static_cast<Ptype>((value >> 24) & 0xf);
48  }
50  return static_cast<Player>((value) >> 28);
51  }
52  operator Move() { return Move(from(), to(), ptype(),
54  player()); }
55  operator int() { return value; }
56  private:
57  int value;
58  };
59 
60  struct OBMove
61  {
64  int stateIndex() const { return state_index; }
65  };
66 
83  {
84  int nStates;
85  std::ifstream ifs;
86  public:
87  WinCountBook(const char *filename);
88  ~WinCountBook();
89  int winCount(int stateIndex);
90  int loseCount(int stateIndex);
91  std::vector<OBMove> moves(int stateIndex);
92  private:
93  int readInt();
94  void seek(int offset);
95  };
96 
97  struct WMove
98  {
101  int weight;
102 
103  int stateIndex() const { return state_index; }
104  void setWeight(const int w) { weight = w; };
105  };
106  std::ostream& operator<<(std::ostream&, const WMove& w);
107  std::istream& operator>>(std::istream&, WMove& w);
108 
109  inline bool operator==(const WMove& l, const WMove& r)
110  {
111  return l.move == r.move && l.stateIndex() == r.stateIndex()
112  && l.weight == r.weight;
113  }
114 
118  struct WMoveSort : public std::binary_function<WMove, WMove, bool>
119  {
120  bool operator()(const WMove& l, const WMove& r) const {
121  return l.weight > r.weight;
122  }
123  };
124 
128  struct WMoveMoveSort : public std::binary_function<WMove, WMove, bool>
129  {
130  bool operator()(const WMove& l, const WMove& r) const {
131  return l.move.intValue() < r.move.intValue();
132  }
133  };
134 
138  struct WMoveWeightMoveSort : public std::binary_function<WMove, WMove, bool>
139  {
140  bool operator()(const WMove& l, const WMove& r) const {
141  if (l.weight != r.weight)
142  return l.weight > r.weight;
143  return l.move.intValue() < r.move.intValue();
144  }
145  };
146 
169  {
170  int n_states;
171  int n_moves;
173  std::ifstream ifs;
174  public:
175  typedef std::vector<WMove> WMoveContainer;
176 
177  WeightedBook(const char *filename);
178  ~WeightedBook();
184  WMoveContainer moves(int stateIndex, const bool zero_include = true);
185  int whiteWinCount(int stateIndex);
186  int blackWinCount(int stateIndex);
187  CompactBoard compactBoard(int stateIndex);
188  SimpleState board(int stateIndex);
189  int totalState() const { return n_states; }
190  int startState() const { return start_state; }
191  void validate();
197  std::vector<int> parents(const int stateIndex);
209  int stateIndex(const SimpleState& state,
210  const bool visit_zero = true,
211  const Player player = BLACK);
220  int stateIndex(const std::vector<Move>& moves);
221  private:
222  void seek(int offset);
223  static const int HEADER_SIZE = 16;
224  static const int STATE_SIZE = 16;
225  static const int MOVE_SIZE = 12;
226  static const int BOARD_SIZE = 41 * 4;
227  };
228  } // book
229  using book::CompactBoard;
230  using book::WeightedBook;
231 } // namespace osl
232 #endif // _OPENING_BOOK_H
233 // ;;; Local Variables:
234 // ;;; mode:c++
235 // ;;; c-basic-offset:2
236 // ;;; End:
WMoveのWeightによるsort.
Definition: openingBook.h:118
std::ostream & operator<<(std::ostream &os, const CompactBoard &c)
Definition: compactBoard.cc:79
bool operator()(const WMove &l, const WMove &r) const
Definition: openingBook.h:130
int stateIndex() const
Definition: openingBook.h:64
void setWeight(const int w)
Definition: openingBook.h:104
WMoveのWeightとMoveによるsort.
Definition: openingBook.h:138
StateとOBMoveを保持する.
Definition: openingBook.h:82
int y() const
将棋としてのY座標を返す.
Definition: basic_type.h:567
bool operator==(const CompactBoard &, const CompactBoard &)
局面を比較する.
Definition: compactBoard.cc:73
Ptype capturePtype()
Definition: openingBook.h:43
int x() const
将棋としてのX座標を返す.
Definition: basic_type.h:563
Ptype
駒の種類を4ビットでコード化する
Definition: basic_type.h:83
Ptype ptype() const
Definition: basic_type.h:1155
std::istream & operator>>(std::istream &os, CompactBoard &c)
Definition: compactBoard.cc:91
int intValue() const
Definition: basic_type.h:1065
WMoveのMoveによるsort.
Definition: openingBook.h:128
const Square from() const
Definition: basic_type.h:1125
StateとWMoveを保持する.
Definition: openingBook.h:168
bool operator()(const WMove &l, const WMove &r) const
Definition: openingBook.h:140
std::vector< WMove > WMoveContainer
Definition: openingBook.h:175
圧縮していない moveの表現 .
Definition: basic_type.h:1051
Player player() const
Definition: basic_type.h:1195
int readInt(std::istream &is)
Definition: openingBook.cc:7
bool isPromotion()
Definition: openingBook.h:42
Ptype capturePtype() const
Definition: basic_type.h:1180
const Square to() const
Definition: basic_type.h:1132
SimpleStateよりcompactな局面の表現
Definition: compactBoard.h:59
bool isPromotion() const
Definition: basic_type.h:1147
Player
Definition: basic_type.h:8
int stateIndex() const
Definition: openingBook.h:103
bool isPieceStand() const
Definition: basic_type.h:576
bool operator()(const WMove &l, const WMove &r) const
Definition: openingBook.h:120
static const Square STAND()
Definition: basic_type.h:548
Player player()
Definition: openingBook.h:49