Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 512 lines (446 sloc) 12.999 kB
302209c Commit the beginnings of a UCT MCTS player
Timo Ewalds authored
1
2 #ifndef __PLAYER_H_
3 #define __PLAYER_H_
4
141e49f The player should first consult the solver for some tactics. Spend 20…
Timo Ewalds authored
5 #include <stdint.h>
6 #include <cmath>
9d34d4e Clean up keeping the subtree...
Timo Ewalds authored
7 #include <cassert>
141e49f The player should first consult the solver for some tactics. Spend 20…
Timo Ewalds authored
8
18c886f add a simpler move struct, add a basic, though likely incomplete, RAV…
Timo Ewalds authored
9 #include "move.h"
302209c Commit the beginnings of a UCT MCTS player
Timo Ewalds authored
10 #include "board.h"
11 #include "time.h"
12 #include "timer.h"
0aa0c76 Factor the stats out into a separate file, display some better stats
Timo Ewalds authored
13 #include "depthstats.h"
141e49f The player should first consult the solver for some tactics. Spend 20…
Timo Ewalds authored
14 #include "solver.h"
302209c Commit the beginnings of a UCT MCTS player
Timo Ewalds authored
15
4214b7b @tewalds Fix two warnings, one lack of return, one signed/unsigned comparison
authored
16 typedef unsigned int uint;
17
302209c Commit the beginnings of a UCT MCTS player
Timo Ewalds authored
18 class Player {
ed6410c Deallocate the root properly, fixes a reverse memory leak
Timo Ewalds authored
19 public:
06950e8 Add a version of ExpPair that updates the value incrementally, and al…
Timo Ewalds authored
20 //*
0d04152 Make ExpPair into a class, use accessor functions...
Timo Ewalds authored
21 class ExpPair {
22 float s;
23 uint32_t n;
24 public:
25 ExpPair() : s(0), n(0) { }
26 ExpPair(float S, uint32_t N) : s(S), n(N) { }
27 float avg() const { return s/n; }
28 float sum() const { return s; }
29 uint32_t num() const { return n; }
1711836 Add the option to add losses instead of just wins
Timo Ewalds authored
30 void addwins(int num){ add(num, num); }
31 void addlosses(int num){ add(0, num); }
1ec81f9 add experience to each other
Timo Ewalds authored
32 void add(float val, int num){
33 s += val;
34 n += num;
35 }
36 ExpPair & operator+=(const ExpPair & a){
37 s += a.s;
38 n += a.n;
39 return *this;
40 }
0d04152 Make ExpPair into a class, use accessor functions...
Timo Ewalds authored
41 ExpPair & operator+=(float nv){
42 s += nv;
43 n++;
6911213 Refactor storing experience in the tree
Timo Ewalds authored
44 return *this;
45 }
c49f5ca Split knowledge out explicitly
Timo Ewalds authored
46 ExpPair operator + (const ExpPair & a){
47 return ExpPair(s + a.s, n + a.n);
48 }
0d04152 Make ExpPair into a class, use accessor functions...
Timo Ewalds authored
49 ExpPair & operator*=(int m){
50 s *= m;
51 n *= m;
6911213 Refactor storing experience in the tree
Timo Ewalds authored
52 return *this;
53 }
54 };
06950e8 Add a version of ExpPair that updates the value incrementally, and al…
Timo Ewalds authored
55 /*/
56 class ExpPair {
aa844d5 Update recency weighted ExpPair to work with knowledge...
Timo Ewalds authored
57 static const float k = 10000;
06950e8 Add a version of ExpPair that updates the value incrementally, and al…
Timo Ewalds authored
58 float v;
59 uint32_t n;
60 public:
61 ExpPair() : v(0), n(0) { }
62 ExpPair(float S, uint32_t N) : v(S/N), n(N) { }
63 float avg() const { return v; }
64 float sum() const { return v*n; }
65 uint32_t num() const { return n; }
aa844d5 Update recency weighted ExpPair to work with knowledge...
Timo Ewalds authored
66 void addwins(int num){ add(num, num); }
67 void addlosses(int num){ add(0, num); }
68 void add(float val, int num){
69 *this += ExpPair(val, num);
70 }
71 ExpPair & operator+=(const ExpPair & a){
72 //there's got to be a better way...
73 for(int i = 0; i < a.n; i++)
74 *this += a.v;
75 return *this;
76 }
06950e8 Add a version of ExpPair that updates the value incrementally, and al…
Timo Ewalds authored
77 ExpPair & operator+=(float nv){
78 n++;
aa844d5 Update recency weighted ExpPair to work with knowledge...
Timo Ewalds authored
79 // v += (nv - v)/n;
80 v += (nv - v)/(n < k ? n : k);
06950e8 Add a version of ExpPair that updates the value incrementally, and al…
Timo Ewalds authored
81 return *this;
82 }
aa844d5 Update recency weighted ExpPair to work with knowledge...
Timo Ewalds authored
83 ExpPair operator + (const ExpPair & a){
84 ExpPair ret = *this;
85 ret += a;
86 return ret;
87 }
06950e8 Add a version of ExpPair that updates the value incrementally, and al…
Timo Ewalds authored
88 ExpPair & operator*=(int m){
89 n *= m;
90 return *this;
91 }
92 };
93 //*/
6911213 Refactor storing experience in the tree
Timo Ewalds authored
94
302209c Commit the beginnings of a UCT MCTS player
Timo Ewalds authored
95 struct Node {
74af2df @tewalds Move children pointer to be managed in a class, hopefully remove the …
authored
96 class Children {
97 uint16_t _num;
98 Node * _children;
99 public:
100 typedef Node * iterator;
8e12915 @tewalds Clean up the asserts a bit
authored
101 Children() : _num(0), _children(NULL) { }
102 Children(int n) : _num(0), _children(NULL) { alloc(n); }
103 ~Children() { assert_empty(); }
74af2df @tewalds Move children pointer to be managed in a class, hopefully remove the …
authored
104
8e12915 @tewalds Clean up the asserts a bit
authored
105 void assert_consistent() const { assert((_num == 0) == (_children == NULL)); }
106 void assert_empty() const { assert((_num == 0) && (_children == NULL)); }
74af2df @tewalds Move children pointer to be managed in a class, hopefully remove the …
authored
107
108 unsigned int alloc(unsigned int n){
109 assert(_num == 0);
110 assert(_children == NULL);
111 assert(n > 0);
112
113 _num = n;
114 _children = new Node[_num];
115
116 return _num;
117 }
118 void neuter(){
119 _children = 0;
120 _num = 0;
121 }
122 unsigned int dealloc(){
123 assert_consistent();
124
125 int n = _num;
126 if(_children){
127 Node * temp = _children; //CAS!
128 neuter();
129 delete[] temp;
130 }
131 return n;
132 }
133 void swap(Children & other){
134 Children temp = other;
135 other = *this;
136 *this = temp;
e98a091 @tewalds Simple cleanup and comment
authored
137 temp.neuter(); //to avoid problems with the destructor of temp
74af2df @tewalds Move children pointer to be managed in a class, hopefully remove the …
authored
138 }
139 unsigned int num() const {
140 assert_consistent();
141 return _num;
142 }
143 bool empty() const {
144 return num() == 0;
145 }
146 Node & operator[](unsigned int offset){
147 assert(_children);
148 assert(offset >= 0 && offset < _num);
149 return _children[offset];
150 }
151 Node * begin() const {
152 assert_consistent();
153 return _children;
154 }
155 Node * end() const {
156 assert_consistent();
157 return _children + _num;
158 }
159 };
160
161 public:
6911213 Refactor storing experience in the tree
Timo Ewalds authored
162 ExpPair rave;
163 ExpPair exp;
c49f5ca Split knowledge out explicitly
Timo Ewalds authored
164 ExpPair know;
74af2df @tewalds Move children pointer to be managed in a class, hopefully remove the …
authored
165 Move move;
166 Move bestmove; //if outcome is set, then bestmove is the way to get there
8e8be5f Backup wins in the tree, possibly proving the root
Timo Ewalds authored
167 int16_t outcome;
74af2df @tewalds Move children pointer to be managed in a class, hopefully remove the …
authored
168 Children children;
8e8be5f Backup wins in the tree, possibly proving the root
Timo Ewalds authored
169 //don't forget to update the copy constructor/operator
302209c Commit the beginnings of a UCT MCTS player
Timo Ewalds authored
170
74af2df @tewalds Move children pointer to be managed in a class, hopefully remove the …
authored
171 Node() : outcome(-1) { }
172 Node(const Move & m, char o = -1) : move(m), outcome(o) { }
8e8be5f Backup wins in the tree, possibly proving the root
Timo Ewalds authored
173 Node(const Node & n) { *this = n; }
9d34d4e Clean up keeping the subtree...
Timo Ewalds authored
174 Node & operator = (const Node & n){
74af2df @tewalds Move children pointer to be managed in a class, hopefully remove the …
authored
175 if(this != & n){ //don't copy to self
9d34d4e Clean up keeping the subtree...
Timo Ewalds authored
176 //don't copy to a node that already has children
74af2df @tewalds Move children pointer to be managed in a class, hopefully remove the …
authored
177 assert(children.empty());
9d34d4e Clean up keeping the subtree...
Timo Ewalds authored
178
179 rave = n.rave;
180 exp = n.exp;
74af2df @tewalds Move children pointer to be managed in a class, hopefully remove the …
authored
181 know = n.know;
9d34d4e Clean up keeping the subtree...
Timo Ewalds authored
182 move = n.move;
8e8be5f Backup wins in the tree, possibly proving the root
Timo Ewalds authored
183 bestmove = n.bestmove;
74af2df @tewalds Move children pointer to be managed in a class, hopefully remove the …
authored
184 outcome = n.outcome;
9d34d4e Clean up keeping the subtree...
Timo Ewalds authored
185 }
186 return *this;
187 }
188
189 void swap_tree(Node & n){
74af2df @tewalds Move children pointer to be managed in a class, hopefully remove the …
authored
190 children.swap(n.children);
ced4d1c Keep the tree between moves. For now that means turning off the solve…
Timo Ewalds authored
191 }
192
193 void print() const {
c033243 Fix size(), add Node::to_s()
Timo Ewalds authored
194 printf("%s\n", to_s().c_str());
195 }
196 string to_s() const {
0d04152 Make ExpPair into a class, use accessor functions...
Timo Ewalds authored
197 return "Node: exp " + to_str(exp.avg(), 2) + "/" + to_str(exp.num()) +
198 ", rave " + to_str(rave.avg(), 2) + "/" + to_str(rave.num()) +
74af2df @tewalds Move children pointer to be managed in a class, hopefully remove the …
authored
199 ", move " + to_str(move.x) + "," + to_str(move.y) + ", " + to_str(children.num());
ced4d1c Keep the tree between moves. For now that means turning off the solve…
Timo Ewalds authored
200 }
201
a0fc6cf Fix warnings about signed/unsigned
Timo Ewalds authored
202 unsigned int size() const {
74af2df @tewalds Move children pointer to be managed in a class, hopefully remove the …
authored
203 unsigned int num = children.num();
204
205 for(Node * i = children.begin(); i != children.end(); i++)
206 num += i->size();
207
208 return num;
b9d644e Add a tree size function
Timo Ewalds authored
209 }
74af2df @tewalds Move children pointer to be managed in a class, hopefully remove the …
authored
210
9878c2e Disable proof number search proofs properly
Timo Ewalds authored
211 /*
8b97aa5 Include the proof tree in the player tree size
Timo Ewalds authored
212 int construct(const Solver::PNSNode * n, int pnsscore){
12ee6f8 Switch the solver to use Move instead of individual x and y for a few…
Timo Ewalds authored
213 move = n->move;
141e49f The player should first consult the solver for some tactics. Spend 20…
Timo Ewalds authored
214
215 if(n->delta == 0){ //a win!
6911213 Refactor storing experience in the tree
Timo Ewalds authored
216 exp = ExpPair(1000, 1000);
141e49f The player should first consult the solver for some tactics. Spend 20…
Timo Ewalds authored
217 }else if(n->phi == 0){ //a loss or tie
218 //set high but not insurmountable visits just in case it is a tie
6911213 Refactor storing experience in the tree
Timo Ewalds authored
219 exp = ExpPair(0, 100);
0d545c2 Fix proof score initialization to be in the right range, and fix the …
Timo Ewalds authored
220 }else if(pnsscore > 0){
221 if(n->phi >= n->delta)
6911213 Refactor storing experience in the tree
Timo Ewalds authored
222 exp = (ExpPair((1 - n->delta/(2*n->phi)), 1) *= pnsscore);
0d545c2 Fix proof score initialization to be in the right range, and fix the …
Timo Ewalds authored
223 else
6911213 Refactor storing experience in the tree
Timo Ewalds authored
224 exp = (ExpPair((n->phi/(2*n->delta)), 1) *= pnsscore);
141e49f The player should first consult the solver for some tactics. Spend 20…
Timo Ewalds authored
225 }
226
227 numchildren = n->numchildren;
228 children = NULL;
229
9d34d4e Clean up keeping the subtree...
Timo Ewalds authored
230 int num = numchildren;
141e49f The player should first consult the solver for some tactics. Spend 20…
Timo Ewalds authored
231 if(numchildren){
232 children = new Node[numchildren];
233 for(int i = 0; i < numchildren; i++)
8b97aa5 Include the proof tree in the player tree size
Timo Ewalds authored
234 num += children[i].construct(& n->children[i], pnsscore);
141e49f The player should first consult the solver for some tactics. Spend 20…
Timo Ewalds authored
235 }
8b97aa5 Include the proof tree in the player tree size
Timo Ewalds authored
236 return num;
141e49f The player should first consult the solver for some tactics. Spend 20…
Timo Ewalds authored
237 }
9878c2e Disable proof number search proofs properly
Timo Ewalds authored
238 */
302209c Commit the beginnings of a UCT MCTS player
Timo Ewalds authored
239 ~Node(){
74af2df @tewalds Move children pointer to be managed in a class, hopefully remove the …
authored
240 assert(children.empty());
302209c Commit the beginnings of a UCT MCTS player
Timo Ewalds authored
241 }
242
74af2df @tewalds Move children pointer to be managed in a class, hopefully remove the …
authored
243 unsigned int alloc(unsigned int num){
244 return children.alloc(num);
302209c Commit the beginnings of a UCT MCTS player
Timo Ewalds authored
245 }
74af2df @tewalds Move children pointer to be managed in a class, hopefully remove the …
authored
246 unsigned int dealloc(){
247 unsigned int num = 0;
f9e6f77 Force the dealloc to be used for deallocation.
Timo Ewalds authored
248
74af2df @tewalds Move children pointer to be managed in a class, hopefully remove the …
authored
249 for(Node * i = children.begin(); i != children.end(); i++)
250 num += i->dealloc();
251 num += children.dealloc();
f9e6f77 Force the dealloc to be used for deallocation.
Timo Ewalds authored
252
74af2df @tewalds Move children pointer to be managed in a class, hopefully remove the …
authored
253 return num;
302209c Commit the beginnings of a UCT MCTS player
Timo Ewalds authored
254 }
255
576fa84 Fix a few bugs: use factor instead of scale. Stop assigning rave loss…
Timo Ewalds authored
256 //*
214afe6 Update to a more standard way of doing rave scores.
Timo Ewalds authored
257 //new way, more standard way of changing over from rave scores to real scores
22038b1 Major refactoring of UCT/RAVE. Pulled rave updates out of walk_tree. …
Timo Ewalds authored
258 float value(float ravefactor, float fpurgency){
c49f5ca Split knowledge out explicitly
Timo Ewalds authored
259 ExpPair efexp = exp + know;
260
6beda47 Make the minimum rave_factor a constant
Timo Ewalds authored
261 if(ravefactor <= min_rave)
c49f5ca Split knowledge out explicitly
Timo Ewalds authored
262 return (efexp.num() == 0 ? fpurgency : efexp.avg());
22038b1 Major refactoring of UCT/RAVE. Pulled rave updates out of walk_tree. …
Timo Ewalds authored
263
c49f5ca Split knowledge out explicitly
Timo Ewalds authored
264 if(rave.num() == 0 && efexp.num() == 0)
8d2523b Allow the first play urgency to be configured
Timo Ewalds authored
265 return fpurgency;
214afe6 Update to a more standard way of doing rave scores.
Timo Ewalds authored
266
c49f5ca Split knowledge out explicitly
Timo Ewalds authored
267 float alpha = ravefactor/(ravefactor + efexp.num());
0d04152 Make ExpPair into a class, use accessor functions...
Timo Ewalds authored
268 // float alpha = sqrt(ravefactor/(ravefactor + 3*exp.num()));
269 // float alpha = (float)rave.num()/((float)exp.num() + (float)rave.num() + 4.0*exp.num()*rave.num()*ravefactor);
214afe6 Update to a more standard way of doing rave scores.
Timo Ewalds authored
270
7cc84b2 Avoid a potential divide by 0, set the explore rate based on tests of…
Timo Ewalds authored
271 float val = 0;
0d04152 Make ExpPair into a class, use accessor functions...
Timo Ewalds authored
272 if(rave.num()) val += alpha*rave.avg();
c49f5ca Split knowledge out explicitly
Timo Ewalds authored
273 if(efexp.num())val += (1-alpha)*efexp.avg();
7cc84b2 Avoid a potential divide by 0, set the explore rate based on tests of…
Timo Ewalds authored
274
275 return val;
214afe6 Update to a more standard way of doing rave scores.
Timo Ewalds authored
276 }
576fa84 Fix a few bugs: use factor instead of scale. Stop assigning rave loss…
Timo Ewalds authored
277 /*/
214afe6 Update to a more standard way of doing rave scores.
Timo Ewalds authored
278 //my understanding of how fuego does it
22038b1 Major refactoring of UCT/RAVE. Pulled rave updates out of walk_tree. …
Timo Ewalds authored
279 float value(float ravefactor, float fpurgency){
214afe6 Update to a more standard way of doing rave scores.
Timo Ewalds authored
280 float val = 0;
281 float weight = 0;
282 if(visits) {
283 val += score;
284 weight += visits;
285 }
576fa84 Fix a few bugs: use factor instead of scale. Stop assigning rave loss…
Timo Ewalds authored
286 if(ravevisits){
cd8a3a9 Update the fuego based value function, fix a misunderstanding as well…
Timo Ewalds authored
287 float bias = 1.0/(1.1 + ravevisits/20000.0);
288 val += rave*bias;
289 weight += ravevisits*bias;
214afe6 Update to a more standard way of doing rave scores.
Timo Ewalds authored
290 }
291 if(weight > 0)
292 return val / weight;
293 else
cd8a3a9 Update the fuego based value function, fix a misunderstanding as well…
Timo Ewalds authored
294 return fpurgency;
295 }
296
297 //based directly on fuego
22038b1 Major refactoring of UCT/RAVE. Pulled rave updates out of walk_tree. …
Timo Ewalds authored
298 float value(float ravefactor, float fpurgency){
cd8a3a9 Update the fuego based value function, fix a misunderstanding as well…
Timo Ewalds authored
299 float val = 0.f;
300 float weightSum = 0.f;
301 bool hasValue = false;
302 if(visits){
303 val += score;
304 weightSum += visits;
305 hasValue = true;
306 }
307 if(ravevisits){
308 float weight = ravevisits / ( 1.1 + ravevisits/20000.);
309 val += weight * rave / ravevisits;
310 weightSum += weight;
311 hasValue = true;
312 }
313 if(hasValue)
314 return val / weightSum;
315 else
316 return fpurgency;
214afe6 Update to a more standard way of doing rave scores.
Timo Ewalds authored
317 }
cd8a3a9 Update the fuego based value function, fix a misunderstanding as well…
Timo Ewalds authored
318
214afe6 Update to a more standard way of doing rave scores.
Timo Ewalds authored
319 //*/
302209c Commit the beginnings of a UCT MCTS player
Timo Ewalds authored
320 };
321
7149ea6 Add an option to make the rave scores scale from 2 down to 0
Timo Ewalds authored
322 struct RaveMoveList {
1199606 Some cleanups to rave...
Timo Ewalds authored
323 struct RaveMove : public Move {
609a971 Fix rave values so it updates wins to the winner and losses to the lo…
Timo Ewalds authored
324 char player;
325 float score;
326
1199606 Some cleanups to rave...
Timo Ewalds authored
327 RaveMove(const Move & m, char p = 0, float s = 1) : Move(m), player(p), score(s) { }
609a971 Fix rave values so it updates wins to the winner and losses to the lo…
Timo Ewalds authored
328 };
71d4142 Rewrite update_rave to use iterators, which makes it a bit faster, an…
Timo Ewalds authored
329 typedef vector<RaveMove>::const_iterator iterator;
609a971 Fix rave values so it updates wins to the winner and losses to the lo…
Timo Ewalds authored
330
331 vector<RaveMove> list;
7149ea6 Add an option to make the rave scores scale from 2 down to 0
Timo Ewalds authored
332
333 RaveMoveList(int s = 0){
334 list.reserve(s);
335 }
336
1199606 Some cleanups to rave...
Timo Ewalds authored
337 void add(const Move & move, char player){
338 list.push_back(RaveMove(move, player, 1));
7149ea6 Add an option to make the rave scores scale from 2 down to 0
Timo Ewalds authored
339 }
340 void clear(){
341 list.clear();
342 }
22038b1 Major refactoring of UCT/RAVE. Pulled rave updates out of walk_tree. …
Timo Ewalds authored
343 unsigned int size() const {
7149ea6 Add an option to make the rave scores scale from 2 down to 0
Timo Ewalds authored
344 return list.size();
345 }
609a971 Fix rave values so it updates wins to the winner and losses to the lo…
Timo Ewalds authored
346 const RaveMove & operator[](int i) const {
7149ea6 Add an option to make the rave scores scale from 2 down to 0
Timo Ewalds authored
347 return list[i];
348 }
71d4142 Rewrite update_rave to use iterators, which makes it a bit faster, an…
Timo Ewalds authored
349 iterator begin() const {
350 return list.begin();
351 }
352 iterator end() const {
353 return list.end();
354 }
355
7149ea6 Add an option to make the rave scores scale from 2 down to 0
Timo Ewalds authored
356 //remove the moves that were played by the loser
357 //sort in y,x order
1199606 Some cleanups to rave...
Timo Ewalds authored
358 void clean(bool scale){
7149ea6 Add an option to make the rave scores scale from 2 down to 0
Timo Ewalds authored
359 if(scale){
1199606 Some cleanups to rave...
Timo Ewalds authored
360 float base = 2; //2 instead of 1 so the average of wins stays at 1
361 float factor = 2*base/(list.size()+1); //+1 to keep it from going negative, 4 = base*2 since half the values are skipped
7149ea6 Add an option to make the rave scores scale from 2 down to 0
Timo Ewalds authored
362
1199606 Some cleanups to rave...
Timo Ewalds authored
363 for(unsigned int i = 0; i < list.size(); i++)
364 list[i].score = base - i/2*factor;
609a971 Fix rave values so it updates wins to the winner and losses to the lo…
Timo Ewalds authored
365 }
7149ea6 Add an option to make the rave scores scale from 2 down to 0
Timo Ewalds authored
366
576fa84 Fix a few bugs: use factor instead of scale. Stop assigning rave loss…
Timo Ewalds authored
367 sort(list.begin(), list.end()); //sort in y,x order
7149ea6 Add an option to make the rave scores scale from 2 down to 0
Timo Ewalds authored
368 }
369 };
370
cb365ad Allow the explore and ravefactor values to be set over gtp
Timo Ewalds authored
371 public:
6beda47 Make the minimum rave_factor a constant
Timo Ewalds authored
372
373 static const float min_rave = 0.1;
374
b930ead Set the parameters based on all the recent testing, changing by board…
Timo Ewalds authored
375 bool defaults; //use the default settings on board reset
0ec1b32 Prepare for new knowledge initialization code
Timo Ewalds authored
376 float prooftime; //fraction of time spent in proof number search, looking for a provable win and losses to avoid
377 //tree traversal
18c886f add a simpler move struct, add a basic, though likely incomplete, RAV…
Timo Ewalds authored
378 float explore; //greater than one favours exploration, smaller than one favours exploitation
22038b1 Major refactoring of UCT/RAVE. Pulled rave updates out of walk_tree. …
Timo Ewalds authored
379 float ravefactor; //big numbers favour rave scores, small ignore it
7149ea6 Add an option to make the rave scores scale from 2 down to 0
Timo Ewalds authored
380 bool ravescale; //scale rave numbers from 2 down to 0 in decreasing order of move recency instead of always 1
2fd58eb Add the good opponent moves are good moves for me heuristic
Timo Ewalds authored
381 bool opmoves; //take the opponents rave updates too, a good move for my opponent is a good move for me.
8d2523b Allow the first play urgency to be configured
Timo Ewalds authored
382 int skiprave; //how often to skip rave, skip once in this many checks
4b36b92 Add an option to only use short rollouts, for higher quality rave upd…
Timo Ewalds authored
383 bool shortrave; //only update rave values on short rollouts
dca42bd Make keeping tree optional
Timo Ewalds authored
384 bool keeptree; //reuse the tree from the previous move
4a5b4c7 Add an option to keep the solved tree for use next turn
Timo Ewalds authored
385 bool minimaxtree;//keep the solved part of the tree
4d2b548 @tewalds Allow solving to deeper levels at node expansion
authored
386 int minimax; //solve the minimax tree within the uct tree
8d2523b Allow the first play urgency to be configured
Timo Ewalds authored
387 float fpurgency; //what value to return for a move that hasn't been played yet
4214b7b @tewalds Fix two warnings, one lack of return, one signed/unsigned comparison
authored
388 uint visitexpand;//number of visits before expanding a node
0ec1b32 Prepare for new knowledge initialization code
Timo Ewalds authored
389 //knowledge
8d2523b Allow the first play urgency to be configured
Timo Ewalds authored
390 int proofscore; //how many virtual rollouts to assign based on the proof number search values
96ca373 Add move locality
Timo Ewalds authored
391 bool localreply; //boost for a local reply, ie a move near the previous move
392 bool locality; //boost for playing near previous stones
eaf1777 Add a connectivity heuristic
Timo Ewalds authored
393 bool connect; //boost for having connections to edges and corners
22caa03 Add the virtual connection pattern as node initialization
Timo Ewalds authored
394 bool bridge; //boost replying to a probe at a bridge
0ec1b32 Prepare for new knowledge initialization code
Timo Ewalds authored
395 //rollout
f10b673 @tewalds Let the random moves in the rollout be weighted by the root's rave va…
authored
396 bool weightedrandom; //use a weighted shuffle for move ordering, based on the rave results
ff9a09b Parameterize a few features
Timo Ewalds authored
397 bool rolloutpattern; //play the response to a virtual connection threat in rollouts
f10b673 @tewalds Let the random moves in the rollout be weighted by the root's rave va…
authored
398 int instantwin; //look for instant wins in rollouts
399 bool lastgoodreply; //use the last-good-reply rollout heuristic
0ec1b32 Prepare for new knowledge initialization code
Timo Ewalds authored
400
4d2b548 @tewalds Allow solving to deeper levels at node expansion
authored
401 Solver solver;
ced4d1c Keep the tree between moves. For now that means turning off the solve…
Timo Ewalds authored
402 Node root;
403 Board rootboard;
404
f08fab7 @tewalds Add the last-good-reply rollout heuristic, disable swap during rollou…
authored
405 Move goodreply[2][361]; //361 is big enough for size 10 (ie 19x19), but no bigger...
406
302209c Commit the beginnings of a UCT MCTS player
Timo Ewalds authored
407 int runs;
0aa0c76 Factor the stats out into a separate file, display some better stats
Timo Ewalds authored
408 DepthStats treelen, gamelen;
8a654dd Allow the player to be reused multiple times
Timo Ewalds authored
409 uint64_t nodes, maxnodes;
302209c Commit the beginnings of a UCT MCTS player
Timo Ewalds authored
410 bool timeout;
411
bdc8b7d Add some version of time control...
Timo Ewalds authored
412 double time_used;
413
302209c Commit the beginnings of a UCT MCTS player
Timo Ewalds authored
414 Player() {
ced4d1c Keep the tree between moves. For now that means turning off the solve…
Timo Ewalds authored
415 nodes = 0;
8a654dd Allow the player to be reused multiple times
Timo Ewalds authored
416 time_used = 0;
141e49f The player should first consult the solver for some tactics. Spend 20…
Timo Ewalds authored
417
b930ead Set the parameters based on all the recent testing, changing by board…
Timo Ewalds authored
418 set_default_params();
419 }
420 void set_default_params(){
421 int s = rootboard.get_size();
422 defaults = true;
9a99821 @tewalds Update some defaults
authored
423 explore = (s == 4 ? 0.9 : 0);
b930ead Set the parameters based on all the recent testing, changing by board…
Timo Ewalds authored
424 ravefactor = (s == 4 ? 0 : 500);
5e20d95 formatting, and disable locality by default
Timo Ewalds authored
425 ravescale = false;
426 opmoves = false;
427 skiprave = 0;
4b36b92 Add an option to only use short rollouts, for higher quality rave upd…
Timo Ewalds authored
428 shortrave = false;
5e20d95 formatting, and disable locality by default
Timo Ewalds authored
429 keeptree = true;
9a99821 @tewalds Update some defaults
authored
430 minimaxtree = true;
d226d91 @tewalds Enable forced move checking by default
authored
431 minimax = 2;
5e20d95 formatting, and disable locality by default
Timo Ewalds authored
432 fpurgency = 1;
309f0ce @tewalds Allow the number of visits per expansion to be increased for low memo…
authored
433 visitexpand = 1;
5e20d95 formatting, and disable locality by default
Timo Ewalds authored
434 prooftime = 0;
435 proofscore = 0;
5d68456 Disable all the features that aren't proven to be good, to form a bas…
Timo Ewalds authored
436 localreply = false;
5e20d95 formatting, and disable locality by default
Timo Ewalds authored
437 locality = false;
b930ead Set the parameters based on all the recent testing, changing by board…
Timo Ewalds authored
438 connect = (s == 4);
5d68456 Disable all the features that aren't proven to be good, to form a bas…
Timo Ewalds authored
439 bridge = false;
f10b673 @tewalds Let the random moves in the rollout be weighted by the root's rave va…
authored
440 weightedrandom = false;
b930ead Set the parameters based on all the recent testing, changing by board…
Timo Ewalds authored
441 rolloutpattern = true;
f08fab7 @tewalds Add the last-good-reply rollout heuristic, disable swap during rollou…
authored
442 lastgoodreply = false;
443 instantwin = 0;
302209c Commit the beginnings of a UCT MCTS player
Timo Ewalds authored
444 }
f9e6f77 Force the dealloc to be used for deallocation.
Timo Ewalds authored
445 ~Player(){ root.dealloc(); }
302209c Commit the beginnings of a UCT MCTS player
Timo Ewalds authored
446 void timedout(){ timeout = true; }
447
ced4d1c Keep the tree between moves. For now that means turning off the solve…
Timo Ewalds authored
448 void set_board(const Board & board){
449 rootboard = board;
ed6410c Deallocate the root properly, fixes a reverse memory leak
Timo Ewalds authored
450 nodes -= root.dealloc();
ced4d1c Keep the tree between moves. For now that means turning off the solve…
Timo Ewalds authored
451 root = Node();
b930ead Set the parameters based on all the recent testing, changing by board…
Timo Ewalds authored
452
453 if(defaults)
454 set_default_params();
f08fab7 @tewalds Add the last-good-reply rollout heuristic, disable swap during rollou…
authored
455
456 for(int p = 0; p < 2; p++)
457 for(int i = 0; i < 361; i++)
458 goodreply[p][i] = M_UNKNOWN;
ced4d1c Keep the tree between moves. For now that means turning off the solve…
Timo Ewalds authored
459 }
460 void move(const Move & m){
ce05c67 Actually update the locality scores...
Timo Ewalds authored
461 rootboard.move(m, true);
9d34d4e Clean up keeping the subtree...
Timo Ewalds authored
462
61a6b44 Output the amount of the tree that is saved by keeping the tree
Timo Ewalds authored
463 uint64_t nodesbefore = nodes;
464
dca42bd Make keeping tree optional
Timo Ewalds authored
465 if(keeptree){
9d34d4e Clean up keeping the subtree...
Timo Ewalds authored
466 Node child;
467
74af2df @tewalds Move children pointer to be managed in a class, hopefully remove the …
authored
468 for(Node * i = root.children.begin(); i != root.children.end(); i++){
469 if(i->move == m){
470 child = *i; //copy the child experience to temp
471 child.swap_tree(*i); //move the child tree to temp
9d34d4e Clean up keeping the subtree...
Timo Ewalds authored
472 break;
473 }
474 }
475
dca42bd Make keeping tree optional
Timo Ewalds authored
476 nodes -= root.dealloc();
9d34d4e Clean up keeping the subtree...
Timo Ewalds authored
477 root = child;
478 root.swap_tree(child);
61a6b44 Output the amount of the tree that is saved by keeping the tree
Timo Ewalds authored
479
480 if(nodesbefore > 0)
481 fprintf(stderr, "Nodes before: %llu, after: %llu, saved %.1f%% of the tree\n", nodesbefore, nodes, 100.0*nodes/nodesbefore);
dca42bd Make keeping tree optional
Timo Ewalds authored
482 }else{
483 nodes -= root.dealloc();
484 root = Node();
6a97002 Don't forget to set the previous move...
Timo Ewalds authored
485 root.move = m;
dca42bd Make keeping tree optional
Timo Ewalds authored
486 }
61a6b44 Output the amount of the tree that is saved by keeping the tree
Timo Ewalds authored
487 assert(nodes == root.size());
ced4d1c Keep the tree between moves. For now that means turning off the solve…
Timo Ewalds authored
488 }
489
48c2aba @tewalds Fix memory limit for above 2gb
authored
490 Move mcts(double time, int maxruns, uint64_t memlimit);
4214b7b @tewalds Fix two warnings, one lack of return, one signed/unsigned comparison
authored
491 void solve(double time, uint64_t memlimit);
ced4d1c Keep the tree between moves. For now that means turning off the solve…
Timo Ewalds authored
492 vector<Move> get_pv();
302209c Commit the beginnings of a UCT MCTS player
Timo Ewalds authored
493
494 protected:
53f89b9 Choose moves in a better order
Timo Ewalds authored
495 Node * return_move(const Node * node, int toplay) const;
496 Node * return_move_outcome(const Node * node, int outcome) const;
7149ea6 Add an option to make the rave scores scale from 2 down to 0
Timo Ewalds authored
497 int walk_tree(Board & board, Node * node, RaveMoveList & movelist, int depth);
ca1f353 Move node knowledge into its own function
Timo Ewalds authored
498 void add_knowledge(Board & board, Node * node, Node * child);
8e8be5f Backup wins in the tree, possibly proving the root
Timo Ewalds authored
499 Node * choose_move(const Node * node, int toplay) const;
22038b1 Major refactoring of UCT/RAVE. Pulled rave updates out of walk_tree. …
Timo Ewalds authored
500 void update_rave(const Node * node, const RaveMoveList & movelist, int won, int toplay);
bd17646 Switch the way the bridge pattern is checked. This is potentially fas…
Timo Ewalds authored
501 bool test_bridge_probe(const Board & board, const Move & move, const Move & test);
6ae9e06 @tewalds Refactor the rollouts, add make both instant wins and forced replies …
authored
502
503 int rollout(Board & board, RaveMoveList & movelist, Move move, int depth);
504 Move rollout_choose_move(Board & board, const Move & prev);
505 Move rollout_pattern(const Board & board, const Move & move);
d41e75f @tewalds Add a player based solver. It works, though it is questionable whethe…
authored
506
507 void solve_recurse(Board & board, Node * node, double rate, double solvetime, uint64_t memlimit, int depth);
302209c Commit the beginnings of a UCT MCTS player
Timo Ewalds authored
508 };
509
510 #endif
511
Something went wrong with that request. Please try again.