bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
parallel_envs.h
Go to the documentation of this file.
1#ifndef BITRL_PARALLEL_ENVS_H
2#define BITRL_PARALLEL_ENVS_H
3
7
8#include <any>
9#include <memory>
10#include <stdexcept>
11#include <string>
12#include <unordered_map>
13#include <vector>
14
15namespace bitrl
16{
17namespace envs
18{
19
35template <typename EnvType> class ParallelEnvs
36{
37 public:
38 typedef EnvType env_type;
39 typedef typename EnvType::time_step_type time_step_type;
40 typedef typename EnvType::state_type state_type;
41 typedef typename EnvType::action_type action_type;
42
46 template <typename... Args> explicit ParallelEnvs(uint_t n, Args &&...args);
47
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);
55
59 const std::vector<time_step_type> &reset();
60
64 const std::vector<time_step_type> &step(const std::vector<action_type> &actions);
65
66 uint_t size() const noexcept { return static_cast<uint_t>(envs_.size()); }
67
71 bool done(uint_t i) const { return time_steps_.at(i).last(); }
72
76 bool all_done() const;
77
78 const std::vector<time_step_type> &time_steps() const noexcept { return time_steps_; }
79
80 EnvType &env(uint_t i) { return *envs_.at(i); }
81
82 const EnvType &env(uint_t i) const { return *envs_.at(i); }
83
87 void set_pool(bitsim::utils::concurrency::ThreadPool &pool) noexcept { pool_ = &pool; }
88
92 void use_threads(bool threads) noexcept { use_threads_ = threads; }
93
94 bool is_created() const noexcept { return is_created_; }
95
99 void close();
100
101 private:
102 template <typename F> void for_each_(F &&f);
103
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};
109};
110
111template <typename EnvType>
112template <typename... Args>
114{
115 if (n == 0)
116 {
117 throw std::invalid_argument("ParallelEnvs: needs at least one copy");
118 }
119 envs_.reserve(n);
120 for (uint_t i = 0; i < n; ++i)
121 {
122 envs_.push_back(std::make_unique<EnvType>(args...));
123 }
124 time_steps_.resize(n);
125}
126
127template <typename EnvType> template <typename F> void ParallelEnvs<EnvType>::for_each_(F &&f)
128{
129 if (!use_threads_ || envs_.size() == 1)
130 {
131 for (uint_t i = 0; i < size(); ++i)
132 {
133 f(i);
134 }
135 return;
136 }
137 auto &pool = pool_ ? *pool_ : bitsim::utils::concurrency::default_thread_pool();
139}
140
141template <typename EnvType>
142void ParallelEnvs<EnvType>::make(const std::string &version,
143 const std::unordered_map<std::string, std::any> &make_options,
144 const std::unordered_map<std::string, std::any> &reset_options)
145{
146 uint_t seed = 42;
147 if (const auto itr = reset_options.find("seed"); itr != reset_options.end())
148 {
149 seed = std::any_cast<uint_t>(itr->second);
150 }
151 // one after the other: making an environment may not be safe on threads
152 for (uint_t i = 0; i < size(); ++i)
153 {
154 auto options = reset_options;
155 options["seed"] = std::any(bitsim::utils::concurrency::task_seed(seed, i));
156 envs_[i]->make(version, make_options, options);
157 }
158 is_created_ = true;
159}
160
161template <typename EnvType>
162const std::vector<typename ParallelEnvs<EnvType>::time_step_type> &ParallelEnvs<EnvType>::reset()
163{
164 if (!is_created_)
165 {
166 throw std::logic_error("ParallelEnvs: call make() before reset()");
167 }
168 for_each_([this](uint_t i) { time_steps_[i] = envs_[i]->reset(); });
169 return time_steps_;
170}
171
172template <typename EnvType>
173const std::vector<typename ParallelEnvs<EnvType>::time_step_type> &
174ParallelEnvs<EnvType>::step(const std::vector<action_type> &actions)
175{
176 if (actions.size() != envs_.size())
177 {
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()));
181 }
182 for_each_(
183 [&](uint_t i)
184 {
185 if (!time_steps_[i].last())
186 {
187 time_steps_[i] = envs_[i]->step(actions[i]);
188 }
189 });
190 return time_steps_;
191}
192
193template <typename EnvType> bool ParallelEnvs<EnvType>::all_done() const
194{
195 for (const auto &t : time_steps_)
196 {
197 if (!t.last())
198 {
199 return false;
200 }
201 }
202 return true;
203}
204
205template <typename EnvType> void ParallelEnvs<EnvType>::close()
206{
207 for (auto &env : envs_)
208 {
209 env->close();
210 }
211 is_created_ = false;
212}
213
214} // namespace envs
215} // namespace bitrl
216
217#endif // BITRL_PARALLEL_ENVS_H
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