Go to the documentation of this file.00001 #ifndef _OPENING_BOOK_H
00002 #define _OPENING_BOOK_H
00003 #include "osl/move.h"
00004 #include "osl/stl/vector.h"
00005 #include "osl/state/simpleState.h"
00006 #include "osl/record/compactBoard.h"
00007 #include <fstream>
00008 #include <functional>
00009
00010 namespace osl
00011 {
00012 namespace record
00013 {
00014 namespace opening
00015 {
00016 class OMove
00017 {
00018 public:
00019 OMove(int i) { value = i; }
00020 OMove(Move m)
00021 {
00022 const Square from = m.from();
00023 const Square to = m.to();
00024 const int bitFrom = (from.isPieceStand() ? 0 :
00025 (from.x() << 4 | from.y()));
00026 const int bitTo = (to.isPieceStand() ? 0 :
00027 (to.x() << 12 | to.y() << 8));
00028 value = (bitFrom | bitTo |
00029 static_cast<unsigned int>(m.isPromotion()) << 19 |
00030 static_cast<unsigned int>(m.capturePtype()) << 20 |
00031 static_cast<unsigned int>(m.ptype()) << 24 |
00032 static_cast<int>(m.player()) << 28);
00033 }
00034 Square getFrom()
00035 {
00036 if ((value & 0xff) == 0)
00037 return Square::STAND();
00038 else
00039 return Square((value >> 4) & 0xf, value & 0xf);
00040 }
00041 Square getTo()
00042 {
00043 if (((value >> 8) & 0xff) == 0)
00044 return Square::STAND();
00045 else
00046 return Square((value >> 12) & 0xf, (value >> 8) & 0xf);
00047 }
00048 bool isPromotion()
00049 {
00050 return (value >> 19) & 1;
00051 }
00052 Ptype getCapturePtype()
00053 {
00054 return static_cast<Ptype>((value >> 20) & 0xf);
00055 }
00056 Ptype getPtype()
00057 {
00058 return static_cast<Ptype>((value >> 24) & 0xf);
00059 }
00060 Player getPlayer()
00061 {
00062 return static_cast<Player>((value) >> 28);
00063 }
00064
00065 operator Move() { return Move(getFrom(), getTo(), getPtype(),
00066 getCapturePtype(), isPromotion(),
00067 getPlayer()); }
00068 operator int() { return value; }
00069 private:
00070 int value;
00071 };
00072
00073 class OBMove
00074 {
00075 Move move;
00076 int stateIndex;
00077 public:
00078 OBMove(Move m,int i) :move(m),stateIndex(i) {}
00079 Move getMove() const { return move; }
00080 int getStateIndex() const { return stateIndex; }
00081 };
00082
00098 class WinCountBook
00099 {
00100 int nStates;
00101 std::ifstream ifs;
00102 public:
00103 WinCountBook(const char *filename);
00104 ~WinCountBook();
00105 int getWinCount(int stateIndex);
00106 int getLoseCount(int stateIndex);
00107 vector<OBMove> getMoves(int stateIndex);
00108 private:
00109 int readInt();
00110 void seek(int offset);
00111 };
00112
00113 class WMove;
00114 std::ostream& operator<<(std::ostream& os, const WMove& w);
00115 std::istream& operator>>(std::istream& is, WMove& w);
00116 class WMove
00117 {
00118 Move move;
00119 int stateIndex;
00120 int weight;
00121 public:
00122 WMove() {}
00123 WMove(Move m, int i, int w)
00124 : move(m), stateIndex(i), weight(w) {}
00125 Move getMove() const { return move; }
00126 int getStateIndex() const { return stateIndex; }
00127 int getWeight() const { return weight; }
00128 void setWeight(const int w) { weight = w; };
00129 friend std::ostream& operator<<(std::ostream& os, const WMove& w);
00130 friend std::istream& operator>>(std::istream& is, WMove& w);
00131 };
00132 inline bool operator==(const WMove& l, const WMove& r)
00133 {
00134 return l.getMove() == r.getMove() && l.getStateIndex() == r.getStateIndex()
00135 && l.getWeight() == r.getWeight();
00136 }
00137
00141 struct WMoveSort : public std::binary_function<WMove, WMove, bool>
00142 {
00143 bool operator()(const WMove& l, const WMove& r) const
00144 {
00145 return l.getWeight() > r.getWeight();
00146 }
00147 };
00148
00152 struct WMoveMoveSort : public std::binary_function<WMove, WMove, bool>
00153 {
00154 bool operator()(const WMove& l, const WMove& r) const
00155 {
00156 return l.getMove().intValue() < r.getMove().intValue();
00157 }
00158 };
00159
00163 struct WMoveWeightMoveSort : public std::binary_function<WMove, WMove, bool>
00164 {
00165 bool operator()(const WMove& l, const WMove& r) const
00166 {
00167 if (l.getWeight() != r.getWeight())
00168 return l.getWeight() > r.getWeight();
00169 return l.getMove().intValue() < r.getMove().intValue();
00170 }
00171 };
00172
00194 class WeightedBook
00195 {
00196 int nStates;
00197 int nMoves;
00198 int startState;
00199 std::ifstream ifs;
00200 public:
00201 typedef vector<WMove> WMoveContainer;
00202
00203 WeightedBook(const char *filename);
00204 ~WeightedBook();
00210 WMoveContainer getMoves(int stateIndex, const bool zero_include = true);
00211 int getWhiteWinCount(int stateIndex);
00212 int getBlackWinCount(int stateIndex);
00213 osl::record::CompactBoard getCompactBoard(int stateIndex);
00214 SimpleState getBoard(int stateIndex);
00215 int getTotalState() const { return nStates; }
00216 int getStartState() const { return startState; }
00217 void validate();
00223 std::vector<int> getParents(const int stateIndex);
00235 int getStateIndex(const SimpleState& state,
00236 const bool visit_zero = true,
00237 const Player player = BLACK);
00246 int getStateIndex(const vector<Move>& moves);
00247 private:
00248 void seek(int offset);
00249 static const int HEADER_SIZE = 16;
00250 static const int STATE_SIZE = 16;
00251 static const int MOVE_SIZE = 12;
00252 static const int BOARD_SIZE = 41 * 4;
00253 };
00254 }
00255 }
00256 }
00257 #endif // _OPENING_BOOK_H
00258
00259
00260
00261