-
Notifications
You must be signed in to change notification settings - Fork 3
/
UCTSearch.h
101 lines (88 loc) · 2.89 KB
/
UCTSearch.h
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
/*
This file is part of Leela Zero.
Copyright (C) 2017 Gian-Carlo Pascutto
Leela Zero is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Leela Zero is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Leela Zero. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef UCTSEARCH_H_INCLUDED
#define UCTSEARCH_H_INCLUDED
#include <memory>
#include <atomic>
#include <tuple>
#include <unordered_map>
#include "Position.h"
#include "UCTNode.h"
class SearchResult {
public:
SearchResult() = default;
bool valid() const { return m_valid; }
float eval() const { return m_eval; }
static SearchResult from_eval(float eval) {
return SearchResult(eval);
}
static SearchResult from_score(float board_score) {
if (board_score > 0.0f) {
return SearchResult(1.0f);
} else if (board_score < 0.0f) {
return SearchResult(0.0f);
} else {
return SearchResult(0.5f);
}
}
private:
explicit SearchResult(float eval)
: m_valid(true), m_eval(eval) {}
bool m_valid{false};
float m_eval{0.0f};
};
class UCTSearch {
public:
/*
Maximum size of the tree in memory. Nodes are about
40 bytes, so limit to ~1.6G.
*/
static constexpr auto MAX_TREE_SIZE = 40'000'000;
UCTSearch(Position& pos, StateListPtr& states);
Move think();
void set_playout_limit(int playouts);
void set_analyzing(bool flag);
void set_quiet(bool flag);
void ponder();
bool is_running() const;
bool playout_limit_reached() const;
void increment_playouts();
SearchResult play_simulation(Position& currstate, UCTNode* const node);
static std::unordered_map<Move, int> move_lookup;
private:
void dump_stats(Position& pos, UCTNode& parent);
std::string get_pv(Position& pos, UCTNode& parent);
void dump_analysis(int playouts);
Move get_best_move();
Position& m_rootstate;
StateListPtr& m_statelist;
UCTNode m_root{MOVE_NONE, 0.0f, 0.5f};
std::atomic<int> m_nodes{0};
std::atomic<int> m_playouts{0};
std::atomic<bool> m_run{false};
int m_maxplayouts;
};
class UCTWorker {
public:
UCTWorker(Position& state, StateListPtr& states, UCTSearch* search, UCTNode* root)
: m_rootstate(state), m_statelist(states), m_search(search), m_root(root) {}
void operator()();
private:
Position& m_rootstate;
StateListPtr& m_statelist;
UCTSearch* m_search;
UCTNode* m_root;
};
#endif