16 #ifndef MLPACK_METHODS_RL_ENVIRONMENT_PENDULUM_HPP 17 #define MLPACK_METHODS_RL_ENVIRONMENT_PENDULUM_HPP 53 State(
const arma::colvec& data): theta(0), data(data)
57 arma::colvec&
Data() {
return data; }
60 double Theta()
const {
return theta; }
62 double&
Theta() {
return theta; }
70 const arma::colvec&
Encode() {
return data; }
75 data[0] = std::sin(theta);
76 data[1] = std::cos(theta);
105 static const size_t size = 1;
119 const double maxAngularVelocity = 8,
120 const double maxTorque = 2.0,
121 const double dt = 0.05,
122 const double doneReward = 0.0) :
124 maxAngularVelocity(maxAngularVelocity),
125 maxTorque(maxTorque),
127 doneReward(doneReward),
148 double theta = state.
Theta();
152 const double gravity = 10.0;
153 const double mass = 1.0;
154 const double length = 1.0;
161 std::pow(angularVelocity, 2) + 0.001 * std::pow(torque, 2);
164 double newAngularVelocity = angularVelocity + (-3.0 * gravity / (2 *
165 length) * std::sin(theta +
M_PI) + 3.0 / (mass * std::pow(length, 2)) *
167 nextState.
Theta() = theta + newAngularVelocity * dt;
169 -maxAngularVelocity, maxAngularVelocity);
188 return Sample(state, action, nextState);
215 double x = fmod(theta +
M_PI, 2 *
M_PI);
229 if (maxSteps != 0 && stepsPerformed >= maxSteps)
231 Log::Info <<
"Episode terminated due to the maximum number of steps" 251 double maxAngularVelocity;
263 size_t stepsPerformed;
bool IsTerminal(const State &) const
This function checks if the pendulum has reaches a terminal state.
double Theta() const
Get the theta.
double & Theta()
Modify the value of theta.
Implementation of Pendulum task.
Linear algebra utility functions, generally performed on matrices or vectors.
size_t MaxSteps() const
Get the maximum number of steps allowed.
const arma::colvec & Encode()
Encode the state to a column vector.
double Sample(const State &state, const Action &action, State &nextState)
Dynamics of Pendulum.
The core includes that mlpack expects; standard C++ includes and Armadillo.
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 angle for a particular theta.
double AngularVelocity() const
Get the angular velocity.
Miscellaneous math clamping routines.
Implementation of action of Pendulum.
State()
Construct a state instance.
static MLPACK_EXPORT util::PrefixedOutStream Info
Prints informational messages if –verbose is specified, prefixed with [INFO ].
size_t StepsPerformed() const
Get the number of steps performed.
void SetState()
Updates the theta transformations in data.
double & AngularVelocity()
Modify the value of angular velocity.
static constexpr size_t dimension
Dimension of the encoded state.
size_t & MaxSteps()
Set the maximum number of steps allowed.
Implementation of state of Pendulum.
double Sample(const State &state, const Action &action)
Dynamics of Pendulum.
double Random()
Generates a uniform random number between 0 and 1.
State InitialSample()
Initial theta is randomly generated within [-pi, pi].
Action()
Construct an action instance.
Pendulum(const size_t maxSteps=200, const double maxAngularVelocity=8, const double maxTorque=2.0, const double dt=0.05, const double doneReward=0.0)
Construct a Pendulum instance using the given values.
std::vector< double > action
double ClampRange(double value, const double rangeMin, const double rangeMax)
Clamp a number between a particular range.