ARGoS  3
A parallel, multi-engine simulator for swarm robotics
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 LoadExperiment();
307 
312  void Init();
313 
318  void Reset();
319 
329  inline void Reset(UInt32 un_new_random_seed) {
330  SetRandomSeed(un_new_random_seed);
331  Reset();
332  }
333 
337  void Destroy();
338 
342  void Execute();
343 
347  void UpdateSpace();
348 
363  bool IsExperimentFinished() const;
364 
365  private:
366 
367  void InitFramework(TConfigurationNode& t_tree);
368  void InitLoopFunctions(TConfigurationNode& t_tree);
369  void InitControllers(TConfigurationNode& t_tree);
370  void InitSpace(TConfigurationNode& t_tree);
371  void InitPhysics(TConfigurationNode& t_tree);
372  void InitPhysics2();
373  void InitMedia(TConfigurationNode& t_tree);
374  void InitMedia2();
375  void InitVisualization(TConfigurationNode& t_tree);
376 
377  private:
378 
379  typedef std::map<std::string, TConfigurationNode*> TControllerConfigurationMap;
380 
381  private:
382 
386  TControllerConfigurationMap m_mapControllerConfig;
387 
391  CVisualization* m_pcVisualization;
392 
396  CPhysicsEngine::TMap m_mapPhysicsEngines;
397 
401  CPhysicsEngine::TVector m_vecPhysicsEngines;
402 
406  CMedium::TMap m_mapMedia;
407 
411  CMedium::TVector m_vecMedia;
412 
416  CSpace* m_pcSpace;
417 
421  CLoopFunctions* m_pcLoopFunctions;
422 
426  std::string m_strExperimentConfigFileName;
427 
431  UInt32 m_unMaxSimulationClock;
432 
436  UInt32 m_unRandomSeed;
437 
441  CRandom::CRNG* m_pcRNG;
442 
448  bool m_bWasRandomSeedSet;
449 
453  ticpp::Document m_tConfiguration;
454 
458  TConfigurationNode m_tConfigurationRoot;
459 
463  UInt32 m_unThreads;
464 
468  CProfiler* m_pcProfiler;
469 
473  bool m_bHumanReadableProfile;
474 
478  bool m_bRealTimeClock;
479 
483  bool m_bTerminated;
484 
485  };
486 
487 }
488 
489 #endif
argos::CSimulator::GetInstance
static CSimulator & GetInstance()
Returns the instance to the CSimulator class.
Definition: simulator.cpp:87
argos::CSimulator::Terminate
void Terminate()
Puts an end to the simulation.
Definition: simulator.h:283
argos
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
argos::CSpace
Definition: space.h:30
argos::CProfiler
Definition: profiler.h:20
argos::CSimulator::GetVisualization
CVisualization & GetVisualization()
Returns a reference to the visualization.
Definition: simulator.h:157
argos::CSimulator::Init
void Init()
Initializes the experiment.
Definition: simulator.cpp:128
argos::CSimulator::SetExperimentFileName
void SetExperimentFileName(const std::string &str_file_name)
Sets the name of the XML configuration file parsed by Load().
Definition: simulator.h:228
argos::CSimulator::IsRealTimeClock
bool IsRealTimeClock() const
Returns true if the clock tick follows the real time.
Definition: simulator.h:268
argos::CSimulator::GetInstallationDirectory
std::string GetInstallationDirectory() const
Returns the base directory in which the ARGoS core was installed.
Definition: simulator.h:292
argos::CSimulator
The core class of ARGOS.
Definition: simulator.h:62
argos::CSimulator::GetRandomSeed
UInt32 GetRandomSeed() const
Returns the random seed of the "argos" category of the random seed.
Definition: simulator.h:191
argos::TConfigurationNode
ticpp::Element TConfigurationNode
The ARGoS configuration XML node.
Definition: argos_configuration.h:27
argos::CRandom::CRNG
The RNG.
Definition: rng.h:90
argos::CSimulator::Reset
void Reset(UInt32 un_new_random_seed)
Resets the experiment.
Definition: simulator.h:329
argos::CSimulator::GetConfigForController
TConfigurationNode & GetConfigForController(const std::string &str_id)
Returns the XML portion relative to the controller with the given ID.
Definition: simulator.cpp:104
argos::CSimulator::LoadExperiment
void LoadExperiment()
Loads the XML configuration file.
Definition: simulator.cpp:115
argos::CSimulator::GetSpace
CSpace & GetSpace() const
Returns a reference to the simulated space.
Definition: simulator.h:104
argos::CMedium::TMap
std::map< std::string, CMedium * > TMap
Definition: medium.h:26
argos::CSimulator::SetRandomSeed
void SetRandomSeed(UInt32 un_random_seed)
Sets the random seed of the "argos" category of the random seed.
Definition: simulator.h:200
argos::CSimulator::GetProfiler
CProfiler & GetProfiler()
Returns a reference to the profiler.
Definition: simulator.h:174
argos::CMedium::TVector
std::vector< CMedium * > TVector
Definition: medium.h:25
argos::CSimulator::GetConfigurationRoot
TConfigurationNode & GetConfigurationRoot()
Returns a reference to the root node of the parsed XML configuration file.
Definition: simulator.h:166
THROW_ARGOSEXCEPTION
#define THROW_ARGOSEXCEPTION(message)
This macro throws an ARGoS exception with the passed message.
Definition: argos_exception.h:111
argos::CSimulator::GetMedia
CMedium::TVector & GetMedia()
Returns the list of currently existing media.
Definition: simulator.h:149
argos::CSimulator::SetLoopFunctions
void SetLoopFunctions(CLoopFunctions &c_loop_functions)
Asociates loop functions to the current experiment.
Definition: simulator.h:244
argos::CSimulator::GetPhysicsEngines
CPhysicsEngine::TVector & GetPhysicsEngines()
Returns the list of currently existing physics engines.
Definition: simulator.h:119
argos::CSimulator::~CSimulator
~CSimulator()
Class destructor.
Definition: simulator.cpp:56
argos::CSimulator::GetRNG
CRandom::CRNG * GetRNG()
Returns the random generator of the "argos" category.
Definition: simulator.h:210
argos::CSimulator::GetLoopFunctions
CLoopFunctions & GetLoopFunctions()
Returns a reference to the loop functions associated to the current experiment.
Definition: simulator.h:236
argos::CSimulator::IsProfiling
bool IsProfiling() const
Returns true if ARGoS is being profiled.
Definition: simulator.h:182
argos::CPhysicsEngine
Definition: physics_engine.h:90
ARGOS_ASSERT
#define ARGOS_ASSERT(condition, message)
When code is compiled in debug, this macro throws an ARGoS exception with the passed message if the s...
Definition: argos_exception.h:122
UInt32
unsigned int UInt32
32-bit unsigned integer.
Definition: datatypes.h:97
argos::CSimulator::IsExperimentFinished
bool IsExperimentFinished() const
Returns true if the experiment has finished.
Definition: simulator.cpp:284
argos::CSimulator::UpdateSpace
void UpdateSpace()
Performs an update step of the space.
Definition: simulator.cpp:276
argos::CSimulator::SetRealTimeClock
void SetRealTimeClock(bool b_real_time)
When passed true, the clock tick follows the real time.
Definition: simulator.h:276
argos::CVisualization
Definition: visualization.h:24
argos::CSimulator::GetExperimentFileName
const std::string & GetExperimentFileName() const
Returns the name of the XML configuration file parsed by Load().
Definition: simulator.h:219
argos::CPhysicsEngine::TVector
std::vector< CPhysicsEngine * > TVector
Definition: physics_engine.h:130
argos::CSimulator::Reset
void Reset()
Resets the experiment.
Definition: simulator.cpp:175
argos::CSimulator::Execute
void Execute()
Executes the simulation loop.
Definition: simulator.cpp:269
argos::CLoopFunctions
A set of hook functions to customize an experimental run.
Definition: loop_functions.h:68
argos::CSimulator::GetPhysicsEngine
CPhysicsEngine & GetPhysicsEngine(const std::string &str_id) const
Returns a reference to a physics engine.
Definition: simulator.cpp:95
argos::CSimulator::Destroy
void Destroy()
Undoes whatever was done by Init().
Definition: simulator.cpp:213
argos::CSimulator::GetMaxSimulationClock
UInt32 GetMaxSimulationClock() const
Returns the time limit on this experiment.
Definition: simulator.h:252
argos::CSimulator::GetMedium
T & GetMedium(const std::string &str_id)
Returns a reference to a medium.
Definition: simulator.h:129
argos::CSimulator::GetNumThreads
UInt32 GetNumThreads() const
Returns the number of threads used during the experiment.
Definition: simulator.h:260
argos::CPhysicsEngine::TMap
std::map< std::string, CPhysicsEngine *, std::less< std::string > > TMap
Definition: physics_engine.h:131