speculativeAllMoves.h Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
speculativeAllMoves.h
Go to the documentation of this file.
1 /* speculativeAllMoves.h
2 */
3 #ifndef OSL_SPECULATIVEALLMOVES_H
4 #define OSL_SPECULATIVEALLMOVES_H
5
6 #include "osl/game_playing/computerPlayer.h"
7 #include "osl/game_playing/speculativeModel.h"
8 #include "osl/misc/lightMutex.h"
9 #include "osl/misc/fixedCapacityVector.h"
10 #include "osl/misc/milliSeconds.h"
11 #include <boost/thread/thread.hpp>
12 #include <boost/thread/condition.hpp>
13 #include <boost/scoped_ptr.hpp>
14 #include <boost/shared_ptr.hpp>
15
16 namespace osl
17 {
18 namespace misc
19 {
20 class RealTime;
21 }
22 namespace search
23 {
24 struct TimeAssigned;
25 }
26 namespace game_playing
27 {
28 class SearchPlayer;
32 class SpeculativeAllMoves : public SpeculativeModel
33 {
34 public:
35 class SearchAllMoves;
36 class ResultVector;
37 private:
38 boost::shared_ptr<SearchAllMoves> searcher;
39 boost::scoped_ptr<boost::thread> thread;
40 boost::scoped_ptr<ResultVector> results;
41 boost::mutex mutex;
42 int last_search_seconds;
43 bool has_byoyomi;
44 bool allowed;
45 HashKey search_state;
46 public:
47 SpeculativeAllMoves();
48 ~SpeculativeAllMoves();
49
50 void startSpeculative(const boost::shared_ptr<GameState> state,
51 const SearchPlayer& main_player);
52 void stopOtherThan(Move);
53 void stopAll();
54
55 void setMaxThreads(int new_max_threads)
56 {
57 boost::mutex::scoped_lock lk(mutex);
58 allowed = (new_max_threads > 0);
59 }
60
61 const MoveWithComment waitResult(Move last_move, search::TimeAssigned wait_for,
62 SearchPlayer& main_player, int byoyomi);
63
64 void selectBestMoveCleanUp();
65 void clearResource();
66 const HashKey searchState() const { return search_state; }
67 private:
68 struct Runner;
69 };
70
71 class SpeculativeAllMoves::ResultVector
72 {
73 typedef FixedCapacityVector<std::pair<Move,MoveWithComment>,Move::MaxUniqMoves> vector_t;
74 vector_t data;
75 typedef LightMutex Mutex;
76 mutable Mutex mutex;
77 public:
78 ResultVector();
79 ~ResultVector();
80
81 void add(Move prediction, const MoveWithComment& result);
82 const MoveWithComment* find(Move prediction) const;
83 void clear();
84 void show(std::ostream&) const;
85 };
86
91 class SpeculativeAllMoves::SearchAllMoves
92 {
93 public:
94 enum Status {
95 INITIAL, RUNNING, PREDICTION1, PREDICTION2, SEARCH1, SEARCH2, FINISHED
96 };
97 struct Generator;
98 friend struct Generator;
99 friend class SpeculativeAllMoves;
100 private:
101 boost::shared_ptr<GameState> state;
102 boost::shared_ptr<SearchPlayer> player;
103 boost::scoped_ptr<Generator> generator;
104 SpeculativeAllMoves::ResultVector& results;
105 double next_iteration_coefficient;
106 Move current_move;
107 volatile Status status;
108 int seconds;
109 typedef boost::mutex Mutex;
110 mutable Mutex mutex;
111 boost::condition condition;
113 volatile bool stop_flag;
114 public:
115 explicit SearchAllMoves(SpeculativeAllMoves::ResultVector&);
116 ~SearchAllMoves();
117
118 void setUp(const GameState&, const SearchPlayer&, int standard_seconds,
119 bool has_byoyomi);
120
121 void run();
122
123 void stopNow();
124 void stopOtherThan(Move);
125 void waitRunning();
126 bool isFinished() const { return status == FINISHED; }
127
128 void setTimeAssign(const search::TimeAssigned&);
129 const MilliSeconds startTime();
130 const Move currentMove() const;
131
132 SearchPlayer* currentPlayer() { return player.get(); }
133 private:
134 const MoveWithComment testMove(Move);
135 struct StatusLock;
136 };
137 } // game_playing
138 } // osl
139
140 #endif /* OSL_SPECULATIVEALLMOVES_H */
141 // ;;; Local Variables:
142 // ;;; mode:c++
143 // ;;; c-basic-offset:2
144 // ;;; End: