説明を見る。00001
00002
00003 #include "osl/book/bookInMemory.h"
00004 #include "osl/book/openingBook.h"
00005 #include "osl/numEffectState.h"
00006 #include "osl/oslConfig.h"
00007 #include <memory>
00008 #include <map>
00009
00010 osl::book::
00011 BookInMemory::BookInMemory(const std::string& filename)
00012 {
00013 readAll(filename);
00014 }
00015
00016 osl::book::
00017 BookInMemory::~BookInMemory()
00018 {
00019 }
00020
00021 int osl::book::
00022 BookInMemory::readRecursive(const HashKey& key, int index, WeightedBook& book, int depth, int )
00023 {
00024 const int depth_threshold = 60, visit_threshold = 4, scale = 16;
00025 const int visit = book.blackWinCount(index)+book.whiteWinCount(index);
00026 if (depth > depth_threshold || table.find(key) != table.end()
00027 || visit < visit_threshold)
00028 return visit;
00029 const std::vector<book::WMove>& moves = book.moves(index);
00030 FixedCapacityVector<std::pair<int, Move>, 40> children;
00031 for (WMove move: moves)
00032 {
00033 const HashKey child = key.newMakeMove(move.move);
00034 const int cv = readRecursive(child, move.stateIndex(), book, depth+1, visit);
00035 if (cv < visit_threshold || cv*scale < visit || move.weight == 0)
00036 continue;
00037 children.push_back(std::make_pair(cv, move.move));
00038 if (children.size() == children.capacity())
00039 break;
00040 }
00041 std::sort(children.begin(), children.end());
00042 std::reverse(children.begin(), children.end());
00043 if (! children.empty()) {
00044 moves_t& store = table[key];
00045 store.fill(Move());
00046 for (size_t i=0; i<children.size(); ++i) {
00047 store[i] = children[i].second;
00048 if (i+1 == store.size())
00049 break;
00050 }
00051 }
00052 return visit;
00053 }
00054
00055 void osl::book::
00056 BookInMemory::readAll(const std::string& filename)
00057 {
00058 WeightedBook book(OslConfig::openingBook(filename));
00059 int index = book.startState();
00060 const NumEffectState state;
00061 readRecursive(HashKey(state), index, book, 0, 0);
00062 }
00063
00064 void osl::book::
00065 BookInMemory::find(const HashKey& key, MoveVector& out) const
00066 {
00067 table_t::const_iterator p = table.find(key);
00068 if (p == table.end())
00069 return;
00070 for (Move move: p->second)
00071 if (move.isNormal())
00072 out.push_back(move);
00073 }
00074
00075 const osl::book::BookInMemory&
00076 osl::book::
00077 BookInMemory::instance(const std::string& filename)
00078 {
00079 static std::map<std::string,std::shared_ptr<BookInMemory> > table;
00080 std::shared_ptr<BookInMemory> &book = table[filename];
00081 if (! book)
00082 book.reset(new BookInMemory(filename));
00083 return *book;
00084 }
00085
00086
00087
00088
00089