00001
00002
00003 #ifndef _MTDF_RECORDER_H
00004 #define _MTDF_RECORDER_H
00005
00006 #include "osl/player.h"
00007 #include "osl/move.h"
00008 #include "osl/misc/lightMutex.h"
00009 #include <boost/scoped_ptr.hpp>
00010 #include <iosfwd>
00011 namespace osl
00012 {
00013 class MoveLogProb;
00014 namespace state
00015 {
00016 class SimpleState;
00017 }
00018 namespace search
00019 {
00027 class CountRecorder
00028 {
00029 size_t node_count;
00030 size_t quiescence_count;
00031 size_t checkmate_count;
00032 #ifdef OSL_SMP
00033 mutable LightMutex mutex;
00034 #endif
00035 public:
00036 CountRecorder();
00037 virtual ~CountRecorder();
00038
00040 void addNodeCount(int count=1) {
00041 #if (defined OSL_SMP) && (defined OSL_USE_RACE_DETECTOR)
00042 SCOPED_LOCK(lk,mutex);
00043 #endif
00044 node_count += count;
00045 }
00046 void addQuiescenceCount(int count=1) {
00047 #if (defined OSL_SMP) && (defined OSL_USE_RACE_DETECTOR)
00048 SCOPED_LOCK(lk,mutex);
00049 #endif
00050 quiescence_count += count;
00051 }
00052 void addCheckmateCount(int count=1) {
00053 #ifdef OSL_SMP
00054 SCOPED_LOCK(lk,mutex);
00055 #endif
00056 checkmate_count += count;
00057 }
00058 void setCheckmateCount(int count) {
00059 #ifdef OSL_SMP
00060 SCOPED_LOCK(lk,mutex);
00061 #endif
00062 checkmate_count = count;
00063 }
00064
00065 void resetNodeCount();
00066 size_t nodeCount() const {
00067 #if (defined OSL_SMP) && (defined OSL_USE_RACE_DETECTOR)
00068 SCOPED_LOCK(lk,mutex);
00069 #endif
00070 return node_count;
00071 }
00072 size_t quiescenceCount() const {
00073 #if (defined OSL_SMP) && (defined OSL_USE_RACE_DETECTOR)
00074 SCOPED_LOCK(lk,mutex);
00075 #endif
00076 return quiescence_count;
00077 }
00078 size_t checkmateCount() const {
00079 #if (defined OSL_SMP) && (defined OSL_USE_RACE_DETECTOR)
00080 SCOPED_LOCK(lk,mutex);
00081 #endif
00082 return checkmate_count;
00083 }
00084 size_t searchNodeCount() const
00085 {
00086 #if (defined OSL_SMP) && (defined OSL_USE_RACE_DETECTOR)
00087 SCOPED_LOCK(lk,mutex);
00088 #endif
00089 return node_count + quiescence_count;
00090 }
00091 size_t allNodeCount() const {
00092 #if (defined OSL_SMP) && (defined OSL_USE_RACE_DETECTOR)
00093 SCOPED_LOCK(lk,mutex);
00094 #endif
00095 return node_count + quiescence_count + checkmate_count;
00096 }
00097 double checkmateRatio() const
00098 {
00099 const double checkmate = checkmateCount();
00100 const double search = searchNodeCount();
00101 return checkmate / (checkmate + search);
00102 }
00104 void tryMove(const MoveLogProb& , int , int ) const {}
00106 void retryMove(const MoveLogProb& , int , int ,
00107 int ) const {}
00109 void recordValue(const MoveLogProb&, int , bool ,
00110 int ) const {}
00111
00113 void recordTopLevelLowFail(const MoveLogProb& , int ) const {}
00114 void recordTopLevelHighFail(const MoveLogProb& , int ) const {}
00115
00116 void tableHitLowerBound(Player, int, int , int ) const {}
00117 void tableHitUpperBound(Player, int, int , int ) const {}
00118
00119 void tableStoreLowerBound(Player, const MoveLogProb&, int, int) const {}
00120 void tableStoreUpperBound(Player, const MoveLogProb&, int, int) const {}
00121
00122
00123 void startSearch(int ) const {}
00125 virtual void finishSearch(Move best, double seconds_consumed,
00126 bool verbose) const;
00127
00128 void recordInvalidMoveInTable(const state::SimpleState&,
00129 const MoveLogProb&, int limit) const;
00130 void newCategory(const char * , int ) const {}
00131
00133 void gotoCheckmateSearch(const state::SimpleState&, int) const {}
00134 void backFromCheckmateSearch() const {}
00135
00136 void reportCount(std::ostream&, double seconds) const;
00137 void reportCount(std::ostream&) const;
00138 };
00139
00140 class SearchRecorder : public CountRecorder
00141 {
00142 struct Recorder;
00144 boost::scoped_ptr<Recorder> recorder;
00145 public:
00146 explicit SearchRecorder(const char *filename="mtdf.log");
00147 ~SearchRecorder();
00148
00150 void setLogMargin(int margin=500);
00151
00152 void tryMove(const MoveLogProb& m, int last_f, int limit) const;
00153 void retryMove(const MoveLogProb& m, int last_f, int limit,
00154 int retryCount) const;
00155
00156 void recordValue(const MoveLogProb& m, int val, bool betterMove, int limit) const;
00157
00158 void tableHitLowerBound(Player p, int val, int last_f, int limit) const;
00159 void tableHitUpperBound(Player p, int val, int last_f, int limit) const;
00160
00161 void tableStoreLowerBound(Player p, const MoveLogProb& best_move, int val, int limit) const;
00162 void tableStoreUpperBound(Player p, const MoveLogProb& best_move, int val, int limit) const;
00163
00164 void recordTopLevelLowFail(const MoveLogProb& , int last_f) const;
00165 void recordTopLevelHighFail(const MoveLogProb& best_move, int last_f) const;
00166
00167 void startSearch(int limit) const;
00168 void finishSearch(Move best_move, double seconds_consumed, bool verbose) const;
00169
00170 void newCategory(const char *name, int limit) const;
00171
00172 void gotoCheckmateSearch(const state::SimpleState&, int nodeLimit) const;
00173 void backFromCheckmateSearch() const;
00174
00176 std::ostream& stream() const;
00177 };
00178 }
00179
00180 using search::CountRecorder;
00181 using search::SearchRecorder;
00182 }
00183
00184
00185 #endif
00186
00187
00188
00189