-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcts_tree.cpp
46 lines (38 loc) · 1.28 KB
/
mcts_tree.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
#include "mcts_tree.h"
#include <iostream>
// Function to search for the best move for the given state
int MonteCarloTreeSearch::search(int iterations, double cp) {
// Build up the tree and do the rollouts
for (int i = 0; i < iterations; ++i) {
MCTSNode* selectedNode = select(cp);
MCTSNode* expandedNode = selectedNode->expand();
int simulationResult = expandedNode->simulate();
expandedNode->backpropagate(simulationResult);
}
// Find the best move
int abest = 0;
int nbest = 0;
double qbest = -99999;
for (MCTSNode* c : root->children) {
double q = c->cumScore * (1. / c->visits);
double n = c->visits;
if (n > nbest) {
abest = c->action;
nbest = n;
qbest = q;
}
int i = c->action / 9;
int j = c->action % 9;
//std::cout << i+1 << j+1 << " " << c->cumScore * (1. / c->visits) << " " << c->visits << std::endl;
}
//std::cout << qbest << std::endl; // **
Q = qbest;
return abest;
}
// Helper Function to select a node which can be expanded
MCTSNode* MonteCarloTreeSearch::select(double cp) {
MCTSNode* node = root;
while (node->toexpand.size() == 0 && node->terminal == GOING_ON)
node = node->selectChild(cp);
return node;
}