utils.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. /*
  2. * utils.cpp
  3. *
  4. * This file is part of the refactored Izhikevich polychronization model application.
  5. *
  6. * Copyright (C) 2018, Author: G. Trensch
  7. *
  8. * The refactored Izhikevich polychronization model application is free software:
  9. * you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * It is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this application. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. #include "params.h"
  24. #if( __RUN_REFACTORED_VERSION__ )
  25. #define __IMPORT_GLOBAL_VARIABLES__ true
  26. #include <math.h>
  27. #include <stdio.h>
  28. #include <errno.h>
  29. #include <stdlib.h>
  30. #include <cstring>
  31. #include "globals.h"
  32. #include "utils.h"
  33. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  34. // = E X P O R T N E T W O R K S T A T E S
  35. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  36. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  37. // = Export connection matrix to file in printable ASCII format.
  38. // =
  39. // = 001 002 100 <- synapse
  40. // = Source neuron 0001 nnn nnn nnn nnn nnn nnn ... nnn
  41. // = Source neuron 0002 nnn nnn nnn nnn nnn nnn ... nnn
  42. // = ...
  43. // = Source neuron 1000 nnn nnn nnn nnn nnn nnn ... nnn
  44. // =
  45. // = nnn ... target neuron id
  46. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  47. void ExportConnectionMatrixToFile( const char *pFileName ) {
  48. FILE *pFile = fopen( pFileName, "w" );
  49. if( pFile == nullptr ) {
  50. printf( "[ERROR] Failed to open file: %s (errno: %d)\n", pFileName, errno);
  51. exit(RC_ERROR_EXIT);
  52. }
  53. printf( "[INFO] Export connection matrix to file: %s\n", pFileName );
  54. for( int n = 0; n < NUM_TOTAL_NEURONS; ++n ) {
  55. for( int s = 0; s < NUM_SYNAPSES_PER_NEURON; ++s ) {
  56. fprintf( pFile, "%03d ", matrixOfPostSynapticNeurons[n][s] );
  57. }
  58. fprintf( pFile, "\n" );
  59. }
  60. fclose( pFile );
  61. }
  62. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  63. // = Export weight matrix to file in printable ASCII format.
  64. // =
  65. // = 001 002 100 <- synapse
  66. // = Source neuron 0001 ww.wwww ww.wwww ww.wwww ww.wwww ... ww.wwww
  67. // = Source neuron 0002 ww.wwww ww.wwww ww.wwww ww.wwww ... ww.wwww
  68. // = ...
  69. // = Source neuron 1000 ww.wwww ww.wwww ww.wwww ww.wwww ... ww.wwww
  70. // =
  71. // = ww.wwww ... synaptic strength
  72. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  73. void ExportWeightMatrixToFile( const char *pFileName ) {
  74. FILE *pFile = fopen( pFileName, "w" );
  75. if( pFile == nullptr ) {
  76. printf( "[ERROR] Failed to open file: %s (errno: %d)\n", pFileName, errno);
  77. exit(RC_ERROR_EXIT);
  78. }
  79. printf( "[INFO] Export weight matrix to file: %s\n", pFileName );
  80. for( int n = 0; n < NUM_TOTAL_NEURONS; ++n ) {
  81. for( int s = 0; s < NUM_SYNAPSES_PER_NEURON; ++s ) {
  82. fprintf( pFile, "%f ", matrixOfSynapticWeights[n][s] );
  83. }
  84. fprintf( pFile, "\n" );
  85. }
  86. fclose( pFile );
  87. }
  88. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  89. // = Export delay matrix to file in printable ASCII format.
  90. // =
  91. // = 001 002 100 <- synapse
  92. // = Source neuron 0001 ddd ddd ddd ddd ddd ddd ... ddd
  93. // = Source neuron 0002 ddd ddd ddd ddd ddd ddd ... ddd
  94. // = ...
  95. // = Source neuron 1000 ddd ddd ddd ddd ddd ddd ... ddd
  96. // =
  97. // = ddd ... conduction delay
  98. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  99. void ExportDelayMatrixToFile( const char *pFileName ) {
  100. FILE *pFile = fopen( pFileName, "w" );
  101. if( pFile == nullptr ) {
  102. printf( "[ERROR] Failed to open file: %s (errno: %d)\n", pFileName, errno);
  103. exit(RC_ERROR_EXIT);
  104. }
  105. printf( "[INFO] Export delay matrix to file: %s\n", pFileName );
  106. for( int n = 0; n < NUM_TOTAL_NEURONS; ++n ) {
  107. for( int s = 0; s < NUM_SYNAPSES_PER_NEURON; ++s ) {
  108. int delayValue = GetDelayOfConnection( n, s );
  109. fprintf( pFile, "%03d ", delayValue );
  110. }
  111. fprintf( pFile, "\n" );
  112. }
  113. fclose( pFile );
  114. }
  115. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  116. // = Export connection, weight, and delay matrix to file for import into PyNN.
  117. // =
  118. // =
  119. // = exc_exc_connections = [
  120. // = ( source neuron, target neuron, weight, delay ), ...,
  121. // = ... , ( source neuron, target neuron, weight, delay )
  122. // = ]
  123. // =
  124. // = exc_inh_connections = [
  125. // = ( source neuron, target neuron, weight, delay ), ...,
  126. // = ... , ( source neuron, target neuron, weight, delay )
  127. // = ]
  128. // = inh_exc_connections = [
  129. // = ( source neuron, target neuron, weight, delay ), ...,
  130. // = ... , ( source neuron, target neuron, weight, delay )
  131. // = ]
  132. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  133. void ExportConnectionMatrixWeightAndDelay( const char *pFileName ) {
  134. const int CONST_ENTRIES_PER_LINE = 8;
  135. FILE *pFile = fopen( pFileName, "w" );
  136. if( pFile == nullptr ) {
  137. printf( "[ERROR] Failed to open file: %s (errno: %d)\n", pFileName, errno);
  138. exit(RC_ERROR_EXIT);
  139. }
  140. printf( "[INFO] Export connection, weight and delay matrix for import into PyNN to file: %s\n", pFileName );
  141. // excitatory to excitatory connections
  142. fprintf( pFile, "exc_exc_connections = [\n" );
  143. int entriesCount = 0;
  144. for( int excPreSynNeuron = 0; excPreSynNeuron < NUM_EXCITATORY_NEURONS; ++excPreSynNeuron ) {
  145. for( int synapse = 0; synapse < NUM_SYNAPSES_PER_NEURON; ++synapse ) {
  146. if( matrixOfPostSynapticNeurons[excPreSynNeuron][synapse] < NUM_EXCITATORY_NEURONS ) {
  147. int excPostSynNeuron = matrixOfPostSynapticNeurons[excPreSynNeuron][synapse];
  148. double weight = fabs( matrixOfSynapticWeights[excPreSynNeuron][synapse] ); // PyNN expects positive weight values for inhibitory synapses
  149. double delay = GetDelayOfConnection( excPreSynNeuron, synapse );
  150. fprintf( pFile, "( %3d, %3d, %f, %f ), ", excPreSynNeuron, excPostSynNeuron, weight, delay );
  151. entriesCount++;
  152. if( entriesCount == CONST_ENTRIES_PER_LINE ) {
  153. fprintf( pFile, "\n" );
  154. entriesCount = 0;
  155. }
  156. }
  157. }
  158. }
  159. fprintf( pFile, "]\n\n" );
  160. // excitatory to inhibitory connections
  161. fprintf( pFile, "exc_inh_connections = [\n" );
  162. entriesCount = 0;
  163. for( int excPreSynNeuron = 0; excPreSynNeuron < NUM_EXCITATORY_NEURONS; ++excPreSynNeuron ) {
  164. for( int synapse = 0; synapse < NUM_SYNAPSES_PER_NEURON; ++synapse ) {
  165. if( matrixOfPostSynapticNeurons[excPreSynNeuron][synapse] >= NUM_EXCITATORY_NEURONS) {
  166. int inhPostSynNeuron = matrixOfPostSynapticNeurons[excPreSynNeuron][synapse];
  167. double weight = fabs( matrixOfSynapticWeights[excPreSynNeuron][synapse] ); // PyNN expects positive weight values for inhibitory synapses
  168. double delay = GetDelayOfConnection( excPreSynNeuron, synapse );
  169. fprintf( pFile, "( %3d, %3d, %f, %f ), "
  170. , excPreSynNeuron, inhPostSynNeuron - NUM_EXCITATORY_NEURONS, weight, delay );
  171. entriesCount++;
  172. if( entriesCount == CONST_ENTRIES_PER_LINE ) {
  173. fprintf( pFile, "\n" );
  174. entriesCount = 0;
  175. }
  176. }
  177. }
  178. }
  179. fprintf( pFile, "]\n\n" );
  180. // inhibitory to excitatory connections
  181. entriesCount = 0;
  182. fprintf( pFile, "inh_exc_connections = [\n" );
  183. for( int inhPreSynNeuron = NUM_EXCITATORY_NEURONS; inhPreSynNeuron < NUM_TOTAL_NEURONS; ++inhPreSynNeuron ) {
  184. for( int synapse = 0; synapse < NUM_SYNAPSES_PER_NEURON; ++synapse ) {
  185. if( matrixOfPostSynapticNeurons[inhPreSynNeuron][synapse] < NUM_EXCITATORY_NEURONS) {
  186. int excPostSynNeuron = matrixOfPostSynapticNeurons[inhPreSynNeuron][synapse];
  187. double weight = fabs( matrixOfSynapticWeights[inhPreSynNeuron][synapse] ); // PyNN expects positive weight values for inhibitory synapses
  188. double delay = GetDelayOfConnection( inhPreSynNeuron, synapse );
  189. fprintf( pFile, "( %3d, %3d, %6.3f, %6.3f ), "
  190. , inhPreSynNeuron - NUM_EXCITATORY_NEURONS, excPostSynNeuron, weight, delay );
  191. entriesCount++;
  192. if( entriesCount == CONST_ENTRIES_PER_LINE ) {
  193. fprintf( pFile, "\n" );
  194. entriesCount = 0;
  195. }
  196. }
  197. }
  198. }
  199. fprintf( pFile, "]\n\n" );
  200. fclose( pFile );
  201. }
  202. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  203. // = I M P O R T N E T W O R K S T A T E S
  204. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  205. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  206. // = Import the connection matrix from file generated with
  207. // = ExportConnectionMatrixToFile().
  208. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  209. void ImportConnectionMatrixFromFile( const char *pFileName ) {
  210. FILE *pFile = fopen( pFileName, "r" );
  211. if( pFile == nullptr ) {
  212. printf( "[ERROR] Failed to open file: %s (errno: %d)\n", pFileName, errno);
  213. exit(RC_ERROR_EXIT);
  214. }
  215. printf( "[INFO] Import connection matrix from file: %s\n", pFileName );
  216. char line[NUM_SYNAPSES_PER_NEURON * SIZE_OF_ENTRY_NEURON] = {};
  217. int targetNeuron = 0;
  218. char *pToken = nullptr;
  219. for( int n = 0; n < NUM_TOTAL_NEURONS; ++n ) {
  220. fgets( line, sizeof( line ), pFile );
  221. // first token, i.e. synapse 0 target neuron
  222. pToken = strtok( line, " " );
  223. targetNeuron = atoi( pToken );
  224. matrixOfPostSynapticNeurons[n][0] = targetNeuron;
  225. // subsequent tokens, i.e. synapses 1 .. s
  226. for( int s = 1; s < NUM_SYNAPSES_PER_NEURON; ++s ) {
  227. pToken = strtok( NULL, " " );
  228. targetNeuron = atoi( pToken );
  229. matrixOfPostSynapticNeurons[n][s] = targetNeuron;
  230. }
  231. }
  232. fclose( pFile );
  233. }
  234. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  235. // = Import the weight matrix from file generated with
  236. // = ExportWeightMatrixToFile().
  237. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  238. void ImportWeightMatrixFromFile( const char *pFileName ) {
  239. FILE *pFile = fopen( pFileName, "r" );
  240. if( pFile == nullptr ) {
  241. printf( "[ERROR] Failed to open file: %s (errno: %d)\n", pFileName, errno );
  242. exit( RC_ERROR_EXIT );
  243. }
  244. printf( "[INFO] Import weight matrix from file: %s\n", pFileName );
  245. char line[NUM_SYNAPSES_PER_NEURON * SIZE_OF_ENTRY_WEIGHT] = {};
  246. double weight = 0.0;
  247. char *pToken = nullptr;
  248. for( int n = 0; n < NUM_TOTAL_NEURONS; ++n ) {
  249. fgets( line, sizeof( line ), pFile );
  250. // first token, i.e. weight of connection synapse 0 of target neuron n
  251. pToken = strtok( line, " " );
  252. weight = atof( pToken );
  253. matrixOfSynapticWeights[n][0] = weight;
  254. // subsequent tokens, i.e. weights of connections synapses s of target neuron n
  255. for( int s = 1; s < NUM_SYNAPSES_PER_NEURON; ++s ) {
  256. pToken = strtok( NULL, " " );
  257. weight = atof( pToken );
  258. matrixOfSynapticWeights[n][s] = weight;
  259. }
  260. }
  261. fclose( pFile );
  262. }
  263. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  264. // = Import the delay matrix from file generated with
  265. // = ExportDelayMatrixToFile().
  266. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  267. void ImportDelayMatrixFromFile( const char *pFileName ) {
  268. FILE *pFile = fopen( pFileName, "r" );
  269. if( pFile == nullptr ) {
  270. printf( "[ERROR] Failed to open file: %s (errno: %d)\n", pFileName, errno );
  271. exit( RC_ERROR_EXIT );
  272. }
  273. printf( "[INFO] Import delay matrix from file: %s\n", pFileName );
  274. char line[NUM_SYNAPSES_PER_NEURON * SIZE_OF_ENTRY_DELAY] = {};
  275. char *pToken = nullptr;
  276. for( int n = 0; n < NUM_TOTAL_NEURONS; ++n ) {
  277. fgets( line, sizeof( line ), pFile );
  278. // first token, i.e. delay of synapse 0
  279. pToken = strtok( line, " " );
  280. int delayIdx = atoi( pToken ) - 1;
  281. int idx = numEntriesPerDelay[n][delayIdx];
  282. listOfSynsByNeuronAndDelay[n][delayIdx][idx] = 0; // synapse 0
  283. numEntriesPerDelay[n][delayIdx]++;
  284. // subsequent tokens, i.e. delays of synapses s
  285. for( int s = 1; s < NUM_SYNAPSES_PER_NEURON; ++s ) {
  286. pToken = strtok( NULL, " " );
  287. delayIdx = atoi( pToken ) - 1;
  288. idx = numEntriesPerDelay[n][delayIdx];
  289. listOfSynsByNeuronAndDelay[n][delayIdx][idx] = s;
  290. numEntriesPerDelay[n][delayIdx]++;
  291. }
  292. }
  293. fclose( pFile );
  294. }
  295. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  296. // = R E C O R D E X T E R N A L S T I M U L U S
  297. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  298. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  299. // = Record the random input data the network is stimulated with.
  300. // =
  301. // = second millisecond id of the neuron that receives the input
  302. // = ssssss mmm nnn
  303. // = ...
  304. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  305. void RecordRandomStimulusToFile( const char *pFileName, int simTimeSecond, int simTimeMillisecond, int inputNeuron ) {
  306. if( pFileStimulusOutput == nullptr ) {
  307. printf( "[ERROR] File not open: %s (errno: %d)\n", pFileName, errno );
  308. exit( RC_ERROR_EXIT );
  309. }
  310. if( inputNeuron > 0 ) {
  311. fprintf( pFileStimulusOutput, "%06d %03d %03d\n", simTimeSecond, simTimeMillisecond, inputNeuron );
  312. }
  313. }
  314. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  315. // = R E A D E X T E R N A L S T I M U L U S
  316. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  317. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  318. // = Read next record from external stimulus file and return the neuron id.
  319. // =
  320. // = second millisecond id of the neuron that receives the input
  321. // = ssssss mmm nnn
  322. // = ...
  323. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  324. int GetNextExternalStimulusFromFile( const char *pFileName, int simTimeSecond, int t ) {
  325. char line[32] = {};
  326. char *pToken = nullptr;
  327. if( pFileStimulusInput == nullptr ) {
  328. printf( "[ERROR] File not open: %s (errno: %d)\n", pFileName, errno );
  329. exit( RC_ERROR_EXIT );
  330. }
  331. if( !fgets( line, sizeof( line ), pFileStimulusInput )) {
  332. printf( "[ERROR] Unexpected end of file while reading stimulus data. Simulation time: %ds %dms Filename: %s\n"
  333. , simTimeSecond, t, pFileName );
  334. exit( RC_ERROR_EXIT );
  335. }
  336. pToken = strtok( line, " " );
  337. if( pToken == nullptr ) {
  338. return RC_NOID;
  339. }
  340. pToken = strtok( nullptr, " " );
  341. if( pToken == nullptr ) {
  342. return RC_NOID;
  343. }
  344. pToken = strtok( nullptr, " " );
  345. if( pToken == nullptr ) {
  346. return RC_NOID;
  347. }
  348. int neuronId = atoi( pToken );
  349. return neuronId;
  350. }
  351. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  352. // = R E C O R D N E T W O R K A C T I V I T Y D A T A
  353. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  354. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  355. // = Write the spike times to file in a printable ASCII format.
  356. // =
  357. // = second millisecond id of the neuron that has fired
  358. // = ssssss mmm nnn
  359. // = ...
  360. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  361. void RecordNetworkActivityToFile( const char *pFileName, int simulationSecond, int numFirings ) {
  362. FILE *pFile = fopen( pFileName, "a+" );
  363. if( pFile == nullptr ) {
  364. printf( "[ERROR] Failed to open file: %s (errno: %d)\n", pFileName, errno );
  365. exit( RC_ERROR_EXIT );
  366. }
  367. // skip negative times
  368. int idx = 0;
  369. while( firings[idx][TIME] < 0 ) {
  370. idx++;
  371. }
  372. for( ; idx < numFirings; ++idx ) {
  373. fprintf( pFile, "%06d %03d %03d\n", simulationSecond, firings[idx][TIME], firings[idx][NEURON] );
  374. }
  375. fclose( pFile );
  376. }
  377. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  378. // = H E L P E R F U N C T I O N S
  379. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  380. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  381. // = Return the delay value of a connection.
  382. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  383. int GetDelayOfConnection( int preSynNeuron, int synapse ) {
  384. int delay = 0;
  385. for( int delayIdx = 0; delayIdx < MAX_SYNAPSE_DELAY; ++delayIdx ) {
  386. int preSynNeuron_numEntriesOfDelay = numEntriesPerDelay[preSynNeuron][delayIdx];
  387. for( int i = 0; i < preSynNeuron_numEntriesOfDelay; ++i ) {
  388. if( synapse == listOfSynsByNeuronAndDelay[preSynNeuron][delayIdx][i] ) {
  389. delay = delayIdx + 1; // index 0 corresponds to 1ms delay
  390. }
  391. }
  392. }
  393. return delay;
  394. }
  395. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  396. // = Delete a file from disk.
  397. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  398. void DeleteFile( const char *fileName ) {
  399. if( remove( fileName ) == 0 ) {
  400. printf( "[INFO] File deleted: %s\n", fileName );
  401. }
  402. else {
  403. printf( "[INFO] File could not be deleted: %s\n", fileName );
  404. }
  405. }
  406. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  407. // = D E B U G F U N C T I O N S
  408. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  409. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  410. // = Debugprint: connection matrix
  411. // =
  412. // = Matrix of Post Synaptic Neurons
  413. // =
  414. // = 0 1 2 3 4 5 6 7 8 9 ... <- synapse
  415. // = -------------------------------------------------------- ...
  416. // = 0 : 840 394 783 798 911 197 335 768 277 553 ...
  417. // = 1 : 950 920 147 881 641 431 619 281 786 307 ... <- target neuron id
  418. // = 2 : 76 649 248 629 229 700 316 328 231 74 ...
  419. // = 3 : 291 180 684 727 139 603 492 838 724 178 ...
  420. // = 4 : 98 923 169 481 225 826 290 357 878 344 ...
  421. // = 5 : 324 874 589 637 759 775 794 262 604 470 ...
  422. // = 6 : 887 933 173 447 487 795 639 965 155 292 ...
  423. // = 7 : 347 205 522 400 307 679 645 443 269 703 ...
  424. // = 8 : 452 160 308 433 5 649 126 461 84 780 ...
  425. // = 9 : 805 749 398 366 394 272 599 68 901 432 ...
  426. // = 10 : 20 53 897 899 39 419 183 219 778 622 ...
  427. // = ....
  428. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  429. void PrintMatrixOfPostSynapticNeurons() {
  430. printf( " Matrix of Post Synaptic Neurons\n\n" );
  431. printf( " " );
  432. for( int s = 0; s < NUM_SYNAPSES_PER_NEURON; ++s ) {
  433. printf( " %3d ", s );
  434. }
  435. printf( "\n" );
  436. printf( "-------" );
  437. for( int s = 0; s < NUM_SYNAPSES_PER_NEURON; ++s ) {
  438. printf( "-----" );
  439. }
  440. printf( " \n" );
  441. for( int n = 0; n < NUM_TOTAL_NEURONS; ++n ) {
  442. printf( "%3d : ", n );
  443. for( int s = 0; s < NUM_SYNAPSES_PER_NEURON; ++s ) {
  444. printf( " %3d ", matrixOfPostSynapticNeurons[n][s] );
  445. }
  446. printf( "\n" );
  447. }
  448. printf( "Press enter ...\n" );
  449. getchar();
  450. }
  451. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  452. // = Debugprint: weight matrix
  453. // =
  454. // = Matrix of Synaptic Weights
  455. // =
  456. // = 0 1 2 3 4 ... <- synapse
  457. // = --------------------------------------------------------- ...
  458. // = 0 : 6.000000 6.000000 6.000000 6.000000 6.000000 ...
  459. // = 1 : 6.000000 6.000000 6.000000 6.000000 6.000000 ... <- connection strength
  460. // = 2 : 6.000000 6.000000 6.000000 6.000000 6.000000 ...
  461. // = 3 : 6.000000 6.000000 6.000000 6.000000 6.000000 ...
  462. // = 4 : 6.000000 6.000000 6.000000 6.000000 6.000000 ...
  463. // = 5 : 6.000000 6.000000 6.000000 6.000000 6.000000 ...
  464. // = 6 : 6.000000 6.000000 6.000000 6.000000 6.000000 ...
  465. // = 7 : 6.000000 6.000000 6.000000 6.000000 6.000000 ...
  466. // = 8 : 6.000000 6.000000 6.000000 6.000000 6.000000 ...
  467. // = 9 : 6.000000 6.000000 6.000000 6.000000 6.000000 ...
  468. // = 10 : 6.000000 6.000000 6.000000 6.000000 6.000000 ...
  469. // = ....
  470. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  471. void PrintMatrixOfSynapticWeights() {
  472. printf( " Matrix of Synaptic Weights\n\n" );
  473. printf( " " );
  474. for( int s = 0; s < NUM_SYNAPSES_PER_NEURON; ++s ) {
  475. printf( " %8d ", s );
  476. }
  477. printf( "\n" );
  478. printf( "-------" );
  479. for( int s = 0; s < NUM_SYNAPSES_PER_NEURON; ++s ) {
  480. printf( "----------" );
  481. }
  482. printf( " \n" );
  483. for( int n = 0; n < NUM_TOTAL_NEURONS; ++n ) {
  484. printf( "%3d : ", n );
  485. for( int s = 0; s < NUM_SYNAPSES_PER_NEURON; ++s ) {
  486. printf( " %f ", matrixOfSynapticWeights[n][s] );
  487. }
  488. printf( "\n" );
  489. }
  490. printf( "Press enter ...\n" );
  491. getchar();
  492. }
  493. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  494. // = Debugprint: delay matrix
  495. // =
  496. // = Matrix of Synaptic Delays
  497. // =
  498. // = 0 1 2 3 4 5 6 7 8 9 10 ... <- synapse
  499. // = -------------------------------------------------------------- ...
  500. // = 0 : 001 001 001 001 001 002 002 002 002 002 003 ...
  501. // = 1 : 001 001 001 001 001 002 002 002 002 002 003 ... <- conduction delay
  502. // = 2 : 001 001 001 001 001 002 002 002 002 002 003 ...
  503. // = 3 : 001 001 001 001 001 002 002 002 002 002 003 ...
  504. // = 4 : 001 001 001 001 001 002 002 002 002 002 003 ...
  505. // = 5 : 001 001 001 001 001 002 002 002 002 002 003 ...
  506. // = 6 : 001 001 001 001 001 002 002 002 002 002 003 ...
  507. // = 7 : 001 001 001 001 001 002 002 002 002 002 003 ...
  508. // = 8 : 001 001 001 001 001 002 002 002 002 002 003 ...
  509. // = 9 : 001 001 001 001 001 002 002 002 002 002 003 ...
  510. // = 10 : 001 001 001 001 001 002 002 002 002 002 003 ...
  511. // = ....
  512. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  513. void PrintMatrixOfSynapticDelays() {
  514. printf( " Matrix of Synaptic Delays\n\n" );
  515. printf( " " );
  516. for( int s = 0; s < NUM_SYNAPSES_PER_NEURON; ++s ) {
  517. printf( " %8d ", s );
  518. }
  519. printf( "\n" );
  520. printf( "-------" );
  521. for( int s = 0; s < NUM_SYNAPSES_PER_NEURON; ++s ) {
  522. printf( "----------" );
  523. }
  524. printf( " \n" );
  525. for( int n = 0; n < NUM_TOTAL_NEURONS; ++n ) {
  526. printf( "%3d : ", n );
  527. for( int s = 0; s < NUM_SYNAPSES_PER_NEURON; ++s ) {
  528. printf( " %03d ", GetDelayOfConnection( n, s ));
  529. }
  530. printf( "\n" );
  531. }
  532. printf( "Press enter ...\n" );
  533. getchar();
  534. }
  535. #endif // __RUN_REFACTORED_VERSION__