From ae2ef99ccd8305c32d3b2c629e3673f89f5ea41f Mon Sep 17 00:00:00 2001 From: Shashank Shekhar Date: Mon, 14 May 2018 10:06:50 +0530 Subject: [PATCH] Switched to Camel Case and fixed some style issues --- .../environment/pendulum.hpp | 35 +++++++++---------- src/mlpack/tests/rl_components_test.cpp | 4 +-- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/mlpack/methods/reinforcement_learning/environment/pendulum.hpp b/src/mlpack/methods/reinforcement_learning/environment/pendulum.hpp index 1ea437ef33..25df22f3da 100644 --- a/src/mlpack/methods/reinforcement_learning/environment/pendulum.hpp +++ b/src/mlpack/methods/reinforcement_learning/environment/pendulum.hpp @@ -115,34 +115,33 @@ class Pendulum const Action& action, State& nextState) const { - // Get current state + // Get current state. double theta = state.Theta(); - double angular_velocity = state.AngularVelocity(); + double angularVelocity = state.AngularVelocity(); - // Define required variables - double gravity = 10.0; - double mass = 1.0; - double length = 1.0; + // Define constants which specify our pendulum. + const gravity = 10.0; + const mass = 1.0; + const length = 1.0; - // Get action and clip the values + // Get action and clip the values between max and min limits. double torque = std::min( std::max(action.action[0], -maxTorque), maxTorque); - // Calculate costs + // Calculate costs of taking this action in the current state. double costs = pow(AngleNormalize(theta), 2) + 0.1 * - pow(angular_velocity, 2) + 0.001 * pow(torque, 2); + pow(angularVelocity, 2) + 0.001 * pow(torque, 2); - // Calculate new state values - double new_angular_velocity = angular_velocity + (-3.0 * gravity / (2 * - length) * std::sin(theta + M_PI) + 3.0 / (pow(mass * length, 2)) * + // Calculate new state values and assign to the next state. + double newAngularVelocity = angularVelocity + (-3.0 * gravity / (2 * + length) * std::sin(theta + M_PI) + 3.0 / pow(mass * length, 2) * torque) * dt; - double new_theta = theta + new_angular_velocity * dt; - - // Set values for next state - nextState.Theta() = new_theta; - nextState.AngularVelocity() = std::min(std::max(new_angular_velocity, + nextState.AngularVelocity() = std::min(std::max(newAngularVelocity, -maxAngularVelocity), maxAngularVelocity); + nextState.Theta() = theta + newAngularVelocity * dt; + // Return the reward of taking the action in current state. + // The reward is simply the negative of cost incurred for the action. return -costs; } @@ -180,7 +179,7 @@ class Pendulum */ double AngleNormalize(double theta) const { - // Scale angle within [-pi, pi) + // Scale angle within [-pi, pi). return double(fmod(theta + M_PI, 2 * M_PI) - M_PI); } diff --git a/src/mlpack/tests/rl_components_test.cpp b/src/mlpack/tests/rl_components_test.cpp index 70f37b8d8a..e84875f269 100644 --- a/src/mlpack/tests/rl_components_test.cpp +++ b/src/mlpack/tests/rl_components_test.cpp @@ -42,10 +42,10 @@ BOOST_AUTO_TEST_CASE(SimplePendulumTest) action.action[0] = math::Random(-2.0, 2.0); double reward = task.Sample(state, action); - // The reward is always negative. Check if not lower than lowest possible + // The reward is always negative. Check if not lower than lowest possible. BOOST_REQUIRE(reward >= -(pow(M_PI, 2) + 6.404)); - // The reward is simply the torque. Check if dimension is 1 + // The state is simply the torque. Check if dimension is 1. BOOST_REQUIRE_EQUAL(1, action.size); }