Go to the documentation of this file.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 struct TimeAssigned;
00025 }
00026 namespace game_playing
00027 {
00028 class SearchPlayer;
00032 class SpeculativeAllMoves : public SpeculativeModel
00033 {
00034 public:
00035 class SearchAllMoves;
00036 class 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 struct 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 struct Generator;
00098 friend struct 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 double next_iteration_coefficient;
00106 Move current_move;
00107 volatile Status status;
00108 int seconds;
00109 typedef boost::mutex Mutex;
00110 mutable Mutex mutex;
00111 boost::condition condition;
00113 volatile bool stop_flag;
00114 public:
00115 explicit SearchAllMoves(SpeculativeAllMoves::ResultVector&);
00116 ~SearchAllMoves();
00117
00118 void setUp(const GameState&, const SearchPlayer&, int standard_seconds,
00119 bool has_byoyomi);
00120
00121 void run();
00122
00123 void stopNow();
00124 void stopOtherThan(Move);
00125 void waitRunning();
00126 bool isFinished() const { return status == FINISHED; }
00127
00128 void setTimeAssign(const search::TimeAssigned&);
00129 const MilliSeconds startTime();
00130 const Move currentMove() const;
00131
00132 SearchPlayer* currentPlayer() { return player.get(); }
00133 private:
00134 const MoveWithComment testMove(Move);
00135 struct StatusLock;
00136 };
00137 }
00138 }
00139
00140 #endif
00141
00142
00143
00144