1#ifndef MC_TREE_SEARCH_SOLVER_H
2#define MC_TREE_SEARCH_SOLVER_H
27template <
typename EnvTp>
29 requires(EnvTp env,
typename EnvTp::action_type a)
31 typename EnvTp::state_type;
34 } -> std::convertible_to<uint_t>;
37 } -> std::convertible_to<real_t>;
40 } -> std::convertible_to<bool>;
49 template <
typename EnvTp>
50 typename EnvTp::action_type
operator()(
const EnvTp &env, std::mt19937 &generator)
const
52 std::uniform_int_distribution<uint_t> action(0,
static_cast<uint_t>(env.n_actions()) - 1);
53 return static_cast<typename EnvTp::action_type
>(action(generator));
114template <mcts_env_concept EnvTp,
typename RolloutPolicy = UniformRolloutPolicy>
class MCTSSolver
123 : config_(
config), rollout_policy_(std::move(rollout_policy)), generator_(
config.seed)
137 std::shared_ptr<const node_type>
root() const noexcept {
return root_; }
143 RolloutPolicy rollout_policy_;
144 std::mt19937 generator_;
145 std::shared_ptr<node_type> root_;
147 void simulate_(
const env_type &env);
148 void expand_(
const std::shared_ptr<node_type> &node,
uint_t n_actions);
151template <mcts_env_concept EnvTp,
typename RolloutPolicy>
155 root_ = std::make_shared<node_type>(
nullptr,
action_type{});
156 expand_(root_,
static_cast<uint_t>(env.n_actions()));
158 for (
uint_t i = 0; i < config_.n_simulations; ++i)
165 std::shared_ptr<node_type> best;
166 for (
uint_t c = 0; c < root_->n_children(); ++c)
168 auto child = root_->get_child(c);
169 if (!best || child->total_visits() > best->total_visits() ||
170 (child->total_visits() == best->total_visits() && child->total_visits() > 0 &&
171 child->win_pct() > best->win_pct()))
176 return best->get_action();
179template <mcts_env_concept EnvTp,
typename RolloutPolicy>
180void MCTSSolver<EnvTp, RolloutPolicy>::expand_(
const std::shared_ptr<node_type> &node,
183 for (
uint_t a = 0; a < n_actions; ++a)
185 node->add_child(node,
static_cast<action_type
>(a));
189 node->shuffle_children(generator_);
192template <mcts_env_concept EnvTp,
typename RolloutPolicy>
193void MCTSSolver<EnvTp, RolloutPolicy>::simulate_(
const env_type &env)
196 const auto n_actions =
static_cast<uint_t>(sim.n_actions());
200 std::vector<std::shared_ptr<node_type>> path{root_};
201 std::vector<real_t> rewards;
206 while (!done && path.size() <= config_.max_depth && node->has_children() &&
207 node->n_explored_children() == node->n_children())
209 node = node->max_ucb_child(config_.temperature);
210 auto time_step = sim.step(node->get_action());
211 rewards.push_back(time_step.reward());
212 done = time_step.done();
213 path.push_back(node);
217 if (!done && path.size() <= config_.max_depth)
219 if (!node->has_children())
221 expand_(node, n_actions);
223 auto child = node->get_child(node->n_explored_children());
224 node->update_explored_children();
225 auto time_step = sim.step(child->get_action());
226 rewards.push_back(time_step.reward());
227 done = time_step.done();
228 path.push_back(child);
232 real_t rollout_return = 0.0;
234 for (
auto depth = path.size() - 1; !done && depth < config_.max_depth; ++depth)
236 auto time_step = sim.step(rollout_policy_(sim, generator_));
237 rollout_return += discount * time_step.reward();
238 discount *= config_.gamma;
239 done = time_step.done();
245 real_t g = rollout_return;
246 for (
auto i = path.size(); i-- > 1;)
248 g = rewards[i - 1] + config_.gamma * g;
249 path[i]->update_visits();
250 path[i]->update_total_score(g);
253 root_->update_visits();
254 root_->update_total_score(g);
Base class for Nodes in a MC tree search.
Definition mcts_node.h:31
MCTSSolver(const MCTSConfig &config, RolloutPolicy rollout_policy=RolloutPolicy())
Definition mc_tree_search_solver.h:122
std::shared_ptr< const node_type > root() const noexcept
The root of the tree of the last plan(), or null before the first.
Definition mc_tree_search_solver.h:137
MCTSNodeBase< action_type, state_type > node_type
Definition mc_tree_search_solver.h:120
action_type plan(const env_type &env)
plan. Runs config().n_simulations simulations from copies of env and returns the action that is best ...
Definition mc_tree_search_solver.h:153
EnvTp env_type
Definition mc_tree_search_solver.h:117
const MCTSConfig & config() const noexcept
Definition mc_tree_search_solver.h:139
env_type::action_type action_type
Definition mc_tree_search_solver.h:118
env_type::state_type state_type
Definition mc_tree_search_solver.h:119
What MCTSSolver needs from an environment: it is copied for every simulation, has a fixed number of d...
Definition mc_tree_search_solver.h:28
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
bool done
Definition play.py:40
The configuration of MCTSSolver.
Definition mc_tree_search_solver.h:61
real_t temperature
How much UCB favours children that were visited less. It has the units of the returns,...
Definition mc_tree_search_solver.h:77
uint_t n_simulations
How many simulations plan() runs before it picks an action.
Definition mc_tree_search_solver.h:65
uint_t max_depth
The most steps a simulation looks ahead, in the tree and in the rollout after it.
Definition mc_tree_search_solver.h:71
uint_t seed
The seed of the generator of the random rollouts and of the order the children are tried in.
Definition mc_tree_search_solver.h:88
real_t gamma
The discount factor of the returns.
Definition mc_tree_search_solver.h:82