16 #ifndef MLPACK_METHODS_RL_ENVIRONMENT_PENDULUM_HPP 17 #define MLPACK_METHODS_RL_ENVIRONMENT_PENDULUM_HPP 51 State(
const arma::colvec& data): data(data)
55 arma::colvec&
Data() {
return data; }
58 double Theta()
const {
return data[0]; }
60 double&
Theta() {
return data[0]; }
68 const arma::colvec&
Encode()
const {
return data; }
98 const double maxTorque = 2.0,
99 const double dt = 0.05) :
100 maxAngularVelocity(maxAngularVelocity),
101 maxTorque(maxTorque),
116 State& nextState)
const 119 double theta = state.
Theta();
123 const double gravity = 10.0;
124 const double mass = 1.0;
125 const double length = 1.0;
128 double torque = std::min(
129 std::max(action.
action[0], -maxTorque), maxTorque);
133 std::pow(angularVelocity, 2) + 0.001 * std::pow(torque, 2);
136 double newAngularVelocity = angularVelocity + (-3.0 * gravity / (2 *
137 length) * std::sin(theta +
M_PI) + 3.0 / std::pow(mass * length, 2) *
140 -maxAngularVelocity), maxAngularVelocity);
141 nextState.
Theta() = theta + newAngularVelocity * dt;
158 return Sample(state, action, nextState);
188 double maxAngularVelocity;
double Theta() const
Get the theta.
double & Theta()
Modify the value of theta.
Implementation of Pendulum task.
The core includes that mlpack expects; standard C++ includes and Armadillo.
Pendulum(const double maxAngularVelocity=8, const double maxTorque=2.0, const double dt=0.05)
Construct a Pendulum instance using the given values.
Implementation of action of Pendulum.
double Sample(const State &state, const Action &action, State &nextState) const
Dynamics of Pendulum.
arma::colvec & Data()
Modify the internal representation of the state.
State(const arma::colvec &data)
Construct a state based on the given data.
double AngleNormalize(double theta) const
This function calculates the normalized anlge for a particular theta.
double AngularVelocity() const
Get the angular velocity.
State()
Construct a state instance.
double & AngularVelocity()
Modify the value of angular velocity.
State InitialSample() const
Initial theta is randomly generated within [-pi, pi].
static constexpr size_t dimension
Dimension of the encoded state.
double Sample(const State &state, const Action &action) const
Dynamics of Pendulum.
Implementation of state of Pendulum.
double Random()
Generates a uniform random number between 0 and 1.
const arma::colvec & Encode() const
Encode the state to a column vector.