My Project
bookInMemory.cc
Go to the documentation of this file.
1 /* bookInMemory.cc
2  */
4 #include "osl/book/openingBook.h"
5 #include "osl/numEffectState.h"
6 #include "osl/oslConfig.h"
7 #include <memory>
8 #include <map>
9 
11 BookInMemory::BookInMemory(const std::string& filename)
12 {
13  readAll(filename);
14 }
15 
18 {
19 }
20 
22 BookInMemory::readRecursive(const HashKey& key, int index, WeightedBook& book, int depth, int /*parent_visit*/)
23 {
24  const int depth_threshold = 60, visit_threshold = 4, scale = 16;
25  const int visit = book.blackWinCount(index)+book.whiteWinCount(index);
26  if (depth > depth_threshold || table.find(key) != table.end()
27  || visit < visit_threshold)
28  return visit;
29  const std::vector<book::WMove>& moves = book.moves(index);
31  for (WMove move: moves)
32  {
33  const HashKey child = key.newMakeMove(move.move);
34  const int cv = readRecursive(child, move.stateIndex(), book, depth+1, visit);
35  if (cv < visit_threshold || cv*scale < visit || move.weight == 0)
36  continue;
37  children.push_back(std::make_pair(cv, move.move));
38  if (children.size() == children.capacity())
39  break;
40  }
41  std::sort(children.begin(), children.end());
42  std::reverse(children.begin(), children.end());
43  if (! children.empty()) {
44  moves_t& store = table[key];
45  store.fill(Move());
46  for (size_t i=0; i<children.size(); ++i) {
47  store[i] = children[i].second;
48  if (i+1 == store.size())
49  break;
50  }
51  }
52  return visit;
53 }
54 
56 BookInMemory::readAll(const std::string& filename)
57 {
58  WeightedBook book(OslConfig::openingBook(filename));
59  int index = book.startState();
60  const NumEffectState state;
61  readRecursive(HashKey(state), index, book, 0, 0);
62 }
63 
65 BookInMemory::find(const HashKey& key, MoveVector& out) const
66 {
67  table_t::const_iterator p = table.find(key);
68  if (p == table.end())
69  return;
70  for (Move move: p->second)
71  if (move.isNormal())
72  out.push_back(move);
73 }
74 
77 BookInMemory::instance(const std::string& filename)
78 {
79  static std::map<std::string,std::shared_ptr<BookInMemory> > table;
80  std::shared_ptr<BookInMemory> &book = table[filename];
81  if (! book)
82  book.reset(new BookInMemory(filename));
83  return *book;
84 }
85 
86 // ;;; Local Variables:
87 // ;;; mode:c++
88 // ;;; c-basic-offset:2
89 // ;;; End:
void fill(const T_simple &value=T_simple())
Definition: container.h:67
static size_t size()
Definition: container.h:76
size_t size() const
Definition: container.h:243
void push_back(const T &e)
Definition: container.h:204
size_t capacity() const
Definition: container.h:245
圧縮していない moveの表現 .
Definition: basic_type.h:1052
bool isNormal() const
INVALID でも PASS でもない.
Definition: basic_type.h:1088
利きを持つ局面
int readRecursive(const HashKey &key, int index, WeightedBook &book, int, int)
Definition: bookInMemory.cc:22
static const BookInMemory & instance(const std::string &filename="")
Definition: bookInMemory.cc:77
void readAll(const std::string &filename)
Definition: bookInMemory.cc:56
void find(const HashKey &key, MoveVector &out) const
Definition: bookInMemory.cc:65
BookInMemory(const std::string &filename)
Definition: bookInMemory.cc:11
StateとWMoveを保持する.
Definition: openingBook.h:169
int blackWinCount(int stateIndex)
Definition: openingBook.cc:194
WMoveContainer moves(int stateIndex, const bool zero_include=true)
Return moves from the state of the stateIndex.
Definition: openingBook.cc:147
int whiteWinCount(int stateIndex)
Definition: openingBook.cc:184
const HashKey newMakeMove(Move) const
Definition: hashKey.cc:69