1#ifndef BITRL_PARALLEL_ENVS_H
2#define BITRL_PARALLEL_ENVS_H
12#include <unordered_map>
52 void make(
const std::string &version,
53 const std::unordered_map<std::string, std::any> &make_options,
54 const std::unordered_map<std::string, std::any> &reset_options);
59 const std::vector<time_step_type> &
reset();
64 const std::vector<time_step_type> &
step(
const std::vector<action_type> &actions);
71 bool done(
uint_t i)
const {
return time_steps_.at(i).last(); }
78 const std::vector<time_step_type> &
time_steps() const noexcept {
return time_steps_; }
82 const EnvType &
env(
uint_t i)
const {
return *envs_.at(i); }
92 void use_threads(
bool threads)
noexcept { use_threads_ = threads; }
102 template <
typename F>
void for_each_(F &&f);
104 std::vector<std::unique_ptr<EnvType>> envs_;
105 std::vector<time_step_type> time_steps_;
107 bool use_threads_{
true};
108 bool is_created_{
false};
111template <
typename EnvType>
112template <
typename... Args>
117 throw std::invalid_argument(
"ParallelEnvs: needs at least one copy");
120 for (
uint_t i = 0; i < n; ++i)
122 envs_.push_back(std::make_unique<EnvType>(args...));
124 time_steps_.resize(n);
127template <
typename EnvType>
template <
typename F>
void ParallelEnvs<EnvType>::for_each_(F &&f)
129 if (!use_threads_ || envs_.size() == 1)
131 for (
uint_t i = 0; i < size(); ++i)
141template <
typename EnvType>
143 const std::unordered_map<std::string, std::any> &make_options,
144 const std::unordered_map<std::string, std::any> &reset_options)
147 if (
const auto itr = reset_options.find(
"seed"); itr != reset_options.end())
149 seed = std::any_cast<uint_t>(itr->second);
154 auto options = reset_options;
156 envs_[i]->make(version, make_options, options);
161template <
typename EnvType>
166 throw std::logic_error(
"ParallelEnvs: call make() before reset()");
168 for_each_([
this](
uint_t i) { time_steps_[i] = envs_[i]->reset(); });
172template <
typename EnvType>
173const std::vector<typename ParallelEnvs<EnvType>::time_step_type> &
176 if (actions.size() != envs_.size())
178 throw std::invalid_argument(
"ParallelEnvs: step() needs an action per copy, " +
179 std::to_string(envs_.size()) +
", but got " +
180 std::to_string(actions.size()));
185 if (!time_steps_[i].last())
187 time_steps_[i] = envs_[i]->step(actions[i]);
195 for (
const auto &t : time_steps_)
207 for (
auto &
env : envs_)
EnvType::state_type state_type
Definition parallel_envs.h:40
bool done(uint_t i) const
Whether the episode of copy i is over.
Definition parallel_envs.h:71
void close()
Close every copy.
Definition parallel_envs.h:205
bool all_done() const
Whether the episode of every copy is over.
Definition parallel_envs.h:193
const std::vector< time_step_type > & reset()
Start a new episode in every copy, each with its next seed.
Definition parallel_envs.h:162
const EnvType & env(uint_t i) const
Definition parallel_envs.h:82
const std::vector< time_step_type > & step(const std::vector< action_type > &actions)
Step every copy whose episode is not over with its action.
Definition parallel_envs.h:174
uint_t size() const noexcept
Definition parallel_envs.h:66
EnvType & env(uint_t i)
Definition parallel_envs.h:80
EnvType env_type
Definition parallel_envs.h:38
void use_threads(bool threads) noexcept
Step on threads, or on this thread one copy after the other.
Definition parallel_envs.h:92
void set_pool(bitsim::utils::concurrency::ThreadPool &pool) noexcept
Step on the threads of pool, the default pool if not set.
Definition parallel_envs.h:87
const std::vector< time_step_type > & time_steps() const noexcept
Definition parallel_envs.h:78
EnvType::action_type action_type
Definition parallel_envs.h:41
EnvType::time_step_type time_step_type
Definition parallel_envs.h:39
bool is_created() const noexcept
Definition parallel_envs.h:94
ParallelEnvs(uint_t n, Args &&...args)
n copies, each built with args
Definition parallel_envs.h:113
void make(const std::string &version, const std::unordered_map< std::string, std::any > &make_options, const std::unordered_map< std::string, std::any > &reset_options)
Make every copy with the same options; copy i resets first with the seed task_seed(seed,...
Definition parallel_envs.h:142
A fixed number of threads that run the tasks given to them, in the order given.
Definition thread_pool.h:33
Definition cart_pole.h:18
Various utilities used when working with RL problems.
Definition bitrl_log.h:25
uint_t task_seed(uint_t base_seed, uint_t task) noexcept
A seed for task task, made from base_seed.
Definition parallel.h:148
void parallel_for(Index begin, Index end, F &&f, ThreadPool &pool=default_thread_pool(), Index grain=1)
Call f(i) for every i in [begin, end), spread over the threads of pool.
Definition parallel.h:38
ThreadPool & default_thread_pool()
The pool that parallel_for() and parallel_map() use when given none. It starts ThreadPool::default_n_...
Definition thread_pool.cpp:77
std::size_t uint_t
uint_t
Definition bitsim_types.h:40