00001 //---------------------------------------------------------------------------- 00002 /** @file Book.hpp 00003 */ 00004 //---------------------------------------------------------------------------- 00005 00006 #ifndef OPENINGBOOK_HPP 00007 #define OPENINGBOOK_HPP 00008 00009 #include "Hex.hpp" 00010 #include "HexBoard.hpp" 00011 #include "HashDB.hpp" 00012 #include "HexEval.hpp" 00013 #include "StateDB.hpp" 00014 #include "SgBookBuilder.h" 00015 00016 _BEGIN_BENZENE_NAMESPACE_ 00017 00018 //---------------------------------------------------------------------------- 00019 00020 /** Class for writing SgBookNodes to the database. */ 00021 class HexBookNode : public SgBookNode 00022 { 00023 public: 00024 HexBookNode(); 00025 00026 HexBookNode(float heurValue); 00027 00028 HexBookNode(const SgBookNode& node); 00029 00030 /** @name Methods for StateDBConcept (so it can be stored in a StateDB) */ 00031 // @{ 00032 00033 int PackedSize() const; 00034 00035 byte* Pack() const; 00036 00037 void Unpack(const byte* t); 00038 00039 void Rotate(const ConstBoard& brd); 00040 00041 // @} 00042 00043 private: 00044 }; 00045 00046 inline HexBookNode::HexBookNode() 00047 : SgBookNode() 00048 { 00049 } 00050 00051 inline HexBookNode::HexBookNode(float heurValue) 00052 : SgBookNode(heurValue) 00053 { 00054 } 00055 00056 inline HexBookNode::HexBookNode(const SgBookNode& node) 00057 : SgBookNode(node) 00058 { 00059 } 00060 00061 inline int HexBookNode::PackedSize() const 00062 { 00063 return sizeof(HexBookNode); 00064 } 00065 00066 inline byte* HexBookNode::Pack() const 00067 { 00068 return (byte*)this; 00069 } 00070 00071 inline void HexBookNode::Unpack(const byte* t) 00072 { 00073 *this = *(const HexBookNode*)t; 00074 } 00075 00076 inline void HexBookNode::Rotate(const ConstBoard& brd) 00077 { 00078 SG_UNUSED(brd); 00079 // No rotation-dependant data 00080 } 00081 00082 //---------------------------------------------------------------------------- 00083 00084 /** A book is just a database of BookNodes. */ 00085 class Book : public StateDB<HexBookNode> 00086 { 00087 public: 00088 static const std::string BOOK_DB_VERSION; 00089 00090 Book(const std::string& filename) 00091 : StateDB<HexBookNode>(filename, BOOK_DB_VERSION) 00092 { } 00093 }; 00094 00095 //---------------------------------------------------------------------------- 00096 00097 /** Utilities on Books. 00098 @ingroup openingbook 00099 */ 00100 namespace BookUtil 00101 { 00102 /** Returns value of board, taking into account swap moves. */ 00103 float Value(const SgBookNode& node, const HexState& brd); 00104 00105 /** Returns score for this node, taking into account the amount of 00106 information in the subtree. Use to select moves when using 00107 book. Note the score is from the pov of the player moving into 00108 this position, not for the player to move in this position. 00109 */ 00110 float Score(const SgBookNode& node, const HexState& brd, 00111 float countWeight); 00112 00113 /** Evaluation for other player. */ 00114 float InverseEval(float eval); 00115 00116 //------------------------------------------------------------------------ 00117 00118 /** Finds best response in book. 00119 @todo Does not consider SWAP_PIECES if it is available. 00120 Returns INVALID_POINT if not in book or if node's count is 00121 less than minCount. */ 00122 HexPoint BestMove(const Book& book, const HexState& state, 00123 unsigned minCount, float countWeight); 00124 00125 //----------------------------------------------------------------------- 00126 00127 /** Writes a (score, depth) pair to output stream for each leaf in 00128 the book. Can be visualized with GnuPlot. */ 00129 void DumpVisualizationData(const Book& book, const HexState& state, 00130 int depth, std::ostream& out); 00131 00132 //----------------------------------------------------------------------- 00133 00134 /** Writes variations leading to non-terminal leafs whose values 00135 differ from 0.5 by at least polarization. The given pv must be 00136 the variation leading to the current state of the board. */ 00137 void DumpPolarizedLeafs(const Book& book, const HexState& state, 00138 float polarization, PointSequence& pv, 00139 std::ostream& out, const StateSet& ignoreSet); 00140 00141 /** Reads solved leaf positions from a file and adds them to the 00142 given book. Overwrites value of any existing states. */ 00143 void ImportSolvedStates(Book& book, const ConstBoard& constBoard, 00144 std::istream& positions); 00145 00146 //----------------------------------------------------------------------- 00147 00148 /** Returns the depth of the mainline from the given state. */ 00149 int GetMainLineDepth(const Book& book, const HexState& state); 00150 00151 /** Returns the number of nodes in the tree rooted at the current 00152 state. */ 00153 std::size_t GetTreeSize(const Book& book, const HexState& state); 00154 } 00155 00156 //---------------------------------------------------------------------------- 00157 00158 _END_BENZENE_NAMESPACE_ 00159 00160 #endif // OPENINGBOOK_HPP