Debian Codegraph @

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

raw
1 #ifndef _CHECKMATE_SEARHCER_H
2 #define _CHECKMATE_SEARHCER_H
3 #include "osl/checkmate/proofDisproof.h"
4 #include "checkHashTable.h"
5 #include "checkHashRecord.h"
6 #include "pieceCost2.h"
7 #include "osl/checkmate/fixedDepthSearcher.h"
8 #include "osl/state/numEffectState.h"
9 #include "osl/hash/hashKey.h"
10 #include "osl/pathEncoding.h"
11 #include "osl/move.h"
12 #include <cstddef>
13
14 #ifndef CHECKMATE_DEFAULT_TOTAL_NODE_LIMIT
15 #define CHECKMATE_DEFAULT_TOTAL_NODE_LIMIT (5000000*2)
16 // for 8gb?
17 // #define CHECKMATE_DEFAULT_TOTAL_NODE_LIMIT 5000000
18 // for 2gb memory
19 // #define CHECKMATE_DEFAULT_TOTAL_NODE_LIMIT 1000000
20 #endif
21
22 namespace osl
23 {
24 namespace checkmate
25 {
26 class CheckMove;
27 class CheckMoveList;
28 class LibertyEstimator;
29 class PieceCost2;
30 /**
31 * dfpn^+ extension.
32 *
33 * - extension by Kishimoto
34 * - avoid GHI problem by path-encoding and smulation
35 * - decrease ratio of re-extension of interior nodes
36 * - evaluation functions
37 * - more pieces -> less proof number (Kishimoto)
38 * @param Table SimpleCheckHashTable とか ArrayCheckHashTable など
39 */
40 template<class Table=CheckHashTable,
41 class HEstimator=LibertyEstimator, class CostEstimator=PieceCost2>
42 class CheckmateSearcher
43 {
44 public:
45 typedef NumEffectState state_t;
46 typedef Table table_t;
47 private:
48 state_t *state; // acquaintance, delete しないこと
49 Table& table;
50 FixedDepthSearcher fixed_searcher;
51 HashKey key;
52 PathEncoding path;
53 int depth;
54 /** 現在の探索で消費したノード数 */
55 size_t node_count;
56 /** 現在の探索で消費可能なノード数 */
57 size_t search_node_limit;
58 /** objectが生成されてから消費したノード数 */
59 size_t total_node_count;
60 /** objectが生成されてから消費可能なノード数 */
61 const size_t total_node_limit;
62 bool verbose_destructor;
63 const Player attacker;
64 void clearNodeCount() { total_node_count+=node_count; node_count=0; }
65 public:
66 /**
67 * @param attacker 攻撃側プレイヤー (オブジェクトはプレイヤ専用)
68 */
69 CheckmateSearcher(Player attacker, Table& t,
70 size_t limit=CHECKMATE_DEFAULT_TOTAL_NODE_LIMIT);
71 ~CheckmateSearcher();
72 size_t totalNodeCount() const { return total_node_count; }
73 size_t totalNodeLimit() const { return total_node_limit; }
74 bool verbose() const { return verbose_destructor; }
75 void setVerbose(bool verbose=true) { verbose_destructor = verbose; }
76 /**
77 * stateがPから詰む局面かを返す.
78 * stateの手番はPと一致しているという前提
79 * @param search_node_limit 探索するnode数の目安
80 * Successの時はbest_moveにその手を
81 */
82 template <Player P>
83 const ProofDisproof
84 hasCheckmateMove(state_t& state,
85 const HashKey& key, const PathEncoding& path,
86 size_t search_node_limit, Move& best_move);
87 const ProofDisproof
88 hasCheckmateMove(state_t& state, const HashKey& key,
89 const PathEncoding& path, size_t limit, Move& best_move)
90 {
91 if (attacker == BLACK)
92 return hasCheckmateMove<BLACK>(state, key, path, limit, best_move);
93 else
94 return hasCheckmateMove<WHITE>(state, key, path, limit, best_move);
95 }
96 /**
97 * stateがPによって詰んでいる局面かを返す
98 * 今のところ打ち歩詰めには対応していない
99 * 王手がかかっていない時には呼ばない
100 * stateの手番はalt(P)と一致しているという前提
101 * stateはPによって王手がかかっているという前提
102 * @param search_node_limit 探索するnode数の目安
103 * @param last_move 打ち歩詰めの判定に必要
104 */
105 template <Player P>
106 const ProofDisproof
107 hasEscapeMove(state_t& state, const HashKey&, const PathEncoding& path,
108 size_t search_node_limit, Move last_move);
109 const ProofDisproof
110 hasEscapeMove(state_t& state,
111 const HashKey& key, const PathEncoding& path,
112 size_t limit, Move last_move)
113 {
114 if (attacker == BLACK)
115 return hasEscapeMove<BLACK>(state, key, path, limit, last_move);
116 else
117 return hasEscapeMove<WHITE>(state, key, path, limit, last_move);
118 }
119 /**
120 * attack側
121 * @param record そのノードの CheckHashRecord のデータ
122 * 0 以外で渡すと attack の中でテーブルをひかずに利用する
123 * 0 で渡すと attack の中で新たにレコードを作った場合そのポインタが帰る
124 * ポインタの参照なので注意
125 * ProofDisproof は record->proofDisproof() に格納されて帰る
126 * @param parent 親ノードのポインタ。合流チェック用。
127 */
128 template <Player P>
129 void attack(unsigned int proofLimit, unsigned int disproofLimit,
130 CheckHashRecord *parent, CheckHashRecord *record);
131 /**
132 * 詰みを逃れる手
133 * alt(P)による
134 * @param record そのノードの CheckHashRecord のデータ
135 * 0 以外で渡すと attack の中でテーブルをひかずに利用する
136 * 0 で渡すと attack の中で新たにレコードを作った場合そのポインタが帰る
137 * ProofDisproof は record->proofDisproof() に格納されて帰る
138 */
139 template <Player P>
140 void defense(unsigned int proofLimit, unsigned int disproofLimit,
141 CheckHashRecord *parent, CheckHashRecord *record);
142
143 const table_t& getTable() const { return table; }
144 private:
145 /**
146 * attack の準備: 初めて訪れる局面で、合流チェック、データ確保、手生成などを行う
147 * @param record 呼び出し時は0, 呼び出し後は確保したデータへのポインタ
148 * @return true ならlimit over
149 */
150 template <Player P>
151 bool setUpAttackNode(CheckHashRecord *record);
152 /**
153 * defense の準備: 初めて訪れる局面で、合流チェック、データ確保、手生成などを行う
154 * @param record 呼び出し時は0, 呼び出し後は確保したデータへのポインタ
155 * @return true ならlimit over
156 */
157 template <Player P>
158 bool setUpDefenseNode(CheckHashRecord *record);
159
160 bool exceedNodeCount(unsigned int futureCost) const;
161 static bool exceedRootTolerance(unsigned int proofNumber, unsigned int disproofNumber,
162 unsigned int continuousNoExpandLoop);
163 };
164 } // namespace checkmate
165 using checkmate::CheckmateSearcher;
166 } // namespace osl
167 #endif // _CHECKMATE_SEARHCER_H
168 // ;;; Local Variables:
169 // ;;; mode:c++
170 // ;;; c-basic-offset:2
171 // ;;; End: