MachineIntelligenceCore:Algorithms
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Action2D.hpp
Go to the documentation of this file.
1 
23 #ifndef SRC_TYPES_Action_HPP_
24 #define SRC_TYPES_Action_HPP_
25 
26 #include <random>
27 #include <iostream>
28 
29 namespace mic {
30 namespace types {
31 
36 enum class NESW : short
37 {
38  North = 0,
39  East,
40  South,
41  West,
42  None,
43  Random,
44  Exit
45 };
46 
47 
53 public:
54 
56  int dx;
57 
59  int dy;
60 
64  Action2DInterface() : dx(0), dy(0) { };
65 
72  friend std::ostream& operator<<(std::ostream& os_, const Action2DInterface& ac_)
73  {
74  os_ << "[dx,dy]: [" << ac_.dx << ',' << ac_.dy << "]";
75  return os_;
76  }
77 
78 protected:
79 };
80 
81 
87 template <typename ActionType>
89 public:
93  virtual ~Action2D() { };
94 
99  virtual void setAction(ActionType type_) = 0;
100 
105  ActionType getType() { return type;}
106 
107 protected:
111  ActionType type;
112 
117 
118 };
119 
124 class NESWAction : public mic::types::Action2D<NESW> {
125 public:
130 
134  NESWAction(NESW type_) : Action2D() { setAction(type_); };
135 
139  NESWAction(size_t type_) : Action2D() { setAction((NESW)type_); };
140 
145  void setAction(NESW type_) {
146  type = type_;
147  switch (type_) {
148  case NESW::North: dy =-1; dx =0; break;
149  case NESW::East: dy =0; dx =1; break;
150  case NESW::South: dy =1; dx =0; break;
151  case NESW::West: dy =0; dx =-1; break;
152  case NESW::None:
153  case NESW::Exit:
154  default: dy =0; dx =0;
155  }//: switch
156  }
157 
164  friend std::ostream& operator<<(std::ostream& os_, const NESWAction& ac_)
165  {
166  os_ << "[dx,dy]: [" << ac_.dx << ',' << ac_.dy << "]";
167  switch (ac_.type) {
168  case NESW::North: os_ << " (North)"; break;
169  case NESW::East: os_ << " (East)"; break;
170  case NESW::South: os_ << " (South)"; break;
171  case NESW::West: os_ << " (West)"; break;
172  case NESW::None: os_ << " (None)"; break;
173  case NESW::Random: os_ << " (Random)"; break;
174  case NESW::Exit: os_ << " (Exit)"; break;
175  default: break;
176  }//: switch
177 
178  return os_;
179  }
180 
181 };
182 
183 
189 public:
194  rng_mt19937_64(rd())
195  {
196  type = NESW::Random;
197 
198  // Initialize uniform integer distribution.
199  std::uniform_int_distribution<> index_dist((int)NESW::North, (int)NESW::West);
200 
201  // Select a random action.
202  setAction((NESW) index_dist(rng_mt19937_64));
203  };
204 
205 
206 private:
210  std::random_device rd;
211 
215  std::mt19937_64 rng_mt19937_64;
216 
217 };
218 
219 
225 public:
230 
231 };
232 
233 
238 #define A_NORTH mic::types::NESWAction(mic::types::NESW::North)
239 
244 #define A_EAST mic::types::NESWAction(mic::types::NESW::East)
245 
250 #define A_SOUTH mic::types::NESWAction(mic::types::NESW::South)
251 
256 #define A_WEST mic::types::NESWAction(mic::types::NESW::West)
257 
262 #define A_RANDOM mic::types::RandomNESWAction()
263 
268 #define A_EXIT mic::types::ExitAction()
269 
274 #define A_NONE mic::types::NESWAction(mic::types::NESW::None)
275 
276 
277 
278 } /* namespace types */
279 } /* namespace mic */
280 
281 #endif /* SRC_TYPES_Action_HPP_ */
Class representing a random N/E/S/W action.
Definition: Action2D.hpp:188
Abstract template class representing an action in 2-D space.
Definition: Action2D.hpp:88
int dy
Increment according to y axis.
Definition: Action2D.hpp:59
Class representing an N/E/S/W action.
Definition: Action2D.hpp:124
virtual void setAction(ActionType type_)=0
NESWAction(NESW type_)
Definition: Action2D.hpp:134
std::mt19937_64 rng_mt19937_64
Definition: Action2D.hpp:215
int dx
Increment according to x axis.
Definition: Action2D.hpp:56
Class representing an exit action.
Definition: Action2D.hpp:224
ActionType getType()
Definition: Action2D.hpp:105
virtual ~Action2D()
Definition: Action2D.hpp:93
void setAction(NESW type_)
Definition: Action2D.hpp:145
NESWAction(size_t type_)
Definition: Action2D.hpp:139
friend std::ostream & operator<<(std::ostream &os_, const NESWAction &ac_)
Definition: Action2D.hpp:164
std::random_device rd
Definition: Action2D.hpp:203
friend std::ostream & operator<<(std::ostream &os_, const Action2DInterface &ac_)
Definition: Action2D.hpp:72
NESW
Enumeration of possible types of actions in 2D.
Definition: Action2D.hpp:36
Interface class representing an action in 2-D space.
Definition: Action2D.hpp:52