bookInMemory.cc
Go to the documentation of this file.
00001 /* bookInMemory.cc
00002  */
00003 #include "osl/record/opening/bookInMemory.h"
00004 #include "osl/record/opening/openingBook.h"
00005 #include "osl/state/numEffectState.h"
00006 #include "osl/oslConfig.h"
00007 #include <boost/foreach.hpp>
00008 
00009 osl::record::opening::
00010 BookInMemory::BookInMemory()
00011 {
00012   readAll();
00013 }
00014 
00015 osl::record::opening::
00016 BookInMemory::~BookInMemory()
00017 {
00018 }
00019 
00020 int osl::record::opening::
00021 BookInMemory::readRecursive(const HashKey& key, int index, WeightedBook& book, int depth, int /*parent_visit*/)
00022 {
00023   const int depth_threshold = 60, visit_threshold = 4, scale = 16;
00024   if (depth > depth_threshold || table.find(key) != table.end())
00025     return 0;
00026   const int visit = book.getBlackWinCount(index)+book.getWhiteWinCount(index);
00027   if (visit < visit_threshold)
00028     return 0;
00029   const vector<record::opening::WMove>& moves = book.getMoves(index);
00030   FixedCapacityVector<std::pair<int, Move>, 40> children;
00031   BOOST_FOREACH(WMove move, moves)
00032   {
00033     const HashKey child = key.newMakeMove(move.getMove());
00034     const int cv = readRecursive(child, move.getStateIndex(), book, depth+1, visit);
00035     if (cv < visit_threshold || cv*scale < visit)
00036       continue;
00037     children.push_back(std::make_pair(cv, move.getMove()));
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::record::opening::
00056 BookInMemory::readAll()
00057 {
00058   WeightedBook book(OslConfig::openingBook());
00059   int index = book.getStartState();
00060   const NumEffectState state;
00061   readRecursive(HashKey(state), index, book, 0, 0);
00062 }
00063 
00064 void osl::record::opening::
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   BOOST_FOREACH(Move move, p->second)
00071     if (move.isNormal())
00072       out.push_back(move);
00073 }
00074 
00075 const osl::record::opening::BookInMemory&
00076 osl::record::opening::
00077 BookInMemory::instance()
00078 {
00079   static BookInMemory book;
00080   return book;
00081 }
00082 
00083 // ;;; Local Variables:
00084 // ;;; mode:c++
00085 // ;;; c-basic-offset:2
00086 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines