bookInMemory.cc
Go to the documentation of this file.
/* bookInMemory.cc
*/
#include "osl/book/bookInMemory.h"
#include "osl/book/openingBook.h"
#include "osl/numEffectState.h"
#include "osl/oslConfig.h"
#include <memory>
#include <map>
osl::book::
BookInMemory::BookInMemory(const std::string& filename)
{
readAll(filename);
}
osl::book::
BookInMemory::~BookInMemory()
{
}
int osl::book::
BookInMemory::readRecursive(const HashKey& key, int index, WeightedBook& book, int depth, int /*parent_visit*/)
{
const int depth_threshold = 60, visit_threshold = 4, scale = 16;
const int visit = book.blackWinCount(index)+book.whiteWinCount(index);
if (depth > depth_threshold || table.find(key) != table.end()
|| visit < visit_threshold)
return visit;
const std::vector<book::WMove>& moves = book.moves(index);
FixedCapacityVector<std::pair<int, Move>, 40> children;
for (WMove move: moves)
{
const HashKey child = key.newMakeMove(move.move);
const int cv = readRecursive(child, move.stateIndex(), book, depth+1, visit);
if (cv < visit_threshold || cv*scale < visit || move.weight == 0)
continue;
children.push_back(std::make_pair(cv, move.move));
if (children.size() == children.capacity())
break;
}
std::sort(children.begin(), children.end());
std::reverse(children.begin(), children.end());
if (! children.empty()) {
moves_t& store = table[key];
store.fill(Move());
for (size_t i=0; i<children.size(); ++i) {
store[i] = children[i].second;
if (i+1 == store.size())
break;
}
}
return visit;
}
void osl::book::
BookInMemory::readAll(const std::string& filename)
{
WeightedBook book(OslConfig::openingBook(filename));
int index = book.startState();
const NumEffectState state;
readRecursive(HashKey(state), index, book, 0, 0);
}
void osl::book::
BookInMemory::find(const HashKey& key, MoveVector& out) const
{
table_t::const_iterator p = table.find(key);
if (p == table.end())
return;
for (Move move: p->second)
if (move.isNormal())
out.push_back(move);
}
const osl::book::BookInMemory&
osl::book::
BookInMemory::instance(const std::string& filename)
{
static std::map<std::string,std::shared_ptr<BookInMemory> > table;
std::shared_ptr<BookInMemory> &book = table[filename];
if (! book)
book.reset(new BookInMemory(filename));
return *book;
}
// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End:
bookInMemory.h
osl::CArray::fill
void fill(const T_simple &value=T_simple())
Definition container.h:67
osl::CArray::size
static size_t size()
Definition container.h:76
osl::FixedCapacityVector
Definition container.h:137
osl::FixedCapacityVector::size
size_t size() const
Definition container.h:243
osl::FixedCapacityVector::end
iterator end()
Definition container.h:199
osl::FixedCapacityVector::push_back
void push_back(const T &e)
Definition container.h:204
osl::FixedCapacityVector::empty
bool empty() const
Definition container.h:244
osl::FixedCapacityVector::capacity
size_t capacity() const
Definition container.h:245
osl::FixedCapacityVector::begin
iterator begin()
Definition container.h:198
osl::MoveVector
Definition container.h:293
osl::Move
圧縮していない moveの表現 .
Definition basic_type.h:1052
osl::Move::isNormal
bool isNormal() const
INVALID でも PASS でもない.
Definition basic_type.h:1088
osl::NumEffectState
利きを持つ局面
Definition numEffectState.h:34
osl::book::BookInMemory
Definition bookInMemory.h:15
osl::book::BookInMemory::~BookInMemory
~BookInMemory()
Definition bookInMemory.cc:17
osl::book::BookInMemory::moves_t
CArray< Move, 8 > moves_t
Definition bookInMemory.h:16
osl::book::BookInMemory::table
table_t table
Definition bookInMemory.h:18
osl::book::BookInMemory::readRecursive
int readRecursive(const HashKey &key, int index, WeightedBook &book, int, int)
Definition bookInMemory.cc:22
osl::book::BookInMemory::instance
static const BookInMemory & instance(const std::string &filename="")
Definition bookInMemory.cc:77
osl::book::BookInMemory::readAll
void readAll(const std::string &filename)
Definition bookInMemory.cc:56
osl::book::BookInMemory::find
void find(const HashKey &key, MoveVector &out) const
Definition bookInMemory.cc:65
osl::book::BookInMemory::BookInMemory
BookInMemory(const std::string &filename)
Definition bookInMemory.cc:11
osl::book::WeightedBook
StateとWMoveを保持する.
Definition openingBook.h:169
osl::hash::HashKey
Definition hashKey.h:153
osl::hash::HashKey::newMakeMove
const HashKey newMakeMove(Move) const
Definition hashKey.cc:69
osl::book
Definition bookInMemory.h:12
numEffectState.h
openingBook.h
oslConfig.h
osl::OslConfig::openingBook
static const char * openingBook(const std::string &filenamme="")
標準の定跡ファイルを返す
Definition oslConfig.cc:369
osl::book::WMove
Definition openingBook.h:98