Go to the documentation of this file.00001
00002
00003
00004
00005 #ifndef GOAUTOBOOK_H
00006 #define GOAUTOBOOK_H
00007
00008 #include <cmath>
00009 #include <iostream>
00010 #include <fstream>
00011 #include <set>
00012 #include <map>
00013 #include "SgBookBuilder.h"
00014 #include "SgThreadedWorker.h"
00015 #include "GoBoard.h"
00016 #include "GoBoardSynchronizer.h"
00017
00018
00019
00020
00021 class GoAutoBookState
00022 {
00023 public:
00024 GoAutoBookState(const GoBoard& brd);
00025
00026 ~GoAutoBookState();
00027
00028 GoBoard& Board();
00029
00030 const GoBoard& Board() const;
00031
00032 void Play(SgMove move);
00033
00034 void Undo();
00035
00036 SgHashCode GetHashCode() const;
00037
00038 void Synchronize();
00039
00040 private:
00041 GoBoardSynchronizer m_synchronizer;
00042
00043 GoBoard m_brd[8];
00044
00045 SgHashCode m_hash;
00046
00047 void ComputeHashCode();
00048 };
00049
00050 inline GoBoard& GoAutoBookState::Board()
00051 {
00052 return m_brd[0];
00053 }
00054
00055 inline const GoBoard& GoAutoBookState::Board() const
00056 {
00057 return m_brd[0];
00058 }
00059
00060
00061
00062 typedef enum
00063 {
00064
00065 GO_AUTOBOOK_SELECT_COUNT,
00066
00067
00068 GO_AUTOBOOK_SELECT_VALUE
00069
00070 } GoAutoBookMoveSelectType;
00071
00072 struct GoAutoBookParam
00073 {
00074
00075
00076 std::size_t m_usageCountThreshold;
00077
00078
00079 GoAutoBookMoveSelectType m_selectType;
00080
00081 GoAutoBookParam();
00082 };
00083
00084
00085
00086
00087
00088 class GoAutoBook
00089 {
00090 public:
00091 GoAutoBook(const std::string& filename,
00092 const GoAutoBookParam& param);
00093
00094 ~GoAutoBook();
00095
00096
00097
00098 bool Get(const GoAutoBookState& state, SgBookNode& node) const;
00099
00100
00101 void Put(const GoAutoBookState& state, const SgBookNode& node);
00102
00103
00104 void Flush();
00105
00106
00107 void Save(const std::string& filename) const;
00108
00109
00110 SgMove LookupMove(const GoBoard& brd) const;
00111
00112
00113
00114
00115 SgMove FindBestChild(GoAutoBookState& state) const;
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126 void Merge(const GoAutoBook& other);
00127
00128
00129
00130
00131 void AddDisabledLines(const std::set<SgHashCode>& disabled);
00132
00133
00134 void TruncateByDepth(int depth, GoAutoBookState& state,
00135 GoAutoBook& other) const;
00136
00137
00138
00139 void ImportHashValuePairs(std::istream& in);
00140
00141
00142
00143
00144 void ExportToOldFormat(GoAutoBookState& state, std::ostream& os) const;
00145
00146
00147
00148
00149 static std::vector< std::vector<SgMove> > ParseWorkList(std::istream& in);
00150
00151 private:
00152 typedef std::map<SgHashCode, SgBookNode> Map;
00153
00154 Map m_data;
00155
00156 const GoAutoBookParam& m_param;
00157
00158 std::set<SgHashCode> m_disabled;
00159
00160 std::string m_filename;
00161
00162 void TruncateByDepth(int depth, GoAutoBookState& state,
00163 GoAutoBook& other,
00164 std::set<SgHashCode>& seen) const;
00165
00166 void ExportToOldFormat(GoAutoBookState& state, std::ostream& out,
00167 std::set<SgHashCode>& seen) const;
00168
00169 };
00170
00171 inline void GoAutoBook::AddDisabledLines(const std::set<SgHashCode>& disabled)
00172 {
00173 m_disabled.insert(disabled.begin(), disabled.end());
00174 SgDebug() << "Disabled " << disabled.size() << " lines.\n";
00175 }
00176
00177
00178
00179 #endif // GOAUTOBOOK_H