-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsosolver.h
More file actions
79 lines (67 loc) · 2.29 KB
/
sosolver.h
File metadata and controls
79 lines (67 loc) · 2.29 KB
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
/*
* Copyright (C) 2019 Steven Franzen <sfranzen85@gmail.com>
* This file is subject to the terms of the MIT License; see the LICENSE file in
* the root directory of this distribution.
*/
#ifndef ISMCTS_SOSOLVER_H
#define ISMCTS_SOSOLVER_H
#include "config.h"
#include "execution.h"
#include "game.h"
#include "solverbase.h"
#include "utility.h"
#include <memory>
#include <vector>
namespace ISMCTS
{
template<class Move, class _ExecutionPolicy = Sequential, template<class> class... Ps>
class SOSolver : public _ExecutionPolicy, public SolverBase<Move, Ps...>
{
public:
using _ExecutionPolicy::_ExecutionPolicy;
using typename SolverBase<Move, Ps...>::Config;
using RootNode = typename Config::RootNode;
using TreeList = typename Config::TreeList;
Move operator()(Game<Move> const &rootState)
{
auto treeGenerator = [&rootState]{ return SOSolver::newRoot(rootState); };
auto treeSearch = [this](RootNode &root, Game<Move> const &state){ search(root.get(), state); };
m_trees = SOSolver::execute(treeSearch, treeGenerator, rootState);
return SOSolver::template bestMove<Move>(m_trees);
}
TreeList currentTrees() const
{
return m_trees;
}
protected:
void search(Node<Move> *rootNode, Game<Move> const &rootState) const
{
auto randomState = rootState.cloneAndRandomise(rootState.currentPlayer());
select(rootNode, *randomState);
expand(rootNode, *randomState);
this->simulate(*randomState);
SOSolver::backPropagate(rootNode, *randomState);
}
void select(Node<Move> *&node, Game<Move> &state) const
{
auto const validMoves = state.validMoves();
if (!SOSolver::selectNode(node, validMoves)) {
node = this->selectChild(node, state, validMoves);
state.doMove(node->move());
select(node, state);
}
}
void expand(Node<Move> *&node, Game<Move> &state) const
{
auto const untriedMoves = node->untriedMoves(state.validMoves());
if (!untriedMoves.empty()) {
auto const &move = randomElement(untriedMoves);
node = node->addChild(SOSolver::newChild(move, state));
state.doMove(move);
}
}
private:
TreeList m_trees;
};
} // ISMCTS
#endif // ISMCTS_SOSOLVER_H