My Project
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);
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:
osl::book::WeightedBook::validate
void validate()
Definition: openingBook.cc:203
osl::book::WinCountBook::winCount
int winCount(int stateIndex)
Definition: openingBook.cc:85
osl::book::WeightedBook::MOVE_SIZE
static const int MOVE_SIZE
Definition: openingBook.h:225
osl::book::WMoveSort::operator()
bool operator()(const WMove &l, const WMove &r) const
Definition: openingBook.h:120
osl::Square
Definition: basic_type.h:532
osl::book::OMove::isPromotion
bool isPromotion()
Definition: openingBook.h:42
osl::book::WMove::move
Move move
Definition: openingBook.h:99
osl::book::WinCountBook::WinCountBook
WinCountBook(const char *filename)
Definition: openingBook.cc:30
osl::book::WeightedBook::stateIndex
int stateIndex(const SimpleState &state, const bool visit_zero=true, const Player player=BLACK)
As traversing the 'tree', find a state index of the state.
Definition: openingBook.cc:242
osl::book::WinCountBook::nStates
int nStates
Definition: openingBook.h:84
osl::book::WeightedBook::start_state
int start_state
Definition: openingBook.h:172
osl::book::WeightedBook::board
SimpleState board(int stateIndex)
Definition: openingBook.cc:177
osl::book::WMove::setWeight
void setWeight(const int w)
Definition: openingBook.h:104
osl::book::WeightedBook::moves
WMoveContainer moves(int stateIndex, const bool zero_include=true)
Return moves from the state of the stateIndex.
Definition: openingBook.cc:147
osl::book::OMove::OMove
OMove(int i)
Definition: openingBook.h:16
osl::book::WeightedBook::n_states
int n_states
Definition: openingBook.h:170
osl::Move
圧縮していない moveの表現 .
Definition: basic_type.h:1052
basic_type.h
osl::book::OBMove
Definition: openingBook.h:61
osl::book::operator>>
std::istream & operator>>(std::istream &os, CompactBoard &c)
Definition: compactBoard.cc:91
osl::book::WMove::stateIndex
int stateIndex() const
Definition: openingBook.h:103
osl::SimpleState
Definition: simpleState.h:35
osl::book::OBMove::move
Move move
Definition: openingBook.h:62
osl::book::WMoveMoveSort::operator()
bool operator()(const WMove &l, const WMove &r) const
Definition: openingBook.h:130
osl::book::WeightedBook::blackWinCount
int blackWinCount(int stateIndex)
Definition: openingBook.cc:194
osl::Ptype
Ptype
駒の種類を4ビットでコード化する
Definition: basic_type.h:84
osl::book::WinCountBook
StateとOBMoveを保持する.
Definition: openingBook.h:83
osl::book::CompactBoard
SimpleStateよりcompactな局面の表現
Definition: compactBoard.h:60
osl::book::OMove::OMove
OMove(Move m)
Definition: openingBook.h:17
osl::book::WeightedBook::STATE_SIZE
static const int STATE_SIZE
Definition: openingBook.h:224
osl::book::WMoveMoveSort
WMoveのMoveによるsort.
Definition: openingBook.h:129
osl::book::WMove::state_index
int state_index
Definition: openingBook.h:100
osl::book::WeightedBook::seek
void seek(int offset)
Definition: openingBook.cc:141
osl::Square::isPieceStand
bool isPieceStand() const
Definition: basic_type.h:576
osl::book::operator<<
std::ostream & operator<<(std::ostream &os, const CompactBoard &c)
Definition: compactBoard.cc:79
osl::book::WMoveWeightMoveSort
WMoveのWeightとMoveによるsort.
Definition: openingBook.h:139
osl::book::OMove::from
Square from()
Definition: openingBook.h:30
osl::Move::capturePtype
Ptype capturePtype() const
Definition: basic_type.h:1180
osl::book::WinCountBook::ifs
std::ifstream ifs
Definition: openingBook.h:85
osl::book::WeightedBook::HEADER_SIZE
static const int HEADER_SIZE
Definition: openingBook.h:223
osl::book::WinCountBook::readInt
int readInt()
Definition: openingBook.cc:48
osl::book::OBMove::state_index
int state_index
Definition: openingBook.h:63
osl::book::WeightedBook::stateIndex
int stateIndex(const std::vector< Move > &moves)
As traversing the 'tree', find a state index of the state reached by applying the moves from the init...
osl::Move::from
const Square from() const
Definition: basic_type.h:1125
osl::Square::x
int x() const
将棋としてのX座標を返す.
Definition: basic_type.h:563
osl::book::WinCountBook::moves
std::vector< OBMove > moves(int stateIndex)
Definition: openingBook.cc:66
osl::book::OMove
Definition: openingBook.h:14
compactBoard.h
osl::book::WinCountBook::~WinCountBook
~WinCountBook()
Definition: openingBook.cc:43
osl::book::WeightedBook::WeightedBook
WeightedBook(const char *filename)
Definition: openingBook.cc:116
osl::book::WeightedBook::parents
std::vector< int > parents(const int stateIndex)
As traversing the 'tree', return all state indices of the state's parents.
Definition: openingBook.cc:318
osl::book::OMove::player
Player player()
Definition: openingBook.h:49
osl::Square::y
int y() const
将棋としてのY座標を返す.
Definition: basic_type.h:567
osl::book::WeightedBook::~WeightedBook
~WeightedBook()
Definition: openingBook.cc:136
osl::book::WMoveWeightMoveSort::operator()
bool operator()(const WMove &l, const WMove &r) const
Definition: openingBook.h:140
osl::book::OMove::to
Square to()
Definition: openingBook.h:36
osl::book::WinCountBook::loseCount
int loseCount(int stateIndex)
Definition: openingBook.cc:92
osl::book::WMove::weight
int weight
Definition: openingBook.h:101
osl::book::WeightedBook::ifs
std::ifstream ifs
Definition: openingBook.h:173
osl::book::WMoveSort
WMoveのWeightによるsort.
Definition: openingBook.h:119
osl::Move::ptype
Ptype ptype() const
Definition: basic_type.h:1155
osl::BLACK
@ BLACK
Definition: basic_type.h:9
osl::book::OMove::capturePtype
Ptype capturePtype()
Definition: openingBook.h:43
osl::book::WeightedBook::n_moves
int n_moves
Definition: openingBook.h:171
osl::book::OMove::ptype
Ptype ptype()
Definition: openingBook.h:46
osl::book::WeightedBook
StateとWMoveを保持する.
Definition: openingBook.h:169
osl::book::WeightedBook::whiteWinCount
int whiteWinCount(int stateIndex)
Definition: openingBook.cc:184
osl::Player
Player
Definition: basic_type.h:8
osl::book::WMove
Definition: openingBook.h:98
numEffectState.h
osl::book::OMove::value
int value
Definition: openingBook.h:57
osl::Square::STAND
static const Square STAND()
Definition: basic_type.h:548
osl::Move::to
const Square to() const
Definition: basic_type.h:1132
osl::book::WeightedBook::compactBoard
CompactBoard compactBoard(int stateIndex)
Definition: openingBook.cc:167
osl::book::OBMove::stateIndex
int stateIndex() const
Definition: openingBook.h:64
osl::book::operator==
bool operator==(const CompactBoard &, const CompactBoard &)
局面を比較する.
Definition: compactBoard.cc:73
osl::book::WinCountBook::seek
void seek(int offset)
Definition: openingBook.cc:60
osl::Move::intValue
int intValue() const
Definition: basic_type.h:1065
osl::Move::isPromotion
bool isPromotion() const
Definition: basic_type.h:1147
osl::book::WeightedBook::startState
int startState() const
Definition: openingBook.h:190
osl::book::WeightedBook::WMoveContainer
std::vector< WMove > WMoveContainer
Definition: openingBook.h:175
osl::book::WeightedBook::BOARD_SIZE
static const int BOARD_SIZE
Definition: openingBook.h:226
osl::book::WeightedBook::totalState
int totalState() const
Definition: openingBook.h:189
osl::Move::player
Player player() const
Definition: basic_type.h:1195
osl
Definition: additionalEffect.h:6