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"
6#include <fstream>
7#include <functional>
8
9namespace 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 }
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 {
85 std::ifstream ifs;
86 public:
87 WinCountBook(const char *filename);
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 {
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 {
173 std::ifstream ifs;
174 public:
175 typedef std::vector<WMove> WMoveContainer;
176
177 WeightedBook(const char *filename);
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:
圧縮していない moveの表現 .
bool isPromotion() const
Ptype ptype() const
Player player() const
Ptype capturePtype() const
const Square to() const
int intValue() const
const Square from() const
bool isPieceStand() const
Definition basic_type.h:576
int y() const
将棋としてのY座標を返す.
Definition basic_type.h:567
static const Square STAND()
Definition basic_type.h:548
int x() const
将棋としてのX座標を返す.
Definition basic_type.h:563
SimpleStateよりcompactな局面の表現
Ptype capturePtype()
Definition openingBook.h:43
StateとWMoveを保持する.
static const int STATE_SIZE
static const int MOVE_SIZE
int blackWinCount(int stateIndex)
static const int HEADER_SIZE
SimpleState board(int stateIndex)
WMoveContainer moves(int stateIndex, const bool zero_include=true)
Return moves from the state of the stateIndex.
std::vector< WMove > WMoveContainer
CompactBoard compactBoard(int stateIndex)
static const int BOARD_SIZE
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.
int whiteWinCount(int stateIndex)
void seek(int offset)
std::vector< int > parents(const int stateIndex)
As traversing the 'tree', return all state indices of the state's parents.
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...
StateとOBMoveを保持する.
Definition openingBook.h:83
void seek(int offset)
int winCount(int stateIndex)
std::vector< OBMove > moves(int stateIndex)
int loseCount(int stateIndex)
bool operator==(const CompactBoard &, const CompactBoard &)
局面を比較する.
std::ostream & operator<<(std::ostream &os, const CompactBoard &c)
std::istream & operator>>(std::istream &os, CompactBoard &c)
Ptype
駒の種類を4ビットでコード化する
Definition basic_type.h:84
Player
Definition basic_type.h:8
@ BLACK
Definition basic_type.h:9
int stateIndex() const
Definition openingBook.h:64
WMoveのMoveによるsort.
bool operator()(const WMove &l, const WMove &r) const
WMoveのWeightによるsort.
bool operator()(const WMove &l, const WMove &r) const
WMoveのWeightとMoveによるsort.
bool operator()(const WMove &l, const WMove &r) const
void setWeight(const int w)
int stateIndex() const