continuous_mountain_car.hpp
Go to the documentation of this file.
1 
16 #ifndef MLPACK_METHODS_RL_ENVIRONMENT_CONTINUOUS_MOUNTAIN_CAR_HPP
17 #define MLPACK_METHODS_RL_ENVIRONMENT_CONTINUOUS_MOUNTAIN_CAR_HPP
18 
19 #include <mlpack/prereqs.hpp>
20 
21 namespace mlpack {
22 namespace rl {
23 
28 {
29  public:
34  class State
35  {
36  public:
40  State() : data(dimension, arma::fill::zeros)
41  { /* Nothing to do here. */ }
42 
48  State(const arma::colvec& data): data(data)
49  { /* Nothing to do here. */ }
50 
52  arma::colvec& Data() { return data; }
53 
55  double Velocity() const { return data[0]; }
57  double& Velocity() { return data[0]; }
58 
60  double Position() const { return data[1]; }
62  double& Position() { return data[1]; }
63 
65  const arma::colvec& Encode() const { return data; }
66 
68  static constexpr size_t dimension = 2;
69 
70  private:
72  arma::colvec data;
73  };
74 
83  struct Action
84  {
85  double action[1];
86  // Storing degree of freedom
87  const int size = 1;
88  };
89 
98  ContinuousMountainCar(const double positionMin = -1.2,
99  const double positionMax = 0.5,
100  const double velocityMin = -0.07,
101  const double velocityMax = 0.07) :
102  positionMin(positionMin),
103  positionMax(positionMax),
104  velocityMin(velocityMin),
105  velocityMax(velocityMax)
106  { /* Nothing to do here */ }
107 
117  double Sample(const State& state,
118  const Action& action,
119  State& nextState) const
120  {
121  // Calculate acceleration.
122  double force = std::min(std::max(action.action[0], -1.0), 1.0);
123  nextState.Velocity() = state.Velocity() + 0.001 * force - 0.0025 *
124  std::cos(3 * state.Position());
125  nextState.Velocity() = std::min(
126  std::max(nextState.Velocity(), velocityMin), velocityMax);
127 
128  // Update states.
129  nextState.Position() = state.Position() + nextState.Velocity();
130  nextState.Position() = std::min(
131  std::max(nextState.Position(), positionMin), positionMax);
132 
133  if (nextState.Position() == positionMin && nextState.Velocity() < 0)
134  {
135  nextState.Velocity() = 0.0;
136  }
137  // If it is a terminal state, reward is 100.0
138  if (IsTerminal(nextState))
139  return 100.0;
140  return -pow(action.action[0], 2)*0.1;
141  }
142 
151  double Sample(const State& state, const Action& action) const
152  {
153  State nextState;
154  return Sample(state, action, nextState);
155  }
156 
164  {
165  State state;
166  state.Velocity() = 0.0;
167  state.Position() = math::Random(-0.6, -0.4);
168  return state;
169  }
170 
177  bool IsTerminal(const State& state) const
178  {
179  return bool(state.Position() == positionMax);
180  }
181 
182  private:
184  double positionMin;
185 
187  double positionMax;
188 
190  double velocityMin;
191 
193  double velocityMax;
194 };
195 
196 } // namespace rl
197 } // namespace mlpack
198 
199 #endif
.hpp
Definition: add_to_po.hpp:21
The core includes that mlpack expects; standard C++ includes and Armadillo.
double Sample(const State &state, const Action &action) const
Dynamics of Continuous Mountain Car.
Implementation of state of Continuous Mountain Car.
const arma::colvec & Encode() const
Encode the state to a column vector.
static constexpr size_t dimension
Dimension of the encoded state.
ContinuousMountainCar(const double positionMin=-1.2, const double positionMax=0.5, const double velocityMin=-0.07, const double velocityMax=0.07)
Construct a Continuous Mountain Car instance using the given constant.
State InitialSample() const
Initial position is randomly generated within [-0.6, -0.4].
State(const arma::colvec &data)
Construct a state based on the given data.
arma::colvec & Data()
Modify the internal representation of the state.
Implementation of action of Continuous Mountain Car.
Implementation of Continuous Mountain Car task.
double Random()
Generates a uniform random number between 0 and 1.
Definition: random.hpp:71
bool IsTerminal(const State &state) const
Whether given state is a terminal state.
double Sample(const State &state, const Action &action, State &nextState) const
Dynamics of Continuous Mountain Car.