Go to the documentation of this file.00001 #ifndef GAME_PLAYING_SEARCHPLAYER_H
00002 #define GAME_PLAYING_SEARCHPLAYER_H
00003
00004 #include "osl/game_playing/computerPlayer.h"
00005 #include "osl/search/searchTimer.h"
00006 #include "osl/misc/milliSeconds.h"
00007 #include "osl/container/moveVector.h"
00008 #include <boost/scoped_ptr.hpp>
00009 #include <boost/shared_ptr.hpp>
00010
00011 namespace osl
00012 {
00013 namespace misc
00014 {
00015 class RealTime;
00016 }
00017 namespace search
00018 {
00019 class CountRecorder;
00020 class SimpleHashTable;
00021 struct TimeAssigned;
00022 class SearchMonitor;
00023 }
00024 namespace checkmate
00025 {
00026 class DualDfpn;
00027 }
00028 namespace game_playing
00029 {
00030 struct Config;
00031 bool operator==(const Config& l, const Config& r);
00032 struct PVHistory;
00036 class SearchPlayer
00037 : public ComputerPlayer,
00038 public ComputerPlayerSelectBestMoveInTime
00039 {
00040 public:
00041 struct NtesukiThread;
00042 struct Config
00043 {
00044 int limit;
00045 size_t node_limit;
00046 size_t table_size;
00047 int table_record_limit;
00048 int initial_limit;
00049 int deepening_step;
00050 size_t total_checkmate_limit;
00051 int verbose;
00053 double next_iteration_coefficient;
00055 int draw_coef;
00056 bool save_pv;
00057 uint64_t node_count_hard_limit;
00059 int multi_pv_width;
00060 vector<boost::shared_ptr<search::SearchMonitor> > monitors;
00061
00062 Config();
00063 friend bool operator==(const Config& l, const Config& r);
00064 };
00065 protected:
00066 Config config;
00067 boost::shared_ptr<search::SimpleHashTable> table_ptr;
00068 boost::shared_ptr<checkmate::DualDfpn> checkmate_ptr;
00069 boost::scoped_ptr<search::CountRecorder> recorder_ptr;
00070 volatile bool searching;
00071 boost::scoped_ptr<search::SearchTimer> searcher;
00073 volatile bool plan_stop;
00074 const MoveVector *root_ignore_moves;
00075 bool prediction_for_speculative_search;
00076 boost::scoped_ptr<PVHistory> pv_history;
00077 int almost_resign_count;
00078 public:
00079 SearchPlayer();
00080 SearchPlayer(const SearchPlayer&);
00081 ~SearchPlayer();
00082
00083 void setDepthLimit(int limit, int initial_limit, int deepening_step);
00084 void setNodeLimit(size_t node_limit);
00085 void setNodeCountHardLimit(size_t node_limit);
00086 void setTableLimit(size_t size, int record_limit);
00087 void setVerbose(int verbose=1);
00088 void setDrawCoef(int new_value) { config.draw_coef = new_value; }
00089 void setNextIterationCoefficient(double new_value);
00090 double nextIterationCoefficient() const
00091 {
00092 return config.next_iteration_coefficient;
00093 }
00094 void enableSavePV(bool enable=true) { config.save_pv = enable; }
00095 void enableMultiPV(int width) { config.multi_pv_width = width; }
00096 void addMonitor(const boost::shared_ptr<search::SearchMonitor>&);
00097
00099 void resetRecorder(search::CountRecorder *new_recorder);
00100
00101 void pushMove(Move m);
00102 void popMove();
00103
00107 void swapTable(SearchPlayer& other);
00108
00109 const search::SimpleHashTable* table() const { return table_ptr.get(); }
00110 const search::CountRecorder& recorder() const { return *recorder_ptr; }
00111
00112 bool stopSearchNow();
00113 bool canStopSearch();
00117 const MoveWithComment selectBestMove(const GameState&, int limit, int elapsed,
00118 int byoyomi);
00119 const MoveWithComment selectBestMoveInTime(const GameState&, const search::TimeAssigned&);
00120 static const search::TimeAssigned assignTime(const GameState& state, int limit, int elapsed,
00121 int byoyomi, int verbose);
00122 const search::TimeAssigned assignTime(const GameState& state, int limit, int elapsed,
00123 int byoyomi) const;
00124 void saveSearchResult(const GameState&, const MoveWithComment&);
00125 protected:
00126 template <class Searcher>
00127 ComputerPlayer* cloneIt(const Searcher&) const;
00129 const MilliSeconds::Interval setUpTable(const GameState&, int pawn_value);
00130 template <class Searcher>
00131 const MoveWithComment search(const GameState&, const search::TimeAssigned&);
00132 template <class Searcher>
00133 bool isReasonableMoveBySearch(Searcher&, Move move, int pawn_sacrifice);
00134 template <class Searcher>
00135 static int pawnValue();
00136 template <class Searcher>
00137 static int pawnValueOfTurn(Player turn);
00138 const search::TimeAssigned adjust(const search::TimeAssigned& org, const MilliSeconds::Interval& elapsed);
00139 public:
00140 virtual const MoveWithComment searchWithSecondsForThisMove(const GameState&, const search::TimeAssigned&)=0;
00141 void setRootIgnoreMoves(const MoveVector *rim, bool prediction)
00142 {
00143 root_ignore_moves = rim;
00144 prediction_for_speculative_search = prediction;
00145 }
00146
00147 const Config& getConfig() const { return config; }
00148
00149 static int secondsForThisMove(const GameState& state,
00150 int limit, int elapsed, int byoyomi, int verboseness);
00151 int secondsForThisMove(const GameState& state, int limit, int elapsed, int byoyomi) const;
00152
00153 void setTimeAssign(const search::TimeAssigned& new_assign);
00154 const MilliSeconds startTime() const;
00155 };
00156
00157 }
00158 }
00159
00160 #endif
00161
00162
00163
00164