-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.cpp
217 lines (177 loc) · 4.86 KB
/
search.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <iostream>
#include <iomanip>
#include <algorithm>
#include "search.h"
#include "board.h"
#include "hash.h"
using namespace std;
Search * S;
void Node::generate(SQ hash_move)
{
stage = 0;
moves = BB();
if (hash_move > 0)
moves.set(hash_move);
looked = moves;
}
SQ Node::get_next_move()
{
SQ move = 0;
while (!move)
{
if (moves)
{
BB move = moves.lsb();
moves &= ~move;
return move.bitscan();
}
BB opp, occ = B->stones[0] | B->stones[1];
switch (stage)
{
case Stage::StageHash:
stage++;
case Stage::StageStickers:
stage++;
opp = B->stones[B->wtm ^ 1];
moves = opp.expand() & ~occ & ~looked;
looked |= moves;
continue;
case Stage::StageGood:
stage++;
moves = occ.expand() & ~occ & ~looked;
looked |= moves;
continue;
case Stage::StageOthers:
stage++;
moves = ~occ & ~looked;
looked |= moves;
break;
default:
break;
}
return 0;
}
return move;
}
void Search::think()
{
nodes = EMPTY;
status = Status::Playing;
state = node;
timer.set();
for (int depth = 1; depth <= Options::MaxPly; depth++)
{
search_depth = depth;
int val = pvs(-INF, INF, depth);
if (status == Status::Waiting) break;
int ms = static_cast<int>(timer.getms());
cout << right << setw(2) << depth << " - " << SQ_OUT(node->best) << " | ";
if (val > MATE) cout << "#" << left << setw(6) << (INF - val) / 2 + 1;
else if (val < -MATE) cout << "#" << left << setw(6) << -(INF + val) / 2 - 1;
else cout << left << setw(7) << val;
cout << " nps " << setw(6) << 1000 * nodes / (ms + 1)
<< " " << ms << "ms\n";
if (correct(node->best)) best = node->best;
if (val > MATE) if (INF - val <= depth) break;
if (val < -MATE) if (INF + val <= depth) break;
}
cout << "bestmove " << SQ_OUT(best) << "\n\n";
}
int Search::pvs(int alpha, int beta, int depth)
{
int ply = state - node;
if (depth <= 0) return B->eval(ply); // qs(alpha, beta);
int hash_type = HashAlpha;
bool search_pv = true;
state->best = SQ();
int val = -INF;
S->nodes++;
if (S->status == Status::Waiting) return 0;
if (ply >= Options::MaxPly) return 0;
bool in_pv = (beta - alpha) > 1;
// 1.1. Mate pruning /////////////////////////////////////
if (ply > 0)
{
alpha = max(-INF + ply, alpha);
beta = min(INF - (ply + 1), beta);
if (alpha >= beta) return alpha;
}
// 1.2. Hash probe ///////////////////////////////////////
HashEntry * he = 0;
SQ hash_move = 0;
if (Options::Hashing)
{
he = H->get(B->hash);
if (he)
{
if (!B->stones[B->wtm].test(he->move))
hash_move = he->move;
if (ply > 0) // Not in root
{
if (he->depth >= depth)
{
int val = he->val;
if (val > MATE && val <= INF) val -= ply;
else if (val < -MATE && val >= -INF) val += ply;
// Exact score
if (he->type == HashExact) return val;
else if (he->type == HashAlpha && val <= alpha) return alpha;
else if (he->type == HashBeta && val >= beta) return beta;
}
}
}
}
state->generate(hash_move);
while (SQ move = state->get_next_move())
{
make(move);
#ifdef DEBUG_MOVES
for (int i = 0; i < state - node; i++) cout << " ";
cout << SQ_OUT(move) << "\n";
#endif
int new_depth = depth - 1;
bool reduced = false;
if (search_pv)
val = -pvs(-beta, -alpha, new_depth);
else
{
val = -pvs(-alpha - 1, -alpha, new_depth);
if (val > alpha && val < beta)
val = -pvs(-beta, -alpha, new_depth);
}
unmake(move);
if (val > alpha)
{
alpha = val;
hash_type = HashExact;
state->best = move;
search_pv = false;
if (val >= beta)
{
alpha = beta;
hash_type = HashBeta;
break;
}
}
}
if (Options::Hashing)
H->set(B->hash, state->best, depth, ply, alpha, hash_type);
return alpha;
}
void Search::make(SQ move)
{
ASSERT(B->is_empty(move),
{
B->print();
cout << SQ_OUT(move) << "\n";
state->looked.print();
state->moves.print();
});
B->place_stone(move);
state++;
}
void Search::unmake(SQ move)
{
state--;
B->remove_stone(move);
}