connection.cpp 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058
  1. #include "sys.hpp" // for libcwd
  2. #include "debug.hpp" // for libcwd
  3. #include "connection.hpp"
  4. #include "learn.hpp"
  5. #include "layer.hpp"
  6. #include "simloop.hpp"
  7. #include <boost/multi_array.hpp>
  8. ////////////////////////////////////
  9. Connection::Connection(): SourceLayer(0), TargetLayer(0), InputPointer(0), ArrayOrderXFast(true)
  10. {
  11. Dmax=DMAX;
  12. maximumDelay = Dmax;
  13. }
  14. Connection::Connection(layer* SL, layer* TL, csimInputChannel _InputChannel, bool _nonself)
  15. : SimElement(seConnection), SourceLayer(SL),
  16. TargetLayer(TL), connectivity(0.1), Strength(0), cpu_time_used(0),
  17. AutoSave(true), NonSelf(_nonself), RewireOn(false),
  18. learn(false), InputChannel(_InputChannel), ArrayOrderXFast(true)
  19. {
  20. Name="Connection";
  21. WeightFileName ="weights.dat";
  22. dt = TargetLayer->GetDt();
  23. MacroTimeStep = TargetLayer->GetMacroTimeStep();
  24. InputPointer = TargetLayer->GetInputPointer(InputChannel);
  25. Dmax=DMAX;
  26. maximumDelay = Dmax;
  27. ns = SourceLayer->N;
  28. nt = TargetLayer->N;
  29. SourceNx=SourceLayer->Nx;
  30. SourceNy=SourceLayer->Ny;
  31. TargetNx=TargetLayer->Nx;
  32. TargetNy=TargetLayer->Ny;
  33. Dout(dc::con, "Connection::Connection " << SourceLayer->Name
  34. << "->"<<TargetLayer->Name);
  35. Dout(dc::con, "Ntarget= " << TargetLayer->N << " Nsource= "
  36. << ns << "TimeStep=" << dt << "ms InputNumber=" << int(InputChannel));
  37. }
  38. Connection::~Connection()
  39. {
  40. Dout(dc::con, "Connection Destructor");
  41. }
  42. bool Connection::Learning()
  43. {
  44. return learn;
  45. }
  46. layer* Connection::GetTargetLayer()
  47. {
  48. return TargetLayer;
  49. }
  50. int Connection::SetAutoSave(bool a)
  51. {
  52. AutoSave = a;
  53. }
  54. void Connection::SetNonSelf(bool value)
  55. {
  56. NonSelf=value;
  57. }
  58. int Connection::SetRewiring(float threshold, float _con, int _nnewcon, float _InitialWeight)
  59. {
  60. RewireOn=true;
  61. RewireMaxConnectivity=_con;
  62. RewireNNewConnections=_nnewcon;
  63. RewireThreshold=threshold;
  64. if (_InitialWeight == -1) {
  65. InitialWeight = 2*RewireThreshold;
  66. } else {
  67. InitialWeight=_InitialWeight;
  68. }
  69. Dout(dc::con, "Set up Rewiring, thres=" << RewireThreshold << " connectivity=" << RewireMaxConnectivity << "");
  70. }
  71. void Connection::SetRewiringOff()
  72. {
  73. RewireOn=false;
  74. }
  75. /** check consistency of file header data. Used on weight file loading
  76. *
  77. *
  78. * @return true if consistent
  79. */
  80. bool Connection::CheckHeaderConsistency()
  81. {
  82. bool consistent=true;
  83. if (float(SourceNx)*float(SourceNy)>ns) {
  84. cerr << "ERROR: float(SourceNx)*float(SourceNy)>ns \n";
  85. consistent=false;
  86. }
  87. if (float(TargetNx)*float(TargetNy)>nt) {
  88. cerr << "ERROR: float(TargetNx)*float(TargetNy)>nt \n";
  89. consistent=false;
  90. }
  91. if (maximumDelay>Dmax) {
  92. cerr << "ERROR: maximumDelay>Dmax \n";
  93. consistent=false;
  94. }
  95. if (SourceLayer) { // Konsistenz-Prüfung, falls connection mit SourceLayer verbunden
  96. Dout(dc::con, "Check consistency with SourceLayer");
  97. if (ns != SourceLayer->N) {
  98. cerr << "ERROR: SourceN doesn't fit\n"; fflush(stderr);
  99. consistent=false;
  100. }
  101. }
  102. if (TargetLayer) { // Konsistenz-Prüfung, falls connection mit TargetLayer verbunden
  103. Dout(dc::con, "Check consistency with TargetLayer");
  104. if (nt != TargetLayer->N) {
  105. cerr << "ERROR: TargetN doesn't fit: nt=" << nt << "\n"; fflush(stderr);
  106. consistent=false;
  107. }
  108. if (maximumDelay > TargetLayer->Dmax) {
  109. cerr << "ERROR: maximumDelay > TargetDmax \n";fflush(stderr);
  110. consistent=false;
  111. }
  112. }
  113. return consistent;
  114. }
  115. ///////////////////////////////////////
  116. connection::connection(): learnobj(0)
  117. {
  118. Name="connection";
  119. // initialize all pointers with 0
  120. post=0;
  121. s=sd=0;
  122. delays_length=0;
  123. delays=0;
  124. N_pre=0;
  125. I_pre=D_pre=0;
  126. s_pre=sd_pre=0;
  127. N_post=0;
  128. }
  129. connection::connection(layer* SL, layer* TL, csimInputChannel _InputChannel, bool _nonself)
  130. : Connection(SL, TL, _InputChannel, _nonself), learnobj(0)
  131. {
  132. Name="connection";
  133. // initialize all pointers with 0
  134. post=0;
  135. s=sd=0;
  136. delays_length=0;
  137. delays=0;
  138. N_pre=0;
  139. I_pre=D_pre=0;
  140. s_pre=sd_pre=0;
  141. N_post=0;
  142. }
  143. connection::~connection()
  144. {
  145. Dout(dc::con, "connection Destructor");
  146. if (learnobj != 0) delete learnobj;
  147. DeleteDynamicalArrays();
  148. DeletePresynapticInfo();
  149. }
  150. int connection::SetLearn(bool l)
  151. {
  152. if (learnobj != 0) learn = l;
  153. }
  154. int connection::SetLearnObj(learning* lo)
  155. {
  156. learnobj = lo;
  157. SetLearn(true);
  158. }
  159. int connection::ObserveSynapse(int SourceNumber, int MNumber, char* RecName)
  160. {
  161. StartRecorder(RecName);
  162. if (SourceNumber < ns) Observe_s = SourceNumber; else Observe_s=0;
  163. if (MNumber < M) Observe_m = MNumber; else Observe_m=0;
  164. }
  165. int connection::ObserveSynapsePrePost(int SourceNumber, int TargetNumber, char* RecName)
  166. {
  167. int TargetM=0;
  168. while((TargetM<M) && post[SourceNumber][TargetM] != TargetNumber) ++TargetM;
  169. if (TargetM<M) {
  170. ObserveSynapse(SourceNumber, TargetM, RecName);
  171. cout << "Observe Synapse: SourceNumber="
  172. << SourceNumber
  173. << " TargetNumber=" << TargetNumber
  174. << " TargetM=" << TargetM << "\n";
  175. }
  176. }
  177. int connection::StartBinRec(int PostsynNumber)
  178. {
  179. int NumPreSyn = N_pre[PostsynNumber];
  180. if (NumPreSyn > 0) {
  181. Dout(dc::con, "\n\nSSSSSSSSSSSSSSSSSSSSNumPreSyn=" << NumPreSyn << "\nSSSSSSSSSSSSS");
  182. float** Buffer = new float* [NumPreSyn];
  183. for (int i=0;i<NumPreSyn; ++i) {
  184. Buffer[i] = s_pre[PostsynNumber][i];
  185. // Dout(dc::con, "i=" << i << " " << *Buffer[i]);
  186. }
  187. string FileName("synweight.dat.bin");
  188. BinRec = new BinRecorder(MacroTimeStep, NumPreSyn, NumPreSyn, Buffer, dt, (DataDirectory+FileName).c_str());
  189. }
  190. }
  191. int connection::WriteSimInfo(fstream &fw)
  192. {
  193. stringstream sstr;
  194. sstr << "<Source id=\"" << SourceLayer->IdNumber << "\"/> \n";
  195. sstr << "<Target id=\"" << TargetLayer->IdNumber << "\"/> \n";
  196. sstr << "<MaxWeight value=\"" << maxWeight << "\"/> \n";
  197. sstr << "<Strength value=\"" << Strength << "\"/> \n";
  198. sstr << "<InputNumber value=\"" << int(InputChannel) << "\"/> \n";
  199. if (learnobj) sstr << learnobj->GetSimInfo();
  200. SimElement::WriteSimInfo(fw, sstr.str());
  201. }
  202. int connection::WriteSimInfo(fstream &fw, const string &ChildInfo)
  203. {
  204. stringstream sstr;
  205. sstr << "<Source id=\"" << SourceLayer->IdNumber << "\"/> \n";
  206. sstr << "<Target id=\"" << TargetLayer->IdNumber << "\"/> \n";
  207. sstr << ChildInfo;
  208. sstr << "<MaxWeight value=\"" << maxWeight << "\"/> \n";
  209. sstr << "<Strength value=\"" << Strength << "\"/> \n";
  210. sstr << "<InputNumber value=\"" << int(InputChannel) << "\"/> \n";
  211. if (learnobj) sstr << learnobj->GetSimInfo();
  212. SimElement::WriteSimInfo(fw, sstr.str());
  213. }
  214. ConnectionInfo connection::GetConnectionInfo()
  215. {
  216. ConnectionInfo info;
  217. info.Dmax = Dmax;
  218. info.maximumDelay = maximumDelay;
  219. info.MaxWeight = maxWeight;
  220. info.TargetLayer = TargetLayer;
  221. info.SourceLayer = SourceLayer;
  222. info.M = M;
  223. info.WeightPointer = s;
  224. info.WeightDerivativePointer = sd;
  225. info.delays_length = delays_length;
  226. info.delays = delays;
  227. info.post = post;
  228. info.maxN_pre = maxN_pre;
  229. info.I_pre = I_pre;
  230. info.N_pre = N_pre;
  231. info.s_pre = s_pre;
  232. info.sd_pre = sd_pre;
  233. info.D_pre = D_pre;
  234. info.m_pre = m_pre;
  235. return info;
  236. }
  237. int connection::InitializeDynamicalArrays(const int _N, const int _M, const int _Dmax)
  238. {
  239. // initialize dynamical arrays
  240. cout<<"initialize dynamical begins";
  241. MainSimLoop->SetMaximumDelay(_Dmax);
  242. // indeces of postsynaptic neurons
  243. NewArray2d(post, _N, _M);
  244. for (int i=0;i<_N;++i) for (int j=0;j<_M;++j) post[i][j]=-1; // initialize with -1 (not connected)
  245. // matrix of synaptic weights
  246. NewArray2d(s, _N,_M);
  247. // NewArray2d(sd,_N,_M); weight derivatives, not used yet
  248. NewArray2d(delays_length, _N, _Dmax); // distribution of delays
  249. // initialize with zero
  250. for (int i=0;i<_N;++i) for (int j=0;j<_Dmax;++j) delays_length[i][j] = 0;
  251. NewArray3d(delays, _N, _Dmax, _M); // arrangement of delays
  252. Dout(dc::con,"initialize dynamical ends");
  253. }
  254. int connection::DeleteDynamicalArrays()
  255. {
  256. if(post!=0) DeleteArray2d(post,ns);
  257. if(s!=0) DeleteArray2d(s,ns);
  258. if(sd!=0) DeleteArray2d(sd,ns);
  259. if(delays_length!=0) DeleteArray2d(delays_length,ns);
  260. if(delays!=0) DeleteArray3d(delays,ns,maximumDelay);
  261. }
  262. int connection::ConnectRandom(float c, float InitialWeights, float maxDelay, float minDelay, bool RandomDelays)
  263. {
  264. Strength=InitialWeights;
  265. maxWeight=1;
  266. connectivity=c;
  267. nt = TargetLayer->N;
  268. // float TAULEARN_pre=50;
  269. // dec_pre = exp(-1./TAULEARN_pre);
  270. // float TAULEARN_post=5;
  271. // dec_pre = exp(-1./TAULEARN_post);
  272. int N;
  273. ns = N = SourceLayer->N;
  274. if (ns != nt) NonSelf = false; // NonSelf makes sense only for layers with same size
  275. M= (int) floor(c*nt);
  276. maxN_pre = 3*ns*M/nt;
  277. if (maxN_pre >= ns) maxN_pre = ns; // because there are no double synapses
  278. Dout(dc::con, "M= " << M << " c= " << c << " ns= " << ns << "maxN_pre=" << maxN_pre);
  279. if (M>nt) Dout(dc::con, "ERROR: too many connections, reduce connectivity!!");
  280. SetMinMaxDelay(maxDelay, minDelay);
  281. // initialize dynamical arrays
  282. InitializeDynamicalArrays(N, M, maximumDelay);
  283. int i,j,k,exists, r;
  284. Dout(dc::con, "connect");
  285. for (i=0;i<ns;i++) for (j=0;j<M;j++)
  286. {
  287. do {
  288. exists = 0; // avoid multiple synapses
  289. r = getrandom(nt);
  290. if (r==i) exists=1; // no self-synapses
  291. for (k=0;k<j;k++) if (post[i][k]==r) exists = 1; // synapse already exists
  292. } while (exists == 1);
  293. post[i][j]=r;
  294. }
  295. Dout(dc::con, "connected");
  296. for (i=0;i<ns;i++) for (j=0;j<M;j++) s[i][j]=InitialWeights; // initial exc. synaptic weights
  297. if (RandomDelays) {
  298. SetupRandomDelays();
  299. } else {
  300. SetupDelays();
  301. }
  302. SetupPresynapticInfo();
  303. // for (i=0;i<ns;i++) for (j=0;j<1001+Dmax;j++) LTP[i][j]=0.0;
  304. // for (i=0;i<nt;i++) LTD[i]=0.0;
  305. // learnobj = new learning(this);
  306. Dout(dc::con, "Connection initialized");
  307. }
  308. // ueberladener Funktionsname ConnectRandom2 mit unterschiedlichen Parameter-Typen
  309. // float _Connectivity vs. int NIncommingConnections
  310. int connection::ConnectRandom2(float _Connectivity, float InitialWeights, float maxDelay, float minDelay, bool RandomDelays)
  311. {
  312. if (_Connectivity <= 1) {
  313. ns = SourceLayer->N;
  314. int NConnections = int(round(float(ns)*_Connectivity));
  315. ConnectRandom2(NConnections, InitialWeights, maxDelay, minDelay, RandomDelays);
  316. } else {
  317. cerr << "ERROR in connection::ConnectRandom2: _Connectivity should be [0..1]\n";
  318. }
  319. }
  320. int connection::ConnectRandom2(int NIncommingConnections, float InitialWeights, float maxDelay, float minDelay, bool RandomDelays)
  321. {
  322. // random connections, but connectivity from postsynaptic perspective
  323. // every postsynaptic neuron has the same number of incomming connections
  324. // source neurons have nearly the same number of outgoing connections (plusminus 1)
  325. Dout(dc::con, " connection::ConnectRandom2");
  326. Strength=InitialWeights;
  327. maxWeight=1;
  328. nt = TargetLayer->N;
  329. int N;
  330. ns = N = SourceLayer->N;
  331. if (ns != nt) NonSelf = false; // NonSelf makes sense only for layers with same size
  332. int NConnections = NIncommingConnections*nt;
  333. connectivity = float(NIncommingConnections)/ns;
  334. float c = connectivity; // remove this
  335. M= (int) ceil(float(NConnections)/ns);
  336. bool MultipleSynapses=false;
  337. maxN_pre = NIncommingConnections;
  338. if (maxN_pre >= ns) {
  339. cerr << "ERROR in connection::ConnectRandom2: more incomming connections than source neurons\n";
  340. MultipleSynapses = true;
  341. }
  342. Dout(dc::con, "M= " << M << " c= " << connectivity << " ns= " << ns << "maxN_pre=" << maxN_pre);
  343. Dout(dc::con, "NConnections=" << NConnections << "nt=" << nt << "ns=" << ns << "NIncommingConnections=" << NIncommingConnections << "");
  344. if (M>nt) cerr << "ERROR: too many connections, reduce connectivity!!\n";
  345. SetMinMaxDelay(maxDelay, minDelay);
  346. // initialize dynamical arrays
  347. InitializeDynamicalArrays(N, M, maximumDelay);
  348. for (int i=0;i<N;++i) for (int j=0;j<M;++j) post[i][j]=-1;
  349. // if post is -1 then this is not a valid connection
  350. int i,j,k,exists, r;
  351. vector<vector <int> > TmpPreI(nt);
  352. vector<int> TmpNPost(ns);
  353. vector<int> ConnectionPool(ns*M);
  354. for (i=0;i<ns;++i) for (j=0;j<M;++j) ConnectionPool[j+M*i]=i;
  355. Dout(dc::con, "connect");
  356. int MaxReTry=5;
  357. int isource;
  358. for (int MPre=0;MPre<maxN_pre;++MPre) for (int itarget=0;itarget<nt;++itarget)
  359. {
  360. int Try=0;
  361. do {
  362. exists = 0; // avoid multiple synapses
  363. r = gsl_rng_uniform_int(gslr, ConnectionPool.size());
  364. isource = ConnectionPool[r];
  365. if ((isource==itarget) && NonSelf) exists=1; // no self-synapses
  366. for (vector<int>::iterator it=TmpPreI[itarget].begin();it!=TmpPreI[itarget].end();++it) {
  367. if ((*it)==isource) {
  368. exists = 1; // synapse already exists
  369. // cerr << "synapse allready exists\n";
  370. }
  371. }
  372. ++Try;
  373. } while ((exists == 1) && (Try<MaxReTry));
  374. if (Try == MaxReTry) cerr << "ERROR: multiple synapses\n";
  375. post[isource][TmpNPost[isource]]=itarget;
  376. ++TmpNPost[isource];
  377. TmpPreI[itarget].push_back(isource);
  378. ConnectionPool.erase(ConnectionPool.begin()+r);
  379. }
  380. Dout(dc::con, "connected");
  381. for (i=0;i<ns;i++) for (j=0;j<M;j++) s[i][j]=InitialWeights; // initial exc. synaptic weights
  382. SetupRandomDelays();
  383. SetupPresynapticInfo();
  384. Dout(dc::con, "Connection initialized");
  385. }
  386. int connection::ConnectPartialRandom(float c, float InitialWeights, int _MaxTarget, int _MaxSource, float maxDelay, float minDelay)
  387. {
  388. Strength=InitialWeights;
  389. Dout(dc::con, "ConnectPartialRandom");
  390. // maxWeight=1;
  391. connectivity=c;
  392. nt = TargetLayer->N;
  393. // float TAULEARN_pre=50;
  394. // dec_pre = exp(-1./TAULEARN_pre);
  395. // float TAULEARN_post=5;
  396. // dec_pre = exp(-1./TAULEARN_post);
  397. int N;
  398. ns = N = SourceLayer->N;
  399. if (ns != nt) NonSelf = false; // NonSelf makes sense only for layers with same size
  400. int MaxTarget = (_MaxTarget<nt) ? _MaxTarget : nt;
  401. int MaxSource = (_MaxSource<ns) ? _MaxSource: ns;
  402. M= (int) floor(c*MaxTarget);
  403. maxN_pre = 3*ns*M/MaxTarget;
  404. if (maxN_pre >= ns) maxN_pre = ns; // because there are no double synapses
  405. Dout(dc::con, "M= " << M << " c= " << c << " ns= " << ns << "maxN_pre=" << maxN_pre);
  406. if (M>nt) Dout(dc::con, "ERROR: too many connections, reduce connectivity!!");
  407. SetMinMaxDelay(maxDelay, minDelay);
  408. // initialize dynamical arrays
  409. InitializeDynamicalArrays(N, M, maximumDelay);
  410. int i,j,k,exists, r;
  411. Dout(dc::con, "connect");
  412. for (i=0;i<ns;i++) for (j=0;j<M;j++)
  413. {
  414. do {
  415. exists = 0; // avoid multiple synapses
  416. r = getrandom(MaxTarget);
  417. if (r==i) exists=1; // no self-synapses
  418. for (k=0;k<j;k++) if (post[i][k]==r) exists = 1; // synapse already exists
  419. } while (exists == 1);
  420. post[i][j]=r;
  421. }
  422. Dout(dc::con, "connected");
  423. for (i=0;i<ns;i++) for (j=0;j<M;j++) s[i][j]=InitialWeights; // initial exc. synaptic weights
  424. SetupDelays();
  425. SetupPresynapticInfo();
  426. // for (i=0;i<ns;i++) for (j=0;j<1001+Dmax;j++) LTP[i][j]=0.0;
  427. // for (i=0;i<nt;i++) LTD[i]=0.0;
  428. // learnobj = new learning(this);
  429. Dout(dc::con, "Connection initialized");
  430. }
  431. int connection::ConnectGaussian(float Sigma, float MaxWeight, float maxDelay, float minDelay, bool Cyclic)
  432. {
  433. Strength=MaxWeight;
  434. Dout(dc::con, "Connection::ConnectGaussian");
  435. nt = TargetLayer->N;
  436. int ConnectionCount=0;
  437. int N;
  438. ns = N = SourceLayer->N;
  439. if (ns != nt) NonSelf = false; // NonSelf makes sense only for layers with same size
  440. int i,j,k,exists, r;
  441. // Dout(dc::con, "connect");
  442. // double loop (ns, nt) goes throug every possible synapse and checks wether to connect or not
  443. int TargetCount;
  444. M=0;
  445. // typedef boost::multi_array<int, 2> int2d_array;
  446. // typedef int2d_array::index index;
  447. boost::multi_array<int, 2> TempPost(boost::extents[ns][nt]);
  448. boost::multi_array<float, 2> TempDistance(boost::extents[ns][nt]);
  449. for (i=0;i<ns;++i) for (j=0;j<nt;++j)
  450. {
  451. TempPost[i][j]=-1;
  452. TempDistance[i][j]=-1;
  453. }
  454. vector<int> TempNpre (nt);
  455. for (i=0;i<nt;++i) TempNpre[i]=0;
  456. float InitialWeights=1;
  457. int CurDelay;
  458. SetMinMaxDelay(maxDelay, minDelay);
  459. maxN_pre = 0;
  460. float Distance;
  461. vector2d basis(1,1);
  462. SimpleTextProgressBar pgbar(ns);
  463. float MaxConDistance = 3*Sigma;
  464. for (i=0;i<ns;i++) {
  465. pgbar.Next(i);
  466. TargetCount=0;
  467. for (j=0;j<nt;j++)
  468. {
  469. // set connection in temporary boost multi_array
  470. if (Cyclic) Distance = (TargetLayer->Pos[j]).CyclicDistance(SourceLayer->Pos[i], basis);
  471. else Distance = (TargetLayer->Pos[j] - SourceLayer->Pos[i]).abs() ;
  472. if ((Distance < MaxConDistance) && (!NonSelf || (i!=j))) {
  473. TempDistance[i][TargetCount] = Distance;
  474. TempPost[i][TargetCount]=j;
  475. ++TempNpre[j]; // count presynaptic Targeting of postsynaptic neuron
  476. ++TargetCount;
  477. ++ConnectionCount;
  478. }
  479. }
  480. if (TargetCount > M) M=TargetCount;
  481. }
  482. for (j=0;j<nt;++j) if (TempNpre[j] > maxN_pre) maxN_pre = TempNpre[j];
  483. Dout(dc::con, "M= " << M << " ns= " << ns << "maxN_pre=" << maxN_pre);
  484. Dout(dc::con, "ConnectionCount=" << ConnectionCount);
  485. // initialize dynamical arrays
  486. InitializeDynamicalArrays(N, M, maximumDelay);
  487. // copy temporary TempPost to post array
  488. for (i=0;i<ns;++i) for (j=0;j<M;++j) post[i][j] = TempPost[i][j];
  489. // setup delays and initial weights
  490. pgbar.Reset(ns);
  491. for (i=0;i<ns;i++) {
  492. pgbar.Next(i);
  493. for (j=0;j<M;j++)
  494. {
  495. if (post[i][j] != -1) {
  496. s[i][j] = MaxWeight*gauss(TempDistance[i][j],Sigma);
  497. // CurDelay = minimumDelay + gsl_rng_uniform_int(gslr, DelayDiff+1);
  498. CurDelay = minimumDelay + int(TempDistance[i][j]*DelayDiff/MaxConDistance); // ToDo: FixMe!!!!!!!!!!!!!!!!!!! think a lot!!!
  499. if (CurDelay>=maximumDelay)
  500. {
  501. CurDelay = maximumDelay;
  502. Dout(dc::con, "ERROR: delay too high");
  503. }
  504. delays[i][CurDelay][delays_length[i][CurDelay]] = j;
  505. ++delays_length[i][CurDelay];
  506. }
  507. }
  508. }
  509. Dout(dc::con, "connected");
  510. SetupPresynapticInfo();
  511. Dout(dc::con, "Connection initialized");
  512. }
  513. int connection::ConnectGaussianColumnwise(float Sigma, float MaxWeight, int dimx, int dimy, bool Cyclic,bool Shifted, float maxDelay, float minDelay,bool divergent,bool notstraight)
  514. { Strength=MaxWeight;
  515. int shiftitornot=0;
  516. if (Shifted==true) shiftitornot=1;
  517. Dout(dc::con, "Connection::ConnectGaussianColumnwise");
  518. nt = TargetLayer->N;
  519. int ConnectionCount=0;
  520. int N;
  521. ns = N = SourceLayer->N;
  522. if (ns != nt) NonSelf = false; // NonSelf makes sense only for layers with same size
  523. float ntns;
  524. if (divergent = true) ntns=(nt/dimy)/ns;
  525. else ntns=1;
  526. float dimxdimy=dimx/dimy;
  527. int i,x,y,j,exists, r;
  528. // Dout(dc::con, "connect"); fflush(stdout);
  529. // double loop (ns, nt) goes throug every possible synapse and checks wether to connect or not
  530. int TargetCount;
  531. M=0;
  532. // typedef boost::multi_array<int, 2> int2d_array;
  533. // typedef int2d_array::index index;
  534. boost::multi_array<int, 2> TempPost(boost::extents[ns][nt]);
  535. boost::multi_array<float, 2> TempDistance(boost::extents[ns][nt]);
  536. for (i=0;i<ns;++i) for (j=0;j<nt;++j)
  537. {
  538. TempPost[i][j]=-1;
  539. TempDistance[i][j]=-1;
  540. }
  541. vector<int> TempNpre (nt);
  542. for (i=0;i<nt;++i) TempNpre[i]=0;
  543. float InitialWeights=1;
  544. int CurDelay;
  545. SetMinMaxDelay(maxDelay, minDelay);
  546. maxN_pre = 0;
  547. float Distance;
  548. vector2d basis(1,1);
  549. SimpleTextProgressBar pgbar(ns);
  550. float MaxConDistance = 3*Sigma;
  551. float yshift=0.;
  552. for (i=0;i<ns;i++)
  553. {
  554. pgbar.Next(i);
  555. TargetCount=0;
  556. for (y=0;y<dimy;y++)
  557. {
  558. for (x=0;x<dimx;x++)
  559. { int synapse_number=x+y*dimx;
  560. // set connection in temporary boost multi_array
  561. yshift=y*(dimxdimy);
  562. int Dist_help;
  563. if (notstraight) Dist_help=int(1000*(x+(yshift)*shiftitornot));
  564. else Dist_help=int(1000*((yshift)*shiftitornot));
  565. int dimx_help=1000*dimx;
  566. if (Cyclic) Distance =(TargetLayer->Pos[(Dist_help%dimx_help)/1000].CyclicDistance(SourceLayer->Pos[i], basis));
  567. else Distance = (TargetLayer->Pos[x] - SourceLayer->Pos[i]).abs();
  568. if ((Distance < MaxConDistance) && (!NonSelf || (i!=synapse_number)))
  569. {
  570. TempDistance[i][TargetCount] = Distance;
  571. TempPost[i][TargetCount]=synapse_number;
  572. // TempPost2[i][TargetCount][0]=k;
  573. // TempPost2[i][TargetCount][1]=j;
  574. ++TempNpre[synapse_number]; // count presynaptic Targeting of postsynaptic neuron
  575. ++TargetCount;
  576. ++ConnectionCount;
  577. }
  578. }
  579. }
  580. if (TargetCount > M) M=TargetCount;
  581. }
  582. for (j=0;j<nt;++j) if (TempNpre[j] > maxN_pre) maxN_pre = TempNpre[j];
  583. Dout(dc::con, "M= " << M << " ns= " << ns << "maxN_pre=" << maxN_pre);
  584. Dout(dc::con, "ConnectionCount=" << ConnectionCount);
  585. // initialize dynamical arrays
  586. InitializeDynamicalArrays(N, M, maximumDelay);
  587. // copy temporary TempPost to post array
  588. for (i=0;i<ns;++i) for (j=0;j<M;++j) post[i][j] = TempPost[i][j];
  589. // setup delays and initial weights
  590. pgbar.Reset(ns);
  591. for (i=0;i<ns;i++) {
  592. pgbar.Next(i);
  593. for (y=0;y<dimy;y++)
  594. {
  595. for (x=0;x<dimx;x++)
  596. { int synapse_number=x+y*dimx;
  597. if ((post[i][synapse_number] != -1) && (synapse_number < M)){
  598. s[i][synapse_number]=MaxWeight*gauss(TempDistance[i][synapse_number],Sigma);
  599. // CurDelay = minimumDelay + gsl_rng_uniform_int(gslr, DelayDiff+1);
  600. CurDelay = minimumDelay + int(TempDistance[i][synapse_number]*DelayDiff/MaxConDistance); // ToDo: FixMe!!!!!!!!!!!!!!!!!!! think a lot!!!
  601. if (CurDelay>=maximumDelay)
  602. {
  603. CurDelay = maximumDelay;
  604. cerr << "ERROR: delay too high\n";
  605. }
  606. delays[i][CurDelay][delays_length[i][CurDelay]] = synapse_number;
  607. ++delays_length[i][CurDelay];
  608. }
  609. }
  610. }
  611. }
  612. Dout(dc::con, "connected");
  613. SetupPresynapticInfo();
  614. Dout(dc::con, "Connection initialized");
  615. }
  616. int connection::ConnectIdenticalGaussian(float Sigma, float MaxWeight, float maxDelay, float minDelay, float mpx, float mpy)
  617. {
  618. Strength=MaxWeight;
  619. Dout(dc::con, "Connection::ConnectIdenticalGaussian");
  620. nt = TargetLayer->N;
  621. int ConnectionCount=0;
  622. int N;
  623. ns = N = SourceLayer->N;
  624. if (ns != nt) NonSelf = false; // NonSelf makes sense only for layers with same size
  625. int i,j,k,exists, r;
  626. // Dout(dc::con, "connect"); fflush(stdout);
  627. // double loop (ns, nt) goes throug every possible synapse and checks wether to connect or not
  628. int TargetCount;
  629. M=0;
  630. // typedef boost::multi_array<int, 2> int2d_array;
  631. // typedef int2d_array::index index;
  632. boost::multi_array<int, 2> TempPost(boost::extents[ns][nt]);
  633. boost::multi_array<float, 2> TempDistance(boost::extents[ns][nt]);
  634. for (i=0;i<ns;++i) for (j=0;j<nt;++j)
  635. {
  636. TempPost[i][j]=-1;
  637. TempDistance[i][j]=-1;
  638. }
  639. vector<int> TempNpre (nt);
  640. for (i=0;i<nt;++i) TempNpre[i]=0;
  641. float InitialWeights=1;
  642. int CurDelay;
  643. SetMinMaxDelay(maxDelay, minDelay);
  644. maxN_pre = 0;
  645. float Distance;
  646. vector2d basis(1,1);
  647. vector2d ConstMP(mpx, mpy);
  648. SimpleTextProgressBar pgbar(ns);
  649. float MaxConDistance = 3*Sigma;
  650. for (i=0;i<ns;i++) {
  651. pgbar.Next(i);
  652. TargetCount=0;
  653. Distance = (SourceLayer->Pos[i] - ConstMP).abs() ;
  654. for (j=0;j<nt;j++)
  655. {
  656. // set connection in temporary boost multi_array
  657. // Distance = (TargetLayer->Pos[j]).CyclicDistance(SourceLayer->Pos[i], basis);
  658. if ((Distance < MaxConDistance) && (!NonSelf || (i!=j))) {
  659. TempDistance[i][TargetCount] = Distance;
  660. TempPost[i][TargetCount]=j;
  661. ++TempNpre[j]; // count presynaptic Targeting of postsynaptic neuron
  662. ++TargetCount;
  663. ++ConnectionCount;
  664. }
  665. }
  666. if (TargetCount > M) M=TargetCount;
  667. }
  668. for (j=0;j<nt;++j) if (TempNpre[j] > maxN_pre) maxN_pre = TempNpre[j];
  669. Dout(dc::con, "M= " << M << " ns= " << ns << "maxN_pre=" << maxN_pre);
  670. Dout(dc::con, "ConnectionCount=" << ConnectionCount);
  671. // initialize dynamical arrays
  672. InitializeDynamicalArrays(N, M, maximumDelay);
  673. // copy temporary TempPost to post array
  674. for (i=0;i<ns;++i) for (j=0;j<M;++j) post[i][j] = TempPost[i][j];
  675. // setup delays and initial weights
  676. pgbar.Reset(ns);
  677. for (i=0;i<ns;i++) {
  678. pgbar.Next(i);
  679. for (j=0;j<M;j++)
  680. {
  681. if (post[i][j] != -1) {
  682. s[i][j] = MaxWeight*gauss(TempDistance[i][j],Sigma);
  683. // CurDelay = minimumDelay + gsl_rng_uniform_int(gslr, DelayDiff+1);
  684. CurDelay = minimumDelay + int(TempDistance[i][j]*DelayDiff/MaxConDistance); // ToDo: FixMe!!!!!!!!!!!!!!!!!!! think a lot!!!
  685. if (CurDelay>=maximumDelay)
  686. {
  687. CurDelay = maximumDelay;
  688. Dout(dc::con, "ERROR: delay too high");
  689. }
  690. delays[i][CurDelay][delays_length[i][CurDelay]] = j;
  691. ++delays_length[i][CurDelay];
  692. }
  693. }
  694. }
  695. Dout(dc::con, "connected");
  696. SetupPresynapticInfo();
  697. Dout(dc::con, "Connection initialized");
  698. }
  699. // Connect according to a profile function that is defined on distance
  700. int connection::ConnectProfile(DistanceProfile* profile, float MaxWeight, float maxDelay, float minDelay, bool Cyclic)
  701. {
  702. Strength=MaxWeight;
  703. Dout(dc::con, "Connection::ConnectProfile");
  704. nt = TargetLayer->N;
  705. int ConnectionCount=0;
  706. int N;
  707. ns = N = SourceLayer->N;
  708. if (ns != nt) NonSelf = false; // NonSelf makes sense only for layers with same size
  709. int i,j,k,exists, r;
  710. // Dout(dc::con, "connect"); fflush(stdout);
  711. // double loop (ns, nt) goes throug every possible synapse and checks wether to connect or not
  712. int TargetCount;
  713. M=0;
  714. // typedef boost::multi_array<int, 2> int2d_array;
  715. // typedef int2d_array::index index;
  716. Dout(dc::con, "MakeFirstTempArray, ns=" << ns << "nt=" << nt);
  717. // boost::multi_array<int, 2> TempPost(boost::extents[ns][nt]);
  718. int** TempPost;
  719. NewArray2d(TempPost, ns, nt);
  720. Dout(dc::con, "MakeSecondTempArray");
  721. boost::multi_array<float, 2> TempDistance(boost::extents[ns][nt]);
  722. for (i=0;i<ns;++i) for (j=0;j<nt;++j)
  723. {
  724. TempPost[i][j]=-1;
  725. TempDistance[i][j]=-1;
  726. }
  727. vector<int> TempNpre (nt);
  728. for (i=0;i<nt;++i) TempNpre[i]=0;
  729. float InitialWeights=1;
  730. int CurDelay;
  731. SetMinMaxDelay(maxDelay, minDelay);
  732. maxN_pre = 0;
  733. float Distance;
  734. vector2d basis(1,1);
  735. SimpleTextProgressBar pgbar(ns);
  736. Dout(dc::con, "Getmaxdist");fflush(stdout);
  737. float MaxConDistance = profile->GetMaxConDistance(0.05);
  738. float ConValue;
  739. for (i=0;i<ns;i++) {
  740. pgbar.Next(i);
  741. TargetCount=0;
  742. for (j=0;j<nt;j++)
  743. {
  744. // set Distance in temporary boost multi_array
  745. if (Cyclic) Distance = (TargetLayer->Pos[j]).CyclicDistance(SourceLayer->Pos[i], basis);
  746. else Distance = (TargetLayer->Pos[j] - SourceLayer->Pos[i]).abs() ;
  747. if ((Distance < MaxConDistance) && (!NonSelf || (i!=j))) {
  748. TempDistance[i][TargetCount] = Distance;
  749. TempPost[i][TargetCount]=j;
  750. ++TempNpre[j]; // count presynaptic Targeting of postsynaptic neuron
  751. ++TargetCount;
  752. ++ConnectionCount;
  753. }
  754. }
  755. if (TargetCount > M) M=TargetCount;
  756. }
  757. for (j=0;j<nt;++j) if (TempNpre[j] > maxN_pre) maxN_pre = TempNpre[j];
  758. Dout(dc::con, "M= " << M << " ns= " << ns << "maxN_pre=" << maxN_pre << ""); fflush(stdout);
  759. Dout(dc::con, "ConnectionCount=" << ConnectionCount << "");
  760. // initialize dynamical arrays
  761. InitializeDynamicalArrays(N, M, maximumDelay);
  762. // copy temporary TempPost to post array
  763. for (i=0;i<ns;++i) for (j=0;j<M;++j) post[i][j] = TempPost[i][j];
  764. // setup delays and initial weights
  765. pgbar.Reset(ns);
  766. for (i=0;i<ns;i++) {
  767. pgbar.Next(i);
  768. for (j=0;j<M;j++)
  769. {
  770. if (post[i][j] != -1) {
  771. s[i][j] = MaxWeight*profile->GetValue(TempDistance[i][j]);
  772. // CurDelay = minimumDelay + gsl_rng_uniform_int(gslr, DelayDiff+1);
  773. CurDelay = minimumDelay + int(TempDistance[i][j]*DelayDiff/MaxConDistance); // ToDo: FixMe!!!!!!!!!!!!!!!!!!! think a lot!!!
  774. if (CurDelay>=maximumDelay)
  775. {
  776. CurDelay = maximumDelay-1;
  777. Dout(dc::con, "ERROR: delay too high");
  778. }
  779. delays[i][CurDelay][delays_length[i][CurDelay]] = j;
  780. ++delays_length[i][CurDelay];
  781. }
  782. }
  783. }
  784. Dout(dc::con, "connected"); fflush(stdout);
  785. SetupPresynapticInfo();
  786. Dout(dc::con, "Connection initialized");
  787. }
  788. /////////
  789. // Connect with given Weight matrix
  790. int connection::ConnectMatrix(const vector<vector<float> > &WeightMatrix, float maxDelay, float minDelay)
  791. {
  792. // Strength=MaxWeight;
  793. Dout(dc::con, "Connection::ConnectMatrix");
  794. nt = TargetLayer->N;
  795. int ConnectionCount=0;
  796. int N;
  797. ns = N = SourceLayer->N;
  798. if (ns != nt) NonSelf = false; // NonSelf makes sense only for layers with same size
  799. int i,j,k,exists, r;
  800. // Dout(dc::con, "connect"); fflush(stdout);
  801. // double loop (ns, nt) goes throug every possible synapse and checks wether to connect or not
  802. int TargetCount;
  803. M=0;
  804. // typedef boost::multi_array<int, 2> int2d_array;
  805. // typedef int2d_array::index index;
  806. Dout(dc::con, "MakeFirstTempArray, ns=" << ns << "nt=" << nt << "");fflush(stdout);
  807. // boost::multi_array<int, 2> TempPost(boost::extents[ns][nt]);
  808. int** TempPost;
  809. NewArray2d(TempPost, ns, nt);
  810. Dout(dc::con, "MakeSecondTempArray");fflush(stdout);
  811. boost::multi_array<float, 2> TempDistance(boost::extents[ns][nt]);
  812. for (i=0;i<ns;++i) for (j=0;j<nt;++j)
  813. {
  814. TempPost[i][j]=-1;
  815. }
  816. vector<int> TempNpre (nt);
  817. for (i=0;i<nt;++i) TempNpre[i]=0;
  818. float InitialWeights=1;
  819. int CurDelay;
  820. SetMinMaxDelay(maxDelay, minDelay);
  821. // ToDo: care for the delays
  822. maxN_pre = 0;
  823. float Distance;
  824. vector2d basis(1,1);
  825. SimpleTextProgressBar pgbar(ns);
  826. float ConValue;
  827. for (i=0;i<ns;i++) {
  828. pgbar.Next(i);
  829. TargetCount=0;
  830. for (j=0;j<nt;j++)
  831. {
  832. //Dout(dc::con, "i=" << i << " j=" << j <<" wm[i][j]=" << WeightMatrix[i][j] << ""); fflush(stdout);
  833. if (WeightMatrix[i][j] != 0) {
  834. TempPost[i][TargetCount]=j;
  835. ++TempNpre[j]; // count presynaptic Targeting of postsynaptic neuron
  836. ++TargetCount;
  837. ++ConnectionCount;
  838. }
  839. }
  840. if (TargetCount > M) M=TargetCount;
  841. }
  842. for (j=0;j<nt;++j) if (TempNpre[j] > maxN_pre) maxN_pre = TempNpre[j];
  843. Dout(dc::con, "M= " << M << " ns= " << ns << "maxN_pre=" << maxN_pre << ""); fflush(stdout);
  844. Dout(dc::con, "ConnectionCount=" << ConnectionCount << "");
  845. // initialize dynamical arrays
  846. InitializeDynamicalArrays(N, M, maximumDelay);
  847. // copy temporary TempPost to post array
  848. for (i=0;i<ns;++i) for (j=0;j<M;++j) post[i][j] = TempPost[i][j];
  849. // setup delays and initial weights
  850. pgbar.Reset(ns);
  851. for (i=0;i<ns;i++) {
  852. pgbar.Next(i);
  853. for (j=0;j<M;j++)
  854. {
  855. if (post[i][j] != -1) {
  856. // s[i][j] = MaxWeight; // ?? TestIt: = WeightMatrix[i][post[i][j]]
  857. s[i][j] = WeightMatrix[i][post[i][j]];
  858. CurDelay = minimumDelay + gsl_rng_uniform_int(gslr, DelayDiff+1);
  859. if (CurDelay>=maximumDelay)
  860. {
  861. CurDelay = maximumDelay-1;
  862. Dout(dc::con, "ERROR: delay too high");
  863. }
  864. delays[i][CurDelay][delays_length[i][CurDelay]] = j;
  865. ++delays_length[i][CurDelay];
  866. }
  867. }
  868. }
  869. Dout(dc::con, "connected"); fflush(stdout);
  870. SetupPresynapticInfo();
  871. Dout(dc::con, "Connection initialized");
  872. }
  873. //////////
  874. // circular connections, const number of postsynaptic connections
  875. int connection::ConnectCircular(float PreConnectivity, float MaxWeight, float maxDelay, float minDelay)
  876. {
  877. Strength=MaxWeight;
  878. Dout(dc::con, "Connection::ConnectProfile");
  879. nt = TargetLayer->N;
  880. if (PreConnectivity > 1) PreConnectivity=1;
  881. if (PreConnectivity <0) Dout(dc::con, "connection::ConnectCircular: ERROR: PreConnectivity shouldn't be zero!!");
  882. int NTargetConnections = int(PreConnectivity*nt);
  883. int ConnectionCount=0;
  884. int N;
  885. ns = N = SourceLayer->N;
  886. if (ns != nt) NonSelf = false; // NonSelf makes sense only for layers with same size
  887. int i,j,k,exists, r;
  888. // Dout(dc::con, "connect"); fflush(stdout);
  889. // double loop (ns, nt) goes throug every possible synapse and checks wether to connect or not
  890. int TargetCount;
  891. M=0;
  892. // typedef boost::multi_array<int, 2> int2d_array;
  893. // typedef int2d_array::index index;
  894. Dout(dc::con, "MakeFirstTempArray, ns=" << ns << "nt=" << nt << "");fflush(stdout);
  895. // boost::multi_array<int, 2> TempPost(boost::extents[ns][nt]);
  896. int** TempPost;
  897. NewArray2d(TempPost, ns, nt);
  898. Dout(dc::con, "MakeSecondTempArray");fflush(stdout);
  899. boost::multi_array<float, 2> TempDistance(boost::extents[ns][nt]);
  900. for (i=0;i<ns;++i) for (j=0;j<nt;++j)
  901. {
  902. TempPost[i][j]=-1;
  903. TempDistance[i][j]=-1;
  904. }
  905. vector<int> TempNpre (nt);
  906. for (i=0;i<nt;++i) TempNpre[i]=0;
  907. float InitialWeights=1;
  908. int CurDelay;
  909. float MaxConDistance =0;
  910. SetMinMaxDelay(maxDelay, minDelay);
  911. maxN_pre = 0;
  912. float Distance;
  913. vector2d basis(1,1);
  914. SimpleTextProgressBar pgbar(ns);
  915. Dout(dc::con, "Getmaxdist");fflush(stdout);
  916. float ConValue;
  917. // for each presynaptic neuron calc a sorted Distance List, and then connect until NTargetConnections is reached
  918. // type map<double,int> DistMap;
  919. map<double,int> CurPostDistances;
  920. map<double,int>::iterator CurDist=CurPostDistances.begin();
  921. for (i=0;i<ns;i++) {
  922. pgbar.Next(i);
  923. TargetCount=0;
  924. CurPostDistances.clear();
  925. for (j=0;j<nt;j++)
  926. {
  927. // set Distance in temporary boost multi_array
  928. Distance = (TargetLayer->Pos[j] - SourceLayer->Pos[i]).abs() ;
  929. CurPostDistances[Distance+0.000001*j] = j; //add minimal value to Distance, because each Distance must be unique (map has no duplicate keys)
  930. }
  931. CurDist=CurPostDistances.begin();
  932. while((TargetCount<NTargetConnections) && (CurDist!=CurPostDistances.end()))
  933. {
  934. j=CurDist->second;
  935. if (!NonSelf || (i!=j))
  936. {
  937. TempDistance[i][TargetCount] = CurDist->first;
  938. TempPost[i][TargetCount]=j;
  939. ++TempNpre[j]; // count presynaptic Targeting of postsynaptic neuron
  940. ++TargetCount;
  941. ++ConnectionCount;
  942. if (CurDist->first > MaxConDistance) MaxConDistance=CurDist->first;
  943. }
  944. ++CurDist;
  945. }
  946. if (TargetCount > M) M=TargetCount;
  947. }
  948. for (j=0;j<nt;++j) if (TempNpre[j] > maxN_pre) maxN_pre = TempNpre[j];
  949. Dout(dc::con, "M= " << M << " ns= " << ns << "maxN_pre=" << maxN_pre << ""); fflush(stdout);
  950. Dout(dc::con, "ConnectionCount=" << ConnectionCount << "");
  951. // initialize dynamical arrays
  952. InitializeDynamicalArrays(N, M, maximumDelay);
  953. // copy temporary TempPost to post array
  954. for (i=0;i<ns;++i) for (j=0;j<M;++j) post[i][j] = TempPost[i][j];
  955. // setup delays and initial weights
  956. pgbar.Reset(ns);
  957. for (i=0;i<ns;i++) {
  958. pgbar.Next(i);
  959. for (j=0;j<M;j++)
  960. {
  961. if (post[i][j] != -1) {
  962. s[i][j] = MaxWeight;
  963. // CurDelay = minimumDelay + gsl_rng_uniform_int(gslr, DelayDiff+1);
  964. CurDelay = minimumDelay + int(TempDistance[i][j]*DelayDiff/MaxConDistance); // ToDo: FixMe!!!!!!!!!!!!!!!!!!! think a lot!!!
  965. if (CurDelay>=maximumDelay)
  966. {
  967. CurDelay = maximumDelay-1;
  968. Dout(dc::con, "ERROR: delay too high");
  969. }
  970. delays[i][CurDelay][delays_length[i][CurDelay]] = j;
  971. ++delays_length[i][CurDelay];
  972. }
  973. }
  974. }
  975. Dout(dc::con, "connected"); fflush(stdout);
  976. SetupPresynapticInfo();
  977. Dout(dc::con, "Connection initialized");
  978. }
  979. int connection::ConnectCircularPre(float Connectivity, float MaxWeight, float maxDelay, float minDelay)
  980. {
  981. Strength=MaxWeight;
  982. Dout(dc::con, "Connection::ConnectProfile");
  983. nt = TargetLayer->N;
  984. int N;
  985. ns = N = SourceLayer->N;
  986. if (Connectivity > 1) Connectivity=1;
  987. if (Connectivity <0) Dout(dc::con, "connection::ConnectCircular: ERROR: Connectivity shouldn't be zero!!");
  988. int NSourceConnections = int(Connectivity*ns);
  989. Dout(dc::con, "NSourceConnections=" << NSourceConnections << "");
  990. int ConnectionCount=0;
  991. if (ns != nt) NonSelf = false; // NonSelf makes sense only for layers with same size
  992. int i,j,k,exists, r;
  993. // Dout(dc::con, "connect"); fflush(stdout);
  994. // double loop (ns, nt) goes throug every possible synapse and checks wether to connect or not
  995. M=0;
  996. // typedef boost::multi_array<int, 2> int2d_array;
  997. // typedef int2d_array::index index;
  998. Dout(dc::con, "MakeFirstTempArray, ns=" << ns << "nt=" << nt << "");fflush(stdout);
  999. // boost::multi_array<int, 2> TempPost(boost::extents[ns][nt]);
  1000. int** TempPost;
  1001. NewArray2d(TempPost, ns, nt);
  1002. Dout(dc::con, "MakeSecondTempArray");fflush(stdout);
  1003. boost::multi_array<float, 2> TempDistance(boost::extents[ns][nt]);
  1004. for (i=0;i<ns;++i) for (j=0;j<nt;++j)
  1005. {
  1006. TempPost[i][j]=-1;
  1007. TempDistance[i][j]=-1;
  1008. }
  1009. vector<int> TargetCount(ns); //by default vectors are initialized with zero
  1010. vector<int> TempNpre (nt);
  1011. float InitialWeights=1;
  1012. int CurDelay;
  1013. float MaxConDistance =0;
  1014. SetMinMaxDelay(maxDelay, minDelay);
  1015. maxN_pre = 0;
  1016. float Distance;
  1017. vector2d basis(1,1);
  1018. SimpleTextProgressBar pgbar(ns);
  1019. Dout(dc::con, "Getmaxdist");fflush(stdout);
  1020. float ConValue;
  1021. // for each presynaptic neuron calc a sorted Distance List, and then connect until NTargetConnections is reached
  1022. // type map<double,int> DistMap;
  1023. map<double,int> CurPostDistances;
  1024. map<double,int>::iterator CurDist=CurPostDistances.begin();
  1025. for (j=0;j<nt;j++) {
  1026. pgbar.Next(j);
  1027. CurPostDistances.clear();
  1028. for (i=0;i<ns;i++)
  1029. {
  1030. Distance = (TargetLayer->Pos[j] - SourceLayer->Pos[i]).abs() ;
  1031. CurPostDistances[Distance+0.000001*i] = i; //add minimal value to Distance, because each Distance must be unique (map has no duplicate keys)
  1032. }
  1033. CurDist=CurPostDistances.begin();
  1034. while((TempNpre[j]<NSourceConnections) && (CurDist!=CurPostDistances.end()))
  1035. {
  1036. i=CurDist->second;
  1037. if (!NonSelf || (i!=j))
  1038. {
  1039. TempDistance[i][TargetCount[i]] = CurDist->first;
  1040. TempPost[i][TargetCount[i]]=j;
  1041. ++TempNpre[j]; // count presynaptic Targeting of postsynaptic neuron
  1042. ++TargetCount[i];
  1043. ++ConnectionCount;
  1044. if (CurDist->first > MaxConDistance) MaxConDistance=CurDist->first;
  1045. }
  1046. ++CurDist;
  1047. }
  1048. }
  1049. for (i=0;i<ns;++i) if (TargetCount[i] > M) M=TargetCount[i];
  1050. for (j=0;j<nt;++j) if (TempNpre[j] > maxN_pre) maxN_pre = TempNpre[j];
  1051. Dout(dc::con, "M= " << M << " ns= " << ns << "maxN_pre=" << maxN_pre << ""); fflush(stdout);
  1052. Dout(dc::con, "ConnectionCount=" << ConnectionCount << "");
  1053. // initialize dynamical arrays
  1054. InitializeDynamicalArrays(N, M, maximumDelay);
  1055. // copy temporary TempPost to post array
  1056. for (i=0;i<ns;++i) for (j=0;j<M;++j) post[i][j] = TempPost[i][j];
  1057. // setup delays and initial weights
  1058. pgbar.Reset(ns);
  1059. for (i=0;i<ns;i++) {
  1060. pgbar.Next(i);
  1061. for (j=0;j<M;j++)
  1062. {
  1063. if (post[i][j] != -1) {
  1064. s[i][j] = MaxWeight;
  1065. // CurDelay = minimumDelay + gsl_rng_uniform_int(gslr, DelayDiff+1);
  1066. CurDelay = minimumDelay + int(TempDistance[i][j]*DelayDiff/MaxConDistance); // ToDo: FixMe!!!!!!!!!!!!!!!!!!! think a lot!!!
  1067. if (CurDelay>=maximumDelay)
  1068. {
  1069. CurDelay = maximumDelay-1;
  1070. Dout(dc::con, "ERROR: delay too high");
  1071. }
  1072. delays[i][CurDelay][delays_length[i][CurDelay]] = j;
  1073. ++delays_length[i][CurDelay];
  1074. }
  1075. }
  1076. }
  1077. Dout(dc::con, "connected"); fflush(stdout);
  1078. SetupPresynapticInfo();
  1079. Dout(dc::con, "Connection initialized");
  1080. }
  1081. int connection::ConnectGaussianProb(float Sigma, float MaxWeight, float maxDelay, float minDelay, float MinConDistance, float MaxConnectivity, bool Cyclic)
  1082. {
  1083. Strength=MaxWeight;
  1084. Dout(dc::con, "Connection::ConnectGaussianProb");
  1085. nt = TargetLayer->N;
  1086. int ConnectionCount=0;
  1087. int N;
  1088. ns = N = SourceLayer->N;
  1089. if (ns != nt) NonSelf = false; // NonSelf makes sense only for layers with same size
  1090. int i,j,k,exists, r;
  1091. // Dout(dc::con, "connect"); fflush(stdout);
  1092. // double loop (ns, nt) goes throug every possible synapse and checks wether to connect or not
  1093. int TargetCount;
  1094. M=0;
  1095. // typedef boost::multi_array<int, 2> int2d_array;
  1096. // typedef int2d_array::index index;
  1097. Dout(dc::con, "make temporary arrays");fflush(stdout);
  1098. boost::multi_array<int, 2> TempPost(boost::extents[ns][nt]);
  1099. Dout(dc::con, "ready temporary arrays");fflush(stdout);
  1100. for (i=0;i<ns;++i) for (j=0;j<nt;++j) TempPost[i][j]=-1;
  1101. vector<int> TempNpre (nt);
  1102. for (i=0;i<nt;++i) TempNpre[i]=0;
  1103. float InitialWeights=1;
  1104. int CurDelay;
  1105. SetMinMaxDelay(maxDelay, minDelay);
  1106. maxN_pre = 0;
  1107. float Distance;
  1108. vector2d basis(1,1);
  1109. // set connections and calculate M and maxN_pre;
  1110. SimpleTextProgressBar pgbar(ns);
  1111. for (i=0;i<ns;i++) {
  1112. TargetCount=0;
  1113. pgbar.Next(i);
  1114. for (j=0;j<nt;j++)
  1115. {
  1116. // set connection in temporary boost multi_array
  1117. if (Cyclic) {
  1118. Distance = (TargetLayer->Pos[j]).CyclicDistance(SourceLayer->Pos[i], basis);
  1119. } else {
  1120. Distance = (TargetLayer->Pos[j] - SourceLayer->Pos[i]).abs() ;
  1121. }
  1122. if ((Distance >= MinConDistance) && (gsl_rng_uniform(gslr) < MaxConnectivity*gauss(Distance,Sigma)))
  1123. {
  1124. TempPost[i][TargetCount]=j;
  1125. ++TempNpre[j]; // count presynaptic Targeting of postsynaptic neuron
  1126. ++TargetCount;
  1127. ++ConnectionCount;
  1128. }
  1129. }
  1130. if (TargetCount > M) M=TargetCount;
  1131. }
  1132. for (j=0;j<nt;++j) if (TempNpre[j] > maxN_pre) maxN_pre = TempNpre[j];
  1133. // now M and maxN_pre are calculated
  1134. Dout(dc::con, "M= " << M << " ns= " << ns << "maxN_pre=" << maxN_pre << ""); fflush(stdout);
  1135. Dout(dc::con, "ConnectionCount=" << ConnectionCount << "");
  1136. // initialize dynamical arrays
  1137. InitializeDynamicalArrays(N, M, maximumDelay);
  1138. // copy temporary TempPost to post array
  1139. for (i=0;i<ns;++i) for (j=0;j<M;++j) post[i][j] = TempPost[i][j];
  1140. // setup delays and initial weights
  1141. pgbar.Reset(ns);
  1142. for (i=0;i<ns;i++) {
  1143. pgbar.Next(i);
  1144. for (j=0;j<M;j++)
  1145. {
  1146. if (post[i][j] != -1) {
  1147. s[i][j] = MaxWeight;
  1148. CurDelay = minimumDelay + gsl_rng_uniform_int(gslr, DelayDiff+1);
  1149. delays[i][CurDelay][delays_length[i][CurDelay]] = j;
  1150. ++delays_length[i][CurDelay];
  1151. }
  1152. }
  1153. }
  1154. Dout(dc::con, "connected"); fflush(stdout);
  1155. SetupPresynapticInfo();
  1156. Dout(dc::con, "Connection initialized");
  1157. }
  1158. int connection::ConnectFull(float MaxWeight, float maxDelay, float minDelay, bool RandomWeights)
  1159. {
  1160. Strength=MaxWeight;
  1161. Dout(dc::con, "Connection::ConnectFull");
  1162. nt = TargetLayer->N;
  1163. int ConnectionCount=0;
  1164. int N;
  1165. ns = N = SourceLayer->N;
  1166. if (ns != nt) NonSelf = false; // NonSelf makes sense only for layers with same size
  1167. int i,j,k,exists, r;
  1168. if (NonSelf) M=nt-1; else M=nt;
  1169. maxN_pre = ns;
  1170. SetMinMaxDelay(maxDelay, minDelay);
  1171. // initialize dynamical arrays
  1172. InitializeDynamicalArrays(N, M, maximumDelay);
  1173. float InitialWeights=1;
  1174. int CurM;
  1175. for (i=0;i<ns;i++) {
  1176. CurM=0;
  1177. for (j=0;j<nt;j++)
  1178. {
  1179. if ((!NonSelf) || (i != j)) {
  1180. post[i][CurM] = j;
  1181. if (RandomWeights) {
  1182. s[i][CurM] = gsl_rng_uniform(gslr)*MaxWeight;
  1183. } else {
  1184. s[i][CurM] = MaxWeight;
  1185. }
  1186. ++CurM;
  1187. }
  1188. }
  1189. }
  1190. Dout(dc::con, "M= " << M << " ns= " << ns << "maxN_pre=" << maxN_pre << ""); fflush(stdout);
  1191. Dout(dc::con, "connected"); fflush(stdout);
  1192. SetupRandomDelays();
  1193. SetupPresynapticInfo();
  1194. Dout(dc::con, "Connection initialized ");
  1195. }
  1196. int connection::ConnectFullColumnwise(float MaxWeight, int tdimx, int tdimy, int sdimx, int sdimy, float maxDelay, float minDelay,bool divergentrow,float sigma_divrow, bool Cyclic, bool convergent)
  1197. {
  1198. Strength=MaxWeight;
  1199. Dout(dc::con, "Connection::ConnectFull");
  1200. nt = TargetLayer->N;
  1201. int ConnectionCount=0;
  1202. int N;
  1203. ns = N = SourceLayer->N;
  1204. if (ns != nt) NonSelf = false; // NonSelf makes sense only for layers with same size
  1205. int x_s,y_s,x_t,y_t,i,j,k,exists, r;
  1206. if (NonSelf) M=nt-1; else M=nt;
  1207. maxN_pre = ns;
  1208. int ntns;
  1209. if (divergentrow = true) ntns=tdimy/sdimy;
  1210. else ntns=1;
  1211. SetMinMaxDelay(maxDelay, minDelay);
  1212. // initialize dynamical arrays
  1213. InitializeDynamicalArrays(N, M, maximumDelay);
  1214. float InitialWeights=1;
  1215. int CurM;
  1216. CurM=0;
  1217. /* for (y_source=0;y_source<sourcedimy;y_source++)
  1218. {
  1219. for (x_source=y_source*nsx; x_source<(y_source+1)*nsx; x_source++) {
  1220. for (y_target=0;y_target<dimy; y_target++)
  1221. for (x_target=dimx*y_target; x_target<dimx*(y_target+1);x_target++)
  1222. {
  1223. post[x_source+y_source][x_target+y_target]=CurM;
  1224. s[x_source+y_source][x_target+y_target]=1;
  1225. CurM++;
  1226. }
  1227. }
  1228. } */
  1229. float Distance;
  1230. vector2d basis(1,1);
  1231. for (y_s=0;y_s<sdimy;y_s++)
  1232. {
  1233. for (x_s=0;x_s<sdimx;x_s++)
  1234. {
  1235. CurM=0;
  1236. int a=y_s*sdimx;
  1237. int here= x_s+a;
  1238. for (y_t=0;y_t<tdimy;y_t++)
  1239. {
  1240. for (x_t=0;x_t<tdimx;x_t++)
  1241. {
  1242. post[here][CurM] =x_t+y_t*tdimx;//+y_t*tdimx;
  1243. if (y_t == y_s*ntns) s[here][CurM] = MaxWeight;
  1244. else s[here][CurM]=0;
  1245. if (Cyclic) Distance= (TargetLayer->Pos[y_t].CyclicDistance(SourceLayer->Pos[y_s*sdimx], basis));
  1246. else Distance=y_t-y_s*ntns;
  1247. if (divergentrow == true) {
  1248. s[here][CurM]=MaxWeight*exp(-0.5*(Distance)*(Distance)/(sigma_divrow*sigma_divrow));
  1249. } //1./(sigma_divrow*sqrt(2.*3.14))
  1250. ++CurM;
  1251. }
  1252. }
  1253. }
  1254. }
  1255. Dout(dc::con, "M= " << M << " ns= " << ns << "maxN_pre=" << maxN_pre << ""); fflush(stdout);
  1256. Dout(dc::con, "connected"); fflush(stdout);
  1257. SetupRandomDelays();
  1258. SetupPresynapticInfo();
  1259. Dout(dc::con, "Connection initialized ");
  1260. }
  1261. int connection::ConnectGradientFields(float MaxWeight, float xslope, float yslope, int tdimx, int tdimy, int sdimx, int sdimy, float maxDelay, float minDelay)
  1262. {
  1263. Strength=MaxWeight;
  1264. Dout(dc::con, "Connection::ConnectGradientField ");
  1265. nt = TargetLayer->N;
  1266. int ConnectionCount=0;
  1267. int N;
  1268. ns = N = SourceLayer->N;
  1269. if (ns != nt) NonSelf = false; // NonSelf makes sense only for layers with same size
  1270. int x_s,y_s,x_t,y_t,i,j,k,exists, r;
  1271. if (NonSelf) M=nt-1; else M=nt;
  1272. maxN_pre = ns;
  1273. SetMinMaxDelay(maxDelay, minDelay);
  1274. // initialize dynamical arrays
  1275. InitializeDynamicalArrays(N, M, maximumDelay);
  1276. float InitialWeights=1;
  1277. int CurM;
  1278. CurM=0;
  1279. float Distance;
  1280. vector2d basis(1,1);
  1281. for (x_s=0;x_s<sdimx;x_s++)
  1282. {
  1283. for (y_s=0;y_s<sdimy;y_s++)
  1284. {
  1285. CurM=0;
  1286. for (x_t=0;x_t<tdimx;x_t++)
  1287. {
  1288. for (y_t=0;y_t<tdimy;y_t++)
  1289. {
  1290. post[x_s+sdimx*y_s][CurM]=x_t+y_t*tdimx;
  1291. s[x_s+sdimx*y_s][CurM]=(-xslope*x_s+MaxWeight/2)+(-yslope*y_s+MaxWeight/2);
  1292. ++CurM;
  1293. }
  1294. }
  1295. }
  1296. }
  1297. Dout(dc::con, "M= " << M << " ns= " << ns << "maxN_pre=" << maxN_pre << ""); fflush(stdout);
  1298. Dout(dc::con, "connected"); fflush(stdout);
  1299. SetupRandomDelays();
  1300. SetupPresynapticInfo();
  1301. Dout(dc::con, "Connection initialized ");
  1302. }
  1303. int connection::SetMinMaxDelay(float maxDelay, float minDelay)
  1304. {
  1305. maximumDelay = int(maxDelay/dt);
  1306. if (maximumDelay == 0) maximumDelay = 1;
  1307. // equivalent to Dmax; compare allways with "<" (not with "<=")
  1308. minimumDelay = int(minDelay/dt);
  1309. if (maximumDelay >= Dmax) {
  1310. cout << "connection::SetMinMaxDelay: Initialization "
  1311. << "Parameter Error: maximumDelay="
  1312. << maximumDelay << " > Dmax=" << "Dmax \n";
  1313. maximumDelay = Dmax-1;
  1314. }
  1315. if (minimumDelay > maximumDelay) {
  1316. cout << "connection::SetMinMaxDelay: Initialization Parameter Error:"
  1317. << " minimumDelay > maximumDelay\n";
  1318. minimumDelay = 0;
  1319. }
  1320. DelayDiff = maximumDelay-minimumDelay-1;
  1321. cout << "MaximumDelay=" << maximumDelay
  1322. << " MinimumDelay=" << minimumDelay
  1323. << " DelayDiff= " << DelayDiff << "\n";
  1324. }
  1325. int connection::ConnectSelf(float MaxWeight, float maxDelay, float minDelay)
  1326. {
  1327. Strength=MaxWeight;
  1328. Dout(dc::con, "Connection::ConnectSelf");
  1329. nt = TargetLayer->N;
  1330. int ConnectionCount=0;
  1331. int N;
  1332. ns = N = SourceLayer->N;
  1333. int i,j,k,exists, r;
  1334. M=1;
  1335. maxN_pre = 1;
  1336. SetMinMaxDelay(maxDelay, minDelay);
  1337. // initialize dynamical arrays
  1338. // InitializeDynamicalArrays(N, M, Dmax);
  1339. InitializeDynamicalArrays(N, M, maximumDelay);
  1340. int CurDelay;
  1341. float InitialWeights=1;
  1342. int MaxCon = min(ns,nt);
  1343. int rnum;
  1344. Dout(dc::con, "MaxCon=" << MaxCon << "");
  1345. for (i=0;i<MaxCon;i++)
  1346. {
  1347. post[i][0] = i;
  1348. s[i][0] = MaxWeight;
  1349. CurDelay = minimumDelay +getrandom(DelayDiff+1);
  1350. delays[i][CurDelay][delays_length[i][CurDelay]] = 0;
  1351. ++delays_length[i][CurDelay];
  1352. }
  1353. Dout(dc::con, "M= " << M << " ns= " << ns << "maxN_pre=" << maxN_pre << ""); fflush(stdout);
  1354. Dout(dc::con, "connected"); fflush(stdout);
  1355. SetupPresynapticInfo();
  1356. Dout(dc::con, "Connection initialized ");
  1357. }
  1358. int connection::ConnectPartial(int maxTarget, float MaxWeight, float maxDelay, float minDelay)
  1359. {
  1360. Strength=MaxWeight;
  1361. Dout(dc::con, "Connection::ConnectPartial");
  1362. nt = TargetLayer->N;
  1363. if (maxTarget>nt)
  1364. {
  1365. maxTarget=nt;
  1366. Dout(dc::con, "ERROR: maxTarget > TargetLayer->N");
  1367. }
  1368. int ConnectionCount=0;
  1369. int N;
  1370. ns = N = SourceLayer->N;
  1371. if (ns != nt) NonSelf = false; // NonSelf makes sense only for layers with same size
  1372. int i,j,k,exists, r;
  1373. M=maxTarget;
  1374. maxN_pre=ns;
  1375. // initialize dynamical arrays
  1376. SetMinMaxDelay(maxDelay, minDelay);
  1377. InitializeDynamicalArrays(N, M, maximumDelay);
  1378. float InitialWeights=1;
  1379. for (i=0;i<ns;i++) {
  1380. for (j=0;j<M;j++)
  1381. {
  1382. post[i][j] = j;
  1383. s[i][j] = MaxWeight;
  1384. }
  1385. }
  1386. Dout(dc::con, "M= " << M << " ns= " << ns << "maxN_pre=" << maxN_pre << ""); fflush(stdout);
  1387. Dout(dc::con, "connected"); fflush(stdout);
  1388. SetupRandomDelays();
  1389. SetupPresynapticInfo();
  1390. Dout(dc::con, "Connection initialized ");
  1391. }
  1392. int connection::SetWeights(vector<vector<float> >& s_new)
  1393. {
  1394. if(s_new.size()==ns && s_new[0].size()==M)
  1395. {
  1396. for(int i=0;i<ns;i++)
  1397. for(int j=0;j<M;j++)
  1398. s[i][j]=s_new[i][j];
  1399. cout<<"Weights has been set\n";
  1400. }else{
  1401. cout<<"couldn't SetWeights because of wrong array-dimension\n";
  1402. }
  1403. }
  1404. int connection::SetupRandomDelays(float maxDelay, float minDelay)
  1405. {
  1406. SetMinMaxDelay(maxDelay, minDelay);
  1407. SetupRandomDelays();
  1408. }
  1409. int connection::SetupRandomDelays()
  1410. {
  1411. Dout(dc::con, "SetupRandomDelays");
  1412. int i,j,k;
  1413. int CurDelay;
  1414. for (i=0;i<ns;i++)
  1415. {
  1416. for (j=0;j<M;++j)
  1417. {
  1418. // check if valid connection (invalid: post == -1)
  1419. if (post[i][j] >= 0) {
  1420. CurDelay = minimumDelay + getrandom(DelayDiff+1);
  1421. delays[i][CurDelay][delays_length[i][CurDelay]] = j;
  1422. ++delays_length[i][CurDelay];
  1423. }
  1424. }
  1425. }
  1426. }
  1427. int connection::SetupDelays(float maxDelay, float minDelay)
  1428. {
  1429. SetMinMaxDelay(maxDelay, minDelay);
  1430. SetupDelays();
  1431. }
  1432. int connection::SetupDelays()
  1433. {
  1434. // setup delays
  1435. // connections must be allready set up!!!
  1436. // ToDo: check this!!
  1437. // ToDo: delays are set up systematicaly (ordered); this works only if the weights are chosen randomly!!
  1438. // --> SetupDelaysRandom() needed!!!!!!!!!
  1439. int i,j,k;
  1440. int MeanDelays_Length = M/DelayDiff;
  1441. cout << "MaximumDelay=" << maximumDelay << " MinimumDelay=" << minimumDelay
  1442. << " MeanDelays_length= " << MeanDelays_Length << " DelayDiff= " << DelayDiff << "\n";
  1443. for (i=0;i<ns;i++)
  1444. {
  1445. short ind=0;
  1446. for (j=0;j<maximumDelay;j++)
  1447. {
  1448. if (j>=minimumDelay)
  1449. {
  1450. if ((M-ind)>MeanDelays_Length) delays_length[i][j]=MeanDelays_Length;
  1451. else delays_length[i][j] = M-ind;
  1452. } else {
  1453. delays_length[i][j]=0;
  1454. }
  1455. for (k=0;k<delays_length[i][j];k++)
  1456. delays[i][j][k]=ind++; // connection index of presynaptic neuron
  1457. }
  1458. }
  1459. }
  1460. int connection::ConnectDirectional(float c, float InitialWeights)
  1461. {
  1462. Dout(dc::con, "Directional Connections");
  1463. learn=true;
  1464. maxWeight=1;
  1465. connectivity=c;
  1466. nt = TargetLayer->N;
  1467. // float TAULEARN_pre=50;
  1468. // dec_pre = exp(-1./TAULEARN_pre);
  1469. // float TAULEARN_post=5;
  1470. // dec_pre = exp(-1./TAULEARN_post);
  1471. int N;
  1472. ns = N = SourceLayer->N;
  1473. if (ns != nt) NonSelf = false; // NonSelf makes sense only for layers with same size
  1474. M= (int) floor(c*ns);
  1475. maxN_pre = 3*ns*M/nt;
  1476. Dout(dc::con, "M= " << M << " c= " << c << " ns= " << ns << "maxN_pre=" << maxN_pre << ""); fflush(stdout);
  1477. minimumDelay=0;
  1478. maximumDelay=Dmax;
  1479. // SetMinMaxDelay(maxDelay, minDelay);
  1480. if (M>nt) Dout(dc::con, "ERROR: too many connections, reduce connectivity!!");
  1481. if ( M%maximumDelay != 0) {
  1482. Dout(dc::con, "ERROR: bad connectivity value!! M%D= " << M%maximumDelay << " !=0 \n" );
  1483. // M += 20 - (M%Dmax);
  1484. M -= M%maximumDelay;
  1485. Dout(dc::con, "reducing weights to M= " << M << ""); // M must be a multiple of Dmax for this random weight initialization;
  1486. }
  1487. // initialize dynamical arrays
  1488. InitializeDynamicalArrays(N, M, maximumDelay);
  1489. int i,j,k,exists, r;
  1490. for (i=0;i<ns;i++) for (j=0;j<M;j++)
  1491. {
  1492. do {
  1493. exists = 0; // avoid multiple synapses
  1494. r = (i+getrandom(nt/10))%nt;
  1495. if (i==0) Dout(dc::con, r << " ");
  1496. if (r==i) exists=1; // no self-synapses
  1497. for (k=0;k<j;k++) if (post[i][k]==r) exists = 1; // synapse already exists
  1498. } while (exists == 1);
  1499. post[i][j]=r;
  1500. }
  1501. for (i=0;i<ns;i++) for (j=0;j<M;j++) s[i][j]=InitialWeights; // initial exc. synaptic weights
  1502. for (i=0;i<N;i++)
  1503. {
  1504. short ind=0;
  1505. for (j=0;j<maximumDelay;j++)
  1506. { delays_length[i][j]=M/maximumDelay; // uniform distribution of exc. synaptic delays
  1507. for (k=0;k<delays_length[i][j];k++)
  1508. delays[i][j][k]=ind++;
  1509. }
  1510. }
  1511. SetupPresynapticInfo();
  1512. // for (i=0;i<ns;i++) for (j=0;j<1001+Dmax;j++) LTP[i][j]=0.0;
  1513. // for (i=0;i<nt;i++) LTD[i]=0.0;
  1514. // learnobj = new learning(this);
  1515. Dout(dc::con, "Connection initialized ");
  1516. Save();
  1517. }
  1518. // ToDo: validate this routine!!
  1519. int connection::SetupPresynapticInfo_old()
  1520. {
  1521. int i,j,k,dd,jj;
  1522. Dout(dc::con, "Setup Presynaptic Information (this might take a while ");
  1523. if ((N_pre != 0) || (I_pre != 0) || (D_pre != 0) || (s_pre != 0) || (sd_pre != 0))
  1524. Dout(dc::con, "SetupPresynapticInfo-ERROR: Pointers must be 0");
  1525. N_pre = new int[nt];
  1526. NewArray2d(I_pre, nt,maxN_pre); NewArray2d(D_pre,nt,maxN_pre); // presynaptic information
  1527. NewArray2d(s_pre,nt,maxN_pre); NewArray2d(sd_pre,nt,maxN_pre); // presynaptic weights
  1528. int ConIndex;
  1529. // get presynaptic information
  1530. SimpleTextProgressBar pgbar(ns);
  1531. for (i=0;i<nt;++i) N_pre[i]=0;
  1532. for (j=0;j<ns;j++)
  1533. {
  1534. pgbar.Next(j);
  1535. for (dd=0;dd<Dmax;++dd)
  1536. {
  1537. for(k=0;k<delays_length[j][dd];++k)
  1538. {
  1539. ConIndex = delays[j][dd][k]; // presynaptic connection index
  1540. i = post[j][ConIndex]; // i: target neuron index
  1541. I_pre[i][N_pre[i]]=j; // add this neuron to the list
  1542. if (N_pre[i] > maxN_pre)
  1543. cout << "programming error: N_pre[i]="
  1544. << N_pre[i] <<" too high (higher than maxN_pre="
  1545. << maxN_pre << "); j=" << j << " i=" << i << "\n";
  1546. D_pre[i][N_pre[i]]=dd; // add the delay
  1547. s_pre[i][N_pre[i]]=&s[j][ConIndex]; // pointer to the synaptic weight
  1548. // sd_pre[i][N_pre[i]]=&sd[j][ConIndex];// pointer to the derivative
  1549. ++N_pre[i];
  1550. }
  1551. }
  1552. }
  1553. CheckPresynapticInfo_old();
  1554. }
  1555. int connection::CheckPresynapticInfo_old()
  1556. {
  1557. // 1.
  1558. // total number of connections: Sum of delays_length
  1559. // should be equel to: Sum of N_pre
  1560. Dout(dc::con, "Checking Presynaptic Info");
  1561. int i, j, k;
  1562. int countPre, countPost;
  1563. countPre=countPost=0;
  1564. for (i=0;i<ns;++i) for (j=0;j<Dmax;++j) countPre += delays_length[i][j];
  1565. for (i=0;i<nt;++i) countPost += N_pre[i];
  1566. if (countPre == countPost) Dout(dc::con, countPre << " bastscho");
  1567. else Dout(dc::con, "countPre=" << countPre << " != countPost" << countPost << "!!!!!!!!!!!!!!");
  1568. // 2.
  1569. // take every synapse (via delays, delays_length, post)
  1570. // and find the corresponding I_pre
  1571. int ErrorSum=0;
  1572. int CurTarget;
  1573. int ppp;
  1574. SimpleTextProgressBar pgbar(ns);
  1575. for (i=0;i<ns;++i)
  1576. {
  1577. pgbar.Next(i);
  1578. for (j=0;j<Dmax;++j) for (k=0;k<delays_length[i][j];++k)
  1579. {
  1580. CurTarget = post[i][delays[i][j][k]];
  1581. // find Source in I_pre[CurTarget]
  1582. ppp=0;
  1583. while((I_pre[CurTarget][ppp] != i) && (ppp++<N_pre[CurTarget]));
  1584. if (ppp == N_pre[CurTarget]) Dout(dc::con, "ERROR!!!!!!!!!! PresynapticInfo not correct!!!!!!");
  1585. // else Dout(dc::con, ppp << " ");
  1586. }
  1587. }
  1588. }
  1589. // ToDo: validate this routine!!
  1590. int connection::SetupPresynapticInfo()
  1591. {
  1592. int i,j,k,dd,jj;
  1593. Dout(dc::con, "Setup Presynaptic Information (this might take a while "); fflush(stdout);
  1594. if ((N_pre != 0) || (I_pre != 0) || (D_pre != 0) || (s_pre != 0) || (sd_pre != 0))
  1595. Dout(dc::con, "SetupPresynapticInfo-ERROR: Pointers must be 0");
  1596. N_pre = new int[nt];
  1597. NewArray2d(I_pre, nt,maxN_pre); NewArray2d(D_pre,nt,maxN_pre); // presynaptic information
  1598. NewArray2d(s_pre,nt,maxN_pre); // presynaptic weights
  1599. // NewArray2d(sd_pre,nt,maxN_pre); // weight derivatives, not used
  1600. NewArray2d(m_pre,nt,maxN_pre);
  1601. N_post = new int[ns];
  1602. int ConIndex;
  1603. // get presynaptic information
  1604. SimpleTextProgressBar pgbar(ns);
  1605. for (i=0;i<nt;++i) N_pre[i]=0;
  1606. for (j=0;j<ns;j++)
  1607. {
  1608. pgbar.Next(j);
  1609. for (dd=0;dd<maximumDelay;++dd)
  1610. {
  1611. for(k=0;k<delays_length[j][dd];++k)
  1612. {
  1613. ++N_post[j]; // increase synapse count for presynaptic neuron
  1614. // Dout(dc::con, "N_post[" <<j<<"]=" << N_post[j] << "");
  1615. ConIndex = delays[j][dd][k]; // presynaptic connection index
  1616. i = post[j][ConIndex]; // i: target neuron index
  1617. I_pre[i][N_pre[i]]=j; // add this neuron to the list
  1618. m_pre[i][N_pre[i]]=ConIndex;
  1619. if (N_pre[i] > maxN_pre)
  1620. cout << "programming error: N_pre[i]="
  1621. << N_pre[i] <<" too high (higher than maxN_pre="
  1622. << maxN_pre << "); j=" << j << " i=" << i << "\n";
  1623. D_pre[i][N_pre[i]]=dd; // add the delay
  1624. s_pre[i][N_pre[i]]=&s[j][ConIndex]; // pointer to the synaptic weight
  1625. // sd_pre[i][N_pre[i]]=&sd[j][ConIndex];// pointer to the derivative
  1626. ++N_pre[i];
  1627. }
  1628. }
  1629. }
  1630. CheckPresynapticInfo();
  1631. }
  1632. int connection::DeletePresynapticInfo()
  1633. {
  1634. if(N_pre!=0) {
  1635. delete[] N_pre;
  1636. N_pre=0;
  1637. }
  1638. if(I_pre!=0) {
  1639. DeleteArray2d(I_pre,nt);
  1640. I_pre=0;
  1641. }
  1642. if(D_pre!=0) {
  1643. DeleteArray2d(D_pre,nt);
  1644. D_pre=0;
  1645. }
  1646. if(s_pre!=0) {
  1647. DeleteArray2d(s_pre,nt);
  1648. s_pre=0;
  1649. }
  1650. if(sd_pre!=0) {
  1651. DeleteArray2d(sd_pre,nt);
  1652. sd_pre=0;
  1653. }
  1654. }
  1655. int connection::Print()
  1656. {
  1657. for (int SourceNr=0;SourceNr<ns;++SourceNr) {
  1658. Dout(dc::con, "S=" << SourceNr << "|");
  1659. for (int SynNr=0;SynNr<N_post[SourceNr];++SynNr) {
  1660. Dout(dc::con, " " << post[SourceNr][SynNr]);
  1661. }
  1662. Dout(dc::con, "");
  1663. }
  1664. for (int TargetNr=0;TargetNr<nt;++TargetNr) {
  1665. Dout(dc::con, "T=" << TargetNr << "|");
  1666. for (int PreSynNr=0;PreSynNr<N_pre[TargetNr];++PreSynNr) {
  1667. Dout(dc::con, " " << I_pre[TargetNr][PreSynNr]);
  1668. }
  1669. Dout(dc::con, "");
  1670. }
  1671. for (int TargetNr=0;TargetNr<nt;++TargetNr) {
  1672. Dout(dc::con, "T=" << TargetNr << "|");
  1673. for (int PreSynNr=0;PreSynNr<N_pre[TargetNr];++PreSynNr) {
  1674. Dout(dc::con, " " << m_pre[TargetNr][PreSynNr]);
  1675. }
  1676. Dout(dc::con, "");
  1677. }
  1678. }
  1679. int connection::CheckConnection()
  1680. {
  1681. int TargetNr=0;
  1682. int SynNr=0;
  1683. for (int SourceNr=0;SourceNr<ns;++SourceNr) {
  1684. for (int Delay=0;Delay<maximumDelay;++Delay) {
  1685. for (int m=0;m<delays_length[SourceNr][Delay];++m) {
  1686. SynNr=delays[SourceNr][Delay][m];
  1687. TargetNr=post[SourceNr][SynNr];
  1688. if (TargetNr <0) {
  1689. cerr << "CheckConnection: ERROR, TargetNr <0\n";
  1690. exit(1);
  1691. }
  1692. }
  1693. }
  1694. }
  1695. }
  1696. int connection::CheckPresynapticInfo()
  1697. {
  1698. // 1.
  1699. // total number of connections: Sum of delays_length
  1700. // should be equel to: Sum of N_pre
  1701. Dout(dc::con, "Checking Presynaptic Info");
  1702. int i, j, k;
  1703. int countPre, countPost;
  1704. countPre=countPost=0;
  1705. for (i=0;i<ns;++i) for (j=0;j<maximumDelay;++j) countPre += delays_length[i][j];
  1706. for (i=0;i<nt;++i) countPost += N_pre[i];
  1707. if (countPre == countPost) Dout(dc::con, countPre << " bastscho");
  1708. else Dout(dc::con, "countPre=" << countPre << " != countPost" << countPost << "!!!!!!!!!!!!!!");
  1709. // 2.
  1710. // take every synapse (via delays, delays_length, post)
  1711. // and find the corresponding I_pre
  1712. int ErrorSum=0;
  1713. int CurTarget;
  1714. int PostIndex;
  1715. SimpleTextProgressBar pgbar(ns);
  1716. int SourceSynNumber;
  1717. int TargetSynNumber;
  1718. for (i=0;i<ns;++i) {
  1719. pgbar.Next(i);
  1720. for (j=0;j<maximumDelay;++j) for (k=0;k<delays_length[i][j];++k) {
  1721. SourceSynNumber = delays[i][j][k];
  1722. CurTarget = post[i][SourceSynNumber];
  1723. if (CurTarget <0) {
  1724. cerr << "CheckPresynapticInfo-ERROR: CurTarget < 0\n";
  1725. cerr << "DelaysLength = " << delays_length[i][j] << "\n";
  1726. cerr << "CurTarget = " << CurTarget << "\n";
  1727. cerr << "SourceSynNumber =" << SourceSynNumber << "\n";
  1728. cerr << "i=" << i << " j=" << j << " k=" << k << "\n";
  1729. fflush(stderr); fflush(stdout);
  1730. exit(1);
  1731. }
  1732. // find Source in I_pre[CurTarget]
  1733. PostIndex=0;
  1734. while((s_pre[CurTarget][PostIndex] != &s[i][SourceSynNumber]) && (++PostIndex<N_pre[CurTarget]));
  1735. // cout << "S=" << i << " --> T="
  1736. // << CurTarget << " PostIndex="
  1737. // << PostIndex << "m=" << SourceSynNumber
  1738. // << " N_pre[T] =" << N_pre[CurTarget] << " ";
  1739. // fflush(stdout);
  1740. if (PostIndex == N_pre[CurTarget]) {
  1741. cerr << "ERROR!!!!!!!!!! PresynapticInfo not correct!!!!!!\n";
  1742. cerr << "Postindex = " << PostIndex << " N_pre[CurTarget] = "
  1743. << N_pre[CurTarget] << "\n";
  1744. } else {
  1745. if ((s_pre[CurTarget][PostIndex] == &s[i][SourceSynNumber]) &&
  1746. (D_pre[CurTarget][PostIndex] == j) &&
  1747. (m_pre[CurTarget][PostIndex] == SourceSynNumber)
  1748. ) {
  1749. // Dout(dc::con, "correct ");
  1750. } else {
  1751. cerr << "ERROR \n";
  1752. cerr << "SourceNr=" << i << " SourceSynNr="
  1753. << SourceSynNumber << " TargetNr=" << CurTarget
  1754. << "\n";
  1755. cerr << "s=s: "
  1756. << (s_pre[CurTarget][PostIndex] == &s[i][SourceSynNumber])
  1757. << " Delay=" << j
  1758. << " D_pre["<<CurTarget<<"][" << PostIndex << "]="
  1759. << D_pre[CurTarget][PostIndex]
  1760. << " d=d: " << (D_pre[CurTarget][PostIndex] == j)
  1761. << " m=m: "
  1762. << (m_pre[CurTarget][PostIndex] == SourceSynNumber)
  1763. << "m=" << SourceSynNumber
  1764. << " m_pre=" << m_pre[CurTarget][PostIndex]
  1765. << "\n";
  1766. cerr << "s=" << &s[i][SourceSynNumber]
  1767. << " s_pre= " << s_pre[CurTarget][PostIndex] << "\n";
  1768. cerr << "CheckPresynapticInfo: ERROR!!!!!!!!!!!!!!!!!!!!!!\n";
  1769. fflush(stderr);
  1770. exit (1);
  1771. }
  1772. }
  1773. }
  1774. }
  1775. }
  1776. int connection::proceede(int TotalTime)
  1777. {
  1778. int t = TotalTime % MacroTimeStep;
  1779. int i,j,k, mi, ipre;
  1780. // calculate input for target layer
  1781. k=SourceLayer->N_firings;
  1782. // cpu_start = clock();
  1783. if (rec) rec->record(dt*TotalTime, s[Observe_s][Observe_m]);
  1784. if (BinRec) BinRec->record();
  1785. while (t-(SourceLayer->firings[--k][0]) < maximumDelay) // Nur Spikes, die nicht laenger als maximumDelay in der Vergangeheit liegen werden berücksichtigt. firins-array besteht aus firings[SpikeNr][0]:zeitpunkte, firnigs[SpikeNr][1]:Neuronennummer
  1786. {
  1787. ipre = SourceLayer->firings[k][1];
  1788. for (j=0; j< delays_length[ipre][t-SourceLayer->firings[k][0]]; j++)
  1789. {
  1790. mi = delays[ipre][t-SourceLayer->firings[k][0]][j];
  1791. i=post[ipre][mi];
  1792. InputPointer[i]+=s[ipre][mi];
  1793. // ToDo: check wether the code above (new) does the same as the code below (old)
  1794. // check performance!!
  1795. // i=post[SourceLayer->firings[k][1]] [delays[SourceLayer->firings[k][1]][t-SourceLayer->firings[k][0]][j]];
  1796. // InputPointer[i]+=s[SourceLayer->firings[k][1]][delays[SourceLayer->firings[k][1]][t-SourceLayer->firings[k][0]][j]];
  1797. }
  1798. }
  1799. // cpu_end = clock();
  1800. // cpu_time_used += ((double) (cpu_end - cpu_start)) / CLOCKS_PER_SEC;
  1801. if (learn == true) {
  1802. learnobj->proceede(TotalTime);
  1803. }
  1804. }
  1805. int connection::prepare(int step)
  1806. {
  1807. SimElement::prepare(step);
  1808. if ((learn == true) && (learnobj != 0)) {
  1809. learnobj->prepare();
  1810. if (RewireOn) {
  1811. Rewire(RewireThreshold, RewireMaxConnectivity);
  1812. }
  1813. if (AutoSave) Save();
  1814. }
  1815. // Dout(dc::con, "PERFORMANCE: cpu-time = " << cpu_time_used << " sec");
  1816. }
  1817. void connection::SetName(const char* _name)
  1818. {
  1819. SimElement::SetName(_name);
  1820. WeightFileName = Name + "weights.dat";
  1821. Dout(dc::con, "xxxxxxxxxxxxxxxxWeightFileName = " << WeightFileName << "");
  1822. }
  1823. int connection::SetFileName(char* FileName)
  1824. {
  1825. WeightFileName = FileName;
  1826. Dout(dc::con, "Set WeightFileName to : " << WeightFileName << "");
  1827. }
  1828. int connection::Save()
  1829. {
  1830. Save(WeightFileName.c_str());
  1831. }
  1832. int connection::Save(int nr)
  1833. {
  1834. Save((WeightFileName + stringify(nr)).c_str());
  1835. }
  1836. int connection::Save(const string& SaveWeightFileName)
  1837. {
  1838. int i,j,k;
  1839. Dout(dc::con, " Save Con.file: " << SaveWeightFileName);fflush(stdout);
  1840. FILE *fw;
  1841. // save weights
  1842. fw = fopen((DataDirectory+SaveWeightFileName).c_str(),"w");
  1843. fwrite(&ns, sizeof(ns), 1, fw);
  1844. fwrite(&(SourceLayer->Nx), sizeof(SourceLayer->Nx), 1, fw);
  1845. fwrite(&(SourceLayer->Ny), sizeof(SourceLayer->Ny), 1, fw);
  1846. fwrite(&nt, sizeof(nt), 1, fw);
  1847. fwrite(&(TargetLayer->Nx), sizeof(TargetLayer->Nx), 1, fw);
  1848. fwrite(&(TargetLayer->Ny), sizeof(TargetLayer->Ny), 1, fw);
  1849. fwrite(&M, sizeof(M), 1, fw);
  1850. fwrite(&maximumDelay, sizeof(maximumDelay), 1, fw);
  1851. fwrite(&maxN_pre, sizeof(maxN_pre), 1, fw);
  1852. // NewArray2d(delays_length,N,Dmax); // distribution of delays
  1853. // NewArray3d(delays,N,Dmax,M); // arrangement of delays
  1854. for (i=0;i<ns;++i) fwrite(post[i], M*sizeof(post[0][0]), 1, fw);
  1855. for (i=0;i<ns;++i) fwrite(s[i], M*sizeof(s[0][0]), 1, fw);
  1856. for (i=0;i<ns;++i) fwrite(delays_length[i], maximumDelay*sizeof(delays_length[0][0]),1,fw);
  1857. for (i=0;i<ns;++i) for (j=0;j<maximumDelay;++j) fwrite(delays[i][j], M*sizeof(delays[0][0][0]),1,fw);
  1858. // fwrite(&N_pre, sizeof(N_pre), 1, fw);
  1859. // fwrite(&I_pre, sizeof(I_pre), 1, fw);
  1860. // fwrite(&D_pre, sizeof(D_pre), 1, fw);
  1861. // fwrite(&s, sizeof(s), 1, fw);
  1862. fclose(fw);
  1863. Dout(dc::con, " saved weights ");
  1864. }
  1865. bool connection::CheckHeaderConsistency()
  1866. {
  1867. bool consistent = Connection::CheckHeaderConsistency();
  1868. if (maxN_pre>ns) {
  1869. cerr << "ERROR: maxN_pre>ns \n";
  1870. consistent=false;
  1871. }
  1872. }
  1873. int connection::Load()
  1874. {
  1875. Load(WeightFileName.c_str());
  1876. }
  1877. int connection::Load(const char* FileName)
  1878. {
  1879. Load(FileName, DataDirectory.c_str());
  1880. }
  1881. int connection::Load(const char* FileName, const char* DirName)
  1882. {
  1883. int i,j,k;
  1884. FILE *fw;
  1885. std::string DirAndFileName = (std::string(DirName)+FileName);
  1886. Dout(dc::con, "DirAndFileName=" << DirAndFileName << "");
  1887. fflush(stdout);
  1888. const char* DFileName = DirAndFileName.c_str();
  1889. if (!fexist(DFileName)) {
  1890. cerr << "\n\nERROR: connection file " << DFileName << " doesn't exist \n\n";
  1891. fflush(stderr);
  1892. return(2);
  1893. } else {
  1894. Dout(dc::con, "\nLoadWeightFile: " << DFileName << ""); fflush(stdout);
  1895. fw = fopen( (std::string(DirName)+FileName).c_str(), "r");
  1896. fread(&ns, sizeof(ns), 1, fw);
  1897. fread(&SourceNx, sizeof(SourceNx), 1, fw);
  1898. fread(&SourceNy, sizeof(SourceNy), 1, fw);
  1899. fread(&nt, sizeof(nt), 1, fw);
  1900. fread(&TargetNx, sizeof(TargetNx), 1, fw);
  1901. fread(&TargetNy, sizeof(TargetNy), 1, fw);
  1902. fread(&M, sizeof(M), 1, fw);
  1903. fread(&maximumDelay, sizeof(maximumDelay), 1, fw);
  1904. fread(&maxN_pre, sizeof(maxN_pre), 1, fw);
  1905. cout << "ns=" << ns << "\n"
  1906. << "nt=" << nt << "\n"
  1907. << "SourceNx=" << SourceNx << "\n"
  1908. << "SourceNy=" << SourceNy << "\n"
  1909. << "TargetNx=" << TargetNx << "\n"
  1910. << "TargetNy=" << TargetNy << "\n"
  1911. << "M=" << M << "\n"
  1912. << "maxN_pre=" << maxN_pre << "\n"
  1913. << "maximumDelay=" << maximumDelay <<"\n"
  1914. << "Dmax=" << Dmax << "\n";
  1915. fflush(stdout);
  1916. bool SuccessfullyLoaded=CheckHeaderConsistency();
  1917. if (!SuccessfullyLoaded) {
  1918. cerr << "\n\nERROR while trying to load " << FileName <<"\n";
  1919. cerr << "try option --NoLoadWeights \n\n";
  1920. fflush(stderr);
  1921. return(1);
  1922. }
  1923. if (post == 0 && (s ==0) && (sd==0) && (delays_length==0) && (delays==0))
  1924. {
  1925. // initialize dynamical arrays
  1926. InitializeDynamicalArrays(ns, M, maximumDelay);
  1927. // NewArray2d(post, ns, M); // indeces of postsynaptic neurons
  1928. // NewArray2d(s, ns,M); NewArray2d(sd,ns,M);
  1929. // NewArray2d(delays_length,ns,Dmax); // distribution of delays
  1930. // NewArray3d(delays,ns,Dmax,M); // arrangement of delays
  1931. for (i=0;i<ns;++i) fread(post[i], M*sizeof(post[0][0]), 1, fw);
  1932. // for (i=0;i<ns;++i) for (j=0;j<M;++j) Dout(dc::con, post[i][j] << " ");
  1933. for (i=0;i<ns;++i) fread(s[i], M*sizeof(s[0][0]), 1, fw);
  1934. for (i=0;i<ns;++i) fread(delays_length[i], maximumDelay*sizeof(delays_length[0][0]),1,fw);
  1935. for (i=0;i<ns;++i) for (j=0;j<maximumDelay;++j) fread(delays[i][j], M*sizeof(delays[0][0][0]),1,fw);
  1936. } else Dout(dc::con, "WeightFileLoadingERROR: Pointers to dynamical arrays must be NULL");
  1937. SetupPresynapticInfo();
  1938. Dout(dc::con, "Loaded Connections");
  1939. fflush(stdout);
  1940. return(0);
  1941. }
  1942. }
  1943. // not in use!!!
  1944. int connection::DeleteWeight(int SourceNr, int ConnectionNr)
  1945. {
  1946. // delete weight in DelayArray
  1947. // mark synapse in post array with -1
  1948. // mark weight with NaN???
  1949. // presynaptic arrays are not corrected but later rebuild from scratch
  1950. bool found = false;
  1951. int WeightDelay=-1;
  1952. int DelayIndex=-1;
  1953. if (SourceNr >= ns) return -1;
  1954. // find Weight in delays array
  1955. for (int delay=0;delay<maximumDelay;++delay) {
  1956. for (int i=0; i<M;++i) {
  1957. if (delays[SourceNr][delay][i] == ConnectionNr) {
  1958. found=true;
  1959. WeightDelay=delay;
  1960. DelayIndex=i;
  1961. i=M;
  1962. delay=maximumDelay;
  1963. }
  1964. }
  1965. }
  1966. if (found) {
  1967. // Verbindung aus Delay-Liste loeschen
  1968. for (int pos=DelayIndex;pos<delays_length[SourceNr][WeightDelay]-1;++pos) {
  1969. delays[SourceNr][WeightDelay][pos] = delays[SourceNr][WeightDelay][pos+1];
  1970. }
  1971. // delays_length anpassen
  1972. --delays_length[SourceNr][WeightDelay];
  1973. post[SourceNr][ConnectionNr]=-1;
  1974. s[SourceNr][ConnectionNr]=0; // noetig??
  1975. --N_post[SourceNr];
  1976. } else return -1;
  1977. return 0;
  1978. }
  1979. int connection::SetSystematicWeights()
  1980. {
  1981. int TargetNr=-1;
  1982. for (int SourceNr=0; SourceNr<ns; ++SourceNr) {
  1983. for (int delay=0;delay<maximumDelay;++delay) {
  1984. for (int i=0; i<delays_length[SourceNr][delay];++i) {
  1985. TargetNr= post[SourceNr][delays[SourceNr][delay][i]];
  1986. s[SourceNr][delays[SourceNr][delay][i]]=float(SourceNr) + 0.001*TargetNr;
  1987. }
  1988. }
  1989. }
  1990. }
  1991. int connection::CheckSystematicWeights()
  1992. {
  1993. Dout(dc::con, "checking systematic weights");
  1994. int SourceNr=-1;
  1995. int errors =0;
  1996. for (int TargetNr=0; TargetNr<nt;++TargetNr) {
  1997. for (int i=0; i<N_pre[TargetNr]; ++i) {
  1998. SourceNr = I_pre[TargetNr][i];
  1999. // Dout(dc::con, "t=" << TargetNr << " s=" << SourceNr << " i=" << i << " weight=" << *s_pre[TargetNr][i] << " "); fflush(stdout);
  2000. if ((*s_pre[TargetNr][i] - (float(SourceNr) + 0.001*TargetNr)) > 0.00001) {
  2001. ++errors;
  2002. cerr << "ERROR nr. " << errors << "\n"; fflush(stderr);
  2003. }
  2004. }
  2005. }
  2006. Dout(dc::con, "Checked Systematic Weights, errors=" << errors << "");
  2007. }
  2008. // 2007/12/11: routine tested with condelins.cpp, seems to work
  2009. int connection::DeleteWeightCorrectPreInfo(int SourceNr, int ConnectionNr, int SupposedDelay)
  2010. {
  2011. bool found = false;
  2012. int WeightDelay=-1;
  2013. int m=-1;
  2014. if (SourceNr >= ns) return -1;
  2015. // find Weight in delays array
  2016. for (int delay=0;delay<maximumDelay;++delay) {
  2017. for (int i=0; i<delays_length[SourceNr][delay];++i) {
  2018. if (delays[SourceNr][delay][i] == ConnectionNr) {
  2019. found=true;
  2020. WeightDelay=delay;
  2021. m=i;
  2022. if ((SupposedDelay !=-1) && (SupposedDelay != WeightDelay)) {
  2023. cerr << "Delays don't fit!!!\n";
  2024. cerr << "SupposedDelay=" << SupposedDelay << "\n";
  2025. cerr << "WeightDelay=" << WeightDelay << "\n";
  2026. }
  2027. break;
  2028. }
  2029. }
  2030. if (found) break; // the previous break ends only the inner loop
  2031. }
  2032. if (found) {
  2033. // Verbindung in praesynaptischen Arrays finden
  2034. int TargetNr = post[SourceNr][ConnectionNr];
  2035. Dout(dc::con, "Delete Connection ");
  2036. Dout(dc::con, "SourceNr=" << SourceNr << "m=" << m << " ConNr=" << ConnectionNr << " TargetNr=" << TargetNr << " Delay=" << WeightDelay << "DelayLength=" << delays_length[SourceNr][WeightDelay] << "");
  2037. int PostIndex = -1;
  2038. for (int i=0;i<N_pre[TargetNr];++i) {
  2039. if (s_pre[TargetNr][i] == &s[SourceNr][ConnectionNr]) {
  2040. PostIndex = i;
  2041. }
  2042. }
  2043. if (PostIndex == -1) {
  2044. cerr << "DeleteWeightCorrectPreInfo: ERROR: connection not found in s_pre array\n";
  2045. fflush(stderr);
  2046. exit(1);
  2047. }
  2048. // Verbindung aus Delay-Liste loeschen
  2049. for (int pos=m;pos<delays_length[SourceNr][WeightDelay]-1;++pos) {
  2050. delays[SourceNr][WeightDelay][pos] = delays[SourceNr][WeightDelay][pos+1];
  2051. }
  2052. // delays_length anpassen
  2053. --delays_length[SourceNr][WeightDelay];
  2054. // connection is not removed from post[N][M] and s[N][M] arrays
  2055. // it is only marked as deleted by setting post[SourceNr][ConnectionNr] to -1
  2056. post[SourceNr][ConnectionNr] = -1;
  2057. --N_post[SourceNr];
  2058. // presynaptic info korrigieren:
  2059. // N_pre: -1
  2060. // I_pre, D_pre, m_pre, s_pre: Eintrag loeschen
  2061. for (int k=PostIndex; k<N_pre[TargetNr]-1;++k) {
  2062. I_pre[TargetNr][k] = I_pre[TargetNr][k+1];
  2063. D_pre[TargetNr][k] = D_pre[TargetNr][k+1];
  2064. s_pre[TargetNr][k] = s_pre[TargetNr][k+1];
  2065. m_pre[TargetNr][k] = m_pre[TargetNr][k+1];
  2066. }
  2067. --N_pre[TargetNr];
  2068. fflush(stdout);fflush(stderr);
  2069. } else {
  2070. cerr << "Connection which to delete was not found\n";
  2071. return -1;
  2072. }
  2073. return 0;
  2074. }
  2075. int connection::InsertNewWeight(int SourceNr, int TargetNr, float InitialWeight, int delay)
  2076. {
  2077. // check parameter
  2078. if (TargetNr >= nt) {
  2079. cerr << "Error in InsertNewWeight: TargetNr too high\n";
  2080. return -1;
  2081. }
  2082. if (delay >= maximumDelay) {
  2083. cerr << "ERROR in InsertNewWeight: delay >= maximumDelay\n";
  2084. return -1;
  2085. }
  2086. // find free position in post array
  2087. int m=-1;
  2088. for (int i=0;i<M;++i) {
  2089. if (post[SourceNr][i] == -1) {
  2090. m=i;
  2091. break;
  2092. }
  2093. }
  2094. if (m!=-1) {
  2095. post[SourceNr][m]=TargetNr;
  2096. s[SourceNr][m]=InitialWeight;
  2097. delays[SourceNr][delay][delays_length[SourceNr][delay]] = m;
  2098. ++delays_length[SourceNr][delay];
  2099. ++N_post[SourceNr];
  2100. // add presynaptic info
  2101. I_pre[TargetNr][N_pre[TargetNr]] = SourceNr;
  2102. D_pre[TargetNr][N_pre[TargetNr]] = delay;
  2103. m_pre[TargetNr][N_pre[TargetNr]] = m;
  2104. s_pre[TargetNr][N_pre[TargetNr]] = &s[SourceNr][m];
  2105. ++N_pre[TargetNr];
  2106. } else {
  2107. cerr << "ERROR: no free position for additional connection found\n";
  2108. return -1;
  2109. }
  2110. return 0;
  2111. }
  2112. int connection::DeleteLowWeights(float threshold)
  2113. {
  2114. // alle Gewichte durchgehen und mit Schwelle vergleichen
  2115. // Delay-Schleife verwenden
  2116. // umkopieren oder nur einzelne Gewichte l�schen und presynaptic info neu berechnen???
  2117. int DelCount=0;
  2118. for (int SourceNr=0; SourceNr<ns;++SourceNr) {
  2119. for (int CurDelay=0;CurDelay<maximumDelay;++CurDelay) {
  2120. for (int j=0;j<delays_length[SourceNr][CurDelay];++j) {
  2121. int SynapseNr=delays[SourceNr][CurDelay][j];
  2122. if (s[SourceNr][SynapseNr] <= threshold) {
  2123. DeleteWeightCorrectPreInfo(SourceNr,SynapseNr, CurDelay);
  2124. ++DelCount;
  2125. --j; // because synapse is deleted, the next synapse now has the position of the deleted synapse
  2126. }
  2127. }
  2128. }
  2129. }
  2130. Dout(dc::con, "DelCount=" << DelCount << " weights deleted");
  2131. // DeletePresynapticInfo();
  2132. // SetupPresynapticInfo();
  2133. // CheckPresynapticInfo();
  2134. return DelCount;
  2135. }
  2136. // set new outgoing connections for Source (presynaptic) neurons
  2137. // problem: we want it the other way around
  2138. // setting new incoming weights to a specific Target (postsynaptic) neuron
  2139. // --> SetNewWeights2
  2140. int connection::SetNewWeights(int NNewTargets, int NMaxTargets)
  2141. {
  2142. int i;
  2143. int AvailableTargetNeurons [nt];
  2144. for (int SourceNr=0;SourceNr<ns;++SourceNr) {
  2145. if (N_post[SourceNr] >= NMaxTargets) {
  2146. Dout(dc::con, "N_post[SourceNr] =" << N_post[SourceNr] << ">= NMaxTargets=" << NMaxTargets << "");
  2147. } else {
  2148. for (i=0;i<nt;++i) {
  2149. AvailableTargetNeurons[i] = 1;
  2150. }
  2151. int NAvailableTargetNeurons = nt;
  2152. int TargetNr=-1;
  2153. // delete existing connections from available target list
  2154. for (int delay=0;delay<maximumDelay;++delay) {
  2155. for (i=0; i<delays_length[SourceNr][delay];++i) {
  2156. TargetNr = post[SourceNr][delays[SourceNr][delay][i]];
  2157. if (TargetNr <0) {
  2158. cerr << "FEHLER: TargetNr<0, should never happen\n";
  2159. fflush(stderr);
  2160. exit(1);
  2161. }
  2162. AvailableTargetNeurons[TargetNr] = 0;
  2163. --NAvailableTargetNeurons;
  2164. }
  2165. }
  2166. for (int NewTargetNr=0;NewTargetNr<NNewTargets;++NewTargetNr) {
  2167. if (NAvailableTargetNeurons>0) {
  2168. int TargetIndex = gsl_rng_uniform_int(gslr, NAvailableTargetNeurons);
  2169. int count=-1;
  2170. TargetNr=-1;
  2171. for (i=0;i<nt;++i) {
  2172. if (AvailableTargetNeurons[i] ==1) {
  2173. ++count; // because it started with -1
  2174. if (count == TargetIndex) {
  2175. TargetNr = i;
  2176. break;
  2177. }
  2178. }
  2179. }
  2180. if (InsertNewWeight(SourceNr, TargetNr, InitialWeight) != -1) {
  2181. AvailableTargetNeurons[TargetNr] = 0;
  2182. --NAvailableTargetNeurons;
  2183. Dout(dc::con, "Set new connection: "<< SourceNr << "to " << TargetNr << ", available Target neurons: " << NAvailableTargetNeurons << "");
  2184. }
  2185. } else {
  2186. break;
  2187. }
  2188. }
  2189. }
  2190. }
  2191. }
  2192. // HIER GEHTS WEITER
  2193. int connection::SetNewWeights2(int _NNewSources, int NMaxSources)
  2194. {
  2195. int i;
  2196. // find out, which source neurons can make connections (check N_post[SourceNr] < M)
  2197. // make a list (vector or map: number of source neuron, number of free connections)
  2198. int NAvailableSource = 0;
  2199. vector <int> AvailableSourceList;
  2200. for (int SourceNr=0;SourceNr<ns;++SourceNr) {
  2201. if (N_post[SourceNr] <M) {
  2202. NAvailableSource += 1;
  2203. AvailableSourceList.push_back(SourceNr);
  2204. }
  2205. }
  2206. for (int TargetNr=0; TargetNr<nt;++TargetNr) {
  2207. int NNewSources = min(_NNewSources, NMaxSources-N_pre[TargetNr]);
  2208. for (int NewConNr=0; NewConNr<NNewSources; ++NewConNr) {
  2209. // choose source neuron (random, use number of available source neurons)
  2210. }
  2211. }
  2212. // connect to selected source neuron
  2213. // if (N_post[SourceNr] == 0) then delete source neuron from list of available source neurons
  2214. // next connection
  2215. // next target neuron
  2216. // int AvailableTargetNeurons [nt];
  2217. // for (int SourceNr=0;SourceNr<ns;++SourceNr) {
  2218. // if (N_post[SourceNr] >= NMaxTargets) {
  2219. // Dout(dc::con, "N_post[SourceNr] =" << N_post[SourceNr] << ">= NMaxTargets=" << NMaxTargets << "");
  2220. // } else {
  2221. // for (i=0;i<nt;++i) {
  2222. // AvailableTargetNeurons[i] = 1;
  2223. // }
  2224. // int NAvailableTargetNeurons = nt;
  2225. // int TargetNr=-1;
  2226. // // delete existing connections from available target list
  2227. // for (int delay=0;delay<maximumDelay;++delay) {
  2228. // for (i=0; i<delays_length[SourceNr][delay];++i) {
  2229. // TargetNr = post[SourceNr][delays[SourceNr][delay][i]];
  2230. // if (TargetNr <0) {
  2231. // cerr << "FEHLER: TargetNr<0, should never happen\n";
  2232. // fflush(stderr);
  2233. // exit(1);
  2234. // }
  2235. // AvailableTargetNeurons[TargetNr] = 0;
  2236. // --NAvailableTargetNeurons;
  2237. // }
  2238. // }
  2239. // for (int NewTargetNr=0;NewTargetNr<NNewTargets;++NewTargetNr) {
  2240. // if (NAvailableTargetNeurons>0) {
  2241. // int TargetIndex = gsl_rng_uniform_int(gslr, NAvailableTargetNeurons);
  2242. // int count=-1;
  2243. // TargetNr=-1;
  2244. // for (i=0;i<nt;++i) {
  2245. // if (AvailableTargetNeurons[i] ==1) {
  2246. // ++count; // because it started with -1
  2247. // if (count == TargetIndex) {
  2248. // TargetNr = i;
  2249. // break;
  2250. // }
  2251. // }
  2252. // }
  2253. // if (InsertNewWeight(SourceNr, TargetNr, InitialWeight) != -1) {
  2254. // AvailableTargetNeurons[TargetNr] = 0;
  2255. // --NAvailableTargetNeurons;
  2256. // Dout(dc::con, "Set new connection: "<< SourceNr << "to " << TargetNr << ", available Target neurons: " << NAvailableTargetNeurons << "");
  2257. // }
  2258. // } else {
  2259. // break;
  2260. // }
  2261. // }
  2262. // }
  2263. // }
  2264. }
  2265. int connection::Rewire(float minWeight, float maxConnectivity)
  2266. {
  2267. // first: delete low weights
  2268. int NFreeWeights = DeleteLowWeights(minWeight);
  2269. // second: set new weights randomly
  2270. SetNewWeights(NFreeWeights, int(floor(maxConnectivity*nt)));
  2271. }
  2272. /** returns list of synapses form source neuron
  2273. *
  2274. * @param [IN] SourceNr
  2275. * @param [OUT] SynList is a return vector containing a list of all synapses from source neuron SourceNr
  2276. * @return maximum weight
  2277. */
  2278. float connection::GetSourceWeights(int CurSource, vector<Synapse>& SynList, int& MaxDelay)
  2279. {
  2280. // Dout(dc::con, "connection::GetSourceWeights");
  2281. SynList.clear();
  2282. float MaxWeight=0;
  2283. MaxDelay=0;
  2284. for(int CurDelay=0;CurDelay<maximumDelay;++CurDelay)
  2285. {
  2286. for (int j=0; j< delays_length[CurSource][CurDelay]; j++) {
  2287. short SynIndex = delays[CurSource][CurDelay][j];
  2288. int CurTarget=post[CurSource][SynIndex];
  2289. float CurWeight=s[CurSource][SynIndex];
  2290. SynList.push_back(Synapse(CurSource,CurTarget, CurWeight, CurDelay));
  2291. if (CurWeight>MaxWeight) {
  2292. MaxWeight=CurWeight;
  2293. }
  2294. if (CurDelay>MaxDelay) {
  2295. MaxDelay=CurDelay;
  2296. }
  2297. }
  2298. }
  2299. // Dout(dc::con, "MaxWeight=" << MaxWeight << "");
  2300. return MaxWeight;
  2301. }
  2302. /** return a vector of all synapses to target neuron
  2303. *
  2304. * @param [IN] index of target neuron
  2305. * @param [OUT] output vector containing all synapses to target neuron nr CurTarget
  2306. * @return maximum weight
  2307. */
  2308. float connection::GetTargetWeights(int CurTarget, vector<Synapse>& SynList, int &MaxDelay)
  2309. {
  2310. // Dout(dc::con, "connection::GetTargetWeights");
  2311. SynList.clear();
  2312. float MaxWeight=0;
  2313. MaxDelay=0;
  2314. for (int j=0;j<N_pre[CurTarget];j++) {
  2315. int CurSource=I_pre[CurTarget][j];
  2316. float CurWeight=*(s_pre[CurTarget][j]);
  2317. int CurDelay = D_pre[CurTarget][j];
  2318. SynList.push_back(Synapse(CurSource, CurTarget, CurWeight, CurDelay));
  2319. if (CurWeight>MaxWeight) {
  2320. MaxWeight=CurWeight;
  2321. }
  2322. if (CurDelay>MaxDelay) {
  2323. MaxDelay=CurDelay;
  2324. }
  2325. }
  2326. // Dout(dc::con, "MaxWeight=" << MaxWeight << "");
  2327. return MaxWeight;
  2328. }
  2329. /** return maximum synaptic weight value
  2330. *
  2331. * @return maximum weight
  2332. */
  2333. float connection::GetMaxWeight()
  2334. {
  2335. float MaxWeight=0;
  2336. for (int CurSource=0;CurSource<ns;++CurSource)
  2337. {
  2338. for(int CurDelay=0;CurDelay<maximumDelay;++CurDelay)
  2339. {
  2340. for (int j=0; j< delays_length[CurSource][CurDelay]; j++) {
  2341. short SynIndex = delays[CurSource][CurDelay][j];
  2342. float CurWeight=s[CurSource][SynIndex];
  2343. if (CurWeight>MaxWeight) {
  2344. MaxWeight=CurWeight;
  2345. }
  2346. }
  2347. }
  2348. }
  2349. // Dout(dc::con, "MaxWeight=" << MaxWeight << "");
  2350. return MaxWeight;
  2351. }
  2352. int connection::GetMaxDelay()
  2353. {
  2354. int MaxDelay=0;
  2355. for (int CurSource=0;CurSource<ns;++CurSource)
  2356. {
  2357. for(int CurDelay=MaxDelay;CurDelay<maximumDelay;++CurDelay)
  2358. {
  2359. if (delays_length[CurSource][CurDelay]>0) {
  2360. if (CurDelay>MaxDelay) {
  2361. MaxDelay=CurDelay;
  2362. }
  2363. }
  2364. }
  2365. }
  2366. return MaxDelay;
  2367. }
  2368. ////////////////////////
  2369. DepressiveConnection::DepressiveConnection(
  2370. layer* SL, layer* TL,
  2371. csimInputChannel InputNumber, float _TauRec, float _U_se)
  2372. : connection(SL, TL, InputNumber),
  2373. // efficacy(boost::extents[SourceLayer->N][M]),
  2374. // LastEpsp(boost::extents[SourceLayer->N][M]),
  2375. U_SE(_U_se),
  2376. TauRec(_TauRec/dt), U_se_fac(1-_U_se)
  2377. {
  2378. Dout(dc::con, "Initialize DepressiveConnection");
  2379. }
  2380. DepressiveConnection::~DepressiveConnection()
  2381. {
  2382. DeleteArray2d(efficacy, SourceLayer->N);
  2383. DeleteArray2d(LastEpsp, SourceLayer->N);
  2384. }
  2385. int DepressiveConnection::WriteSimInfo(fstream &fw)
  2386. {
  2387. stringstream sstr;
  2388. sstr << "<TauRec value=\"" << TauRec*dt << "\"/> \n";
  2389. sstr << "<U_SE value=\"" << U_SE << "\"/> \n";
  2390. connection::WriteSimInfo(fw, sstr.str());
  2391. }
  2392. int DepressiveConnection::proceede(int TotalTime)
  2393. {
  2394. int t = TotalTime % MacroTimeStep;
  2395. int i,j,k,mi,ipre;
  2396. // calculate input for target layer
  2397. k=SourceLayer->N_firings;
  2398. // while (t-SourceLayer->firings[--k][0] < Dmax)
  2399. while (t-SourceLayer->firings[--k][0] < maximumDelay)
  2400. {
  2401. ipre = SourceLayer->firings[k][1];
  2402. for (j=0; j< delays_length[SourceLayer->firings[k][1]][t-SourceLayer->firings[k][0]]; j++)
  2403. {
  2404. mi = delays[ipre][t-SourceLayer->firings[k][0]][j];
  2405. i=post[ipre][mi];
  2406. if (TauRec > 0) {
  2407. efficacy[ipre][mi] = (1-(1-efficacy[ipre][mi])*exp(-(t-LastEpsp[ipre][mi])/TauRec));
  2408. } else {
  2409. efficacy[ipre][mi] =1;
  2410. }
  2411. InputPointer[i]+= s[ipre][mi] * efficacy[ipre][mi] ;
  2412. efficacy[ipre][mi] *= U_se_fac;
  2413. LastEpsp[ipre][mi] = t;
  2414. }
  2415. }
  2416. if (learn == true) {
  2417. learnobj->proceede(TotalTime);
  2418. }
  2419. }
  2420. int DepressiveConnection::reset(int t)
  2421. {
  2422. for(int i=0;i<ns;++i) for (int j=0;j<M;++j) {
  2423. efficacy[i][j]=1;
  2424. LastEpsp[i][j]=0;
  2425. }
  2426. }
  2427. int DepressiveConnection::prepare(int step)
  2428. {
  2429. // if ((learn == true) && (learnobj != 0)) {
  2430. // learnobj->prepare();
  2431. // Save();
  2432. // }
  2433. connection::prepare(step);
  2434. // Dout(dc::con, "PERFORMANCE: cpu-time = " << cpu_time_used << " sec");
  2435. int i,j;
  2436. for (i=0;i<SourceLayer->N;++i) for (j=0;j<M;++j) {
  2437. LastEpsp[i][j]-=MacroTimeStep;
  2438. }
  2439. }
  2440. int DepressiveConnection::InitializeDynamicalArrays(
  2441. const int _N, const int _M, const int Dmax)
  2442. {
  2443. cout <<"DepressiveConnection::InitializeDynamicalArrays\n";
  2444. connection::InitializeDynamicalArrays(_N,_M,Dmax);
  2445. int i,j;
  2446. Dout(dc::con, "SN=" << SourceLayer->N << "");
  2447. Dout(dc::con, "M=" << M << "");
  2448. NewArray2d(efficacy, SourceLayer->N, M);
  2449. NewArray2d(LastEpsp, SourceLayer->N, M);
  2450. for (i=0;i<SourceLayer->N;++i) for (j=0;j<M;++j) {
  2451. efficacy[i][j]=1;
  2452. LastEpsp[i][j]=0;
  2453. }
  2454. }
  2455. ///////////////////////////////////
  2456. FacilitativeConnection::FacilitativeConnection(
  2457. layer* SL, layer* TL, csimInputChannel InputNumber, float _TauRec,
  2458. float _U_se, float _UseTauDec, float _UseInc):
  2459. DepressiveConnection(SL, TL, InputNumber, _TauRec, _U_se), UseTauDec(_UseTauDec/dt), UseInc(_UseInc), UseConst(_U_se)
  2460. {
  2461. Dout(dc::con, "Initialize Facilitative Connection ");
  2462. Dout(dc::con, " UseTauDec=" << UseTauDec << " UseInc=" << UseInc << " UseConst" << UseConst << "");
  2463. }
  2464. FacilitativeConnection::~FacilitativeConnection()
  2465. {
  2466. DeleteArray2d(U_SEvalue,SourceLayer->N);
  2467. }
  2468. int FacilitativeConnection::InitializeDynamicalArrays(
  2469. const int _N, const int _M, const int Dmax)
  2470. {
  2471. cout <<"FacilitativeConnection::InitializeDynamicalArrays\n";
  2472. DepressiveConnection::InitializeDynamicalArrays(_N,_M,Dmax);
  2473. int i,j;
  2474. NewArray2d(U_SEvalue, SourceLayer->N, M);
  2475. for (i=0;i<SourceLayer->N;++i) for (j=0;j<M;++j) {
  2476. U_SEvalue[i][j]=0;
  2477. }
  2478. }
  2479. int FacilitativeConnection::reset(int t)
  2480. {
  2481. DepressiveConnection::reset(t);
  2482. for(int i=0;i<ns;++i) for (int j=0;j<M;++j) {
  2483. U_SEvalue[i][j] = 0;
  2484. }
  2485. }
  2486. int FacilitativeConnection::proceede(int TotalTime)
  2487. {
  2488. int t = TotalTime % MacroTimeStep;
  2489. int i,j,k,mi,ipre;
  2490. // calculate input for target layer
  2491. k=SourceLayer->N_firings;
  2492. // while (t-SourceLayer->firings[--k][0] < Dmax)
  2493. float CurU_SE;
  2494. while (t-SourceLayer->firings[--k][0] < maximumDelay)
  2495. {
  2496. ipre = SourceLayer->firings[k][1];
  2497. for (j=0; j< delays_length[SourceLayer->firings[k][1]][t-SourceLayer->firings[k][0]]; j++)
  2498. {
  2499. mi = delays[ipre][t-SourceLayer->firings[k][0]][j];
  2500. i=post[ipre][mi];
  2501. U_SEvalue[ipre][mi] *= exp(-(t-LastEpsp[ipre][mi])/UseTauDec);
  2502. CurU_SE = UseConst + U_SEvalue[ipre][mi];
  2503. efficacy[ipre][mi] = (1-(1-efficacy[ipre][mi])*exp(-float(t-LastEpsp[ipre][mi])/TauRec));
  2504. // Dout(dc::con, efficacy[ipre][mi] << " "); //remove
  2505. InputPointer[i]+= s[ipre][mi] * efficacy[ipre][mi] * CurU_SE;
  2506. efficacy[ipre][mi] *= (1-CurU_SE);
  2507. LastEpsp[ipre][mi] = t;
  2508. U_SEvalue[ipre][mi] += (1-CurU_SE)*UseInc;
  2509. }
  2510. }
  2511. if (BinRec) BinRec->record();
  2512. if (learn == true) {
  2513. learnobj->proceede(TotalTime);
  2514. }
  2515. }
  2516. int FacilitativeConnection::StartBinRec(int PreSynNumber)
  2517. {
  2518. int NumObserve = 2*M;
  2519. float** Buffer = new float* [NumObserve];
  2520. for (int i=0;i<M; ++i) {
  2521. Buffer[i] = &efficacy[PreSynNumber][i];
  2522. }
  2523. for (int i=0;i<M; ++i) {
  2524. Buffer[i+M] = &U_SEvalue[PreSynNumber][i];
  2525. }
  2526. string FileName("effsynweight.dat.bin");
  2527. BinRec = new BinRecorder(MacroTimeStep, NumObserve, M, Buffer, dt, (DataDirectory+FileName).c_str());
  2528. }
  2529. ///////////////////////////////
  2530. PspConnection::PspConnection(layer* SL, layer* TL, csimInputChannel InputNumber, bool _nonself): connection(SL,TL, InputNumber, _nonself), PspArrayPointer(0)
  2531. {
  2532. float tau2=20;
  2533. float tau1=5;
  2534. float _PspDuration = 50;
  2535. PspDuration = int(_PspDuration/dt);
  2536. PspTemplate = new float [PspDuration];
  2537. NewArray2d(Psp, TargetLayer->N, PspDuration);
  2538. }
  2539. PspConnection::~PspConnection()
  2540. {
  2541. delete[] PspTemplate;
  2542. DeleteArray2d(Psp,TargetLayer->N);
  2543. }