libcsim.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*Copyright (C) 2005, 2006, 2007, 2008 Frank Michler, Philipps-University Marburg, Germany
  2. This program is free software; you can redistribute it and/or
  3. modify it under the terms of the GNU General Public License
  4. as published by the Free Software Foundation; either version 2
  5. of the License, or (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  13. */
  14. #ifndef _H_LIBCSIM
  15. #define _H_LIBCSIM
  16. #include "libvector.h"
  17. #include <vector>
  18. #include <cassert>
  19. #include "anyoption.h"
  20. #include <time.h>
  21. #include <sys/stat.h>
  22. #include <math.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <iostream>
  26. #include <sstream>
  27. #include <string>
  28. #include <stdexcept>
  29. #include <fstream>
  30. #include <map>
  31. #include "lut.hpp"
  32. #include "typedefs.hpp"
  33. #include "stringutils.h"
  34. //#include <idlrpc/idl_rpc.h>
  35. using namespace std;
  36. inline string AddSpaces(const string &st)
  37. {
  38. string Spaces = " ";
  39. string out=st;
  40. string::size_type i;
  41. out.replace(0,0,Spaces);
  42. i=0;
  43. i = out.find("\n", i+1);
  44. while (i != string::npos) {
  45. if (i+1 < out.size()) out.replace(i+1, 0, Spaces);
  46. i = out.find("\n", i+1);
  47. }
  48. return out;
  49. }
  50. #define getrandom(max1) ((rand()%(int)((max1)))) // random integer between 0 and max-1
  51. #define ExpDec(t) (LutExpDec(int(t)))
  52. const int NLutExpDec=100000;
  53. const float TauLut=NLutExpDec/10;
  54. static LUT_function<0, NLutExpDec> LutExpDec(exp,-1./(TauLut));
  55. //define lookup table for NMDA current
  56. inline double NmdaCurrent(double Voltage)
  57. {
  58. // from Deco&Rolls 2005
  59. // (they cite Jahr and Stevens 1990).
  60. return -0.327*Voltage/(1+exp(-0.062*Voltage)/3.57);
  61. }
  62. const int NLutNmda=10000;
  63. const float NmdaV0=-200;
  64. const float NmdaV1=50;
  65. const float NmdaVRange=NmdaV1-NmdaV0;
  66. const float NmdaVToIntFactor=NLutNmda/NmdaVRange;
  67. const float NmdaIntToVFactor=NmdaVRange/NLutNmda;
  68. //!static LUT_function<int(NmdaV0*NmdaVToIntFactor), int(NmdaV1*NmdaVToIntFactor)>
  69. static LUT_function<-8000, 2000> LutNmdaCurrent(NmdaCurrent,0.0250000);
  70. // FixMe clean up this, converted const expressions into literals after
  71. // transition to debian etch g++ 4.1.2
  72. #define INmda(v) (LutNmdaCurrent(int(NmdaVToIntFactor*v)))
  73. double gauss(double x, double sigma=1, double x0=0);
  74. int fexist(const char *filename );
  75. int FileSize(const char *filename );
  76. // alpha function: f(t) = t*exp(-t/tau)/(tau/e)
  77. inline float* AlphaFktLut(
  78. int &N, float TauPeak=10, float Amp=1, float BaseLine=0, float dt=0.25)
  79. {
  80. float Bound = 10*TauPeak;
  81. N = int(Bound/dt);
  82. float* Lut = new float [N];
  83. float Em1 = exp(-1.0); // 1/e
  84. for (int n=0;n<N;++n) {
  85. Lut[n] = BaseLine + Amp*n*dt*exp(-n*dt/TauPeak)/(TauPeak*Em1);
  86. }
  87. return Lut;
  88. }
  89. // exp decay function
  90. inline float* ExpDecayLut(
  91. int &N, float TauDecay=10, float Amp=1, float BaseLine=0, float dt=0.25, float BoundFkt=5)
  92. {
  93. float Bound = BoundFkt*TauDecay;
  94. N = int(Bound/dt);
  95. float* Lut = new float [N];
  96. for (int n=0;n<N;++n) {
  97. Lut[n] = BaseLine + Amp*exp(-n*dt/TauDecay);
  98. }
  99. return Lut;
  100. }
  101. inline const char* testfunction()
  102. {
  103. return "Hallo Welt\n";
  104. }
  105. inline vector<vector2d> Rectangle(float x0, float x1, float y0, float y1, int N)
  106. {
  107. vector<vector2d> tmp;
  108. if (N>=4)
  109. {
  110. float circumference =2*(x1+y1-x0-y0);
  111. float corners [4] = {0, x1-x0, circumference/2, circumference-(y1-y0)};
  112. // float corners[4] = {3,3,3,3};
  113. float CurMP=0;
  114. float StepSize=circumference/(N);
  115. while ((circumference-CurMP)>(StepSize/2))
  116. {
  117. cout << "CurMP=" << CurMP << " circ=" << circumference << "diff=" << circumference-CurMP << "\n";
  118. if (CurMP< corners[1]) tmp.push_back(vector2d(x0 + CurMP, y0));
  119. else if (CurMP<corners[2]) tmp.push_back(vector2d(x1, y0+CurMP-(corners[1])));
  120. else if (CurMP<corners[3]) tmp.push_back(vector2d(x1- (CurMP - corners[2]), y1));
  121. else tmp.push_back(vector2d(x0, y0+(circumference-CurMP)));
  122. CurMP += StepSize;
  123. }
  124. }
  125. return tmp;
  126. }
  127. typedef float* pfloat;
  128. template<class T>
  129. T** NewArray2d(T** &temp, int a, int b)
  130. {
  131. temp = new T*[a];
  132. for (int i=0; i<a;++i) temp[i] = new T[b];
  133. }
  134. template<class T>
  135. int DeleteArray2d(T** &ArrayPointer, int a)
  136. {
  137. int i;
  138. for (i=0; i<a;++i) delete []ArrayPointer[i];
  139. delete []ArrayPointer;
  140. return 0;
  141. }
  142. template<class T>
  143. T*** NewArray3d(T*** &temp, int a, int b, int c)
  144. {
  145. temp = new T**[a];
  146. for (int i=0; i<a;++i)
  147. {
  148. temp[i] = new T*[b];
  149. for (int j=0;j<b;++j) temp[i][j] = new T[c];
  150. }
  151. }
  152. template<class T>
  153. int DeleteArray3d(T*** &ArrayPointer, int a, int b)
  154. {
  155. int i,j;
  156. for (i=0;i<a;++i)
  157. {
  158. for (j=0;j<b;++j) delete [] ArrayPointer[i][j];
  159. delete [] ArrayPointer[i];
  160. }
  161. delete []ArrayPointer;
  162. return 0;
  163. }
  164. enum LookUpType {loNONE, loEXP, loNMDA};
  165. class LookUpTable
  166. {
  167. protected:
  168. float xRange0, xRange1, RDiff;
  169. int LBound, UBound;
  170. float IndexFactor;
  171. public:
  172. float* tmpvalue;
  173. float* value;
  174. int N;
  175. LookUpTable();
  176. ~LookUpTable();
  177. void InitExp(int N, float Tau, float v0=1, float Range0=0, float Range1=0);
  178. float GetValue(int index);
  179. double GetValue(double X);
  180. void Reset();
  181. };
  182. class Recorder
  183. {
  184. FILE* fs;
  185. string FileName;
  186. public:
  187. Recorder(char* ="data.txt");
  188. ~Recorder();
  189. int record(float, float);
  190. int record(float, float, float);
  191. };
  192. typedef struct _BinRecHeader
  193. {
  194. char finfo[7]; // "BinRec" + \0
  195. char version[5]; // "0.05" + \0
  196. int N, M, ValueSize, FrameSize;
  197. float dt;
  198. } BinRecHeader;
  199. enum ParaType {
  200. PARA_U_SE,
  201. PARA_TAU_REC,
  202. };
  203. class BinRecorder
  204. {
  205. FILE* fs;
  206. string FileName;
  207. string BaseFileName;
  208. string SimTag;
  209. float** PBuffer;
  210. float* RecBuffer;
  211. float* RecBuffPointer;
  212. float* RecBuffMax;
  213. int N, M;
  214. int MacroTimeStep;
  215. BinRecHeader Header;
  216. public:
  217. BinRecorder(int _MacroTimeStep, int _N, int _M, float** _PBuffer, float _dt=0.25, const char* ="data.bin");
  218. ~BinRecorder();
  219. int record();
  220. int restart();
  221. int restart(string _SimTag);
  222. int WriteToFile();
  223. };
  224. class DistanceProfile
  225. {
  226. protected:
  227. int N;
  228. float* profile;
  229. float* xscale;
  230. public:
  231. DistanceProfile(int N);
  232. DistanceProfile(int N, float Sigma1, float Sigma2);
  233. DistanceProfile(const vector<float> &Profile);
  234. ~DistanceProfile();
  235. float GetValue(float dist);
  236. float GetMaxConDistance(float minweight);
  237. void print();
  238. };
  239. class CircleDistanceProfile: public DistanceProfile
  240. {
  241. public:
  242. CircleDistanceProfile(int N, float Radius, bool invers=false);
  243. };
  244. enum DirType {CSIM_left=-1, CSIM_right=1};
  245. /** @brief input channels
  246. */
  247. enum csimInputChannel {
  248. csimInputChannel_AMPA, //!< AMPA: fast excitatory
  249. csimInputChannel_GABAa, //!< GABAa: fast inhibitory
  250. //! NMDA_AMPA: combined NMDA and AMPA input, NMDA is long lasting excitatory
  251. csimInputChannel_NMDA_AMPA,
  252. csimInputChannel_GABAb, //!< GABAb: long lasting inhibitory
  253. csimInputChannel_AMPA2, //!<
  254. csimInputChannel_Linking, //!<
  255. csimInputChannel_Endu, //!<
  256. csimInputChannel_EnduLinking,//!<
  257. csimInputChannel_Membrane, //!< inject current directly to membrand potential
  258. };
  259. class layer; // forward declaration
  260. class Connection; //! forward declaration
  261. class connection; //! forward declaration --> loeschen geht noch nicht??
  262. class AbstractNormalize; //! forward declaration
  263. class learning; // forward declaration for learning pointer in class connection
  264. class veclearning; // forward declaration for learning pointer in class connection
  265. class input;
  266. //! structure for transfering connection parameters to learning object
  267. typedef struct {
  268. int Dmax;
  269. int maximumDelay;
  270. float MaxWeight;
  271. layer *TargetLayer, *SourceLayer;
  272. short** delays_length;
  273. short*** delays;
  274. int M;
  275. int **post;
  276. int **I_pre, **D_pre;
  277. int **m_pre;
  278. float **WeightPointer;
  279. float **WeightDerivativePointer;
  280. int maxN_pre;
  281. int *N_pre;
  282. pfloat **s_pre, **sd_pre;
  283. } ConnectionInfo;
  284. //! structure for transfering connection parameters to learning object
  285. typedef struct {
  286. int Dmax;
  287. int maximumDelay;
  288. float MaxWeight;
  289. layer *TargetLayer, *SourceLayer;
  290. vector<float>* PSynWeights;
  291. vector<T_NNeurons>* PSynSourceNr;
  292. vector<T_NNeurons>* PSynTargetNr;
  293. vector<T_Delays>* PSynDelays;
  294. vector<vector<T_NSynapses> >* PPreSynNr;
  295. vector<vector<vector<T_NSynapses> > >* Pdelays;
  296. } VecConnectionInfo;
  297. /////////////////////////////////////////
  298. class ParameterSet
  299. {
  300. public:
  301. // virtual destructor needed to make class polymorphic and use dynamic_cast
  302. virtual ~ParameterSet(){}
  303. };
  304. template<class T1, class T2>
  305. class TwoParameters: public ParameterSet
  306. {
  307. public:
  308. TwoParameters(T1 Val1, T2 Val2): first(Val1), second(Val2) {}
  309. T1 first;
  310. T2 second;
  311. };
  312. class Changable
  313. {
  314. public:
  315. virtual void changeParameter(ParameterSet* Paras)=0;
  316. };
  317. /////////////////////////////////
  318. class SimpleTextProgressBar
  319. {
  320. float Status; // 0<=status<=Final
  321. float StepSize;
  322. int Final;
  323. public:
  324. SimpleTextProgressBar(int MaxSteps=10);
  325. int Next(int StepNr);
  326. int Reset(int MaxSteps);
  327. };
  328. //////////////////////
  329. // forward declaration
  330. class AnyOptionWrapper;
  331. //StandardOptions
  332. class CSimStandardOptions
  333. {
  334. private:
  335. AnyOptionWrapper *myAnyWrap;
  336. public:
  337. CSimStandardOptions(AnyOptionWrapper* _opt);
  338. int NTrials;
  339. int NSteps;
  340. int TestNSteps;
  341. string DataDirectory;
  342. bool LoadWeights;
  343. bool SimFinishButton;
  344. float InputStrength;
  345. bool SaveInitialWeights;
  346. };
  347. /////////////////
  348. /*********IDLrpc******************/
  349. /* class IDLrpc */
  350. /* { */
  351. /* public: */
  352. /* IDLrpc(); */
  353. /* ~IDLrpc(); */
  354. /* }; */
  355. ////////////////////
  356. #endif /*_H_LIBCSIM */