continuous_mountain_car.hpp
Go to the documentation of this file.
1 
17 #ifndef MLPACK_METHODS_RL_ENVIRONMENT_CONTINUOUS_MOUNTAIN_CAR_HPP
18 #define MLPACK_METHODS_RL_ENVIRONMENT_CONTINUOUS_MOUNTAIN_CAR_HPP
19 
20 #include <mlpack/prereqs.hpp>
21 
22 namespace mlpack {
23 namespace rl {
24 
29 {
30  public:
35  class State
36  {
37  public:
41  State() : data(dimension, arma::fill::zeros)
42  { /* Nothing to do here. */ }
43 
49  State(const arma::colvec& data): data(data)
50  { /* Nothing to do here. */ }
51 
53  arma::colvec& Data() { return data; }
54 
56  double Velocity() const { return data[0]; }
58  double& Velocity() { return data[0]; }
59 
61  double Position() const { return data[1]; }
63  double& Position() { return data[1]; }
64 
66  const arma::colvec& Encode() const { return data; }
67 
69  static constexpr size_t dimension = 2;
70 
71  private:
73  arma::colvec data;
74  };
75 
84  struct Action
85  {
86  double action[1];
87  // Storing degree of freedom
88  const int size = 1;
89  };
90 
101  ContinuousMountainCar(const double positionMin = -1.2,
102  const double positionMax = 0.6,
103  const double positionGoal = 0.45,
104  const double velocityMin = -0.07,
105  const double velocityMax = 0.07,
106  const double power = 0.0015) :
107  positionMin(positionMin),
108  positionMax(positionMax),
109  positionGoal(positionGoal),
110  velocityMin(velocityMin),
111  velocityMax(velocityMax),
112  power(power)
113  { /* Nothing to do here */ }
114 
124  double Sample(const State& state,
125  const Action& action,
126  State& nextState) const
127  {
128  // Calculate acceleration.
129  double force = std::min(std::max(action.action[0], -1.0), 1.0);
130 
131  // Update states.
132  nextState.Velocity() = state.Velocity() + force * power - 0.0025 *
133  std::cos(3 * state.Position());
134  nextState.Velocity() = std::min(
135  std::max(nextState.Velocity(), velocityMin), velocityMax);
136  nextState.Position() = state.Position() + nextState.Velocity();
137  nextState.Position() = std::min(
138  std::max(nextState.Position(), positionMin), positionMax);
139  if (nextState.Position() == positionMin && nextState.Velocity() < 0)
140  nextState.Velocity() = 0.0;
141 
142  // Calculate reward
143  double reward = 0.0;
144  // If it is a terminal state, add a reward of 100.0
145  if (IsTerminal(nextState))
146  reward = 100.0;
147  reward -= std::pow(action.action[0], 2) * 0.1;
148  return reward;
149  }
150 
159  double Sample(const State& state, const Action& action) const
160  {
161  State nextState;
162  return Sample(state, action, nextState);
163  }
164 
172  {
173  State state;
174  state.Velocity() = 0.0;
175  state.Position() = math::Random(-0.6, -0.4);
176  return state;
177  }
178 
185  bool IsTerminal(const State& state) const
186  {
187  return state.Position() >= positionGoal;
188  }
189 
190  private:
192  double positionMin;
193 
195  double positionMax;
196 
198  double positionGoal;
199 
201  double velocityMin;
202 
204  double velocityMax;
205 
207  double power;
208 };
209 
210 } // namespace rl
211 } // namespace mlpack
212 
213 #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.
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.
ContinuousMountainCar(const double positionMin=-1.2, const double positionMax=0.6, const double positionGoal=0.45, const double velocityMin=-0.07, const double velocityMax=0.07, const double power=0.0015)
Construct a Continuous Mountain Car instance using the given constant.