00001
00002
00003 #ifndef OSL_SPECULATIVEALLMOVES_H
00004 #define OSL_SPECULATIVEALLMOVES_H
00005
00006 #include "osl/game_playing/computerPlayer.h"
00007 #include "osl/game_playing/speculativeModel.h"
00008 #include "osl/misc/lightMutex.h"
00009 #include "osl/misc/fixedCapacityVector.h"
00010 #include "osl/misc/milliSeconds.h"
00011 #include <boost/thread/thread.hpp>
00012 #include <boost/thread/condition.hpp>
00013 #include <boost/scoped_ptr.hpp>
00014 #include <boost/shared_ptr.hpp>
00015
00016 namespace osl
00017 {
00018 namespace misc
00019 {
00020 class RealTime;
00021 }
00022 namespace search
00023 {
00024 class TimeAssigned;
00025 }
00026 namespace game_playing
00027 {
00028 class SearchPlayer;
00032 class SpeculativeAllMoves : public SpeculativeModel
00033 {
00034 public:
00035 struct SearchAllMoves;
00036 struct ResultVector;
00037 private:
00038 boost::shared_ptr<SearchAllMoves> searcher;
00039 boost::scoped_ptr<boost::thread> thread;
00040 boost::scoped_ptr<ResultVector> results;
00041 boost::mutex mutex;
00042 int last_search_seconds;
00043 bool has_byoyomi;
00044 bool allowed;
00045 HashKey search_state;
00046 public:
00047 SpeculativeAllMoves();
00048 ~SpeculativeAllMoves();
00049
00050 void startSpeculative(const boost::shared_ptr<GameState> state,
00051 const SearchPlayer& main_player);
00052 void stopOtherThan(Move);
00053 void stopAll();
00054
00055 void setMaxThreads(int new_max_threads)
00056 {
00057 boost::mutex::scoped_lock lk(mutex);
00058 allowed = (new_max_threads > 0);
00059 }
00060
00061 const MoveWithComment waitResult(Move last_move, search::TimeAssigned wait_for,
00062 SearchPlayer& main_player, int byoyomi);
00063
00064 void selectBestMoveCleanUp();
00065 void clearResource();
00066 const HashKey searchState() const { return search_state; }
00067 private:
00068 class Runner;
00069 };
00070
00071 class SpeculativeAllMoves::ResultVector
00072 {
00073 typedef FixedCapacityVector<std::pair<Move,MoveWithComment>,Move::MaxUniqMoves> vector_t;
00074 vector_t data;
00075 typedef LightMutex Mutex;
00076 mutable Mutex mutex;
00077 public:
00078 ResultVector();
00079 ~ResultVector();
00080
00081 void add(Move prediction, const MoveWithComment& result);
00082 const MoveWithComment* find(Move prediction) const;
00083 void clear();
00084 void show(std::ostream&) const;
00085 };
00086
00091 class SpeculativeAllMoves::SearchAllMoves
00092 {
00093 public:
00094 enum Status {
00095 INITIAL, RUNNING, PREDICTION1, PREDICTION2, SEARCH1, SEARCH2, FINISHED
00096 };
00097 class Generator;
00098 friend class Generator;
00099 friend class SpeculativeAllMoves;
00100 private:
00101 boost::shared_ptr<GameState> state;
00102 boost::shared_ptr<SearchPlayer> player;
00103 boost::scoped_ptr<Generator> generator;
00104 SpeculativeAllMoves::ResultVector& results;
00105 Move current_move;
00106 volatile Status status;
00107 int seconds;
00108 typedef boost::mutex Mutex;
00109 mutable Mutex mutex;
00110 boost::condition condition;
00112 volatile bool stop_flag;
00113 public:
00114 explicit SearchAllMoves(SpeculativeAllMoves::ResultVector&);
00115 ~SearchAllMoves();
00116
00117 void setUp(const GameState&, const SearchPlayer&, int standard_seconds,
00118 bool has_byoyomi);
00119
00120 void run();
00121
00122 void stopNow();
00123 void stopOtherThan(Move);
00124 void waitRunning();
00125 bool isFinished() const { return status == FINISHED; }
00126
00127 void setTimeAssign(const search::TimeAssigned&);
00128 const MilliSeconds startTime();
00129 const Move currentMove() const;
00130
00131 SearchPlayer* currentPlayer() { return player.get(); }
00132 private:
00133 const MoveWithComment testMove(Move);
00134 class StatusLock;
00135 };
00136 }
00137 }
00138
00139 #endif
00140
00141
00142
00143