simulator.h
Go to the documentation of this file.
1 
23 #ifndef SIMULATOR_H
24 #define SIMULATOR_H
25 
26 namespace argos {
27  class CSimulator;
28  class CLoopFunctions;
29  class CVisualization;
30  class CPhysicsEngine;
31  class CMedium;
32  class CSpace;
33  class CProfiler;
34 }
35 
36 #include <argos3/core/config.h>
37 #include <argos3/core/utility/math/rng.h>
38 #include <argos3/core/utility/configuration/argos_configuration.h>
39 #include <argos3/core/utility/datatypes/datatypes.h>
40 #include <argos3/core/simulator/physics_engine/physics_engine.h>
41 #include <argos3/core/simulator/medium/medium.h>
42 #include <string>
43 #include <map>
44 
50 namespace argos {
51 
62  class CSimulator {
63 
64  private:
65 
69  CSimulator();
70 
75  CSimulator(const CSimulator&) {}
76 
81  CSimulator& operator=(const CSimulator&) {
82  return *this;
83  }
84 
85  public:
86 
90  ~CSimulator();
91 
98  static CSimulator& GetInstance();
99 
104  inline CSpace& GetSpace() const {
105  return *m_pcSpace;
106  }
107 
113  CPhysicsEngine& GetPhysicsEngine(const std::string& str_id) const;
114 
120  return m_vecPhysicsEngines;
121  }
122 
128  template <typename T>
129  T& GetMedium(const std::string& str_id) {
130  CMedium::TMap::const_iterator it = m_mapMedia.find(str_id);
131  if(it != m_mapMedia.end()) {
132  T* pcMedium = dynamic_cast<T*>(it->second);
133  if(pcMedium != NULL) {
134  return *pcMedium;
135  }
136  else {
137  THROW_ARGOSEXCEPTION("Medium \"" << str_id << "\" can't be converted to the wanted type");
138  }
139  }
140  else {
141  THROW_ARGOSEXCEPTION("Medium \"" << str_id << "\" not found.");
142  }
143  }
144 
150  return m_vecMedia;
151  }
152 
158  ARGOS_ASSERT(m_pcVisualization != NULL, "No visualization specified in the XML file.");
159  return *m_pcVisualization;
160  }
161 
167  return m_tConfigurationRoot;
168  }
169 
174  inline CProfiler& GetProfiler() {
175  return *m_pcProfiler;
176  }
177 
182  inline bool IsProfiling() const {
183  return m_pcProfiler != NULL;
184  }
185 
191  inline UInt32 GetRandomSeed() const {
192  return m_unRandomSeed;
193  }
194 
200  inline void SetRandomSeed(UInt32 un_random_seed) {
201  m_unRandomSeed = un_random_seed;
202  m_bWasRandomSeedSet = true;
203  }
204 
210  inline CRandom::CRNG* GetRNG() {
211  return m_pcRNG;
212  }
213 
219  inline const std::string& GetExperimentFileName() const {
220  return m_strExperimentConfigFileName;
221  }
222 
228  inline void SetExperimentFileName(const std::string& str_file_name) {
229  m_strExperimentConfigFileName = str_file_name;
230  }
231 
237  return *m_pcLoopFunctions;
238  }
239 
244  inline void SetLoopFunctions(CLoopFunctions& c_loop_functions) {
245  m_pcLoopFunctions = &c_loop_functions;
246  }
247 
252  inline UInt32 GetMaxSimulationClock() const {
253  return m_unMaxSimulationClock;
254  }
255 
260  inline UInt32 GetNumThreads() const {
261  return m_unThreads;
262  }
263 
268  inline bool IsRealTimeClock() const {
269  return m_bRealTimeClock;
270  }
271 
276  inline void SetRealTimeClock(bool b_real_time) {
277  m_bRealTimeClock = b_real_time;
278  }
279 
283  inline void Terminate() {
284  m_bTerminated = true;
285  }
286 
292  inline std::string GetInstallationDirectory() const {
293  return ARGOS_INSTALL_PREFIX;
294  }
295 
299  TConfigurationNode& GetConfigForController(const std::string& str_id);
300 
306  void Load(ticpp::Document& t_tree);
307 
308 
314  void LoadExperiment();
315 
320  void Init();
321 
326  void Reset();
327 
337  inline void Reset(UInt32 un_new_random_seed) {
338  SetRandomSeed(un_new_random_seed);
339  Reset();
340  }
341 
345  void Destroy();
346 
350  void Execute();
351 
355  void UpdateSpace();
356 
371  bool IsExperimentFinished() const;
372 
373  private:
374 
375  void InitFramework(TConfigurationNode& t_tree);
376  void InitLoopFunctions(TConfigurationNode& t_tree);
377  void InitControllers(TConfigurationNode& t_tree);
378  void InitSpace(TConfigurationNode& t_tree);
379  void InitPhysics(TConfigurationNode& t_tree);
380  void InitPhysics2();
381  void InitMedia(TConfigurationNode& t_tree);
382  void InitMedia2();
383  void InitVisualization(TConfigurationNode& t_tree);
384 
385  private:
386 
387  typedef std::map<std::string, TConfigurationNode*> TControllerConfigurationMap;
388 
389  private:
390 
394  TControllerConfigurationMap m_mapControllerConfig;
395 
399  CVisualization* m_pcVisualization;
400 
404  CPhysicsEngine::TMap m_mapPhysicsEngines;
405 
409  CPhysicsEngine::TVector m_vecPhysicsEngines;
410 
414  CMedium::TMap m_mapMedia;
415 
419  CMedium::TVector m_vecMedia;
420 
424  CSpace* m_pcSpace;
425 
429  CLoopFunctions* m_pcLoopFunctions;
430 
434  std::string m_strExperimentConfigFileName;
435 
439  UInt32 m_unMaxSimulationClock;
440 
444  UInt32 m_unRandomSeed;
445 
449  CRandom::CRNG* m_pcRNG;
450 
456  bool m_bWasRandomSeedSet;
457 
461  ticpp::Document m_tConfiguration;
462 
466  TConfigurationNode m_tConfigurationRoot;
467 
471  UInt32 m_unThreads;
472 
476  CProfiler* m_pcProfiler;
477 
481  bool m_bHumanReadableProfile;
482 
486  bool m_bRealTimeClock;
487 
491  bool m_bTerminated;
492 
493  };
494 
495 }
496 
497 #endif
#define ARGOS_ASSERT(condition, message)
When code is compiled in debug, this macro throws an ARGoS exception with the passed message if the s...
#define THROW_ARGOSEXCEPTION(message)
This macro throws an ARGoS exception with the passed message.
unsigned int UInt32
32-bit unsigned integer.
Definition: datatypes.h:97
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
ticpp::Element TConfigurationNode
The ARGoS configuration XML node.
A set of hook functions to customize an experimental run.
std::vector< CMedium * > TVector
Definition: medium.h:26
std::map< std::string, CMedium * > TMap
Definition: medium.h:27
std::map< std::string, CPhysicsEngine *, std::less< std::string > > TMap
std::vector< CPhysicsEngine * > TVector
The core class of ARGOS.
Definition: simulator.h:62
T & GetMedium(const std::string &str_id)
Returns a reference to a medium.
Definition: simulator.h:129
CRandom::CRNG * GetRNG()
Returns the random generator of the "argos" category.
Definition: simulator.h:210
CProfiler & GetProfiler()
Returns a reference to the profiler.
Definition: simulator.h:174
void Destroy()
Undoes whatever was done by Init().
Definition: simulator.cpp:217
CLoopFunctions & GetLoopFunctions()
Returns a reference to the loop functions associated to the current experiment.
Definition: simulator.h:236
void Terminate()
Puts an end to the simulation.
Definition: simulator.h:283
const std::string & GetExperimentFileName() const
Returns the name of the XML configuration file parsed by Load().
Definition: simulator.h:219
~CSimulator()
Class destructor.
Definition: simulator.cpp:47
CSpace & GetSpace() const
Returns a reference to the simulated space.
Definition: simulator.h:104
void SetExperimentFileName(const std::string &str_file_name)
Sets the name of the XML configuration file parsed by Load().
Definition: simulator.h:228
void Load(ticpp::Document &t_tree)
Loads an already-parsed XML configuration tree.
Definition: simulator.cpp:106
CVisualization & GetVisualization()
Returns a reference to the visualization.
Definition: simulator.h:157
void Init()
Initializes the experiment.
Definition: simulator.cpp:132
static CSimulator & GetInstance()
Returns the instance to the CSimulator class.
Definition: simulator.cpp:78
UInt32 GetNumThreads() const
Returns the number of threads used during the experiment.
Definition: simulator.h:260
UInt32 GetMaxSimulationClock() const
Returns the time limit on this experiment.
Definition: simulator.h:252
TConfigurationNode & GetConfigurationRoot()
Returns a reference to the root node of the parsed XML configuration file.
Definition: simulator.h:166
bool IsExperimentFinished() const
Returns true if the experiment has finished.
Definition: simulator.cpp:288
CMedium::TVector & GetMedia()
Returns the list of currently existing media.
Definition: simulator.h:149
CPhysicsEngine & GetPhysicsEngine(const std::string &str_id) const
Returns a reference to a physics engine.
Definition: simulator.cpp:86
void Execute()
Executes the simulation loop.
Definition: simulator.cpp:273
void SetRealTimeClock(bool b_real_time)
When passed true, the clock tick follows the real time.
Definition: simulator.h:276
bool IsProfiling() const
Returns true if ARGoS is being profiled.
Definition: simulator.h:182
void LoadExperiment()
Loads the XML configuration file.
Definition: simulator.cpp:119
std::string GetInstallationDirectory() const
Returns the base directory in which the ARGoS core was installed.
Definition: simulator.h:292
bool IsRealTimeClock() const
Returns true if the clock tick follows the real time.
Definition: simulator.h:268
void SetRandomSeed(UInt32 un_random_seed)
Sets the random seed of the "argos" category of the random seed.
Definition: simulator.h:200
void Reset(UInt32 un_new_random_seed)
Resets the experiment.
Definition: simulator.h:337
UInt32 GetRandomSeed() const
Returns the random seed of the "argos" category of the random seed.
Definition: simulator.h:191
void UpdateSpace()
Performs an update step of the space.
Definition: simulator.cpp:280
TConfigurationNode & GetConfigForController(const std::string &str_id)
Returns the XML portion relative to the controller with the given ID.
Definition: simulator.cpp:95
void SetLoopFunctions(CLoopFunctions &c_loop_functions)
Asociates loop functions to the current experiment.
Definition: simulator.h:244
void Reset()
Resets the experiment.
Definition: simulator.cpp:179
CPhysicsEngine::TVector & GetPhysicsEngines()
Returns the list of currently existing physics engines.
Definition: simulator.h:119
The RNG.
Definition: rng.h:90