openingBook.h
Go to the documentation of this file.
#ifndef _OPENING_BOOK_H
#define _OPENING_BOOK_H
#include "osl/book/compactBoard.h"
#include "osl/basic_type.h"
#include "osl/numEffectState.h"
#include <fstream>
#include <functional>
namespace osl
{
namespace book
{
class OMove
{
public:
OMove(int i) { value = i; }
OMove(Move m) {
const Square from = m.from();
const Square to = m.to();
const int bitFrom = (from.isPieceStand() ? 0 :
(from.x() << 4 | from.y()));
const int bitTo = (to.isPieceStand() ? 0 :
(to.x() << 12 | to.y() << 8));
value = (bitFrom | bitTo |
static_cast<unsigned int>(m.isPromotion()) << 19 |
static_cast<unsigned int>(m.capturePtype()) << 20 |
static_cast<unsigned int>(m.ptype()) << 24 |
static_cast<int>(m.player()) << 28);
}
Square from() {
if ((value & 0xff) == 0)
return Square::STAND();
else
return Square((value >> 4) & 0xf, value & 0xf);
}
Square to() {
if (((value >> 8) & 0xff) == 0)
return Square::STAND();
else
return Square((value >> 12) & 0xf, (value >> 8) & 0xf);
}
bool isPromotion() { return (value >> 19) & 1; }
Ptype capturePtype() {
return static_cast<Ptype>((value >> 20) & 0xf);
}
Ptype ptype() {
return static_cast<Ptype>((value >> 24) & 0xf);
}
Player player() {
return static_cast<Player>((value) >> 28);
}
operator Move() { return Move(from(), to(), ptype(),
capturePtype(), isPromotion(),
player()); }
operator int() { return value; }
private:
int value;
};
struct OBMove
{
Move move;
int state_index;
int stateIndex() const { return state_index; }
};
class WinCountBook
{
int nStates;
std::ifstream ifs;
public:
WinCountBook(const char *filename);
~WinCountBook();
int winCount(int stateIndex);
int loseCount(int stateIndex);
std::vector<OBMove> moves(int stateIndex);
private:
int readInt();
void seek(int offset);
};
struct WMove
{
Move move;
int state_index;
int weight;
int stateIndex() const { return state_index; }
void setWeight(const int w) { weight = w; };
};
std::ostream& operator<<(std::ostream&, const WMove& w);
std::istream& operator>>(std::istream&, WMove& w);
inline bool operator==(const WMove& l, const WMove& r)
{
return l.move == r.move && l.stateIndex() == r.stateIndex()
&& l.weight == r.weight;
}
struct WMoveSort : public std::binary_function<WMove, WMove, bool>
{
bool operator()(const WMove& l, const WMove& r) const {
return l.weight > r.weight;
}
};
struct WMoveMoveSort : public std::binary_function<WMove, WMove, bool>
{
bool operator()(const WMove& l, const WMove& r) const {
return l.move.intValue() < r.move.intValue();
}
};
struct WMoveWeightMoveSort : public std::binary_function<WMove, WMove, bool>
{
bool operator()(const WMove& l, const WMove& r) const {
if (l.weight != r.weight)
return l.weight > r.weight;
return l.move.intValue() < r.move.intValue();
}
};
class WeightedBook
{
int n_states;
int n_moves;
int start_state;
std::ifstream ifs;
public:
typedef std::vector<WMove> WMoveContainer;
WeightedBook(const char *filename);
~WeightedBook();
WMoveContainer moves(int stateIndex, const bool zero_include = true);
int whiteWinCount(int stateIndex);
int blackWinCount(int stateIndex);
CompactBoard compactBoard(int stateIndex);
SimpleState board(int stateIndex);
int totalState() const { return n_states; }
int startState() const { return start_state; }
void validate();
std::vector<int> parents(const int stateIndex);
int stateIndex(const SimpleState& state,
const bool visit_zero = true,
const Player player = BLACK);
int stateIndex(const std::vector<Move>& moves);
private:
void seek(int offset);
static const int HEADER_SIZE = 16;
static const int STATE_SIZE = 16;
static const int MOVE_SIZE = 12;
static const int BOARD_SIZE = 41 * 4;
};
} // book
using book::CompactBoard;
using book::WeightedBook;
} // namespace osl
#endif // _OPENING_BOOK_H
// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End:
basic_type.h
osl::Move
圧縮していない moveの表現 .
Definition basic_type.h:1052
osl::Move::isPromotion
bool isPromotion() const
Definition basic_type.h:1147
osl::Move::ptype
Ptype ptype() const
Definition basic_type.h:1155
osl::Move::player
Player player() const
Definition basic_type.h:1195
osl::Move::capturePtype
Ptype capturePtype() const
Definition basic_type.h:1180
osl::Move::to
const Square to() const
Definition basic_type.h:1132
osl::Move::intValue
int intValue() const
Definition basic_type.h:1065
osl::Move::from
const Square from() const
Definition basic_type.h:1125
osl::SimpleState
Definition simpleState.h:35
osl::Square
Definition basic_type.h:532
osl::Square::STAND
static const Square STAND()
Definition basic_type.h:548
osl::book::CompactBoard
SimpleStateよりcompactな局面の表現
Definition compactBoard.h:60
osl::book::OMove::player
Player player()
Definition openingBook.h:49
osl::book::OMove::isPromotion
bool isPromotion()
Definition openingBook.h:42
osl::book::OMove::to
Square to()
Definition openingBook.h:36
osl::book::OMove::ptype
Ptype ptype()
Definition openingBook.h:46
osl::book::OMove::from
Square from()
Definition openingBook.h:30
osl::book::OMove::capturePtype
Ptype capturePtype()
Definition openingBook.h:43
osl::book::OMove::OMove
OMove(int i)
Definition openingBook.h:16
osl::book::OMove::OMove
OMove(Move m)
Definition openingBook.h:17
osl::book::OMove::value
int value
Definition openingBook.h:57
osl::book::WeightedBook
StateとWMoveを保持する.
Definition openingBook.h:169
osl::book::WeightedBook::start_state
int start_state
Definition openingBook.h:172
osl::book::WeightedBook::STATE_SIZE
static const int STATE_SIZE
Definition openingBook.h:224
osl::book::WeightedBook::validate
void validate()
Definition openingBook.cc:203
osl::book::WeightedBook::MOVE_SIZE
static const int MOVE_SIZE
Definition openingBook.h:225
osl::book::WeightedBook::blackWinCount
int blackWinCount(int stateIndex)
Definition openingBook.cc:194
osl::book::WeightedBook::HEADER_SIZE
static const int HEADER_SIZE
Definition openingBook.h:223
osl::book::WeightedBook::board
SimpleState board(int stateIndex)
Definition openingBook.cc:177
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::WeightedBook::ifs
std::ifstream ifs
Definition openingBook.h:173
osl::book::WeightedBook::totalState
int totalState() const
Definition openingBook.h:189
osl::book::WeightedBook::WMoveContainer
std::vector< WMove > WMoveContainer
Definition openingBook.h:175
osl::book::WeightedBook::compactBoard
CompactBoard compactBoard(int stateIndex)
Definition openingBook.cc:167
osl::book::WeightedBook::n_states
int n_states
Definition openingBook.h:170
osl::book::WeightedBook::BOARD_SIZE
static const int BOARD_SIZE
Definition openingBook.h:226
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::WeightedBook::whiteWinCount
int whiteWinCount(int stateIndex)
Definition openingBook.cc:184
osl::book::WeightedBook::seek
void seek(int offset)
Definition openingBook.cc:141
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::WeightedBook::WeightedBook
WeightedBook(const char *filename)
Definition openingBook.cc:116
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::book::WeightedBook::n_moves
int n_moves
Definition openingBook.h:171
osl::book::WeightedBook::~WeightedBook
~WeightedBook()
Definition openingBook.cc:136
osl::book::WeightedBook::startState
int startState() const
Definition openingBook.h:190
osl::book::WinCountBook::seek
void seek(int offset)
Definition openingBook.cc:60
osl::book::WinCountBook::winCount
int winCount(int stateIndex)
Definition openingBook.cc:85
osl::book::WinCountBook::moves
std::vector< OBMove > moves(int stateIndex)
Definition openingBook.cc:66
osl::book::WinCountBook::loseCount
int loseCount(int stateIndex)
Definition openingBook.cc:92
osl::book::WinCountBook::~WinCountBook
~WinCountBook()
Definition openingBook.cc:43
osl::book::WinCountBook::WinCountBook
WinCountBook(const char *filename)
Definition openingBook.cc:30
osl::book::WinCountBook::nStates
int nStates
Definition openingBook.h:84
osl::book::WinCountBook::readInt
int readInt()
Definition openingBook.cc:48
osl::book::WinCountBook::ifs
std::ifstream ifs
Definition openingBook.h:85
compactBoard.h
osl::book
Definition bookInMemory.h:12
osl::book::operator==
bool operator==(const CompactBoard &, const CompactBoard &)
局面を比較する.
Definition compactBoard.cc:73
osl::book::operator<<
std::ostream & operator<<(std::ostream &os, const CompactBoard &c)
Definition compactBoard.cc:79
osl::book::operator>>
std::istream & operator>>(std::istream &os, CompactBoard &c)
Definition compactBoard.cc:91
osl
Definition additionalEffect.h:6
osl::Ptype
Ptype
駒の種類を4ビットでコード化する
Definition basic_type.h:84
osl::Player
Player
Definition basic_type.h:8
osl::BLACK
@ BLACK
Definition basic_type.h:9
numEffectState.h
osl::book::OBMove
Definition openingBook.h:61
osl::book::OBMove::stateIndex
int stateIndex() const
Definition openingBook.h:64
osl::book::OBMove::move
Move move
Definition openingBook.h:62
osl::book::OBMove::state_index
int state_index
Definition openingBook.h:63
osl::book::WMoveMoveSort
WMoveのMoveによるsort.
Definition openingBook.h:129
osl::book::WMoveMoveSort::operator()
bool operator()(const WMove &l, const WMove &r) const
Definition openingBook.h:130
osl::book::WMoveSort
WMoveのWeightによるsort.
Definition openingBook.h:119
osl::book::WMoveSort::operator()
bool operator()(const WMove &l, const WMove &r) const
Definition openingBook.h:120
osl::book::WMoveWeightMoveSort
WMoveのWeightとMoveによるsort.
Definition openingBook.h:139
osl::book::WMoveWeightMoveSort::operator()
bool operator()(const WMove &l, const WMove &r) const
Definition openingBook.h:140
osl::book::WMove
Definition openingBook.h:98
osl::book::WMove::weight
int weight
Definition openingBook.h:101
osl::book::WMove::state_index
int state_index
Definition openingBook.h:100
osl::book::WMove::move
Move move
Definition openingBook.h:99
osl::book::WMove::setWeight
void setWeight(const int w)
Definition openingBook.h:104
osl::book::WMove::stateIndex
int stateIndex() const
Definition openingBook.h:103