connection.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. #ifndef _HPP_CONNECTION
  2. #define _HPP_CONNECTION
  3. #include "simelement.hpp"
  4. // forward declaration
  5. class layer;
  6. class learning;
  7. struct Synapse
  8. {
  9. Synapse(int snr, int tnr, float w, int d): Weight(w), SourceNr(snr), TargetNr(tnr), Delay(d) {};
  10. float Weight;
  11. int SourceNr;
  12. int TargetNr;
  13. int Delay;
  14. };
  15. struct SourceTargetDim
  16. {
  17. SourceTargetDim()
  18. : NSource(0), NSx(0), NSy(0), NTarget(0), NTx(0), NTy(0)
  19. {}
  20. SourceTargetDim(int _NSource, int _NSx, int _NSy, int _NTarget, int _NTx, int _NTy)
  21. : NSource(_NSource), NSx(_NSx), NSy(_NSy), NTarget(_NTarget), NTx(_NTx), NTy(_NTy)
  22. {}
  23. std::string stringify()
  24. {
  25. std::stringstream o;
  26. o << NSource << " " << NSx << " " << NSy << " " << NTarget << " " << NTx << " " << NTy;
  27. return o.str();
  28. }
  29. int NSource;
  30. int NSx;
  31. int NSy;
  32. int NTarget;
  33. int NTx;
  34. int NTy;
  35. };
  36. /** like SourceTargetDim plus one new item: ArrayOrderXFast
  37. */
  38. struct SourceTargetDim1
  39. {
  40. SourceTargetDim1()
  41. : NSource(0), NSx(0), NSy(0), NTarget(0), NTx(0), NTy(0)
  42. {}
  43. SourceTargetDim1(int _NSource, int _NSx, int _NSy, int _NTarget, int _NTx, int _NTy)
  44. : NSource(_NSource), NSx(_NSx), NSy(_NSy), NTarget(_NTarget), NTx(_NTx), NTy(_NTy)
  45. {}
  46. std::string stringify()
  47. {
  48. std::stringstream o;
  49. o << NSource << " " << NSx << " " << NSy << " " << NTarget << " " << NTx << " " << NTy << " " << ArrayOrderXFast;
  50. return o.str();
  51. }
  52. int NSource;
  53. int NSx;
  54. int NSy;
  55. int NTarget;
  56. int NTx;
  57. int NTy;
  58. int ArrayOrderXFast;
  59. };
  60. /*! \brief Klasse Connection
  61. * abstrakte Basis-Klasse für connection und VecConnection
  62. *
  63. * Detailed description starts here.
  64. */
  65. class Connection : public SimElement
  66. {
  67. protected:
  68. layer *TargetLayer, *SourceLayer;
  69. float *InputPointer;
  70. std::string WeightFileName;
  71. float connectivity;
  72. float Strength;
  73. float InitialWeight; // for rewiring
  74. bool AutoSave;
  75. bool learn;
  76. bool RewireOn;
  77. float RewireMaxConnectivity;
  78. float RewireNNewConnections;
  79. float RewireThreshold;
  80. clock_t cpu_start, cpu_end;
  81. double cpu_time_used;
  82. public:
  83. Connection();
  84. Connection(layer*, layer*, csimInputChannel _InputChannel=csimInputChannel_AMPA, bool _nonself=true);
  85. ~Connection();
  86. virtual int Save()=0;
  87. virtual int Save(int nr)=0;
  88. virtual int Save(const string&)=0;
  89. virtual bool Learning();
  90. virtual int SetLearn(bool) =0;
  91. virtual int DeleteLowWeights(float threshold) =0;
  92. virtual int SetAllWeights(float WeightValue) {};
  93. virtual int MultiplyAllTargetWeights(float Factor) {cout << "not implemented\n";};
  94. // BEGIN used by TomWaitsQt
  95. virtual float GetSourceWeights(int CurSource, vector<Synapse>& SynList, int &MaxDelay)=0;
  96. virtual float GetTargetWeights(int CurTarget, vector<Synapse>& SynList, int &MaxDelay)=0;
  97. virtual float GetMaxWeight()=0;
  98. virtual int GetMaxDelay()=0;
  99. virtual float GetMinWeight(){return 0;};
  100. virtual int GetMinDelay(){return 0;};
  101. virtual long calcMemoryConsumption(){return 0;};
  102. // END used by TomWaitsQt
  103. virtual bool CheckHeaderConsistency();
  104. int SetRewiring(float threshold, float maxCon, int NNewCon=1, float _InitialWeight=-1);
  105. void SetRewiringOff();
  106. int SetAutoSave(bool);
  107. void SetNonSelf(bool);
  108. // virtual int WriteSimInfo(fstream &fw) =0;
  109. // virtual int WriteSimInfo(fstream &fw, const string &ChildInfo) =0;
  110. layer* GetTargetLayer();
  111. int GetNTarget() {return nt;};
  112. int GetNSource() {return ns;};
  113. void GetSourceTargetDim(unsigned int& snx, unsigned int& sny, unsigned int& tnx, unsigned int& tny)
  114. {snx=SourceNx;sny=SourceNy;tnx=TargetNx;tny=TargetNy;};
  115. SourceTargetDim GetSourceTargetDim()
  116. {
  117. return SourceTargetDim(ns, SourceNx, SourceNy, nt, TargetNx, TargetNy);
  118. }
  119. bool NonSelf;
  120. csimInputChannel InputChannel;
  121. T_Delays Dmax; // global maximum delay
  122. T_Delays maximumDelay; // maximum delay of current delay distribution
  123. T_Delays minimumDelay; // minimum delay of current delay distribution
  124. T_Delays DelayDiff;
  125. bool ArrayOrderXFast;
  126. protected:
  127. int ns,nt;
  128. int SourceNx, SourceNy, TargetNx, TargetNy;
  129. float maxWeight;
  130. };
  131. /*! \brief Klasse connection
  132. * "alte" connection-Klasse, benutzt selbst generierte, mehrdimensionale c-Arrays
  133. *
  134. * Detailed description starts here.
  135. */
  136. class connection : public Connection
  137. {
  138. protected:
  139. learning* learnobj;
  140. int SetupPresynapticInfo_old();
  141. int SetupPresynapticInfo();
  142. int DeletePresynapticInfo();
  143. int CheckPresynapticInfo_old();
  144. int SetMinMaxDelay(float _MaxDelay, float _MinDelay);
  145. virtual int InitializeDynamicalArrays(
  146. const int _N, const int _M, const int Dmax);
  147. int DeleteDynamicalArrays();
  148. int Observe_s, Observe_m;
  149. public:
  150. connection(layer*, layer*, csimInputChannel _InputChannel=csimInputChannel_AMPA, bool _nonself=true);
  151. connection();
  152. ~connection();
  153. virtual int Save();
  154. virtual int Save(int nr);
  155. virtual int Save(const string&);
  156. float GetSourceWeights(int CurSource, vector<Synapse>& SynList, int &MaxDelay);
  157. float GetTargetWeights(int CurTarget, vector<Synapse>& SynList, int &MaxDelay);
  158. float GetMaxWeight();
  159. int GetMaxDelay();
  160. virtual bool CheckHeaderConsistency();
  161. int Load();
  162. int Load(const char* FileName);
  163. int Load(const char* FileName, const char* DirName);
  164. int ConnectMatrix(const vector<vector<float> > &WeightMatrix,
  165. float MaxDelay =20, float MinDelay=0);
  166. int ConnectRandom(float _connectivity,
  167. float _InitialWeights,
  168. float _maxDelay=20,
  169. float minDelay=0,
  170. bool RandomDelays=true);
  171. int ConnectRandom2(float _Connectivity,
  172. float _InitialWeights,
  173. float _maxDelay=20,
  174. float minDelay=0,
  175. bool RandomDelays=true);
  176. int ConnectRandom2(int NIncommingConnections,
  177. float _InitialWeights,
  178. float _maxDelay=20,
  179. float minDelay=0,
  180. bool RandomDelays=true);
  181. int ConnectPartialRandom(float _connectivity, float InitialWeights,
  182. int MaxTarget, int MaxSource, float =20, float =0);
  183. int ConnectDirectional(float, float);
  184. int ConnectGaussianProb(float Sigma, float MaxWeight,
  185. float MaxDelay =20, float MinDelay=0,
  186. float MinConDistance=0, float MaxConnectivity=1, bool Cyclic=false);
  187. int ConnectGaussian(float Sigma,
  188. float MaxWeight,
  189. float MaxDelay =20,
  190. float MinDelay=0,
  191. bool Cyclic=false);
  192. int ConnectGaussianColumnwise(float Sigma,
  193. float MaxWeight,
  194. int dimx,
  195. int dimy,
  196. bool Cyclic=false,
  197. bool Shifted=true,
  198. float MaxDelay =20,
  199. float MinDelay=0,
  200. bool divergent=true,
  201. bool notstraight=true);
  202. int ConnectIdenticalGaussian(float Sigma,
  203. float MaxWeight,
  204. float MaxDelay =20,
  205. float MinDelay=0,
  206. float mpx=0.5,
  207. float mpy=0.5);
  208. int ConnectProfile(DistanceProfile* profile,
  209. float MaxWeight,
  210. float MaxDelay =20,
  211. float MinDelay=0,
  212. bool Cyclic=false);
  213. int ConnectCircular(float PreConnectivity,
  214. float MaxWeight,
  215. float MaxDelay =20,
  216. float MinDelay=0);
  217. int ConnectCircularPre(float Connectivity,
  218. float MaxWeight,
  219. float MaxDelay =20,
  220. float MinDelay=0);
  221. int ConnectFull(float MaxWeight,
  222. float MaxDelay =20,
  223. float MinDelay =0,
  224. bool RandomWeights=false);
  225. int ConnectFullColumnwise(float MaxWeight,
  226. int dimx,
  227. int dimy,
  228. int sourcedimx,
  229. int sourcedimy,
  230. float MaxDelay =20,
  231. float MinDelay =0,
  232. bool divergentrow=false,
  233. float sigma_divrow=0.1,
  234. bool Cyclic=false,
  235. bool convergent=false);
  236. int ConnectGradientFields(float MaxWeight=1,
  237. float xslope=0.5,
  238. float yslope=0.5,
  239. int dimx=50,
  240. int dimy=50,
  241. int sourcedimx=50,
  242. int sourcedimy=50,
  243. float MaxDelay =20,
  244. float MinDelay =0);
  245. int ConnectSelf(float MaxWeight,
  246. float MaxDelay =20,
  247. float MinDelay =0);
  248. int ConnectPartial(int maxTarget,
  249. float MaxWeight,
  250. float MaxDelay =20,
  251. float MinDelay =0);
  252. int CheckPresynapticInfo();
  253. int CheckConnection();
  254. int DeleteWeight(int SourceNr, int ConnectionNr);
  255. int DeleteWeightCorrectPreInfo(int SourceNr, int ConnectionNr, int SupposedDelay=-1); // not tested
  256. int SetSystematicWeights(); // for test purpose only
  257. int CheckSystematicWeights(); // for test purpose only
  258. virtual int DeleteLowWeights(float threshold);
  259. int InsertNewWeight(int SourceNr, int TargetNr, float InitialWeight=0, int delay=1);
  260. int SetNewWeights(int NNewTargets, int NMaxTargets);
  261. int SetNewWeights2(int NNewTargets, int NMaxTargets);
  262. int Rewire(float minWeight, float maxConnectivity);
  263. int Print();
  264. int SetWeights(vector<vector<float> >& );
  265. int SetupDelays(float MaxDelay,float MinDelay=0);
  266. int SetupDelays();
  267. int SetupRandomDelays(float MaxDelay, float minDelay=0);
  268. int SetupRandomDelays();
  269. virtual int proceede(int =0);
  270. virtual int prepare(int =0);
  271. virtual void SetName(const char*);
  272. int SetFileName(char*);
  273. virtual int SetLearn(bool);
  274. int SetLearnObj(learning*);
  275. int ObserveSynapse(int SourceNumber=0, int MNumber=0, char* RecName="synapse.txt");
  276. int ObserveSynapsePrePost(int SourceNumber=0, int TargetNumber=0, char* RecName="synapse.txt");
  277. virtual int StartBinRec(int PostsynNum);
  278. ConnectionInfo GetConnectionInfo();
  279. virtual int WriteSimInfo(fstream &fw);
  280. virtual int WriteSimInfo(fstream &fw, const string &ChildInfo);
  281. int M,maxN_pre;
  282. /// limitation: all presynaptic neurons have the same number of target neurons (M!) ??
  283. /// not true!! --> no delay, no cry!!
  284. int *N_post; // number of synapses per presynaptic neuron;
  285. int **post; // 2D-Array with indices of target neurons. TargetNr=post[SourceNr][index]
  286. float **s; // 2D-Array with weight strenghes. weight=s[SourceNr][index]
  287. float **sd;
  288. short **delays_length;
  289. short ***delays;
  290. int *N_pre;
  291. int **I_pre, **D_pre;
  292. int **m_pre; // presynaptic connection index m in s[i][m] array
  293. pfloat **s_pre, **sd_pre;
  294. };
  295. /*! \brief Klasse DepressiveConnection
  296. * "alte" connection-Klasse, benutzt selbst generierte, mehrdimensionale c-Arrays
  297. *
  298. * Depressive Synapses
  299. * Tsodyks M, Pawelzik K, Markram H.
  300. * Neural networks with dynamic synapses.
  301. * Neural Comput. 1998 May 15;10(4):821-35.
  302. */
  303. class DepressiveConnection : public connection
  304. {
  305. protected:
  306. float U_se_fac; // (proportion of transmitter per spike)
  307. float U_SE;
  308. float TauRec; //recovery dynamics
  309. float **efficacy; // for every synapse
  310. int **LastEpsp; // for every synapse
  311. public:
  312. DepressiveConnection(
  313. layer*, layer*, csimInputChannel =csimInputChannel_AMPA,
  314. float _TauRec=20, float U_se=0.2);
  315. ~DepressiveConnection();
  316. /* boost::multi_array<float,2> efficacy; */
  317. /* boost::multi_array<int,2> LastEpsp; */
  318. virtual int proceede(int =0);
  319. virtual int prepare(int =0);
  320. virtual int reset(int t);
  321. virtual int InitializeDynamicalArrays(
  322. const int _N, const int _M, const int Dmax);
  323. virtual int WriteSimInfo(fstream &fw);
  324. };
  325. /*! \brief Klasse Facilitatory Synapses
  326. * Tsodyks M, Pawelzik K, Markram H.
  327. *
  328. * Tsodyks M, Pawelzik K, Markram H.
  329. * Neural networks with dynamic synapses.
  330. * Neural Comput. 1998 May 15;10(4):821-35.
  331. *
  332. * could be made faster if only one U_SE value per presynaptic neuron
  333. * instead one per synapse
  334. */
  335. class FacilitativeConnection : public DepressiveConnection
  336. {
  337. protected:
  338. float **U_SEvalue;
  339. float UseTauDec, UseInc, UseConst;
  340. public:
  341. FacilitativeConnection(
  342. layer*, layer*, csimInputChannel =csimInputChannel_AMPA,
  343. float _TauRec=20, float U_se=0.2, float _TauFac=20, float _UseInc=0.01);
  344. ~FacilitativeConnection();
  345. virtual int proceede(int =0);
  346. virtual int reset(int t);
  347. virtual int StartBinRec(int PostsynNum);
  348. virtual int InitializeDynamicalArrays(
  349. const int _N, const int _M, const int Dmax);
  350. };
  351. class PspConnection : public connection
  352. {
  353. protected:
  354. float *PspTemplate;
  355. float **Psp;
  356. int PspDuration;
  357. int PspArrayPointer;
  358. public:
  359. PspConnection(layer*, layer*, csimInputChannel InputChannel = csimInputChannel_AMPA, bool _nonself=true);
  360. ~PspConnection();
  361. /* ~PspConnection(); */
  362. /* virtual int proceede(int =0); */
  363. /* virtual int prepare(int =0); */
  364. };
  365. #endif /*_HPP_CONNECTION */