説明を見る。00001 #ifndef _OPENING_BOOK_H
00002 #define _OPENING_BOOK_H
00003 #include "osl/book/compactBoard.h"
00004 #include "osl/basic_type.h"
00005 #include "osl/numEffectState.h"
00006 #include <fstream>
00007 #include <functional>
00008
00009 namespace osl
00010 {
00011 namespace book
00012 {
00013 class OMove
00014 {
00015 public:
00016 OMove(int i) { value = i; }
00017 OMove(Move m) {
00018 const Square from = m.from();
00019 const Square to = m.to();
00020 const int bitFrom = (from.isPieceStand() ? 0 :
00021 (from.x() << 4 | from.y()));
00022 const int bitTo = (to.isPieceStand() ? 0 :
00023 (to.x() << 12 | to.y() << 8));
00024 value = (bitFrom | bitTo |
00025 static_cast<unsigned int>(m.isPromotion()) << 19 |
00026 static_cast<unsigned int>(m.capturePtype()) << 20 |
00027 static_cast<unsigned int>(m.ptype()) << 24 |
00028 static_cast<int>(m.player()) << 28);
00029 }
00030 Square from() {
00031 if ((value & 0xff) == 0)
00032 return Square::STAND();
00033 else
00034 return Square((value >> 4) & 0xf, value & 0xf);
00035 }
00036 Square to() {
00037 if (((value >> 8) & 0xff) == 0)
00038 return Square::STAND();
00039 else
00040 return Square((value >> 12) & 0xf, (value >> 8) & 0xf);
00041 }
00042 bool isPromotion() { return (value >> 19) & 1; }
00043 Ptype capturePtype() {
00044 return static_cast<Ptype>((value >> 20) & 0xf);
00045 }
00046 Ptype ptype() {
00047 return static_cast<Ptype>((value >> 24) & 0xf);
00048 }
00049 Player player() {
00050 return static_cast<Player>((value) >> 28);
00051 }
00052 operator Move() { return Move(from(), to(), ptype(),
00053 capturePtype(), isPromotion(),
00054 player()); }
00055 operator int() { return value; }
00056 private:
00057 int value;
00058 };
00059
00060 struct OBMove
00061 {
00062 Move move;
00063 int state_index;
00064 int stateIndex() const { return state_index; }
00065 };
00066
00082 class WinCountBook
00083 {
00084 int nStates;
00085 std::ifstream ifs;
00086 public:
00087 WinCountBook(const char *filename);
00088 ~WinCountBook();
00089 int winCount(int stateIndex);
00090 int loseCount(int stateIndex);
00091 std::vector<OBMove> moves(int stateIndex);
00092 private:
00093 int readInt();
00094 void seek(int offset);
00095 };
00096
00097 struct WMove
00098 {
00099 Move move;
00100 int state_index;
00101 int weight;
00102
00103 int stateIndex() const { return state_index; }
00104 void setWeight(const int w) { weight = w; };
00105 };
00106 std::ostream& operator<<(std::ostream&, const WMove& w);
00107 std::istream& operator>>(std::istream&, WMove& w);
00108
00109 inline bool operator==(const WMove& l, const WMove& r)
00110 {
00111 return l.move == r.move && l.stateIndex() == r.stateIndex()
00112 && l.weight == r.weight;
00113 }
00114
00118 struct WMoveSort : public std::binary_function<WMove, WMove, bool>
00119 {
00120 bool operator()(const WMove& l, const WMove& r) const {
00121 return l.weight > r.weight;
00122 }
00123 };
00124
00128 struct WMoveMoveSort : public std::binary_function<WMove, WMove, bool>
00129 {
00130 bool operator()(const WMove& l, const WMove& r) const {
00131 return l.move.intValue() < r.move.intValue();
00132 }
00133 };
00134
00138 struct WMoveWeightMoveSort : public std::binary_function<WMove, WMove, bool>
00139 {
00140 bool operator()(const WMove& l, const WMove& r) const {
00141 if (l.weight != r.weight)
00142 return l.weight > r.weight;
00143 return l.move.intValue() < r.move.intValue();
00144 }
00145 };
00146
00168 class WeightedBook
00169 {
00170 int n_states;
00171 int n_moves;
00172 int start_state;
00173 std::ifstream ifs;
00174 public:
00175 typedef std::vector<WMove> WMoveContainer;
00176
00177 WeightedBook(const char *filename);
00178 ~WeightedBook();
00184 WMoveContainer moves(int stateIndex, const bool zero_include = true);
00185 int whiteWinCount(int stateIndex);
00186 int blackWinCount(int stateIndex);
00187 CompactBoard compactBoard(int stateIndex);
00188 SimpleState board(int stateIndex);
00189 int totalState() const { return n_states; }
00190 int startState() const { return start_state; }
00191 void validate();
00197 std::vector<int> parents(const int stateIndex);
00209 int stateIndex(const SimpleState& state,
00210 const bool visit_zero = true,
00211 const Player player = BLACK);
00220 int stateIndex(const std::vector<Move>& moves);
00221 private:
00222 void seek(int offset);
00223 static const int HEADER_SIZE = 16;
00224 static const int STATE_SIZE = 16;
00225 static const int MOVE_SIZE = 12;
00226 static const int BOARD_SIZE = 41 * 4;
00227 };
00228 }
00229 using book::CompactBoard;
00230 using book::WeightedBook;
00231 }
00232 #endif // _OPENING_BOOK_H
00233
00234
00235
00236