Compare commits
1
Commits
6057476214
...
cdeb318016
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cdeb318016 |
No files matched your search
+34
-8
@@ -199,7 +199,10 @@ void Searcher::verify_tree() const {
|
|||||||
next.make_move(m);
|
next.make_move(m);
|
||||||
stack.push_back({node.first_child + c, next});
|
stack.push_back({node.first_child + c, next});
|
||||||
}
|
}
|
||||||
if (!ok || node.visits < child_visits + 1) {
|
// The root is expanded before its first visit, so it may have exactly
|
||||||
|
// as many visits as its children.
|
||||||
|
const u64 own_visit = item.idx == Tree::root() ? 0 : 1;
|
||||||
|
if (!ok || node.visits < child_visits + own_visit) {
|
||||||
std::cerr << "TREE CORRUPT at node " << item.idx << " (" << item.pos.fen() << ") ok=" << ok
|
std::cerr << "TREE CORRUPT at node " << item.idx << " (" << item.pos.fen() << ") ok=" << ok
|
||||||
<< " visits=" << node.visits << " child_visits=" << child_visits
|
<< " visits=" << node.visits << " child_visits=" << child_visits
|
||||||
<< " children=" << int(node.num_children) << " legal=" << legal.size()
|
<< " children=" << int(node.num_children) << " legal=" << legal.size()
|
||||||
@@ -211,7 +214,7 @@ void Searcher::verify_tree() const {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
float Searcher::expand(u32 idx, const Position& pos, int depth) {
|
float Searcher::evaluate_leaf(u32 idx, const Position& pos, int depth) {
|
||||||
Node& node = tree_[idx];
|
Node& node = tree_[idx];
|
||||||
|
|
||||||
MoveList moves;
|
MoveList moves;
|
||||||
@@ -228,11 +231,20 @@ float Searcher::expand(u32 idx, const Position& pos, int depth) {
|
|||||||
return 0.0f;
|
return 0.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return eval::value(pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Searcher::expand(u32 idx, const Position& pos) {
|
||||||
|
Node& node = tree_[idx];
|
||||||
|
|
||||||
|
MoveList moves;
|
||||||
|
generate_legal(pos, moves);
|
||||||
|
|
||||||
const u32 first = tree_.alloc(u32(moves.size()));
|
const u32 first = tree_.alloc(u32(moves.size()));
|
||||||
if (!first) {
|
if (!first) {
|
||||||
// Out of memory: keep the node as a leaf and just evaluate it again.
|
// Out of memory: the node stays a leaf and is evaluated again.
|
||||||
tree_full_ = true;
|
tree_full_ = true;
|
||||||
return eval::value(pos);
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
float priors[256];
|
float priors[256];
|
||||||
@@ -248,8 +260,7 @@ float Searcher::expand(u32 idx, const Position& pos, int depth) {
|
|||||||
node.first_child = first;
|
node.first_child = first;
|
||||||
node.num_children = u8(moves.size());
|
node.num_children = u8(moves.size());
|
||||||
node.state = EXPANDED;
|
node.state = EXPANDED;
|
||||||
|
return true;
|
||||||
return eval::value(pos);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
u32 Searcher::select_child(u32 idx) const {
|
u32 Searcher::select_child(u32 idx) const {
|
||||||
@@ -304,9 +315,17 @@ void Searcher::playout() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (node.state == UNEXPANDED) {
|
if (node.state == UNEXPANDED) {
|
||||||
value = expand(idx, pos, depth);
|
// Most leaves are only ever visited once, so a first visit just
|
||||||
|
// evaluates; children (moves, priors, memory) come on the second.
|
||||||
|
if (node.visits == 0) {
|
||||||
|
value = evaluate_leaf(idx, pos, depth);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if (!expand(idx, pos)) {
|
||||||
|
value = eval::value(pos);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
idx = select_child(idx);
|
idx = select_child(idx);
|
||||||
pos.make_move(Move::from_raw(tree_[idx].move));
|
pos.make_move(Move::from_raw(tree_[idx].move));
|
||||||
@@ -409,8 +428,15 @@ SearchResult Searcher::search(const Position& root, const std::vector<u64>& hist
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The root needs its children before the first playout, so that even a
|
||||||
|
// single playout leaves a move to play.
|
||||||
|
if (tree_[Tree::root()].state == UNEXPANDED && !expand(Tree::root(), root)) {
|
||||||
|
tree_.clear();
|
||||||
|
tree_full_ = false;
|
||||||
|
expand(Tree::root(), root);
|
||||||
|
}
|
||||||
|
|
||||||
i64 last_info = 0;
|
i64 last_info = 0;
|
||||||
// Always run at least one playout so the root gets expanded.
|
|
||||||
do {
|
do {
|
||||||
playout();
|
playout();
|
||||||
if (print && (iterations_ & 1023) == 0) {
|
if (print && (iterations_ & 1023) == 0) {
|
||||||
|
|||||||
+5
-1
@@ -117,7 +117,11 @@ private:
|
|||||||
bool tree_full_ = false;
|
bool tree_full_ = false;
|
||||||
|
|
||||||
void playout();
|
void playout();
|
||||||
float expand(u32 idx, const Position& pos, int depth);
|
// First visit of a node: settles terminal states, otherwise evaluates.
|
||||||
|
float evaluate_leaf(u32 idx, const Position& pos, int depth);
|
||||||
|
// Second visit: creates the children with their priors. False when the
|
||||||
|
// arena is full (a collection is then scheduled).
|
||||||
|
bool expand(u32 idx, const Position& pos);
|
||||||
u32 select_child(u32 idx) const;
|
u32 select_child(u32 idx) const;
|
||||||
bool is_repetition(const Position& pos) const;
|
bool is_repetition(const Position& pos) const;
|
||||||
// Finds the node for `root` in the previous tree and makes it the root.
|
// Finds the node for `root` in the previous tree and makes it the root.
|
||||||
|
|||||||
Reference in new issue
Block a user