bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
mcts_node.h
Go to the documentation of this file.
1#ifndef MCTS_NODE_H
2#define MCTS_NODE_H
3
4#include "bitrl/base/bitrl_config.h"
6
7#include <algorithm>
8#include <cmath>
9#include <limits>
10#include <memory>
11#include <random>
12#include <vector>
13
14#ifdef BITRL_DEBUG
15#include <cassert>
16#endif
17
18namespace bitrl
19{
20namespace rl
21{
22namespace algos
23{
24namespace mc
25{
26
30template <typename ActionTp, typename StateTp> class MCTSNodeBase
31{
32 public:
33 typedef ActionTp action_type;
34 typedef StateTp state_type;
35
41 MCTSNodeBase(std::shared_ptr<MCTSNodeBase> parent, action_type action);
42
46 virtual ~MCTSNodeBase() = default;
47
52 void add_child(std::shared_ptr<MCTSNodeBase<ActionTp, StateTp>> child);
53
60
65 bool has_children() const noexcept { return children_.empty() != true; }
66
72 std::shared_ptr<MCTSNodeBase> get_child(uint_t cidx) const { return children_[cidx]; }
73
78 uint_t n_children() const noexcept { return children_.size(); }
79
83 template <typename RandomEngine> void shuffle_children(RandomEngine &engine);
84
89 uint_t n_explored_children() const noexcept { return explored_children_; }
90
94 void update_visits() noexcept { total_visits_ += 1; }
95
100
105 void update_total_score(real_t score) noexcept { total_score_ += score; }
106
112 real_t ucb(real_t temperature) const;
113
118 std::shared_ptr<MCTSNodeBase> max_ucb_child(real_t temperature) const;
119
125
130 uint_t total_visits() const noexcept { return total_visits_; }
131
136 action_type get_action() const noexcept { return action_; }
137
142 std::shared_ptr<MCTSNodeBase> parent() { return parent_; }
143
144 protected:
149
154
159
164
168 std::shared_ptr<MCTSNodeBase> parent_;
169
173 std::vector<std::shared_ptr<MCTSNodeBase>> children_;
174};
175
176template <typename ActionTp, typename StateTp>
178 action_type action)
180 children_()
181{
182}
183
184template <typename ActionTp, typename StateTp>
186 std::shared_ptr<MCTSNodeBase<ActionTp, StateTp>> child)
187{
188
189#ifdef BITRL_DEBUG
190 assert(child != nullptr && "Cannot add null children");
191#endif
192
193 children_.push_back(child);
194}
195
196template <typename ActionTp, typename StateTp>
197template <typename RandomEngine>
199{
200
201 if (children_.empty())
202 {
203 return;
204 }
205
206 std::shuffle(children_.begin(), children_.end(), engine);
207}
208
209template <typename ActionTp, typename StateTp>
216
217template <typename ActionTp, typename StateTp>
218std::shared_ptr<MCTSNodeBase<ActionTp, StateTp>>
220{
221
222 if (children_.empty())
223 {
224 return std::shared_ptr<MCTSNodeBase>();
225 }
226
227 auto current_ucb = children_[0]->ucb(temperature);
228 auto current_idx = 0;
229 auto dummy_idx = 0;
230 for (auto node : children_)
231 {
232
233 auto node_ucb = node->ucb(temperature);
234
235 if (node_ucb > current_ucb)
236 {
237
238 current_idx = dummy_idx;
239 current_ucb = node_ucb;
240 }
241
242 dummy_idx += 1;
243 }
244
245 return children_[current_idx];
246}
247
248template <typename ActionTp, typename StateTp>
250{
251 // a node that was never visited is explored first
252 if (total_visits_ == 0)
253 {
254 return std::numeric_limits<real_t>::max();
255 }
256
257 // the root has no parent to compare its visits with
258 if (!parent_)
259 {
260 return win_pct();
261 }
262
263 return win_pct() +
264 temperature * std::sqrt(std::log(static_cast<real_t>(parent_->total_visits())) /
265 static_cast<real_t>(total_visits_));
266}
267
268} // namespace mc
269} // namespace algos
270} // namespace rl
271} // namespace bitrl
272#endif
void add_child(std::shared_ptr< MCTSNodeBase< ActionTp, StateTp > > child)
add_child
Definition mcts_node.h:185
std::shared_ptr< MCTSNodeBase > parent()
Definition mcts_node.h:142
std::shared_ptr< MCTSNodeBase > get_child(uint_t cidx) const
get_child
Definition mcts_node.h:72
uint_t total_visits() const noexcept
total_visits
Definition mcts_node.h:130
MCTSNodeBase(std::shared_ptr< MCTSNodeBase > parent, action_type action)
MCTreeNodeBase.
Definition mcts_node.h:177
bool has_children() const noexcept
has_children
Definition mcts_node.h:65
std::shared_ptr< MCTSNodeBase > max_ucb_child(real_t temperature) const
max_ucb_child
Definition mcts_node.h:219
void update_visits() noexcept
update_visits
Definition mcts_node.h:94
real_t win_pct() const
win_pct
Definition mcts_node.h:124
std::shared_ptr< MCTSNodeBase > parent_
Definition mcts_node.h:168
real_t ucb(real_t temperature) const
ucb
Definition mcts_node.h:249
action_type get_action() const noexcept
get_action
Definition mcts_node.h:136
uint_t n_children() const noexcept
n_children
Definition mcts_node.h:78
uint_t n_explored_children() const noexcept
explored_children
Definition mcts_node.h:89
void shuffle_children(RandomEngine &engine)
shuffle_children
Definition mcts_node.h:198
void add_child(std::shared_ptr< MCTSNodeBase< ActionTp, StateTp > > parent, action_type action)
add_child
Definition mcts_node.h:210
std::vector< std::shared_ptr< MCTSNodeBase > > children_
Definition mcts_node.h:173
void update_total_score(real_t score) noexcept
update_total_score
Definition mcts_node.h:105
void update_explored_children() noexcept
Definition mcts_node.h:99
Definition first_visit_mc.h:27
Definition dp_algo_base.h:15
Definition dummy_agent.h:9
Various utilities used when working with RL problems.
Definition bitrl_log.h:25
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
double real_t
real_t
Definition bitsim_types.h:20