vconnection.hpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #ifndef _HPP_VCONNECTION
  2. #define _HPP_VCONNECTION
  3. #include <stdexcept>
  4. #include "connection.hpp"
  5. /**
  6. @page WeightFileFormatDescription Weight file format description
  7. - FileFormatStringLength (64 bit)
  8. - FileFormatString
  9. - FileData
  10. - File Data is Chunk based. Chunk has the following form:
  11. - Chunk Name Length: 1 byte
  12. - Chunk Name
  13. - Chunk Length: 64 bit integer
  14. - Chunk Data
  15. - HeaderChunk:
  16. - Chunk Name: "Header"
  17. - Chunk Length: 64 bit integer
  18. - NSource, NSx, NSy, NTarget, NTx, NTy
  19. - SourceNameLength, SourceName
  20. - TargetNameLength, TargetName
  21. - SourceNrChunk:
  22. - Chunk Name: „SourceNr“,
  23. - Chunk Length: 64 bit integer
  24. - sizeof(T_NNeurons)
  25. - Nweights,
  26. - SourceNr-Array
  27. - TargetNrChunk:
  28. - Chunk Name: „TargeteNr“,
  29. - Chunk Length: 64 bit integer
  30. - sizeof(T_NNeurons)
  31. - Nweights,
  32. - TargeteNr-Array
  33. - WeightsChunk:
  34. - Chunk Name: „Weights“
  35. - Chunk Length: 64 bit integer
  36. - sizeof(T_Weights)
  37. - NWeights
  38. - Weights-Array
  39. - DelaysChunk:
  40. - Chunk Name: „Delays“
  41. - Chunk Length: 64 bit integer
  42. - sizeof(T_Delays)
  43. - NWeights
  44. - Delays-Array
  45. */
  46. // forward declaration
  47. class VecNormalize;
  48. class layer;
  49. class Matrix4D;
  50. Connection* LoadConnection(const char* filename);
  51. // Zugriff auf vector <int> 10% - 15% schneller als Zugriff auf
  52. // Element von vector <class synapse>
  53. // daher SynWeights, SynSourceNr, ...
  54. class NotSupportedWithMemsave: public domain_error
  55. {
  56. public:
  57. explicit NotSupportedWithMemsave(const string& what_arg): domain_error(what_arg){};
  58. };
  59. class RequestedDelayTooLarge: public std::out_of_range
  60. {
  61. public:
  62. explicit RequestedDelayTooLarge (const std::string& what_arg): out_of_range(what_arg) {};
  63. explicit RequestedDelayTooLarge ()
  64. : std::out_of_range("requested Delay is larger than DMAX") {};
  65. explicit RequestedDelayTooLarge (long RequestedDelay)
  66. : std::out_of_range("requested Delay=" + stringify( RequestedDelay ) + " is larger than DMAX=" + stringify(DMAX)) {};
  67. };
  68. class RangeConnectionParameters: public ParameterSet
  69. {
  70. public:
  71. RangeConnectionParameters()
  72. : Range(0), Strength(0), MaxDelay(1), MinDelay(0), Cyclic(false), Connectivity(1)
  73. {}
  74. RangeConnectionParameters(float _Range, float _Strength)
  75. : Range(_Range), Strength(_Strength), Connectivity(1), MaxDelay(1), MinDelay(0), Cyclic(false)
  76. {}
  77. RangeConnectionParameters(float _Range,
  78. float _Strength,
  79. float _MaxDelay,
  80. float _MinDelay,
  81. float _Connectivity,
  82. bool _Cyclic)
  83. : Range(_Range),
  84. Strength(_Strength),
  85. MaxDelay(_MaxDelay),
  86. MinDelay(_MinDelay),
  87. Connectivity(_Connectivity),
  88. Cyclic(_Cyclic)
  89. {}
  90. float Range;
  91. float Strength;
  92. float MaxDelay;
  93. float MinDelay;
  94. float Connectivity;
  95. bool Cyclic;
  96. };
  97. /*! \brief Klasse VecConnection
  98. * Neu-Implementierung der connection mit std::vector statt c-Arrays
  99. *
  100. * Detailed description starts here.
  101. */
  102. class VecConnection : public Connection
  103. {
  104. friend class VecNormalize;
  105. protected:
  106. veclearning* learnobj;
  107. int Observe_s, Observe_m;
  108. string FileTypeString; // identifyer for loading weight file correctly (e.g. in IDL)
  109. public:
  110. VecConnection();
  111. VecConnection(layer*, layer*,
  112. csimInputChannel _InputChannel=csimInputChannel_AMPA, bool _nonself=true);
  113. ~VecConnection();
  114. virtual int proceede(int TotalTime);
  115. virtual int prepare(int TotalTime);
  116. int SetMinMaxDelay(float _MaxDelay, float _MinDelay);
  117. int SetupDelaysArray();
  118. int SetupPreSynNrArray();
  119. virtual int CleanupArrays();
  120. int SetRandomDelays();
  121. int DeleteSynapse(int SynNr);
  122. virtual void SetName(const char*);
  123. virtual int Save();
  124. virtual int Save(int nr);
  125. virtual int Save(const string&);
  126. virtual void Save_VecConnection_2_0(const string& SaveWeightFileName);
  127. virtual void Save_VecConnection_1_0(const string& SaveWeightFileName);
  128. int Load();
  129. int Load(const string& FileName);
  130. int Load(const string& FileName, const string& DirName);
  131. int Load_VecConnection_1_0(const string& FileNameWithDir);
  132. void Load_VecConnection_2_0(const string& FileName);
  133. void Load_VecConnection_2_1(const string& FileName);
  134. virtual long calcMemoryConsumption();
  135. float GetSourceWeights(int SourceNr, vector<float>& WeightMatrix);
  136. float GetSourceWeights(int SourceNr, vector<Synapse>& SynList, int& MaxDelay);
  137. float GetTargetWeights(int TargetNr, vector<float>& WeightMatrix);
  138. float GetTargetWeights(int TargetNr, vector<Synapse>& SynList, int& MaxDelay);
  139. float GetMaxWeight();
  140. float GetMinWeight();
  141. int GetMaxDelay();
  142. void GetWeightMatrix4D(Matrix4D& matrix);
  143. int DeleteLowWeights(float threshold);
  144. int SetAllWeights(float WeightValue);
  145. int MultiplyTargetWeights(int TargetNr, float Factor);
  146. int MultiplyAllTargetWeights(float Factor);
  147. int ScrambleSynTargets();
  148. float GetWeightSum(int TargetNr, bool quadratic);
  149. float GetMeanWeight();
  150. int Rewire(float minWeight, float maxConnectivity);
  151. virtual int WriteSimInfo(fstream &fw);
  152. virtual int WriteSimInfo(fstream &fw, const string &ChildInfo);
  153. int SetLearn(bool);
  154. int SetLearnObj(veclearning*);
  155. VecConnectionInfo GetConnectionInfo();
  156. virtual int DeleteSynapseArrays();
  157. virtual int PushBackNewSynapse(int SourceNr, int TargetNr, float Weight, int Delay);
  158. virtual int ReserveSynapses(int nsyn);
  159. int ConnectFull(float MaxWeight,
  160. float MaxDelay =20,
  161. float MinDelay =0,
  162. bool RandomWeights=false);
  163. int ConnectRandomIncomming(double _connectivity,
  164. float _InitialWeights,
  165. float _maxDelay=20,
  166. float minDelay=0,
  167. bool RandomDelays=true);
  168. int ConnectRandomIncomming(int _NIncommingConnections,
  169. float _InitialWeights,
  170. float _maxDelay=20,
  171. float minDelay=0,
  172. bool RandomDelays=true);
  173. int ConnectSelf(float MaxWeight, float MaxDelay =20, float MinDelay =0);
  174. int ConnectGaussian(float Sigma, float MaxWeight,
  175. float MaxDelay =20, float MinDelay=0, bool Cyclic=false);
  176. void ConnectCircular(const RangeConnectionParameters& Paras);
  177. // Delay-Typen als enum: random, distance-dependent (todo?)
  178. int SetNewWeights(float IncommingConnectivity, float InitialWeights);
  179. int SetNewWeights(vector<int>* NNewWeights, float InitialWeights);
  180. bool hasEqualSynapses(const VecConnection& OtherVecCon);
  181. protected:
  182. void updateNSynapses();
  183. /** @brief number of valid synapses\n (!=SynWeights.size(),
  184. because deleted synapses are still stored in thevectors)
  185. */
  186. int NSynapses;
  187. vector <float> SynWeights; //!< strength of synaptic weights
  188. vector <T_NNeurons> SynTargetNr; //!< index of target neuron
  189. vector <T_NNeurons> SynSourceNr; //!< index of source neuron
  190. vector <T_Delays> SynDelays; //!< synaptic delay of the synapse.
  191. //!< If synapse i is deleted, SynDelays[i]==-1
  192. //! helper array for finding all synapses of a postsynaptic neuron
  193. vector < vector <T_NSynapses> > PreSynNr;
  194. //! helper array for finding all synapses with a given SourceNr and DelayIndex
  195. //! delays[SourceNr][DelayIndex][Index] == SynapseNr //
  196. vector < vector <vector <T_NSynapses> > > delays;
  197. };
  198. /*! \brief Klasse VecDepressiveConnection
  199. *
  200. *
  201. * Depressive Synapses
  202. * Tsodyks M, Pawelzik K, Markram H.
  203. * Neural networks with dynamic synapses.
  204. * Neural Comput. 1998 May 15;10(4):821-35.
  205. */
  206. class VecDepressiveConnection : public VecConnection
  207. {
  208. protected:
  209. float U_se_fac; //!< efficacy reduction factor after transmitter release (after spike)
  210. float U_SE; //!< proportion of transmitter per spike
  211. float TauRec; //!< recovery dynamics in ms
  212. float E0; //!< initialize Efficacy;
  213. public:
  214. VecDepressiveConnection(
  215. layer*, layer*, csimInputChannel =csimInputChannel_AMPA,
  216. float _TauRec=20, float U_se=0.2, float InitializeFrequency=0);
  217. // ~VecDepressiveConnection(); // es werden keine dynamischen Objekte angelegt, daher kein Destruktor noetig
  218. virtual int DeleteSynapseArrays();
  219. virtual int PushBackNewSynapse(int SourceNr, int TargetNr, float Weight, int Delay);
  220. virtual int ReserveSynapses(int nsyn);
  221. virtual int proceede(int =0);
  222. virtual int prepare(int =0);
  223. virtual int reset(int t);
  224. virtual void SetParameter(ParaType p, double value);
  225. virtual int WriteSimInfo(fstream &fw);
  226. virtual float InitializeEfficacy(float frequency);
  227. vector <int> LastEpsp;
  228. vector <float> Efficacy;
  229. virtual long calcMemoryConsumption();
  230. };
  231. #endif /*_HPP_VCONNECTION */