openingBook.cc
Go to the documentation of this file.
#include "osl/book/openingBook.h"
#include "osl/book/compactBoard.h"
#include <algorithm>
#include <iostream>
#include <stdexcept>
int osl::book::readInt(std::istream& is)
{
int ret=0;
CArray<char,4> cs;
is.read(&cs[0],4);
for (int i=0;i<4;i++) {
ret = (ret<<8)|(cs[i]&255);
}
return ret;
}
void osl::book::writeInt(std::ostream& os, int n)
{
CArray<char,4> buf;
for (int i = 0; i < 4; i++)
{
buf[i] = (n >> (8 * (4 - i - 1))) & 255;
}
os.write(&buf[0], 4);
}
#ifndef MINIMAL
osl::book::
WinCountBook::WinCountBook(const char *filename)
: ifs(filename, std::ios_base::binary)
{
if (! ifs)
{
const char *message = "WinCountBook: open failed ";
std::cerr << message << filename << std::endl;
throw std::runtime_error(std::string(message) + filename);
}
nStates=readInt();
}
osl::book::
WinCountBook::~WinCountBook()
{
}
int osl::book::
WinCountBook::readInt()
{
int ret=0;
CArray<char,4> cs;
ifs.read(&cs[0],4);
for (int i=0;i<4;i++) {
ret = (ret<<8)|(cs[i]&255);
}
return ret;
}
void osl::book::
WinCountBook::seek(int offset)
{
ifs.seekg(offset,std::ios::beg);
}
std::vector<osl::book::OBMove> osl::book::
WinCountBook::moves(int stateIndex)
{
assert(stateIndex >= 0);
seek(4+16*stateIndex+8);
int nMoves=readInt();
int moveIndex=readInt();
seek(4+16*nStates+8*moveIndex);
std::vector<OBMove> moves;
moves.reserve(nMoves);
for(int i=0;i<nMoves;i++)
{
Move move=Move::makeDirect(readInt());
int stateIndex=readInt();
moves.push_back({move,stateIndex});
}
return moves;
}
int osl::book::
WinCountBook::winCount(int stateIndex)
{
seek(4+16*stateIndex);
return readInt();
}
int osl::book::
WinCountBook::loseCount(int stateIndex)
{
seek(4+16*stateIndex+4);
return readInt();
}
std::ostream& osl::book::operator<<(std::ostream& os, const WMove& w)
{
writeInt(os, OMove(w.move));
writeInt(os, w.stateIndex());
writeInt(os, w.weight);
return os;
}
#endif
std::istream& osl::book::operator>>(std::istream& is, WMove& w)
{
w.move = OMove(readInt(is)).operator Move();
w.state_index = readInt(is);
w.weight = readInt(is);
return is;
}
osl::book::
WeightedBook::WeightedBook(const char *filename)
: ifs(filename, std::ios_base::binary)
{
if (! ifs)
{
const char *message = "WeightedBook: open failed ";
std::cerr << message << filename << std::endl;
throw std::runtime_error(std::string(message) + filename);
}
#ifndef NDEBUG
int version =
#endif
readInt(ifs);
assert(version == 1);
n_states = readInt(ifs);
n_moves = readInt(ifs);
start_state = readInt(ifs);
}
osl::book::
WeightedBook::~WeightedBook()
{
}
void osl::book::
WeightedBook::seek(int offset)
{
ifs.seekg(offset,std::ios::beg);
}
osl::book::WeightedBook::WMoveContainer osl::book::
WeightedBook::moves(int stateIndex, const bool visit_zero)
{
assert(stateIndex >= 0);
seek(HEADER_SIZE + STATE_SIZE * stateIndex);
int moveIndex=readInt(ifs);
int nWMoves=readInt(ifs);
seek(HEADER_SIZE + STATE_SIZE * n_states + MOVE_SIZE * moveIndex);
std::vector<WMove> moves;
moves.reserve(nWMoves);
for(int i=0;i<nWMoves;i++)
{
WMove wm;
ifs >> wm;
if (!visit_zero && wm.weight == 0) continue;
moves.push_back(wm);
}
return moves;
}
osl::book::CompactBoard osl::book::
WeightedBook::compactBoard(int stateIndex)
{
seek(HEADER_SIZE + STATE_SIZE * n_states + MOVE_SIZE * n_moves
+ BOARD_SIZE * stateIndex);
CompactBoard board;
ifs >> board;
return board;
}
osl::SimpleState osl::book::
WeightedBook::board(int stateIndex)
{
const CompactBoard board = compactBoard(stateIndex);
return board.state();
}
int osl::book::
WeightedBook::whiteWinCount(int stateIndex)
{
seek(HEADER_SIZE + STATE_SIZE * stateIndex);
readInt(ifs);
readInt(ifs);
readInt(ifs);
return readInt(ifs);
}
int osl::book::
WeightedBook::blackWinCount(int stateIndex)
{
seek(HEADER_SIZE + STATE_SIZE * stateIndex);
readInt(ifs);
readInt(ifs);
return readInt(ifs);
}
void osl::book::
WeightedBook::validate()
{
#ifndef NDEBUG
{
SimpleState state(HIRATE);
SimpleState start = board(start_state);
assert(state == start);
}
#endif
std::vector<char> visited(n_states);
std::fill(visited.begin(), visited.end(), false);
std::vector<int> stateToCheck;
stateToCheck.push_back(start_state);
visited[start_state] = true;
while (!stateToCheck.empty())
{
const int index = stateToCheck.back();
stateToCheck.pop_back();
SimpleState state = board(index);
for (WMove move: moves(index))
{
NumEffectState newState(state);
newState.makeMove(move.move);
const int nextIndex = move.stateIndex();
SimpleState stateInFile = board(nextIndex);
assert(newState == stateInFile);
if (!visited[nextIndex])
{
stateToCheck.push_back(nextIndex);
visited[nextIndex] = true;
}
}
}
}
int osl::book::
WeightedBook::stateIndex(const SimpleState& state_to_look_for,
const bool visit_zero,
const Player player)
{
int ret = -1;
const CompactBoard board_to_look_for(state_to_look_for);
const CompactBoard start_state = compactBoard(startState());
if (start_state == board_to_look_for)
{
ret = startState();
return ret;
}
std::vector<char> states(totalState(), false); // mark states that have been visited.
std::vector<int> stateToVisit;
stateToVisit.push_back(startState());
while (!stateToVisit.empty())
{
const int stateIndex = stateToVisit.back();
stateToVisit.pop_back();
states[stateIndex] = true;
WMoveContainer v;
if (visit_zero)
v = moves(stateIndex);
else
{
const CompactBoard stateIndexCB = compactBoard(stateIndex);
const Player turn = stateIndexCB.turn();
const bool zero_include = turn == player ? false : true;
v = moves(stateIndex, zero_include);
}
for (WMove move: v)
{
const int nextIndex = move.stateIndex();
if (! states[nextIndex])
{
const CompactBoard state = compactBoard(nextIndex);
if (state == board_to_look_for)
{
ret = nextIndex;
return ret;
}
stateToVisit.push_back(nextIndex);
}
} // each wmove
} // while loop
return ret;
}
int osl::book::
WeightedBook::stateIndex(const std::vector<osl::Move>& moves)
{
int state_index = startState();
for (Move move: moves)
{
const WMoveContainer wmoves = this->moves(state_index);
WMoveContainer::const_iterator it = wmoves.begin();
for (; it != wmoves.end(); ++it)
if (it->move == move) break;
if (it != wmoves.end())
{
state_index = it->stateIndex(); // next state to visit
continue;
}
return -1; // not found
}
return state_index;
}
std::vector<int> osl::book::
WeightedBook::parents(const int target_state_index)
{
std::vector<int> ret;
if (startState() == target_state_index)
return ret;
std::vector<char> states(totalState(), false); // mark states that have been visited.
std::vector<int> stateToVisit;
stateToVisit.push_back(startState());
while (!stateToVisit.empty())
{
const int stateIndex = stateToVisit.back();
stateToVisit.pop_back();
states[stateIndex] = true;
const WMoveContainer moves = this->moves(stateIndex);
for (WMove move: moves)
{
const int nextIndex = move.stateIndex();
if (nextIndex == target_state_index)
ret.push_back(stateIndex);
if (! states[nextIndex])
stateToVisit.push_back(nextIndex);
} // each wmove
} // while loop
return ret;
}
// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End:
osl::CArray
Definition container.h:20
osl::Move
圧縮していない moveの表現 .
Definition basic_type.h:1052
osl::Move::makeDirect
static const Move makeDirect(int value)
Definition basic_type.h:1093
osl::NumEffectState
利きを持つ局面
Definition numEffectState.h:34
osl::NumEffectState::makeMove
void makeMove(Move move)
Definition numEffectState.cc:201
osl::SimpleState
Definition simpleState.h:35
osl::book::CompactBoard
SimpleStateよりcompactな局面の表現
Definition compactBoard.h:60
osl::book::CompactBoard::turn
Player turn() const
Definition compactBoard.h:66
osl::book::OMove
Definition openingBook.h:14
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::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::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::book::writeInt
void writeInt(std::ostream &os, int n)
Definition openingBook.cc:18
osl::book::readInt
int readInt(std::istream &is)
Definition openingBook.cc:7
osl::Player
Player
Definition basic_type.h:8
osl::HIRATE
@ HIRATE
Definition simpleState.h:21
std
Definition basic_type.h:1353
openingBook.h
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::stateIndex
int stateIndex() const
Definition openingBook.h:103