00001 #ifndef GAME_PLAYING_SEARCHPLAYER_H
00002 #define GAME_PLAYING_SEARCHPLAYER_H
00003
00004 #include "osl/game_playing/computerPlayer.h"
00005 #include "osl/search/historyTable.h"
00006 #include "osl/search/hasTimer.h"
00007 #include "osl/misc/alarm.h"
00008 #include "osl/container/moveVector.h"
00009 #include <boost/scoped_ptr.hpp>
00010 #include <boost/shared_ptr.hpp>
00011
00012 namespace osl
00013 {
00014 namespace misc
00015 {
00016 class RealTime;
00017 }
00018 namespace search
00019 {
00020 class SearchRecorder;
00021 class SimpleHashTable;
00022 class BigramKillerMove;
00023 }
00024 namespace game_playing
00025 {
00026 struct Config;
00027 bool operator==(const Config& l, const Config& r);
00028 class TableStack;
00032 class SearchPlayer : public ComputerPlayer
00033 {
00034 public:
00035 struct CheckmateSearcher;
00036 struct NtesukiThread;
00037 struct Config
00038 {
00039 int limit;
00040 int node_limit;
00041 int table_size;
00042 int table_record_limit;
00043 int initial_limit;
00044 int deepening_step;
00045 size_t total_checkmate_limit;
00046 int verbose;
00048 double next_iteration_coefficient;
00050 int draw_coef;
00051 Config();
00052 friend bool operator==(const Config& l, const Config& r);
00053 };
00054 protected:
00055 Config config;
00056 boost::scoped_ptr<search::SimpleHashTable> table_ptr;
00057 boost::shared_ptr<CheckmateSearcher> checkmate_ptr;
00058 boost::scoped_ptr<search::SearchRecorder> recorder_ptr;
00059 boost::scoped_ptr<search::BigramKillerMove> bigram_ptr;
00060 boost::scoped_ptr<TableStack> table_stack_ptr;
00061 class KeyOfMove;
00062 boost::scoped_ptr<KeyOfMove> key_of_move;
00063 AlarmSwitch stop_flag;
00064 HistoryTable history_table;
00065 boost::scoped_ptr<search::HasTimer> searcher;
00067 volatile bool plan_stop;
00068 const MoveVector *root_ignore_moves;
00069 public:
00070 SearchPlayer();
00071 SearchPlayer(const SearchPlayer&);
00072 ~SearchPlayer();
00073
00075 void setTotalCheckmateLimit(int limit);
00076
00077 void setDepthLimit(int limit, int initial_limit, int deepening_step);
00078 void setNodeLimit(int node_limit);
00079 void setTableLimit(int size, int record_limit);
00080 void setVerbose(int verbose=1);
00081 void setDrawCoef(int new_value) { config.draw_coef = new_value; }
00082 void setNextIterationCoefficient(double new_value);
00083
00084 void enableTableStack(bool enable=true);
00086 void resetRecorder(search::SearchRecorder *new_recorder);
00087
00088 void pushMove(Move m);
00089 void popMove();
00090
00094 void swapTable(SearchPlayer& other);
00095
00096 const search::SimpleHashTable* table() const { return table_ptr.get(); }
00097 const search::SearchRecorder& recorder() const { return *recorder_ptr; }
00098
00099 bool stopSearchNow();
00100 bool canStopSearch();
00104 const MoveWithComment selectBestMove(const GameState&, int, int);
00105 protected:
00106 template <class Searcher>
00107 ComputerPlayer* cloneIt(const Searcher&) const;
00109 int setUpTable(const GameState&, int pawn_value);
00110 template <class Searcher>
00111 const MoveWithComment search(const GameState&, int);
00112 template <class Searcher>
00113 bool isReasonableMoveBySearch(Searcher&, Move move, int pawn_sacrifice);
00114 template <class Searcher>
00115 static int pawnValue();
00116 template <class Searcher>
00117 static int pawnValueOfTurn(Player turn);
00118 public:
00119 virtual const MoveWithComment searchWithSecondsForThisMove(const GameState&, int)=0;
00120 void setRootIgnoreMoves(const MoveVector *rim) { root_ignore_moves = rim; }
00124 void pickUpBestMoves(const GameState& state, MoveVector&) const;
00125
00126 const Config& getConfig() const { return config; }
00127
00128 static int secondsForThisMove(const GameState& state,
00129 int total_seconds_left, int byoyomi);
00130
00131 const HistoryTable& historyTable() const { return history_table; }
00132 void setStopSchedule(const misc::RealTime& new_timer);
00133 private:
00134 void fillMoveComment(MoveWithComment&, const GameState&,
00135 const search::SimpleHashTable *, int pawn_value);
00136 };
00137
00138 }
00139 }
00140
00141 #endif
00142
00143
00144
00145