Menge
Modular Pedestrian Simulation Framework for Research and Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SimulatorBase.h
Go to the documentation of this file.
1 /*
2 
3 License
4 
5 Menge
6 Copyright © and trademark ™ 2012-14 University of North Carolina at Chapel Hill.
7 All rights reserved.
8 
9 Permission to use, copy, modify, and distribute this software and its documentation
10 for educational, research, and non-profit purposes, without fee, and without a
11 written agreement is hereby granted, provided that the above copyright notice,
12 this paragraph, and the following four paragraphs appear in all copies.
13 
14 This software program and documentation are copyrighted by the University of North
15 Carolina at Chapel Hill. The software program and documentation are supplied "as is,"
16 without any accompanying services from the University of North Carolina at Chapel
17 Hill or the authors. The University of North Carolina at Chapel Hill and the
18 authors do not warrant that the operation of the program will be uninterrupted
19 or error-free. The end-user understands that the program was developed for research
20 purposes and is advised not to rely exclusively on the program for any reason.
21 
22 IN NO EVENT SHALL THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL OR THE AUTHORS
23 BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
24 DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
25 DOCUMENTATION, EVEN IF THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL OR THE
26 AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL AND THE AUTHORS SPECIFICALLY
29 DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AND ANY STATUTORY WARRANTY
31 OF NON-INFRINGEMENT. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND
32 THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL AND THE AUTHORS HAVE NO OBLIGATIONS
33 TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
34 
35 Any questions or comments should be sent to the authors {menge,geom}@cs.unc.edu
36 
37 */
38 
39 #ifndef __SIMULATOR_BASE_H__
40 #define __SIMULATOR_BASE_H__
41 
48 // UTILS
49 #include "mengeCommon.h"
50 #include "Utils.h"
51 // Ped Models
52 #include "SimulatorInterface.h"
53 #include "AgentInitializer.h"
55 
56 // STL
57 #include <vector>
58 
59 #if HAVE_OPENMP || _OPENMP
60 #include <omp.h>
61 #endif
62 
63 namespace Menge {
64 
65  namespace Agents {
66 
71  template < class Agent >
73  public:
77  SimulatorBase();
78 
83 
89  void doStep();
90 
94  virtual bool initSpatialQuery();
95 
106  virtual void finalize();
107 
117  virtual BaseAgent * getAgent( size_t agentNo ) { return &_agents[ agentNo ]; }
118 
128  virtual const BaseAgent * getAgent( size_t agentNo ) const { return &_agents[ agentNo ]; }
129 
140  virtual BaseAgent * addAgent( const Vector2 & pos, AgentInitializer * agentInit );
141 
147  virtual size_t getNumAgents() const { return _agents.size(); }
148 
156  virtual bool hasExpTarget() { return false; }
157 
166  virtual bool isExpTarget( const std::string & tagName ) { return false; }
167 
178  virtual bool setExpParam( const std::string & paramName, const std::string & value ) throw( XMLParamException );
179 
180  protected:
181 
187  void computeNeighbors( Agent * agent );
188 
192  std::vector< Agent > _agents;
193  };
194 
196  // Implementation of SimulatorBase
198 
199  template < class Agent >
201  }
202 
204 
205  template < class Agent >
207  _agents.clear();
208  }
209 
211 
212  template < class Agent >
214  assert( _spatialQuery != 0x0 && "Can't run without a spatial query instance defined" );
215 
216  _spatialQuery->updateAgents();
217  int AGT_COUNT = static_cast< int >( _agents.size() );
218  #pragma omp parallel for
219  for (int i = 0; i < AGT_COUNT; ++i) {
220  computeNeighbors( &(_agents[i]) );
221  _agents[i].computeNewVelocity();
222  }
223 
224  #pragma omp parallel for
225  for (int i = 0; i < AGT_COUNT; ++i) {
226  _agents[i].update( TIME_STEP );
227  }
228 
229  _globalTime += TIME_STEP;
230 
231  }
232 
234 
235  template < class Agent >
237  assert( _spatialQuery != 0x0 && "Can't run without a spatial query instance defined" );
238 
239  const size_t AGT_COUNT = _agents.size();
240  std::vector< BaseAgent * > agtPointers( AGT_COUNT );
241  for ( size_t a = 0; a < AGT_COUNT; ++a ) {
242  agtPointers[ a ] = &_agents[a];
243  }
244  _spatialQuery->setAgents( agtPointers );
245 
246  _spatialQuery->processObstacles();
247 
248  return true;
249  }
250 
252 
253  template < class Agent >
256 
257  // initialize agents
258  for ( size_t i = 0; i < _agents.size(); ++i ) {
259  _agents[ i ].initialize();
260  }
261  }
262 
264 
265  template < class Agent >
267  Agent agent;
268 
269  agent._pos = pos;
270  agent._id = _agents.size();
271  if ( ! agentInit->setProperties( &agent ) ) {
272  logger << Logger::ERR_MSG << "Error initializing agent " << agent._id << "\n";
273  return 0x0;
274  }
275  _agents.push_back(agent);
276 
277  return &_agents[ _agents.size() - 1 ];
278  }
279 
281 
282  template < class Agent >
283  bool SimulatorBase<Agent>::setExpParam( const std::string & paramName, const std::string & value ) throw( XMLParamException ) {
284 
285  if ( paramName == "time_step" ) {
286  try {
287  LOGICAL_TIME_STEP = toFloat( value );
288  } catch ( UtilException ) {
289  throw XMLParamException( std::string( "Common parameters \"time_step\" value couldn't be converted to a float. Found the value: " ) + value );
290  }
291  } else {
292  return false;
293  }
294 
295  return true;
296  }
297 
299 
300  template< class Agent >
302  // obstacles
303  agent->startQuery();
304  _spatialQuery->obstacleQuery(agent);
305 
306  // agents
307  if ( agent->_maxNeighbors > 0 ) {
308  _spatialQuery->agentQuery(agent);
309  }
310  }
311  } // namespace Agents
312 } // namespace Menge
313 #endif // __SIMULATOR_BASE_H__
~SimulatorBase()
Destorys a simulator instance.
Definition: SimulatorBase.h:206
virtual void finalize()
After all agents and all obstacles have been added to the scene does the work to finish preparing the...
Definition: SimulatorInterface.cpp:107
virtual bool setProperties(BaseAgent *agent)
Sets the properties of the given agent based on the initializer's values.
Definition: AgentInitializer.cpp:185
The core namespace. All elements of Menge are contained in this namespace.
Definition: AgentGenerator.cpp:43
void doStep()
Lets the simulator perform a simulation step and updates the two-dimensional _p and two-dimensional v...
Definition: SimulatorBase.h:213
virtual bool isExpTarget(const std::string &tagName)
Reports if the given Experiment attribute tag name belongs to this simulator.
Definition: SimulatorBase.h:166
Exception thrown when a utility function fails..
Definition: Utils.h:65
Vector2 _pos
The current 2D position of the agent.
Definition: BaseAgent.h:208
virtual BaseAgent * getAgent(size_t agentNo)
Accessor for agents.
Definition: SimulatorBase.h:117
virtual const BaseAgent * getAgent(size_t agentNo) const
Const accessor for agents.
Definition: SimulatorBase.h:128
Class which determines the agent properties for each new agent.
Definition: AgentInitializer.h:75
The basic simulator interface required by the fsm.
Definition: SimulatorInterface.h:66
A collection of convenience utilities.
The definition of the interface of the simulator required by the finite state machine.
virtual BaseAgent * addAgent(const Vector2 &pos, AgentInitializer *agentInit)
Add an agent with specified position to the simulator whose properties are defined by the given agent...
Definition: SimulatorBase.h:266
virtual bool hasExpTarget()
Reports if there are non-common Experiment parameters that this simulator requires in the XML file...
Definition: SimulatorBase.h:156
virtual bool initSpatialQuery()
Initalize spatial query structure.
Definition: SimulatorBase.h:236
virtual size_t getNumAgents() const
Returns the count of agents in the simulation.
Definition: SimulatorBase.h:147
Exception for invalid parameters from XML specification.
Definition: XMLSimulatorBase.h:78
std::vector< Agent > _agents
The collection of agents in the simulation.
Definition: SimulatorBase.h:192
Logger logger
Globally available Logger.
Definition: Logger.cpp:49
float toFloat(const std::string &value)
Converts a string to a float.
Definition: Utils.cpp:48
virtual bool setExpParam(const std::string &paramName, const std::string &value)
Given an Experiment parameter name and value, sets the appropriate simulator parameter.
Definition: SimulatorBase.h:283
The base class for all objects which support agent spatial queries including: k-nearest agent neighbo...
Defines the basic agent properties and functionality that all simulation agents share.
Definition: BaseAgent.h:123
The infrastructure for initializing agent properties from the scene specification file...
SimulatorBase()
Constructs a simulator instance.
Definition: SimulatorBase.h:200
The namespace that contains the basic simulation mechanisms.
void computeNeighbors(Agent *agent)
Computes the neighbors for the given agent.
Definition: SimulatorBase.h:301
Defines the basic simulator. It is responsible for tracking agents and obstacles as well as initializ...
Definition: SimulatorBase.h:72
Error encountered but not handled.
Definition: Logger.h:69
virtual void finalize()
After all agents and all obstacles have been added to the scene does the work to finish preparing the...
Definition: SimulatorBase.h:254