bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
mlp_policy.h
Go to the documentation of this file.
1#ifndef BITRL_RL_ALGORITHMS_ES_MLP_POLICY_H
2#define BITRL_RL_ALGORITHMS_ES_MLP_POLICY_H
3
6
7#include <algorithm>
8#include <cmath>
9#include <stdexcept>
10#include <vector>
11
12namespace bitrl
13{
14namespace rl::algos::es
15{
16
30{
31 public:
32 explicit MLPPolicy(std::vector<uint_t> layer_sizes);
33
37 uint_t n_parameters() const noexcept { return n_parameters_; }
38
39 const std::vector<uint_t> &layer_sizes() const noexcept { return sizes_; }
40
44 std::vector<real_t> initial_parameters(uint_t seed) const;
45
49 std::vector<real_t> forward(const std::vector<real_t> &parameters,
50 const std::vector<real_t> &x) const;
51
55 uint_t act_discrete(const std::vector<real_t> &parameters, const std::vector<real_t> &x) const;
56
60 std::vector<real_t> act_continuous(const std::vector<real_t> &parameters,
61 const std::vector<real_t> &x, real_t scale = 1.0) const;
62
63 private:
64 std::vector<uint_t> sizes_;
65 uint_t n_parameters_{0};
66};
67
68inline MLPPolicy::MLPPolicy(std::vector<uint_t> layer_sizes) : sizes_(std::move(layer_sizes))
69{
70 if (sizes_.size() < 2 ||
71 std::any_of(sizes_.begin(), sizes_.end(), [](uint_t s) { return s == 0; }))
72 {
73 throw std::invalid_argument("MLPPolicy: needs at least two layers, none empty");
74 }
75 for (std::size_t l = 0; l + 1 < sizes_.size(); ++l)
76 {
77 n_parameters_ += sizes_[l + 1] * (sizes_[l] + 1);
78 }
79}
80
81inline std::vector<real_t> MLPPolicy::initial_parameters(uint_t seed) const
82{
83 PortableNormal normal(seed);
84 std::vector<real_t> p;
85 p.reserve(n_parameters_);
86 for (std::size_t l = 0; l + 1 < sizes_.size(); ++l)
87 {
88 const real_t scale = 1.0 / std::sqrt(static_cast<real_t>(sizes_[l]));
89 for (uint_t i = 0; i < sizes_[l + 1] * sizes_[l]; ++i)
90 {
91 p.push_back(scale * normal());
92 }
93 p.insert(p.end(), sizes_[l + 1], 0.0);
94 }
95 return p;
96}
97
98inline std::vector<real_t> MLPPolicy::forward(const std::vector<real_t> &parameters,
99 const std::vector<real_t> &x) const
100{
101 if (parameters.size() != n_parameters_ || x.size() != sizes_.front())
102 {
103 throw std::invalid_argument("MLPPolicy: wrong number of parameters or inputs");
104 }
105 std::vector<real_t> in = x;
106 std::size_t at = 0;
107 for (std::size_t l = 0; l + 1 < sizes_.size(); ++l)
108 {
109 const uint_t n_in = sizes_[l];
110 const uint_t n_out = sizes_[l + 1];
111 const real_t *w = parameters.data() + at;
112 const real_t *b = w + n_out * n_in;
113 std::vector<real_t> out(n_out);
114 for (uint_t o = 0; o < n_out; ++o)
115 {
116 real_t s = b[o];
117 for (uint_t i = 0; i < n_in; ++i)
118 {
119 s += w[o * n_in + i] * in[i];
120 }
121 out[o] = (l + 2 < sizes_.size()) ? std::tanh(s) : s;
122 }
123 at += n_out * (n_in + 1);
124 in = std::move(out);
125 }
126 return in;
127}
128
129inline uint_t MLPPolicy::act_discrete(const std::vector<real_t> &parameters,
130 const std::vector<real_t> &x) const
131{
132 const auto out = forward(parameters, x);
133 return static_cast<uint_t>(std::max_element(out.begin(), out.end()) - out.begin());
134}
135
136inline std::vector<real_t> MLPPolicy::act_continuous(const std::vector<real_t> &parameters,
137 const std::vector<real_t> &x,
138 real_t scale) const
139{
140 auto out = forward(parameters, x);
141 for (auto &o : out)
142 {
143 o = scale * std::tanh(o);
144 }
145 return out;
146}
147
152template <typename EnvType, typename Act>
153real_t episode_return(EnvType &env, Act &&act, uint_t seed, uint_t max_steps)
154{
155 auto step = env.reset(seed);
156 real_t total = 0.0;
157 for (uint_t t = 0; t < max_steps && !step.done(); ++t)
158 {
159 step = env.step(act(step.observation()));
160 total += step.reward();
161 }
162 return total;
163}
164
165} // namespace rl::algos::es
166} // namespace bitrl
167
168#endif // BITRL_RL_ALGORITHMS_ES_MLP_POLICY_H
uint_t act_discrete(const std::vector< real_t > &parameters, const std::vector< real_t > &x) const
The output with the largest value: the action of a discrete action space.
Definition mlp_policy.h:129
std::vector< real_t > act_continuous(const std::vector< real_t > &parameters, const std::vector< real_t > &x, real_t scale=1.0) const
scale * tanh(outputs): the action of a bounded continuous action space
Definition mlp_policy.h:136
const std::vector< uint_t > & layer_sizes() const noexcept
Definition mlp_policy.h:39
uint_t n_parameters() const noexcept
How many parameters the network has.
Definition mlp_policy.h:37
MLPPolicy(std::vector< uint_t > layer_sizes)
Definition mlp_policy.h:68
std::vector< real_t > forward(const std::vector< real_t > &parameters, const std::vector< real_t > &x) const
The outputs of the network for observation x with parameters.
Definition mlp_policy.h:98
std::vector< real_t > initial_parameters(uint_t seed) const
Weights drawn from N(0, 1 / fan_in), biases 0.
Definition mlp_policy.h:81
Normal random numbers that are the same on every machine.
Definition evolution_strategy.h:33
Definition es_config.h:9
real_t episode_return(EnvType &env, Act &&act, uint_t seed, uint_t max_steps)
The undiscounted return of one episode of env, reset with seed, acting with act, at most max_steps st...
Definition mlp_policy.h:153
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