Debian Codegraph @

old-sample/legacy-checkmate/lib/dualCheckmateSearcher.h

raw
1 /* dualCheckmateSearcher.h
2 */
3 #ifndef _DUALCHECKMATESEARCHER_H
4 #define _DUALCHECKMATESEARCHER_H
5
6 #include "checkmateSearcher.h"
7 #include "checkHashTable.h"
8 #include "oraclePool.h"
9 #include "oraclePoolLastMove.h"
10 #include "oracleProver.h"
11 #include "oracleProverLight.h"
12 #include "oracleDisprover.h"
13 #include "oracleAges.h"
14
15 #ifdef CHECKMATE_DEBUG
16 # include "osl/checkmate/analyzer/checkTableAnalyzer.h"
17 #endif
18
19 #include "osl/misc/carray.h"
20
21 #include <boost/shared_ptr.hpp>
22 #include <cassert>
23
24 /** ORACLE を最初に試す */
25 #define USE_ORACLE
26
27 namespace osl
28 {
29 class RepetitionCounter;
30 namespace container
31 {
32 class MoveStack;
33 } // namespace container
34 using container::MoveStack;
35 namespace checkmate
36 {
37 inline size_t limitToCheckCount(int limit){
38 assert(limit >= 0);
39 extern CArray<size_t,32> limitToCheckCountTable;
40 return limitToCheckCountTable[limit>>7];
41 }
42
43 struct SharedOracles
44 {
45 OraclePool oracles;
46 OraclePoolLastMove oracles_last_move;
47 OraclePool oracles_after_attack;
48 void showStatus() const;
49
50 SharedOracles(Player attack);
51 };
52
53 /**
54 * 両方の Player の詰みを扱える詰将棋.
55 * CAVEAT: コピーした場合は、全てのobjectの寿命を揃える必要がある.
56 * 共有される SharedOracles がテーブル内部へのポインタを含むため.
57 */
58 template <class Table=CheckHashTable, class HEstimator=LibertyEstimator, class CostEstimator=PieceCost2>
59 class DualCheckmateSearcher
60 {
61 public:
62 typedef Table table_t;
63 typedef CheckmateSearcher<table_t, HEstimator, CostEstimator> checkmate_t;
64 typedef OracleProver<table_t> prover_t;
65 typedef OracleDisprover<table_t> disprover_t;
66 struct CheckmatorWithOracle
67 {
68 table_t table;
69 checkmate_t searcher;
70 #ifdef OSL_SMP
71 table_t table_small;
72 checkmate_t searcher_small;
73 #endif
74 prover_t prover;
75 disprover_t disprover;
76 int num_cleared;
77 std::shared_ptr<SharedOracles> shared;
78
79 CheckmatorWithOracle(Player attacker, size_t total_nodelimit);
80 /** shared をshareする以外は新規に作る */
81 CheckmatorWithOracle(const CheckmatorWithOracle& src);
82 ~CheckmatorWithOracle();
83 };
84 private:
85 CheckmatorWithOracle black, white;
86 CheckmatorWithOracle& get(Player attacker)
87 {
88 return (attacker == BLACK) ? black : white;
89 }
90 const CheckmatorWithOracle& get(Player attacker) const
91 {
92 return (attacker == BLACK) ? black : white;
93 }
94 SharedOracles& oracles(Player attacker)
95 {
96 return *(get(attacker).shared);
97 }
98 static const int disproof_oracle_record_limit = 500;
99 // statistics
100 size_t simulation_node_count;
101 unsigned int proof_by_oracle, unknown_by_oracle, disproof_by_oracle;
102 unsigned int proof_by_search, unknown_by_search, disproof_by_search;
103 public:
104 /**
105 * @param total_node_count このinstance経由で読む最大のノード数.
106 * isWinningState などで適切な node_limit を与えていれば通常は
107 * 非常に大きな値で良い.
108 */
109 explicit DualCheckmateSearcher(size_t total_node_limit=CHECKMATE_DEFAULT_TOTAL_NODE_LIMIT);
110
111 virtual ~DualCheckmateSearcher();
112 void setVerbose(bool verbose=true) {
113 get(BLACK).searcher.setVerbose(verbose);
114 get(WHITE).searcher.setVerbose(verbose);
115 }
116 private:
117 template <Player P>
118 bool isWinningStateByOracle(int node_limit, NumEffectState& state,
119 const HashKey& key, const PathEncoding& path,
120 Move& best_move, ProofOracleAttack<P> oracle);
121 template <Player P>
122 bool isNotWinningStateByOracle(NumEffectState& state,
123 const HashKey& key, const PathEncoding& path,
124 const DisproofOracleAttack<P>& oracle);
125 public:
126 /**
127 * @return 詰かどうか
128 */
129 template <Player P>
130 bool isWinningStateByOracle(NumEffectState& state,
131 const HashKey& key, const PathEncoding& path,
132 Move& best_move, unsigned short& oracle_age,
133 int node_limit=0);
134 /**
135 * @param oracle_age 同じoracleを2度試さないための栞
136 * @return 不詰かどうか
137 */
138 template <Player P>
139 bool isNotWinningStateByOracle(NumEffectState& state,
140 const HashKey& key, const PathEncoding& path,
141 unsigned short& oracle_age)
142 {
143 assert(state.turn() == P);
144 const PieceStand black_stand = key.blackStand();
145 const CheckHashRecord *guide
146 = oracles(P).oracles.findDisproofOracle(state, black_stand, oracle_age);
147 DisproofOracleAttack<P> oracle(guide);
148 return isNotWinningStateByOracle(state, key, path, oracle);
149 }
150 /**
151 * @param oracle_age 同じoracleを2度試さないための栞
152 * @return 詰かどうか
153 */
154 template <Player P>
155 bool isWinningStateByOracleLastMove(NumEffectState& state,
156 const HashKey& key, const PathEncoding& path,
157 Move& best_move, Move last_move,
158 unsigned short& oracle_age,
159 int node_limit=0);
160 bool isWinningStateByOracleLastMove(NumEffectState& state,
161 const HashKey& key, const PathEncoding& path,
162 Move& best_move, Move last_move,
163 unsigned short& age)
164 {
165 if (state.turn() == BLACK)
166 return isWinningStateByOracleLastMove<BLACK>
167 (state, key, path, best_move, last_move, age);
168 else
169 return isWinningStateByOracleLastMove<WHITE>
170 (state, key, path, best_move, last_move, age);
171 }
172 /**
173 * @return 相手玉が詰み
174 * @param oracle_age 入力:既に試したoracleの数,出力:returnまでに試したoracleの数. (oracleは順にならんでいるので増えたものだけ試す)
175 */
176 template <Player P>
177 bool isWinningState(int node_limit, NumEffectState& state,
178 const HashKey& key, const PathEncoding& path,
179 Move& best_move, AttackOracleAges& oracle_age,
180 Move last_move=Move::INVALID());
181 bool isWinningState(int node_limit, NumEffectState& state,
182 const HashKey& key, const PathEncoding& path,
183 Move& best_move, AttackOracleAges& oracle_age,
184 Move last_move=Move::INVALID());
185 bool isWinningState(int node_limit, NumEffectState& state,
186 const HashKey& key, const PathEncoding& path,
187 Move& best_move, Move last_move=Move::INVALID())
188 {
189 AttackOracleAges dummy_age;
190 return isWinningState(node_limit, state, key, path, best_move,
191 dummy_age, last_move);
192 }
193 /**
194 * @return 自玉が詰み
195 * 条件: 自玉に王手がかかっていること
196 * @param P 手番
197 * @param oracle_age 入力:既に試したoracleの数,出力:returnまでに試したoracleの数. (oracleは順にならんでいるので増えたものだけ試す)
198 */
199 template <Player P>
200 bool isLosingState(int node_limit, NumEffectState& state,
201 const HashKey& key, const PathEncoding& path,
202 Move last_move=Move::INVALID());
203 bool isLosingState(int node_limit, NumEffectState& state,
204 const HashKey&, const PathEncoding& path,
205 Move last_move=Move::INVALID());
206
207 const checkmate_t& searcher(Player P) const { return get(P).searcher; }
208 const table_t& getTable(Player P) const { return get(P).table; }
209 table_t& getTable(Player P) { return get(P).table; }
210 size_t mainNodeCount() const
211 {
212 return searcher(BLACK).totalNodeCount()
213 + searcher(WHITE).totalNodeCount();
214 }
215 size_t totalNodeCount() const
216 {
217 return (simulation_node_count
218 + searcher(BLACK).totalNodeCount()
219 + searcher(WHITE).totalNodeCount()
220 #ifdef OSL_SMP
221 + get(BLACK).searcher_small.totalNodeCount()
222 + get(WHITE).searcher_small.totalNodeCount()
223 #endif
224 + get(BLACK).prover.nodeCount()
225 + get(WHITE).prover.nodeCount()
226 + get(BLACK).disprover.nodeCount()
227 + get(WHITE).disprover.nodeCount());
228 }
229
230 void writeRootHistory(const RepetitionCounter& counter,
231 const MoveStack& moves,
232 const SimpleState& state, Player attack);
233 void undoWriteRootHistory(const RepetitionCounter& counter,
234 const MoveStack& moves,
235 const SimpleState& state, Player attack);
236 };
237
238 } // namespace checkmate
239 using checkmate::DualCheckmateSearcher;
240 } // namespace osl
241
242 #endif /* _DUALCHECKMATESEARCHER_H */
243 // ;;; Local Variables:
244 // ;;; mode:c++
245 // ;;; c-basic-offset:2
246 // ;;; End: