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 public:
00078 SearchPlayer();
00079 SearchPlayer(const SearchPlayer&);
00080 ~SearchPlayer();
00081
00082 void setDepthLimit(int limit, int initial_limit, int deepening_step);
00083 void setNodeLimit(size_t node_limit);
00084 void setNodeCountHardLimit(size_t node_limit);
00085 void setTableLimit(size_t size, int record_limit);
00086 void setVerbose(int verbose=1);
00087 void setDrawCoef(int new_value) { config.draw_coef = new_value; }
00088 void setNextIterationCoefficient(double new_value);
00089 double nextIterationCoefficient() const
00090 {
00091 return config.next_iteration_coefficient;
00092 }
00093 void enableSavePV(bool enable=true) { config.save_pv = enable; }
00094 void enableMultiPV(int width) { config.multi_pv_width = width; }
00095 void addMonitor(const boost::shared_ptr<search::SearchMonitor>&);
00096
00098 void resetRecorder(search::CountRecorder *new_recorder);
00099
00100 void pushMove(Move m);
00101 void popMove();
00102
00106 void swapTable(SearchPlayer& other);
00107
00108 const search::SimpleHashTable* table() const { return table_ptr.get(); }
00109 const search::CountRecorder& recorder() const { return *recorder_ptr; }
00110
00111 bool stopSearchNow();
00112 bool canStopSearch();
00116 const MoveWithComment selectBestMove(const GameState&, int, int, int);
00117 const MoveWithComment selectBestMoveInTime(const GameState&, const search::TimeAssigned&);
00118 static const search::TimeAssigned assignTime(const GameState& state, int limit, int elapsed,
00119 int byoyomi, int verbose);
00120 const search::TimeAssigned assignTime(const GameState& state, int limit, int elapsed,
00121 int byoyomi) const;
00122 void saveSearchResult(const GameState&, const MoveWithComment&);
00123 protected:
00124 template <class Searcher>
00125 ComputerPlayer* cloneIt(const Searcher&) const;
00127 const MilliSeconds::Interval setUpTable(const GameState&, int pawn_value);
00128 template <class Searcher>
00129 const MoveWithComment search(const GameState&, const search::TimeAssigned&);
00130 template <class Searcher>
00131 bool isReasonableMoveBySearch(Searcher&, Move move, int pawn_sacrifice);
00132 template <class Searcher>
00133 static int pawnValue();
00134 template <class Searcher>
00135 static int pawnValueOfTurn(Player turn);
00136 const search::TimeAssigned adjust(const search::TimeAssigned& org, const MilliSeconds::Interval& elapsed);
00137 public:
00138 virtual const MoveWithComment searchWithSecondsForThisMove(const GameState&, const search::TimeAssigned&)=0;
00139 void setRootIgnoreMoves(const MoveVector *rim, bool prediction)
00140 {
00141 root_ignore_moves = rim;
00142 prediction_for_speculative_search = prediction;
00143 }
00144
00145 const Config& getConfig() const { return config; }
00146
00147 static int secondsForThisMove(const GameState& state,
00148 int limit, int elapsed, int byoyomi, int verboseness);
00149 int secondsForThisMove(const GameState& state, int limit, int elapsed, int byoyomi) const;
00150
00151 void setTimeAssign(const search::TimeAssigned& new_assign);
00152 const MilliSeconds startTime() const;
00153 };
00154
00155 }
00156 }
00157
00158 #endif
00159
00160
00161
00162