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; }
103 const double maxTorque = 2.0,
104 const double dt = 0.05,
105 const double angleThreshold =
M_PI / 12,
106 const double doneReward = 0.0,
107 const size_t maxSteps = 0) :
108 maxAngularVelocity(maxAngularVelocity),
109 maxTorque(maxTorque),
111 angleThreshold(angleThreshold),
112 doneReward(doneReward),
134 double theta = state.
Theta();
138 const double gravity = 10.0;
139 const double mass = 1.0;
140 const double length = 1.0;
143 double torque = std::min(
144 std::max(action.
action[0], -maxTorque), maxTorque);
148 std::pow(angularVelocity, 2) + 0.001 * std::pow(torque, 2);
151 double newAngularVelocity = angularVelocity + (-3.0 * gravity / (2 *
152 length) * std::sin(theta +
M_PI) + 3.0 / std::pow(mass * length, 2) *
155 -maxAngularVelocity), maxAngularVelocity);
156 nextState.
Theta() = theta + newAngularVelocity * dt;
162 if (done && maxSteps != 0 && stepsPerformed >= maxSteps)
182 return Sample(state, action, nextState);
219 if (maxSteps != 0 && stepsPerformed >= maxSteps)
221 Log::Info <<
"Episode terminated due to the maximum number of steps" 225 else if (state.
Theta() >
M_PI - angleThreshold ||
228 Log::Info <<
"Episode terminated due to agent succeeding.";
244 double maxAngularVelocity;
253 double angleThreshold;
262 size_t stepsPerformed;
double Theta() const
Get the theta.
double & Theta()
Modify the value of theta.
Implementation of Pendulum task.
size_t MaxSteps() const
Get the maximum number of steps allowed.
double Sample(const State &state, const Action &action, State &nextState)
Dynamics of Pendulum.
The core includes that mlpack expects; standard C++ includes and Armadillo.
Implementation of action 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 angle for a particular theta.
Pendulum(const double maxAngularVelocity=8, const double maxTorque=2.0, const double dt=0.05, const double angleThreshold=M_PI/12, const double doneReward=0.0, const size_t maxSteps=0)
Construct a Pendulum instance using the given values.
bool IsTerminal(const State &state) const
This function checks if the pendulum has reaches a terminal state.
double AngularVelocity() const
Get the angular velocity.
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.
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].
const arma::colvec & Encode() const
Encode the state to a column vector.