AnimatLab  2
Test
NeuralModule.cpp
Go to the documentation of this file.
1 
7 #include "StdAfx.h"
8 #include "IMovableItemCallback.h"
9 #include "ISimGUICallback.h"
10 #include "AnimatBase.h"
11 
12 #include "Node.h"
13 #include "Link.h"
14 #include "IPhysicsMovableItem.h"
15 #include "IPhysicsBody.h"
16 #include "BoundingBox.h"
17 #include "MovableItem.h"
18 #include "BodyPart.h"
19 #include "Gain.h"
20 #include "Adapter.h"
21 #include "Joint.h"
22 #include "ReceptiveField.h"
23 #include "ContactSensor.h"
24 #include "RigidBody.h"
25 #include "Structure.h"
26 #include "NeuralModule.h"
27 #include "NervousSystem.h"
28 #include "Organism.h"
29 #include "ActivatedItem.h"
30 #include "ActivatedItemMgr.h"
31 #include "DataChartMgr.h"
32 #include "ExternalStimuliMgr.h"
33 #include "KeyFrame.h"
34 #include "SimulationRecorder.h"
35 #include "OdorType.h"
36 #include "Odor.h"
37 #include "Light.h"
38 #include "LightManager.h"
39 #include "Simulator.h"
40 
41 namespace AnimatSim
42 {
43 
53  namespace Behavior
54  {
55 
65 {
68  m_fltTimeStep = -1;
69  m_iTimeStepCount = 0;
70  m_lpClassFactory = NULL;
71  m_lpSim = NULL;
72  m_lpOrganism = NULL;
73 }
74 
86 {
87 
88 try
89 {
91  {delete m_lpClassFactory; m_lpClassFactory = NULL;}
92 
93  m_arySourceAdapters.RemoveAll();
94  m_aryTargetAdapters.RemoveAll();
95 }
96 catch(...)
97 {Std_TraceMsg(0, "Caught Error in desctructor of NeuralModule\r\n", "", -1, false, true);}
98 }
99 
111 
120 void NeuralModule::ClassFactory(IStdClassFactory *lpFactory) {m_lpClassFactory = lpFactory;}
121 
133 
145 
160 {return m_iTimeStepInterval;}
161 
175 {
176  if(iVal == 0) iVal = 1;
177 
178  Std_IsAboveMin((int) 0, (int) iVal, true, "TimeStepInterval");
179  m_iTimeStepInterval = iVal;
180 }
181 
191 {return m_fltTimeStep;}
192 
203 void NeuralModule::TimeStep(float fltVal)
204 {
205  Std_IsAboveMin((float) 0, (float) fltVal, true, "TimeStep");
206 
207  //Set it so that it will be taken into consideration when finding min value.
208  m_fltTimeStep = fltVal;
209 
210  //Find min time step.
211  float fltMin = m_lpSim->MinTimeStep();
212 
213  //Division
214  int iDiv = (int) ((fltVal / fltMin) + 0.5f);
215 
216  //Find the number of timeslices that need to occur before this module is updated
217  TimeStepInterval(iDiv);
218 
219  //Now recaculate the time step using the minimum time step as the base.
221 
222  //Now reset the m_fltTimeStep of the sim.
223  if(m_iTimeStepInterval == 1) fltMin = m_lpSim->MinTimeStep();
224 }
225 
227 {
228  //We need to rerun the code to set the time step here in initialize. The reason is that we set this when
229  //loading the simulator and neural modules, but if one of the neural modules has the miniumum time step then
230  //we need to recalculate the time slice per step for all modules in initialize after everything has loaded.
231  // Once everything is loaded and initialized, then if a given time step is changed then that one is changed in
232  // the sim, and events will change it for the rest of them afterwards, so the values should be correct.
234 
235  int iCount = m_aryExternalSynapses.GetSize();
236  for(int iIndex=0; iIndex<iCount; iIndex++)
238 }
239 
250 bool NeuralModule::NeedToStep(bool bIncrement)
251 {
252  if(bIncrement)
254 
256  return true;
257  else
258  return false;
259 }
260 
261 void NeuralModule::ResetStepCounter()
262 {
263  m_iTimeStepCount = 0;
264 }
265 
266 int NeuralModule::FindAdapterListIndex(CStdArray<Adapter *> aryAdapters, std::string strID, bool bThrowError)
267 {
268  int iCount = aryAdapters.GetSize();
269  for(int iIdx=0; iIdx<iCount; iIdx++)
270  if(aryAdapters[iIdx]->ID() == strID)
271  return iIdx;
272 
273  if(bThrowError)
274  THROW_PARAM_ERROR(Al_Err_lAdapterIDNotFound, Al_Err_strAdapterIDNotFound, "ID", strID);
275 
276  return -1;
277 }
278 
280 {
281  if(FindAdapterListIndex(m_arySourceAdapters, lpAdapter->ID(), false) == -1)
282  m_arySourceAdapters.Add(lpAdapter);
283 }
284 
286 {
287  int iIdx = FindAdapterListIndex(m_arySourceAdapters, lpAdapter->ID(), false);
288  if(iIdx > -1)
289  m_arySourceAdapters.RemoveAt(iIdx);
290 }
291 
293 {
294  if(FindAdapterListIndex(m_aryTargetAdapters, lpAdapter->ID(), false) == -1)
295  {
296  m_aryTargetAdapters.Add(lpAdapter);
298  }
299 }
300 
302 {
303  int iIdx = FindAdapterListIndex(m_aryTargetAdapters, lpAdapter->ID(), false);
304  if(iIdx > -1)
305  {
306  m_aryTargetAdapters.RemoveAt(iIdx);
308  }
309 }
310 
311 void NeuralModule::AddExternalSynapse(AnimatSim::Link *lpSynapse)
312 {
313  if(!lpSynapse)
314  THROW_ERROR(Al_Err_lSynapseToAddNull, Al_Err_strSynapseToAddNull);
315  m_aryExternalSynapses.Add(lpSynapse);
316 }
317 
326 void NeuralModule::AddExternalSynapse(std::string strXml, bool bDoNotInit)
327 {
328  CStdXml oXml;
329  oXml.Deserialize(strXml);
330  oXml.FindElement("Root");
331  oXml.FindChildElement("Synapse");
332 
333  AnimatSim::Link *lpSynapse = LoadExternalSynapse(oXml);
334  if(!bDoNotInit)
335  lpSynapse->Initialize();
336 }
337 
347 void NeuralModule::RemoveExternalSynapse(std::string strID, bool bThrowError)
348 {
349  int iPos = FindExternalSynapseListPos(strID, bThrowError);
350  m_aryExternalSynapses.RemoveAt(iPos);
351 }
352 
364 int NeuralModule::FindExternalSynapseListPos(std::string strID, bool bThrowError)
365 {
366  std::string sID = Std_ToUpper(Std_Trim(strID));
367 
368  int iCount = m_aryExternalSynapses.GetSize();
369  for(int iIndex=0; iIndex<iCount; iIndex++)
370  if(m_aryExternalSynapses[iIndex]->ID() == sID)
371  return iIndex;
372 
373  if(bThrowError)
374  THROW_TEXT_ERROR(Al_Err_lSynapseNotFound, Al_Err_strSynapseNotFound, "ID");
375 
376  return -1;
377 }
378 
390 {
391 
392  //*** Begin Loading Neurons. *****
393  if(oXml.FindChildElement("ExternalSynapses", false))
394  {
395  oXml.IntoChildElement("ExternalSynapses");
396 
397  int iCount = oXml.NumberOfChildren();
398  for(int iIdx=0; iIdx<iCount; iIdx++)
399  {
400  oXml.FindChildByIndex(iIdx);
401  LoadExternalSynapse(oXml);
402  }
403 
404  oXml.OutOfElem();
405  //*** End Loading Neurons. *****
406  }
407 }
408 
420 {
421  std::string strModuleFilename, strType;
422  AnimatSim::Link *lpSynapse=NULL;
423 
424 try
425 {
426  oXml.IntoElem(); //Into Synapse Element
427  strModuleFilename = oXml.GetChildString("ModuleFilename", "");
428  strType = oXml.GetChildString("Type");
429  oXml.OutOfElem(); //OutOf Synapse Element
430 
431  lpSynapse = dynamic_cast<AnimatSim::Link *>(m_lpSim->CreateObject(strModuleFilename, "Synapse", strType));
432  if(!lpSynapse)
433  THROW_TEXT_ERROR(Al_Err_lConvertingClassToType, Al_Err_strConvertingClassToType, "Synapse");
434 
435  lpSynapse->SetSystemPointers(m_lpSim, m_lpStructure, this, NULL, true);
436  lpSynapse->Load(oXml);
437  AddExternalSynapse(lpSynapse);
438 
439  return lpSynapse;
440 }
441 catch(CStdErrorInfo oError)
442 {
443  if(lpSynapse) delete lpSynapse;
444  RELAY_ERROR(oError);
445  return NULL;
446 }
447 catch(...)
448 {
449  if(lpSynapse) delete lpSynapse;
450  THROW_ERROR(Std_Err_lUnspecifiedError, Std_Err_strUnspecifiedError);
451  return NULL;
452 }
453 }
454 
455 void NeuralModule::SetSystemPointers(Simulator *lpSim, Structure *lpStructure, NeuralModule *lpModule, Node *lpNode, bool bVerify)
456 {
457  AnimatBase::SetSystemPointers(lpSim, lpStructure, lpModule, lpNode, false);
458 
459  m_lpOrganism = dynamic_cast<Organism *>(lpStructure);
460 
461  if(bVerify) VerifySystemPointers();
462 }
463 
465 {
467 
468  if(!m_lpStructure)
469  THROW_PARAM_ERROR(Al_Err_lStructureNotDefined, Al_Err_strStructureNotDefined, "ConstraintLimit: ", m_strName);
470 
471  if(!m_lpOrganism)
472  THROW_TEXT_ERROR(Al_Err_lConvertingClassToType, Al_Err_strConvertingClassToType, "Organism");
473 }
474 
475 
476 float *NeuralModule::GetDataPointer(const std::string &strDataType)
477 {
478  std::string strType = Std_CheckString(strDataType);
479 
480  if(strType == "TIMESTEP")
481  return &m_fltTimeStep;
482 
483  return AnimatBase::GetDataPointer(strDataType);
484 }
485 
487 {
488  int iCount = m_aryExternalSynapses.GetSize();
489  for(int iIdx=0; iIdx<iCount; iIdx++)
491 }
492 
494 {
495  int iCount = m_aryExternalSynapses.GetSize();
496  for(int iIdx=0; iIdx<iCount; iIdx++)
498 }
499 
501 {
502  for(int iIndex=0; iIndex<m_iTargetAdapterCount; iIndex++)
503  if(m_aryTargetAdapters[iIndex]->Enabled())
504  m_aryTargetAdapters[iIndex]->StepSimulation();
505 }
506 
507  } //Behavior
508 } //AnimatSim
CStdPtrArray< AnimatSim::Link > m_aryExternalSynapses
An array of external synapses connected to nodes within this module.
Definition: NeuralModule.h:62
virtual void RemoveExternalSynapse(std::string strID, bool bThrowError=true)
Removes the synapse by the GUID ID.
virtual void Deserialize(std::string &strXml)
Deserializes a string into an xml document.
Definition: StdXml.cpp:162
virtual IStdClassFactory * ClassFactory()
Gets the class factory.
CStdArray< Adapter * > m_arySourceAdapters
An array of source adapters for this module.
Definition: NeuralModule.h:56
Base class file for all Animat simulation objects.
virtual short TimeStepInterval()
Gets the time step interval.
Declares the nervous system class.
virtual void RemoveSourceAdapter(Adapter *lpAdapter)
Removes a source adapter.
Declares the simulation recorder class.
virtual void SetSystemPointers(Simulator *lpSim, Structure *lpStructure, NeuralModule *lpModule, Node *lpNode, bool bVerify)
Sets the system pointers.
Simulates the entire environment.
Definition: Simulator.h:31
Organism * GetOrganism()
Gets the organism.
virtual bool FindChildElement(std::string strElementName, bool fThrowError=true)
Finds a child element by name.
Definition: StdXml.cpp:256
Root namespace for the base simulation library for AnimatLab.
Simulator * GetSimulator()
Gets the simulator.
virtual float TimeStep()
Gets the time step for this moudle in time units.
Declares the body part class.
IStdClassFactory * m_lpClassFactory
The pointer to the class factory for this module.
Definition: NeuralModule.h:53
virtual void Initialize()
Initializes this object.
virtual bool FindElement(std::string strElementName, bool fThrowError=true)
Finds an element with the specified name.
Definition: StdXml.cpp:179
Simulator * m_lpSim
The pointer to a Simulation.
Definition: AnimatBase.h:43
Information about the standard error.
Definition: StdErrorInfo.h:19
virtual std::string ID()
Gets the unique GUID ID of this object.
Definition: AnimatBase.cpp:167
virtual bool IntoElem()
Goes into the next element where the cursor is located.
Definition: StdXml.cpp:42
virtual int FindExternalSynapseListPos(std::string strID, bool bThrowError=true)
Searches for a synapse with the specified ID and returns its position in the list.
virtual float * GetDataPointer(const std::string &strDataType)
Returns a float pointer to a data item of interest in this object.
Definition: AnimatBase.cpp:340
virtual bool NeedToStep(bool bIncrement)
Tells whether this NeuralModule needs to call StepSimulation.
virtual void Initialize()
Initializes this object.
Definition: AnimatBase.cpp:573
Declares the key frame class.
Declares the joint class.
virtual void AttachSourceAdapter(Adapter *lpAdapter)
Attaches a source adapter.
Declares the organism class.
Standard class factory.
bool Std_IsAboveMin(int iMinVal, int iVal, bool bThrowError, std::string strParamName, bool bInclusiveLimit)
Tests if a number is above a minimum value.
AnimatSim::Environment::Structure * m_lpStructure
The pointer to this items parent Structure. If this is not relevant for this object then this is NULL...
Definition: AnimatBase.h:46
std::string Std_Trim(std::string strVal)
Trims a string.
Declares a light object.
Declares the activated item class.
virtual void AttachTargetAdapter(Adapter *lpAdapter)
Attaches a target adapter.
virtual float TimeStep()
Gets the smallest integration time step used within the simulation.
Definition: Simulator.cpp:951
short m_iTimeStepCount
Count variable that keeps track of how many slices have occured since the last StepSimulation.
Definition: NeuralModule.h:44
virtual CStdSerialize * CreateObject(std::string strModule, std::string strClassName, std::string strType, bool bThrowError=true)
Creates an object using a class factory.
Definition: Simulator.cpp:3440
virtual void ResetSimulation()
Resets the simulation back to time 0.
Declares a light manager object.
virtual AnimatSim::Link * LoadExternalSynapse(CStdXml &oXml)
Loads an external synapse.
Declares the bounding box class.
CStdArray< Adapter * > m_aryTargetAdapters
An array of target adapters for this module.
Definition: NeuralModule.h:59
Declares the gain base class.
virtual bool Enabled()
Tells whether this item is enabled or not. This is not actually used for all objects, only specific ones. I am putting it in the base class though to prevent numerous duplications.
Definition: AnimatBase.cpp:113
virtual ~NeuralModule()
Destructor.
A standard xml manipulation class.
Definition: StdXml.h:19
A "static" structure in the simulation.
Definition: Structure.h:84
Organism * m_lpOrganism
The pointer to the organism.
Definition: NeuralModule.h:33
Declares the node class.
virtual void LoadExternalSynapses(CStdXml &oXml)
Loads external synapses.
virtual void RemoveTargetAdapter(Adapter *lpAdapter)
Removes a target adapter.
virtual std::string GetChildString(std::string strElementName)
Gets a string value from the element with the specified name.
Definition: StdXml.cpp:307
virtual void Load(StdUtils::CStdXml &oXml)
Loads the item using an XML data packet.
Definition: AnimatBase.cpp:771
void Std_TraceMsg(const int iLevel, std::string strMessage, std::string strSourceFile, int iSourceLine, bool bLogToFile, bool bPrintHeader)
Traces a message to the debugger window.
virtual void StepAdapters()
Step adapters.
virtual int NumberOfChildren()
Gets the number of children of the current element.
Definition: StdXml.cpp:202
A dynamic organism that is controlled by a neural network.
Definition: Organism.h:31
Base class for body parts and neural network nodes.
Definition: Node.h:25
virtual bool OutOfElem()
Goes out of the element where the cursor is located.
Definition: StdXml.cpp:56
virtual void StepSimulation()
Step the simulation for this object.
Declares the data chart manager class.
short m_iTargetAdapterCount
Number of target adapters.
Definition: NeuralModule.h:65
Declares the rigid body class.
virtual bool IntoChildElement(std::string strElementName, bool bThrowError=true)
Goes into the child element with the specified name.
Definition: StdXml.cpp:278
std::string Std_CheckString(std::string strVal)
Converts a string to upper case and trims it.
NeuralModule()
Default constructor.
virtual void VerifySystemPointers()
Verify that system pointers have been set correctly.
Declares the structure class.
Declares the odor type class.
virtual void SetSystemPointers(Simulator *lpSim, Structure *lpStructure, NeuralModule *lpModule, Node *lpNode, bool bVerify)
Sets the system pointers.
virtual void VerifySystemPointers()
Verify that system pointers have been set correctly.
Definition: AnimatBase.cpp:315
float m_fltTimeStep
The DT time step for this neural module in seconds.
Definition: NeuralModule.h:40
Declares the odor class.
Declares the simulator class.
Declares the neural module class.
virtual bool FindChildByIndex(int iIndex, bool bThrowError=true)
Finds a child element by index.
Definition: StdXml.cpp:225
std::string Std_ToUpper(std::string strVal)
Converts a string to upper case.
Declares the activated item manager class.
Declares the contact sensor class.
Declares the external stimuli manager class.
Declares the receptive field class.
std::string m_strName
The name for this object.
Definition: AnimatBase.h:61
virtual float * GetDataPointer(const std::string &strDataType)
Returns a float pointer to a data item of interest in this object.